diff options
| author | Barry Warsaw | 2015-10-20 22:48:59 -0400 |
|---|---|---|
| committer | Barry Warsaw | 2015-10-20 22:48:59 -0400 |
| commit | 5f917c8aec735608cb85195e691fb68fbb835af7 (patch) | |
| tree | 2a963a102f7610a118cd211711c4df90aada94cd /src/mailman/database | |
| parent | adb9e3164ee1e19802a4373e76b42b1435d1e687 (diff) | |
| download | mailman-5f917c8aec735608cb85195e691fb68fbb835af7.tar.gz mailman-5f917c8aec735608cb85195e691fb68fbb835af7.tar.zst mailman-5f917c8aec735608cb85195e691fb68fbb835af7.zip | |
Clean up pass through abompard's branch.
Diffstat (limited to 'src/mailman/database')
| -rw-r--r-- | src/mailman/database/alembic/versions/42756496720_header_matches.py | 35 | ||||
| -rw-r--r-- | src/mailman/database/tests/test_migrations.py | 18 |
2 files changed, 28 insertions, 25 deletions
diff --git a/src/mailman/database/alembic/versions/42756496720_header_matches.py b/src/mailman/database/alembic/versions/42756496720_header_matches.py index 83a1275a2..5e8db9756 100644 --- a/src/mailman/database/alembic/versions/42756496720_header_matches.py +++ b/src/mailman/database/alembic/versions/42756496720_header_matches.py @@ -18,7 +18,8 @@ from mailman.database.helpers import is_sqlite, exists_in_db def upgrade(): # Create the new table - header_match_table = op.create_table('headermatch', + header_match_table = op.create_table( + 'headermatch', sa.Column('id', sa.Integer(), nullable=False), sa.Column('mailing_list_id', sa.Integer(), nullable=True), sa.Column('header', sa.Unicode(), nullable=False), @@ -26,17 +27,17 @@ def upgrade(): sa.Column('chain', sa.Unicode(), nullable=True), sa.ForeignKeyConstraint(['mailing_list_id'], ['mailinglist.id'], ), sa.PrimaryKeyConstraint('id') - ) - - # Now migrate the data. It can't be offline because we need to read the + ) + # Now migrate the data. It can't be offline because we need to read the # pickles. connection = op.get_bind() # Don't import the table definition from the models, it may break this # migration when the model is updated in the future (see the Alembic doc). - mlist_table = sa.sql.table('mailinglist', + mlist_table = sa.sql.table( + 'mailinglist', sa.sql.column('id', sa.Integer), sa.sql.column('header_matches', sa.PickleType) - ) + ) for mlist_id, old_matches in connection.execute(mlist_table.select()): for old_match in old_matches: connection.execute(header_match_table.insert().values( @@ -44,8 +45,7 @@ def upgrade(): header=old_match[0], pattern=old_match[1], chain=None - )) - + )) # Now that data is migrated, drop the old column (except on SQLite which # does not support this) if not is_sqlite(connection): @@ -56,23 +56,25 @@ def downgrade(): if not exists_in_db(op.get_bind(), 'mailinglist', 'header_matches'): # SQLite will not have deleted the former column, since it does not # support column deletion. - op.add_column('mailinglist', sa.Column( - 'header_matches', sa.PickleType, nullable=True)) - - # Now migrate the data. It can't be offline because we need to read the + op.add_column( + 'mailinglist', + sa.Column('header_matches', sa.PickleType, nullable=True)) + # Now migrate the data. It can't be offline because we need to read the # pickles. connection = op.get_bind() # Don't import the table definition from the models, it may break this # migration when the model is updated in the future (see the Alembic doc). - mlist_table = sa.sql.table('mailinglist', + mlist_table = sa.sql.table( + 'mailinglist', sa.sql.column('id', sa.Integer), sa.sql.column('header_matches', sa.PickleType) - ) - header_match_table = sa.sql.table('headermatch', + ) + header_match_table = sa.sql.table( + 'headermatch', sa.sql.column('mailing_list_id', sa.Integer), sa.sql.column('header', sa.Unicode), sa.sql.column('pattern', sa.Unicode), - ) + ) for mlist_id, header, pattern in connection.execute( header_match_table.select()).fetchall(): mlist = connection.execute(mlist_table.select().where( @@ -84,5 +86,4 @@ def downgrade(): connection.execute(mlist_table.update().where( mlist_table.c.id == mlist_id).values( header_matches=header_matches)) - op.drop_table('headermatch') diff --git a/src/mailman/database/tests/test_migrations.py b/src/mailman/database/tests/test_migrations.py index 6f167cd13..91f19bfb2 100644 --- a/src/mailman/database/tests/test_migrations.py +++ b/src/mailman/database/tests/test_migrations.py @@ -46,9 +46,9 @@ class TestMigrations(unittest.TestCase): md = sa.MetaData(bind=config.db.engine) md.reflect() # We have circular dependencies between user and address, thus we can't - # use drop_all() without getting a warning. Setting use_alter to True + # use drop_all() without getting a warning. Setting use_alter to True # on the foreign keys helps SQLAlchemy mark those loops as known. - for tablename in ("user", "address"): + for tablename in ('user', 'address'): if tablename not in md.tables: continue for fk in md.tables[tablename].foreign_keys: @@ -70,17 +70,19 @@ class TestMigrations(unittest.TestCase): ('test-header-1', 'test-pattern-1'), ('test-header-2', 'test-pattern-2'), ('test-header-3', 'test-pattern-3'), - ] - mlist_table = sa.sql.table('mailinglist', + ] + mlist_table = sa.sql.table( + 'mailinglist', sa.sql.column('id', sa.Integer), sa.sql.column('header_matches', sa.PickleType) ) - header_match_table = sa.sql.table('headermatch', + header_match_table = sa.sql.table( + 'headermatch', sa.sql.column('mailing_list_id', sa.Integer), sa.sql.column('header', sa.Unicode), sa.sql.column('pattern', sa.Unicode), - ) - # Downgrading + ) + # Downgrading. config.db.store.execute(mlist_table.insert().values(id=1)) config.db.store.execute(header_match_table.insert().values( [{'mailing_list_id': 1, 'header': hm[0], 'pattern': hm[1]} @@ -92,7 +94,7 @@ class TestMigrations(unittest.TestCase): self.assertEqual(results[0].header_matches, test_header_matches) self.assertFalse(exists_in_db(config.db.engine, 'headermatch')) config.db.store.commit() - # Upgrading + # Upgrading. alembic.command.upgrade(alembic_cfg, '42756496720') results = config.db.store.execute( header_match_table.select()).fetchall() |
