diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2013-10-10 14:06:07 -0500 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2013-10-10 14:06:07 -0500 |
commit | 2c4decf2607f5005523b319b2a26cf218bce8833 (patch) | |
tree | 15c5dbe3887bab93f024cd01edf12ec80ad3466c /mediagoblin/db/migration_tools.py | |
parent | f1318b59c817c1847dd19c3c7d01559039f6e049 (diff) | |
parent | e5196ff0006da687ae31466d8c8e384c6be05ccd (diff) | |
download | mediagoblin-2c4decf2607f5005523b319b2a26cf218bce8833.tar.lz mediagoblin-2c4decf2607f5005523b319b2a26cf218bce8833.tar.xz mediagoblin-2c4decf2607f5005523b319b2a26cf218bce8833.zip |
Merge remote-tracking branch 'tilly-q/merge-tillyq-moderation' into merge-tillyq-moderation
Diffstat (limited to 'mediagoblin/db/migration_tools.py')
-rw-r--r-- | mediagoblin/db/migration_tools.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/mediagoblin/db/migration_tools.py b/mediagoblin/db/migration_tools.py index e75f3757..70cd92c8 100644 --- a/mediagoblin/db/migration_tools.py +++ b/mediagoblin/db/migration_tools.py @@ -16,6 +16,7 @@ from mediagoblin.tools.common import simple_printer from sqlalchemy import Table +from sqlalchemy.sql import select class TableAlreadyExists(Exception): pass @@ -286,3 +287,29 @@ def inspect_table(metadata, table_name): """Simple helper to get a ref to an already existing table""" return Table(table_name, metadata, autoload=True, autoload_with=metadata.bind) + +def replace_table(db, old_table,replacement_table): + """A function to fully replace a current table with a new one for migrati- + -ons. This is necessary because some changes are made tricky in some situa- + -tion, for example, dropping a boolean column in sqlite is impossible w/o + this method + + :param old_table A ref to the old table, gotten through + inspect_table + + :param replacement_table A ref to the new table, gotten through + inspect_table""" + surviving_columns = replacement_table.columns.keys() + old_table_name = old_table.name + for row in db.execute(select( + [column for column in old_table.columns + if column.name in surviving_columns])): + + db.execute(replacement_table.insert().values(**row)) + db.commit() + + old_table.drop() + db.commit() + + replacement_table.name=old_table_name + db.commit() |