"Fossies" - the Fresh Open Source Software Archive

Member "mailman-3.3.7/src/mailman/database/alembic/versions/42756496720_header_matches.py" (10 Nov 2022, 3552 Bytes) of package /linux/misc/mailman-3.3.7.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "42756496720_header_matches.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.3.6_vs_3.3.7.

    1 """header_matches
    2 
    3 Revision ID: 42756496720
    4 Revises: 2bb9b382198
    5 Create Date: 2015-09-11 10:11:38.310315
    6 
    7 """
    8 
    9 import sqlalchemy as sa
   10 
   11 from alembic import op
   12 from mailman.database.helpers import exists_in_db, is_sqlite
   13 from mailman.database.types import SAUnicode
   14 
   15 
   16 # Revision identifiers, used by Alembic.
   17 revision = '42756496720'
   18 down_revision = '2bb9b382198'
   19 
   20 
   21 def upgrade():
   22     # Create the new table
   23     header_match_table = op.create_table(
   24         'headermatch',
   25         sa.Column('id', sa.Integer(), nullable=False),
   26         sa.Column('mailing_list_id', sa.Integer(), nullable=True),
   27         sa.Column('header', SAUnicode(), nullable=False),
   28         sa.Column('pattern', SAUnicode(), nullable=False),
   29         sa.Column('chain', SAUnicode(), nullable=True),
   30         sa.ForeignKeyConstraint(['mailing_list_id'], ['mailinglist.id'], ),
   31         sa.PrimaryKeyConstraint('id')
   32         )
   33     # Now migrate the data.  It can't be offline because we need to read the
   34     # pickles.
   35     connection = op.get_bind()
   36     # Don't import the table definition from the models, it may break this
   37     # migration when the model is updated in the future (see the Alembic doc).
   38     mlist_table = sa.sql.table(
   39         'mailinglist',
   40         sa.sql.column('id', sa.Integer),
   41         sa.sql.column('header_matches', sa.PickleType)
   42         )
   43     for mlist_id, old_matches in connection.execute(mlist_table.select()):
   44         for old_match in old_matches:
   45             connection.execute(header_match_table.insert().values(
   46                 mailing_list_id=mlist_id,
   47                 header=old_match[0],
   48                 pattern=old_match[1],
   49                 chain=None
   50                 ))
   51     # Now that data is migrated, drop the old column (except on SQLite which
   52     # does not support this)
   53     if not is_sqlite(connection):
   54         op.drop_column('mailinglist', 'header_matches')
   55 
   56 
   57 def downgrade():
   58     if not exists_in_db(op.get_bind(), 'mailinglist', 'header_matches'):
   59         # SQLite will not have deleted the former column, since it does not
   60         # support column deletion.
   61         op.add_column(
   62             'mailinglist',
   63             sa.Column('header_matches', sa.PickleType, nullable=True))
   64     # Now migrate the data.  It can't be offline because we need to read the
   65     # pickles.
   66     connection = op.get_bind()
   67     # Don't import the table definition from the models, it may break this
   68     # migration when the model is updated in the future (see the Alembic doc).
   69     mlist_table = sa.sql.table(
   70         'mailinglist',
   71         sa.sql.column('id', sa.Integer),
   72         sa.sql.column('header_matches', sa.PickleType)
   73         )
   74     header_match_table = sa.sql.table(
   75         'headermatch',
   76         sa.sql.column('mailing_list_id', sa.Integer),
   77         sa.sql.column('header', SAUnicode),
   78         sa.sql.column('pattern', SAUnicode),
   79         )
   80     for mlist_id, header, pattern in connection.execute(
   81             header_match_table.select()).fetchall():
   82         stmt = sa.select(mlist_table).where(
   83             mlist_table.c.id == mlist_id)
   84         mlist = connection.execute(stmt).one()
   85         # Above returns a tuple with `mlist.id, mlist.header_matches`
   86         # So pick the 2nd element, whichi is the header_matches.
   87         header_matches = mlist[1]
   88         if not header_matches:
   89             header_matches = []
   90         header_matches.append((header, pattern))
   91         connection.execute(mlist_table.update().where(
   92             mlist_table.c.id == mlist_id).values(
   93             header_matches=header_matches))
   94     op.drop_table('headermatch')