Esempio n. 1
0
def alembic_tests():

    print(f'Alembic tests')
    conn = engine.connect()
    ctx = MigrationContext.configure(conn)
    op = Operations(ctx)

    try:
        op.drop_table('waste')
    except:
        pass

    t = op.create_table(
        'waste',
        Column('bools', sa.Boolean),
        Column('ubytes', sa.Tinyint),
        Column('shorts', sa.SmallInteger),
        Column('ints', sa.Integer),
        Column('bigints', sa.BigInteger),
        Column('floats', sa.REAL),
        Column('doubles', sa.Float),
        Column('dates', sa.Date),
        Column('datetimes', sa.DateTime),
        Column('varchars', sa.String(10)),
        Column('nvarchars', sa.UnicodeText),
        Column('numerics', sa.Numeric(38, 10)),
    )

    data = [{
        'bools': True,
        'ubytes': 5,
        'shorts': 55,
        'ints': 555,
        'bigints': 5555,
        'floats': 5.0,
        'doubles': 5.5555555,
        'dates': date(2012, 11, 23),
        'datetimes': datetime(2012, 11, 23, 16, 34, 56),
        'varchars': 'bla',
        'nvarchars': 'bla2',
        'numerics': Decimal("1.1")
    }, {
        'bools': False,
        'ubytes': 6,
        'shorts': 66,
        'ints': 666,
        'bigints': 6666,
        'floats': 6.0,
        'doubles': 6.6666666,
        'dates': date(2012, 11, 24),
        'datetimes': datetime(2012, 11, 24, 16, 34, 57),
        'varchars': 'bla',
        'nvarchars': 'bla2',
        'numerics': Decimal("-1.1")
    }]

    op.bulk_insert(t, data)

    res = engine.execute('select * from waste').fetchall()
    assert (res == [tuple(dikt.values()) for dikt in data])
Esempio n. 2
0
def init_online(app: Flask):
    db: SQLAlchemy = app.extensions['sqlalchemy'].db
    conn = db.engine.connect()
    ctx = MigrationContext.configure(conn)
    op = Operations(ctx)
    try:
        op.drop_table('seeds')
    except:
        pass
    op.create_table('seeds', sa.Column('file', sa.String(255), primary_key=True))
Esempio n. 3
0
    def swap_tables(self):
        """Swap tables around to present the exported data.

        Swaps the current tables to old tables, then swaps write tables to current.
        Finally drops the old tables leaving just the current tables.
        """
        connection = self.engine.connect()
        ctx = MigrationContext.configure(connection)
        op = Operations(ctx)

        def gen_table_names(write_table):
            """Generate current and old table names from write tables."""
            # Current tables do not have the prefix 'w'
            current_table = write_table[1:]
            old_table = current_table + "_old"
            return write_table, current_table, old_table

        tables = dict(Base.metadata.tables)
        tables.pop("kvittering")
        tables = tables.keys()
        tables = list(map(gen_table_names, tables))

        # Drop any left-over old tables that may exist
        with ctx.begin_transaction():
            for _, _, old_table in tables:
                try:
                    op.drop_table(old_table)
                except Exception:
                    pass

        # Rename current to old and write to current
        with ctx.begin_transaction():
            for write_table, current_table, old_table in tables:
                # Rename current table to old table
                # No current tables is OK
                try:
                    op.rename_table(current_table, old_table)
                except Exception:
                    pass
                # Rename write table to current table
                op.rename_table(write_table, current_table)

        # Drop any old tables that may exist
        with ctx.begin_transaction():
            for _, _, old_table in tables:
                # Drop old tables
                try:
                    op.drop_table(old_table)
                except Exception:
                    pass
Esempio n. 4
0
def recheck_alembic_table(conn):
    """check and update alembic version table.

    Should check current alembic version table against conf and rename the
    existing table if the two values don't match.
    """
    conf_table = getattr(CONF, 'version_table')
    conf_table_version = get_table_version(conn, conf_table)
    current_table, default_table = get_db_tables(conn)
    if current_table[0]:
        if current_table[0] != conf_table:
            context = alembic_migration.MigrationContext.configure(conn)
            op = Operations(context)
            if conf_table and not conf_table_version:
                # make sure there is not present-but-empty table
                # that will prevent us from renaming the current table
                op.drop_table(conf_table)
            op.rename_table(current_table[0], conf_table)
Esempio n. 5
0
def recheck_alembic_table(conn):
    """check and update alembic version table.

    Should check current alembic version table against conf and rename the
    existing table if the two values don't match.
    """
    conf_table = getattr(CONF, 'version_table')
    conf_table_version = get_table_version(conn, conf_table)
    current_table, default_table = get_db_tables(conn)
    if current_table[0]:
        if current_table[0] != conf_table:
            context = alembic_migration.MigrationContext.configure(conn)
            op = Operations(context)
            if conf_table and not conf_table_version:
                # make sure there is not present-but-empty table
                # that will prevent us from renaming the current table
                op.drop_table(conf_table)
            op.rename_table(current_table[0], conf_table)
Esempio n. 6
0
from sqlalchemy.ext.declarative import declarative_base

from alembic.operations import Operations
from alembic.migration import MigrationContext

Base = declarative_base()
engine = sa.create_engine('mysql://*****:*****@localhost/jpic')
Session = orm.sessionmaker(bind=engine)
session = Session()

conn = engine.connect()
ctx = MigrationContext.configure(conn)
op = Operations(ctx)

for table in ('person_car', 'cars', 'houses', 'persons'):
    op.drop_table(table)


class PersonTest(unittest.TestCase):
    def test_000_create_table(self):

        self.__class__.Person = type('Person', (Base,), {'__tablename__': 'persons',
            'id': sa.Column(sa.Integer, primary_key=True)})

        self.__class__.Car = type('Car', (Base,), {'__tablename__': 'cars',
            'id': sa.Column(sa.Integer, primary_key=True)})

        self.__class__.House = type('House', (Base,), {'__tablename__': 'houses',
            'id': sa.Column(sa.Integer, primary_key=True)})

        Base.metadata.create_all(engine)