New method for update tables using directly old tables in mariadb

This commit is contained in:
Antonio de la Rosa 2024-04-16 01:27:29 +02:00
parent dec0613c75
commit 2599eabed0
9 changed files with 151 additions and 22 deletions

View file

@ -130,6 +130,32 @@ def start():
new_tables=[x for x in tables if x not in table_exists]
# Get foreignkeys
# SELECT * FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA='catalogdev_db' AND information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY' ;
foreignkey_fields={}
#| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE |
#+--------------------+-------------------+-----------------------------------------+---------------+-------------------+-----------------+
#| def | catalogdev_db | product_id_attributesIDX | catalogdev_db | attributes | FOREIGN KEY |
#WebModel.connections
db_name=WebModel.connections['default']['db']
with connection.query('SELECT * FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA=%s AND information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = %s', [db_name, 'FOREIGN KEY']) as cursor:
for row in cursor:
if not row['TABLE_NAME'] in foreignkey_fields:
foreignkey_fields[row['TABLE_NAME']]=[]
foreignkey_fields[row['TABLE_NAME']]=row['CONSTRAINT_NAME'].replace('_{}IDX'.format(row['TABLE_NAME']), '')
pass
#If don't want order
#new_tables=set(tables)-set(table_exists)
@ -187,6 +213,23 @@ def start():
print(Style.BRIGHT+"Checking old versions of model for find changes...")
for table in tables:
#print(table)
table_fields={table: {}}
# Field | Type | Null | Key | Default | Extra |
#| id | int(11) | NO | PRI | NULL | auto_increment |
with connection.query('describe %s' % table) as cursor:
#all_fields=cursor.fetchall()
#print(all_fields)
for row in cursor:
table_fields[table][row['Field']]={'type': row['Type'], 'key': row['Key']}
pass
#print(table_fields)
#connection.query("")
#Check if new table
@ -209,7 +252,8 @@ def start():
for f, v in WebModel.model[table].fields.items():
if not f in WebModel.model[old_table].fields:
#if not f in WebModel.model[old_table].fields:
if not f in table_fields[table]:
fields_to_add.append(f)
@ -253,13 +297,15 @@ def start():
#Add index
if v.indexed==True and v_old.indexed==False:
#if v.indexed==True and v_old.indexed==False:
if v.indexed==True and table_fields[table][f]['key']!='MUL':
fields_to_add_index.append(f)
changes+=1
if v.indexed==False and v_old.indexed==True:
#if v.indexed==False and v_old.indexed==True:
if v.indexed==False and table_fields[table][f]['key']=='MUL' and v.foreignkey==False:
fields_to_delete_index.append(f)
@ -267,13 +313,15 @@ def start():
#Add unique
if v.unique==True and v_old.unique==False:
#if v.unique==True and v_old.unique==False:
if v.unique==True and table_fields[table][f]['key']!='UNI':
fields_to_add_unique.append(f)
changes+=1
if v.unique==False and v_old.unique==True:
#if v.unique==False and v_old.unique==True:
if v.unique==False and table_fields[table][f]['key']=='UNI':
fields_to_delete_unique.append(f)
@ -281,29 +329,43 @@ def start():
#Add constraint
if v.foreignkey==True and v_old.foreignkey==False:
#if v.foreignkey==True and v_old.foreignkey==False:
if v.foreignkey==True and table_fields[table][f]['key']!='MUL':
fields_to_add_constraint.append(f)
changes+=1
if v.foreignkey==False and v_old.foreignkey==True:
#if v.foreignkey==False and v_old.foreignkey==True:
if v.foreignkey==False and table_fields[table][f]['key']=='MUL':
fields_to_delete_constraint.append(f)
if table in foreignkey_fields:
changes+=1
for f, v in WebModel.model[old_table].fields.items():
if f in foreignkey_fields[table]:
fields_to_delete_constraint.append(f)
changes+=1
# Clean fields
#for f, v in WebModel.model[old_table].fields.items():
for f, v in table_fields[table].items():
if not f in WebModel.model[table].fields:
#Add constraint
if v.foreignkey==True:
fields_to_delete_constraint.append(f)
changes+=1
#if v.foreignkey==True:
if table in foreignkey_fields:
if f in foreignkey_fields[table]:
fields_to_delete_constraint.append(f)
changes+=1
fields_to_delete.append(f)