Last active
December 8, 2022 20:13
-
-
Save bird-in-hat/ab52c9f638f294dd1030ed439bba3f02 to your computer and use it in GitHub Desktop.
Ghost database translation from sqlite to mysql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import fileinput | |
import datetime | |
'''Usage: python3 translator.py dump.sqlite > dump.mysql ''' | |
def _translate_line(line: str): | |
if 'PRAGMA foreign_keys=OFF;' in line: | |
return 'SET FOREIGN_KEY_CHECKS = 0;' | |
if 'BEGIN TRANSACTION;' in line: | |
return 'START TRANSACTION;' | |
if '"expertise" varchar(' in line: | |
line = line.replace('"expertise" varchar(', '`expertise` varchar(') | |
if 'AUTOINCREMENT' in line: | |
line = line.replace('AUTOINCREMENT', 'AUTO_INCREMENT') | |
if ' `stripe_prices_ibfk_1`' in line: | |
return '' | |
elif 'CREATE TABLE `stripe_products`' in line: | |
line += 'CREATE UNIQUE INDEX `stripe_prices_ibfk_1` ON `stripe_products`(`stripe_product_id`);' | |
if 'sqlite_sequence' in line: | |
return '' | |
for s in re.findall('CREATE TABLE IF NOT EXISTS "[a-z_]+"', line): | |
line = line.replace(s, s.replace('"', '`')) | |
if 'sessions' in line or 'api_keys' in line: | |
for s in re.findall('(,1[0-9]{12})', line): | |
d = datetime.datetime.fromtimestamp(int(s[1:-3])).strftime('%Y-%m-%d %H:%M:%S') | |
line = line.replace(s, f",'{d}'", 1) | |
if 'CREATE UNIQUE INDEX `members_stripe_customers_customer_id_unique`' in line: | |
return '' | |
elif 'CREATE TABLE `members_stripe_customers`' in line: | |
line += 'CREATE UNIQUE INDEX `members_stripe_customers_customer_id_unique` on `members_stripe_customers` (`customer_id`);' | |
line = line.replace(r'"', r'\"') | |
line = line.replace(r'\\"', r'\\\"') # for html injections | |
return line | |
for line in fileinput.input(): | |
print(_translate_line(line), end='') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment