Skip to content

Instantly share code, notes, and snippets.

@zhy0216
Created March 14, 2014 03:24

Revisions

  1. zhy0216 created this gist Mar 14, 2014.
    63 changes: 63 additions & 0 deletions gerrit-migrate-script.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    # -*- coding: utf-8 -*-

    import codecs


    STATE = "IDEL" # INSERT, SEQUENCE


    f = open("1.sql", "r")
    f_w = codecs.open("test.sql", 'w', 'utf-8')


    f_w.write("DELETE FROM ACCOUNT_GROUP_NAMES;DELETE FROM ACCOUNT_GROUPS;" + \
    # "ALTER TABLE changes ALTER COLUMN subject TYPE varchar(2047);" + \
    "CREATE TABLE SYSTEM_LOB_STREAM(ID INT NOT NULL, PART INT NOT NULL, CDATA VARCHAR, BDATA BYTEA);")

    for line in f:
    line_strip = line.strip();
    if STATE == "INSERT":
    l = line.replace("STRINGDECODE(", "(E")\
    .replace(r"\',","',")\
    .replace(r"\\'),", r"\\\\'),")\
    .replace("SYSTEM_COMBINE_CLOB", "")\
    .replace("''''", '""')\
    .replace("''',", '"\',')\
    .replace("''')", '"\')')\
    .replace("'''", '\'"')\
    .replace(r"\0", "")

    # unicode(l, "unicode-escape")
    l = unicode(l, "unicode-escape").replace(r"\''", r'""').replace(r"\u2026", u"…").replace("''", '"').replace('\\', r"\\")
    f_w.write(l)

    if line_strip.startswith("INSERT") and "SCHEMA_VERSION" not in line_strip and "SYSTEM_CONFIG" not in line_strip:
    STATE = "INSERT"
    l = line.replace("STRINGDECODE(", "(E")\
    .replace(r"\',","',")\
    .replace(r"\\'),", r"\\\\'),")\
    .replace("SYSTEM_COMBINE_CLOB", "")\
    .replace("''''", '""')\
    .replace("''',", '"\',')\
    .replace("''')", '"\')')\
    .replace("'''", '\'"')\
    .replace(r"\0", "")
    l = unicode(l, "unicode-escape").replace(r"\''", r'""').replace(r"\u2026", u"…").replace("''", '"').replace('\\', r"\\")
    f_w.write(l)
    if line_strip.endswith(";"):
    STATE = "IDEL"
    elif line_strip.startswith("CREATE SEQUENCE"):
    STATE = "SEQUENCE"
    f_w.write(line.replace("CREATE", "ALTER").replace("START", "RESTART"))
    elif line_strip.endswith(";"):
    STATE = "IDEL"

    f.close()
    f_w.close()