Skip to content

Instantly share code, notes, and snippets.

@ckhung
Last active May 24, 2021 03:03
Show Gist options
  • Save ckhung/a0a53bec34c85bac65ae6163c57c86ab to your computer and use it in GitHub Desktop.
Save ckhung/a0a53bec34c85bac65ae6163c57c86ab to your computer and use it in GitHub Desktop.
a hackish script to convert this MS sql server script: https://www.dofactory.com/sql to a sqlite script
#!/bin/sh
# a hackish script to convert this MS sql server script:
# https://www.dofactory.com/sql
# to a sqlite script
#
# usage:
# 1. ./ms2sqlite.sh sample-model.sql > model-sqlite.sql
# 2. edit model-sqlite.sql and manually:
# 2.1 remove all the "if exists ..."
# 2.2 change "datetime" to something else such as text. See
# https://www.sqlitetutorial.net/sqlite-date/
# Also <change not null default getdate()> to <default ''>
# 2.3 change the unfortunate table name "Order" to
# something else such as "COrder"
# 2.4 remove all the "alter table ..." at the end
# because to specify the foreign key,sqlite requires a different syntax:
# https://www.techonthenet.com/sqlite/foreign_keys/foreign_keys.php
# 3. rm -f dofactory.db ; sqlite3 dofactory.db < model-sqlite.sql
# 4. ./ms2sqlite.sh sample-data.sql > data-sqlite.sql
# 5. edit data-sqlite.sql and manually:
# 5.1 add ";" to the end of each command
# 5.2 change the unfortunate table name "Order" to
# the new name in step 2.3.
# 6. sqlite3 dofactory.db < data-sqlite.sql
perl -ne ' \
s/^[[:^ascii:]]+//; \
s/\015// ; \
s/^go$/;/ ; \
s/nvarchar\(\d+\)/text/ ; \
s/identity/primary key/ ; \
s/\x27[A-Z][a-z][a-z]\s+[\d:\s]+[AP]M\x27/null/g ; \
s#^(\s*set\s.*)#/* $1 */#i ;
s/[\[\]]//g if /^\s*insert/i ;
print unless /constraint.*primary key.*/ ' $1 | \
perl -000 -pe 's/\)\s*;/ );/g ; s/,\s*\)/)/g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment