From b00647e3f323804753f14dc38a5ec3ff03ac217f Mon Sep 17 00:00:00 2001 From: j Date: Sun, 17 Jan 2016 13:49:31 +0530 Subject: [PATCH] fix drop column --- oml/db.py | 11 +++++++---- oml/item/models.py | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/oml/db.py b/oml/db.py index 06bcecf..5d0f3cd 100644 --- a/oml/db.py +++ b/oml/db.py @@ -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', diff --git a/oml/item/models.py b/oml/item/models.py index b621412..546dfc9 100644 --- a/oml/item/models.py +++ b/oml/item/models.py @@ -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 = []