fix drop column
This commit is contained in:
parent
fd96d2da37
commit
b00647e3f3
2 changed files with 9 additions and 6 deletions
11
oml/db.py
11
oml/db.py
|
@ -93,24 +93,27 @@ def get_create_table(table):
|
||||||
row = s.connection().execute(sql, (table, )).fetchone()
|
row = s.connection().execute(sql, (table, )).fetchone()
|
||||||
return row[0] if row else None
|
return row[0] if row else None
|
||||||
|
|
||||||
def get_table_columms(table):
|
def get_table_columns(table):
|
||||||
create_table = get_create_table(table)
|
create_table = get_create_table(table)
|
||||||
return [r.strip().split()[0] for r in re.compile('(.*?),').findall(create_table)][:-1]
|
columns = [r.strip().split()[0] for r in re.compile('(.*?),').findall(create_table)][:-1]
|
||||||
|
columns = [col for col in columns if col.islower()]
|
||||||
|
return columns
|
||||||
|
|
||||||
def drop_columns(table, columns):
|
def drop_columns(table, columns):
|
||||||
if isinstance(columns, str):
|
if isinstance(columns, str):
|
||||||
columns = [columns]
|
columns = [columns]
|
||||||
new_columns = [c for c in get_table_columms(table) if c not in columns]
|
new_columns = [c for c in get_table_columns(table) if c not in columns]
|
||||||
info = {
|
info = {
|
||||||
'table': table,
|
'table': table,
|
||||||
'columns': ','.join(new_columns),
|
'columns': ','.join(new_columns),
|
||||||
}
|
}
|
||||||
create_table = get_create_table(table)
|
create_table = get_create_table(table)
|
||||||
for column in columns:
|
for column in columns:
|
||||||
create_table = re.sub('( %s .*?,)'%column, '', create_table)
|
create_table = re.sub('(%s .*?,)'%column, '', create_table)
|
||||||
create_table = create_table.replace('\n', '').replace(',', ',\n')
|
create_table = create_table.replace('\n', '').replace(',', ',\n')
|
||||||
create_table = re.sub('\n *', '\n ', create_table).replace('( ', '(\n ')
|
create_table = re.sub('\n *', '\n ', create_table).replace('( ', '(\n ')
|
||||||
sql = [
|
sql = [
|
||||||
|
'DROP TABLE IF EXISTS {table}_old',
|
||||||
'ALTER TABLE {table} RENAME TO {table}_old',
|
'ALTER TABLE {table} RENAME TO {table}_old',
|
||||||
create_table,
|
create_table,
|
||||||
'INSERT INTO {table} ({columns}) SELECT {columns} FROM {table}_old',
|
'INSERT INTO {table} ({columns}) SELECT {columns} FROM {table}_old',
|
||||||
|
|
|
@ -783,11 +783,11 @@ def remove_unused_names():
|
||||||
state.db.session.commit()
|
state.db.session.commit()
|
||||||
|
|
||||||
def update_sort_table():
|
def update_sort_table():
|
||||||
current = db.get_table_columms('sort')
|
current = db.get_table_columns('sort')
|
||||||
drop_columns = list(set(current) - set(Item.sort_keys+['item_id']))
|
drop_columns = list(set(current) - set(Item.sort_keys+['item_id']))
|
||||||
add_columns = list(set(Item.sort_keys+['item_id'])-set(current))
|
|
||||||
if drop_columns:
|
if drop_columns:
|
||||||
db.drop_columns('sort', drop_columns)
|
db.drop_columns('sort', drop_columns)
|
||||||
|
add_columns = list(set(Item.sort_keys)-set(current+['item_id']))
|
||||||
if add_columns:
|
if add_columns:
|
||||||
create_table = str(CreateTable(Sort.__table__).compile(db.engine)).split('\n')
|
create_table = str(CreateTable(Sort.__table__).compile(db.engine)).split('\n')
|
||||||
sql = []
|
sql = []
|
||||||
|
|
Loading…
Reference in a new issue