Skip to content

Commit 656d50c

Browse files
committed
Add options to skip ID and class fields in migration
1 parent aebb637 commit 656d50c

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

migrate.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
DEFAULT_VARCHAR_SIZE = 25
1212
MAX_VARCHAR_LENGTH = 1000
13+
SKIP_ID_FIELD = False
14+
SKIP_CLASS_FIELD = False
1315

1416
def camel_to_snake(name):
1517
name = name.replace(' ', '_')
@@ -26,10 +28,9 @@ def enquote(identifier):
2628
return f"`{identifier}`"
2729

2830
def type_to_mysql(column_name, py_type, max_length):
29-
DEFAULT_VARCHAR_SIZE = 25
30-
MAX_VARCHAR_LENGTH = 1000
31+
varchar_type=f'VARCHAR({min(max(DEFAULT_VARCHAR_SIZE, (max_length // DEFAULT_VARCHAR_SIZE + 1) * DEFAULT_VARCHAR_SIZE), MAX_VARCHAR_LENGTH)})' if max_length and max_length <= MAX_VARCHAR_LENGTH else 'TEXT'
3132
type_mapping = {
32-
'str': f'VARCHAR({min(max(DEFAULT_VARCHAR_SIZE, (max_length // DEFAULT_VARCHAR_SIZE + 1) * DEFAULT_VARCHAR_SIZE), MAX_VARCHAR_LENGTH)})' if max_length <= MAX_VARCHAR_LENGTH else 'TEXT',
33+
'str': varchar_type,
3334
'int': 'INT',
3435
'float': 'FLOAT',
3536
'bool': 'BOOLEAN',
@@ -107,7 +108,7 @@ def create_mysql_table(mysql_cursor, collection_name, document):
107108
max_lengths = {camel_to_snake(key): len(str(value)) for key, value in document.items() if key not in ['_id', '_class']}
108109
structure = {}
109110
for key, value in document.items():
110-
if key not in ['_id', '_class']:
111+
if key not in ['_id', '_class'] or (key == '_id' and not SKIP_ID_FIELD) or (key == '_class' and not SKIP_CLASS_FIELD):
111112
if isinstance(value, dict):
112113
structure.update(process_nested_document(value, camel_to_snake(key)))
113114
else:
@@ -123,7 +124,7 @@ def create_mysql_table(mysql_cursor, collection_name, document):
123124

124125
def insert_into_mysql(mysql_cursor, collection_name, document):
125126
collection_name = camel_to_snake(collection_name)
126-
document = {camel_to_snake(key): value for key, value in document.items() if key not in ['_id', '_class']}
127+
document = {camel_to_snake(key): value for key, value in document.items() if key not in ['_id', '_class'] or (key == '_id' and not SKIP_ID_FIELD) or (key == '_class' and not SKIP_CLASS_FIELD)}
127128
document = convert_document(document)
128129
keys = ', '.join(enquote(key) for key in document.keys())
129130
values = ', '.join(['%s' for _ in document.values()])

0 commit comments

Comments
 (0)