Project

General

Profile

RE: Migrating from sqlite3 to mysql ยป sqlite3-to-mysql.py

Pavel Medvedev, 2012-04-27 14:19

 
1
#! /usr/bin/env python
2
import re, fileinput
3
from optparse import OptionParser
4

    
5

    
6
IGNOREDPREFIXES = [
7
    'PRAGMA',
8
    'BEGIN TRANSACTION;',
9
    'COMMIT;',
10
    'DELETE FROM sqlite_sequence;',
11
    'INSERT INTO "sqlite_sequence"',
12
]
13

    
14
def _replace(line):
15
    if any(line.startswith(prefix) for prefix in IGNOREDPREFIXES):
16
        return
17
    line = line.replace("INTEGER PRIMARY KEY", "INTEGER AUTO_INCREMENT PRIMARY KEY")
18
    line = line.replace("AUTOINCREMENT", "AUTO_INCREMENT")
19
    line = line.replace("DEFAULT 't'", "DEFAULT '1'")
20
    line = line.replace("DEFAULT 'f'", "DEFAULT '0'")
21
    line = line.replace(",'t'", ",'1'")
22
    line = line.replace(",'f'", ",'0'")
23
    return line
24

    
25

    
26
def _backticks(line, in_string):
27
    """Replace double quotes by backticks outside (multiline) strings
28

    
29
    >>> _backticks('''INSERT INTO "table" VALUES ('"string"');''', False)
30
    ('INSERT INTO `table` VALUES (\\'"string"\\');', False)
31

    
32
    >>> _backticks('''INSERT INTO "table" VALUES ('"Heading''', False)
33
    ('INSERT INTO `table` VALUES (\\'"Heading', True)
34

    
35
    >>> _backticks('''* "text":http://link.com''', True)
36
    ('* "text":http://link.com', True)
37

    
38
    >>> _backticks(" ');", True)
39
    (" ');", False)
40

    
41
    """
42
    new = ''
43
    for c in line:
44
        if not in_string:
45
            if c == "'":
46
                in_string = True
47
            elif c == '"':
48
                new = new + '`'
49
                continue
50
        elif c == "'":
51
            in_string = False
52
        new = new + c
53
    return new, in_string
54

    
55
def _process(opts, lines):
56
    if opts.database:
57
        yield '''\
58
drop database {d};
59
create database {d} character set utf8;
60
grant all on {d}.* to {u}@'%' identified by '{p}';
61
use {d};\n'''.format(d=opts.database, u=opts.username, p=opts.password)
62
    yield "SET sql_mode='NO_BACKSLASH_ESCAPES';\n"
63

    
64
    in_string = False
65
    for line in lines:
66
        if not in_string:
67
            line = _replace(line)
68
            if line is None:
69
                continue
70
        line, in_string = _backticks(line, in_string)
71
        yield line
72

    
73
def main():
74
    op = OptionParser()
75
    op.add_option('-d', '--database')
76
    op.add_option('-u', '--username')
77
    op.add_option('-p', '--password')
78
    opts, args = op.parse_args()
79

    
80
    lines = (l for l in fileinput.input(args))
81
    for line in _process(opts, lines):
82
        print line,
83

    
84

    
85
if __name__ == "__main__":
86
    main()
    (1-1/1)