Skip to content

Commit

Permalink
A few fixes to migtool uuid & common list of cols
Browse files Browse the repository at this point in the history
The MS webapp used to create uppercase UUIDs. Most setups won't experience it because they used the hybrid version but older setups might (like Niger).
We tried to detect the data type of DB columns but it's a mess. Some are of `uniqueidentifier` type but others are plain char(32) or char(36). So we resorted to adding a check in the string formatting itself.

Additionally, the tool was only based on the new DB column list, so in tblLanguages, a country_code appeared in Postgres, causing a missing column error on the source. We now take the common denominator between the old and new columns.
  • Loading branch information
edarchis committed Dec 20, 2023
1 parent c4411ec commit 0e418c2
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion migtool/migtool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import uuid

import pyodbc # adapter for SQL Server
import psycopg2 # adapter for PostgreSQL
Expand Down Expand Up @@ -50,6 +51,14 @@ def get_settings_from_file():
exit(1)


def is_uuid(value):
try:
uuid.UUID(value, version=4)
return True
except ValueError:
return False


# tries to connect to both databases
def connect():
print("Setting up connection to the databases:")
Expand Down Expand Up @@ -134,6 +143,9 @@ def generate_insertion_string(row):
for x in row:
# Strings must be enclosed in apostrophes, also escape singe quotes in a string by doubling them
if isinstance(x, str):
# The .NET webapp used to create uppercase UUIDs, so we try to detect it and lowercase it
if 32 <= len(x) <= 36 and is_uuid(x):
x = x.lower()
row_list.append("'" + str(x).replace("'", "''") + "'")
# Dates and datetimes must be enclosed in apostrophes
elif isinstance(x, datetime.datetime) or isinstance(x, datetime.date):
Expand Down Expand Up @@ -225,6 +237,9 @@ def migrate():
"\"FeedbackUUID\", \"AuditUserID\") VALUES ('2000 01 01 00:00:00.000000', 0, 0, 0);")

# Set up all the columns we're going to migrate.
cursor = old_cursor.execute("SELECT TOP 1 * FROM " + table + ";")
old_columns_with_types = {column[0].lower(): column[1] for column in cursor.description}

new_cursor.execute("SELECT COLUMN_NAME, COLUMN_DEFAULT "
"FROM information_schema.COLUMNS WHERE TABLE_NAME = '" + table + "';")
rows = new_cursor.fetchall()
Expand All @@ -238,7 +253,7 @@ def migrate():
old_cols_list = []
new_cols_list = []
for row in rows:
if row[0] not in EXCLUDED_COLUMNS:
if row[0] not in EXCLUDED_COLUMNS and row[0].lower() in old_columns_with_types:
col_default = extract_sequence_name(row[1])
if col_default:
sequence_columns[row[0]] = col_default
Expand Down

0 comments on commit 0e418c2

Please sign in to comment.