Example #1
0
def _prep_testing_database(options, file_config):
    from alembic.testing import config
    from alembic.testing.exclusions import against
    from sqlalchemy import schema
    from alembic import util

    if util.sqla_08:
        from sqlalchemy import inspect
    else:
        from sqlalchemy.engine.reflection import Inspector
        inspect = Inspector.from_engine

    if options.dropfirst:
        for cfg in config.Config.all_configs():
            e = cfg.db
            inspector = inspect(e)
            try:
                view_names = inspector.get_view_names()
            except NotImplementedError:
                pass
            else:
                for vname in view_names:
                    e.execute(schema._DropView(
                        schema.Table(vname, schema.MetaData())
                    ))

            if config.requirements.schemas.enabled_for_config(cfg):
                try:
                    view_names = inspector.get_view_names(
                        schema="test_schema")
                except NotImplementedError:
                    pass
                else:
                    for vname in view_names:
                        e.execute(schema._DropView(
                            schema.Table(vname, schema.MetaData(),
                                         schema="test_schema")
                        ))

            for tname in reversed(inspector.get_table_names(
                    order_by="foreign_key")):
                e.execute(schema.DropTable(
                    schema.Table(tname, schema.MetaData())
                ))

            if config.requirements.schemas.enabled_for_config(cfg):
                for tname in reversed(inspector.get_table_names(
                        order_by="foreign_key", schema="test_schema")):
                    e.execute(schema.DropTable(
                        schema.Table(tname, schema.MetaData(),
                                     schema="test_schema")
                    ))

            if against(cfg, "postgresql") and util.sqla_100:
                from sqlalchemy.dialects import postgresql
                for enum in inspector.get_enums("*"):
                    e.execute(postgresql.DropEnumType(
                        postgresql.ENUM(
                            name=enum['name'],
                            schema=enum['schema'])))
Example #2
0
def _prep_testing_database(options, file_config):
    from sqlalchemy.testing import config, util
    from sqlalchemy.testing.exclusions import against
    from sqlalchemy import schema, inspect

    if options.dropfirst:
        for cfg in config.Config.all_configs():
            e = cfg.db

            # TODO: this has to be part of provision.py in postgresql
            if against(cfg, "postgresql"):
                with e.connect().execution_options(
                        isolation_level="AUTOCOMMIT") as conn:
                    for xid in conn.execute(
                            "select gid from pg_prepared_xacts").scalars():
                        conn.execute("ROLLBACK PREPARED '%s'" % xid)

            inspector = inspect(e)
            try:
                view_names = inspector.get_view_names()
            except NotImplementedError:
                pass
            else:
                for vname in view_names:
                    e.execute(
                        schema._DropView(schema.Table(vname,
                                                      schema.MetaData())))

            if config.requirements.schemas.enabled_for_config(cfg):
                try:
                    view_names = inspector.get_view_names(schema="test_schema")
                except NotImplementedError:
                    pass
                else:
                    for vname in view_names:
                        e.execute(
                            schema._DropView(
                                schema.Table(
                                    vname,
                                    schema.MetaData(),
                                    schema="test_schema",
                                )))

            util.drop_all_tables(e, inspector)

            if config.requirements.schemas.enabled_for_config(cfg):
                util.drop_all_tables(e, inspector, schema=cfg.test_schema)

            # TODO: this has to be part of provision.py in postgresql
            if against(cfg, "postgresql"):
                from sqlalchemy.dialects import postgresql

                for enum in inspector.get_enums("*"):
                    e.execute(
                        postgresql.DropEnumType(
                            postgresql.ENUM(name=enum["name"],
                                            schema=enum["schema"])))
Example #3
0
def drop_all_schema_objects_post_tables(cfg, eng):
    from sqlalchemy.dialects import postgresql

    inspector = inspect(eng)
    with eng.begin() as conn:
        for enum in inspector.get_enums("*"):
            conn.execute(
                postgresql.DropEnumType(
                    postgresql.ENUM(name=enum["name"], schema=enum["schema"])))
Example #4
0
def _prep_testing_database(options, file_config):
    from sqlalchemy.testing import config, util
    from sqlalchemy.testing.exclusions import against
    from sqlalchemy import schema, inspect

    if options.dropfirst:
        for cfg in config.Config.all_configs():
            e = cfg.db
            inspector = inspect(e)
            try:
                view_names = inspector.get_view_names()
            except NotImplementedError:
                pass
            else:
                for vname in view_names:
                    e.execute(
                        schema._DropView(
                            schema.Table(vname, schema.MetaData())
                        )
                    )

            if config.requirements.schemas.enabled_for_config(cfg):
                try:
                    view_names = inspector.get_view_names(schema="test_schema")
                except NotImplementedError:
                    pass
                else:
                    for vname in view_names:
                        e.execute(
                            schema._DropView(
                                schema.Table(
                                    vname,
                                    schema.MetaData(),
                                    schema="test_schema",
                                )
                            )
                        )

            util.drop_all_tables(e, inspector)

            if config.requirements.schemas.enabled_for_config(cfg):
                util.drop_all_tables(e, inspector, schema=cfg.test_schema)

            if against(cfg, "postgresql"):
                from sqlalchemy.dialects import postgresql

                for enum in inspector.get_enums("*"):
                    e.execute(
                        postgresql.DropEnumType(
                            postgresql.ENUM(
                                name=enum["name"], schema=enum["schema"]
                            )
                        )
                    )
Example #5
0
 def test_generic_enum(self):
     e1 = Enum('x', 'y', 'z', name='somename')
     e2 = Enum('x', 'y', 'z', name='somename', schema='someschema')
     self.assert_compile(postgresql.CreateEnumType(e1),
                         "CREATE TYPE somename AS ENUM ('x', 'y', 'z')")
     self.assert_compile(
         postgresql.CreateEnumType(e2),
         "CREATE TYPE someschema.somename AS ENUM "
         "('x', 'y', 'z')")
     self.assert_compile(postgresql.DropEnumType(e1), 'DROP TYPE somename')
     self.assert_compile(postgresql.DropEnumType(e2),
                         'DROP TYPE someschema.somename')
     t1 = Table('sometable', MetaData(), Column('somecolumn', e1))
     self.assert_compile(schema.CreateTable(t1),
                         'CREATE TABLE sometable (somecolumn '
                         'somename)')
     t1 = Table(
         'sometable', MetaData(),
         Column('somecolumn', Enum('x', 'y', 'z', native_enum=False)))
     self.assert_compile(
         schema.CreateTable(t1), "CREATE TABLE sometable (somecolumn "
         "VARCHAR(1), CHECK (somecolumn IN ('x', "
         "'y', 'z')))")