Skip to content

Commit 45b1eb0

Browse files
committed
Hope it helps someone :)
1 parent f70f8b2 commit 45b1eb0

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

index.html

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>MySQL to SQLite online converter 🔨</title>
6+
<style type="text/css">
7+
html,
8+
body {
9+
font-family: sans-serif;
10+
padding: 0;
11+
margin: 0;
12+
width: 100%;
13+
background: #999;
14+
text-align: center;
15+
}
16+
body {
17+
max-width: 1024px;
18+
margin: 1em auto;
19+
}
20+
textarea {
21+
display: block;
22+
width: 100%;
23+
height: 300px;
24+
}
25+
button {
26+
margin: 30px 0;
27+
padding: 5px 10px;
28+
font-size: 32px;
29+
}
30+
p {
31+
font-size: 20px;
32+
}
33+
</style>
34+
</head>
35+
<body>
36+
<script>
37+
function convert() {
38+
var sqlite = "PRAGMA journal_mode = MEMORY;\n" +
39+
"PRAGMA synchronous = OFF;\n" +
40+
"BEGIN TRANSACTION;\n";
41+
sqlite = "";
42+
43+
var currentTable = '';
44+
45+
var lines = document.getElementById('mysql').value.split('\n');
46+
var skip = [/^CREATE DATABASE/i, /^USE/i, /^\/\*/i, /^--/i];
47+
var keys = [];
48+
49+
// Used this site to test regexes: https://regex101.com/
50+
51+
lineLoop:
52+
for (var i = 0; i < lines.length; i++) {
53+
line = lines[i].trim();
54+
// Skip lines that match regexes in the skip[] array above
55+
for (var j = 0; j < skip.length; j++) if (skip[j].test(line)) continue lineLoop;
56+
// Include all `INSERT` lines. Replace \' by ''
57+
if (/^(INSERT|\()/i.test(line)) {
58+
sqlite += line.replace(/\\'/gi, "''") + "\n";
59+
continue;
60+
}
61+
// Print the ´CREATE´ line as is and capture the table name
62+
if ((m = /^\s*CREATE TABLE.*[`"](.*)[`"]/i.exec(line)) !== null) {
63+
currentTable = m[1];
64+
sqlite += "\n" + line + "\n";
65+
continue;
66+
}
67+
// Clean table end line like:
68+
// ) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8 COMMENT='By definition:\r\n- user_group #1 is administrator and will always have all permissions.\r\n- user_group #2 is guest and always have no permissions.\r\n';
69+
if (line.startsWith(")")) {
70+
sqlite += ");\n";
71+
continue;
72+
}
73+
// Remove CONSTRAINT `fk_address_state1`" part from lines
74+
line = line.replace(/^CONSTRAINT [`'"][\w]+[`'"][\s]+/gi, '');
75+
// Replace "XXXXX KEY" by "KEY" except "PRIMARY KEY" "FOREIGN KEY" and "UNIQUE KEY"
76+
line = line.replace(/^[^FOREIGN][^PRIMARY][^UNIQUE]\w+\s+KEY/gi, 'KEY');
77+
78+
// Lines starting with (UNIQUE) KEY are extracted so we declare them all at the end of the script
79+
// We also append key name with table name to avoid duplicate index name
80+
// Example: KEY `name` (`permission_name`)
81+
if ((m = /^(UNIQUE\s)*KEY\s+[`'"](\w+)[`'"]\s+\([`'"](\w+)[`'"]/ig.exec(line)) !== null) {
82+
keyUnique = m[1] || "";
83+
keyName = m[2];
84+
colName = m[3];
85+
keys.push('CREATE '+ keyUnique +'INDEX `'+ currentTable + '_' + keyName + '` ON `' + currentTable +'` (`' + colName + '`);');
86+
continue;
87+
}
88+
// Print all fields definition lines except "KEY" lines and lines starting with ")"
89+
if (/^[^)]((?![\w]+\sKEY).)*$/gi.test(line)) {
90+
// Clear invalid keywords
91+
line = line.replace(/AUTO_INCREMENT|CHARACTER SET [^ ]+|CHARACTER SET [^ ]+|UNSIGNED/ig, "");
92+
line = line.replace(/DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP|COLLATE [^ ]+/ig, "");
93+
line = line.replace(/COMMENT\s['"`].*['"`]/ig, "");
94+
line = line.replace(/SET\([^)]+\)|ENUM[^)]+\)/ig, "TEXT ");
95+
// Clear weird MySQL types such as varchar(40) and int(11)
96+
line = line.replace(/int\([0-9]*\)/ig, "INTEGER");
97+
line = line.replace(/varchar\([0-9]*\)|LONGTEXT/ig, "TEXT");
98+
}
99+
100+
if (line != "") {
101+
sqlite += line + "\n";
102+
}
103+
}
104+
sqlite += "\n";
105+
106+
// Fix last table line with comma
107+
sqlite = sqlite.replace(/,\n\);/g, "\n);");
108+
109+
// Include all gathered keys as CREATE INDEX
110+
sqlite += "\n\n" + keys.join("\n");
111+
112+
document.getElementById('sqlite').value = sqlite;
113+
}
114+
</script>
115+
116+
<h1>MySQL to SQLite online converter 🔨</h1>
117+
<textarea id="mysql">Paste MySQL SQL code here</textarea>
118+
119+
<button onclick="convert()">Convert</button>
120+
121+
<h1>SQLite result:</h1>
122+
<textarea id="sqlite"></textarea>
123+
124+
<p>Client side only. No dependencies or server side processing. You can save this page for offline use.<br>
125+
Send requests, bug reports & suggestions to git repo: <a href="http://github.com/ww9/mysql2sqlite">http://github.com/ww9/mysql2sqlite</a>
126+
<br>License: <a href="http://unlicense.org/">The Unlicense</a>, <a href="https://gist.github.com/ww9/4c4481fb7b55186960a34266078c88b1">Public Domain</a>. As free as it gets.</p>
127+
</body>
128+
129+
</html>

0 commit comments

Comments
 (0)