Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few fixes to migtool uuid & common list of cols #23

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading