From 0e418c2dbfd59b547372db7b7abaa23683492e04 Mon Sep 17 00:00:00 2001 From: Eric Darchis Date: Wed, 20 Dec 2023 18:32:27 +0100 Subject: [PATCH] A few fixes to migtool uuid & common list of cols 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. --- migtool/migtool.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/migtool/migtool.py b/migtool/migtool.py index 0110505..e00d4c8 100644 --- a/migtool/migtool.py +++ b/migtool/migtool.py @@ -1,4 +1,5 @@ import re +import uuid import pyodbc # adapter for SQL Server import psycopg2 # adapter for PostgreSQL @@ -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:") @@ -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): @@ -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() @@ -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