fix drop column

This commit is contained in:
j 2016-01-17 13:49:31 +05:30
parent fd96d2da37
commit b00647e3f3
2 changed files with 9 additions and 6 deletions

View File

@ -93,24 +93,27 @@ def get_create_table(table):
row = s.connection().execute(sql, (table, )).fetchone()
return row[0] if row else None
def get_table_columms(table):
def get_table_columns(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):
if isinstance(columns, str):
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 = {
'table': table,
'columns': ','.join(new_columns),
}
create_table = get_create_table(table)
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 = re.sub('\n *', '\n ', create_table).replace('( ', '(\n ')
sql = [
'DROP TABLE IF EXISTS {table}_old',
'ALTER TABLE {table} RENAME TO {table}_old',
create_table,
'INSERT INTO {table} ({columns}) SELECT {columns} FROM {table}_old',

View File

@ -783,11 +783,11 @@ def remove_unused_names():
state.db.session.commit()
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']))
add_columns = list(set(Item.sort_keys+['item_id'])-set(current))
if drop_columns:
db.drop_columns('sort', drop_columns)
add_columns = list(set(Item.sort_keys)-set(current+['item_id']))
if add_columns:
create_table = str(CreateTable(Sort.__table__).compile(db.engine)).split('\n')
sql = []