From: Mike Bayer Date: Tue, 22 Aug 2017 17:31:29 +0000 (-0400) Subject: - merge latest CI options from SQLAlchemy X-Git-Tag: rel_0_9_6~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c667a91d8b6305f252671e917d6d5601f2a05e9b;p=thirdparty%2Fsqlalchemy%2Falembic.git - merge latest CI options from SQLAlchemy Change-Id: Ia84a7f0779260874137e8d1b3a14b4f6e4778752 --- diff --git a/alembic/testing/plugin/plugin_base.py b/alembic/testing/plugin/plugin_base.py index 7b27e69e..0ed8f847 100644 --- a/alembic/testing/plugin/plugin_base.py +++ b/alembic/testing/plugin/plugin_base.py @@ -63,7 +63,7 @@ def setup_options(make_option): make_option("--db", action="append", type="string", dest="db", help="Use prefab database uri. Multiple OK, " "first one is run by default.") - make_option('--dbs', action='callback', callback=_list_dbs, + make_option('--dbs', action='callback', zeroarg_callback=_list_dbs, help="List available prefab dbs") make_option("--dburi", action="append", type="string", dest="dburi", help="Database uri. Multiple OK, " @@ -72,6 +72,9 @@ def setup_options(make_option): help="Drop all tables in the target database first") make_option("--backend-only", action="store_true", dest="backend_only", help="Run only tests marked with __backend__") + make_option("--postgresql-templatedb", type="string", + help="name of template database to use for Postgresql " + "CREATE DATABASE (defaults to current database)") make_option("--low-connections", action="store_true", dest="low_connections", help="Use a low number of distinct connections - " diff --git a/alembic/testing/plugin/pytestplugin.py b/alembic/testing/plugin/pytestplugin.py index d3d39ba3..15c7e078 100644 --- a/alembic/testing/plugin/pytestplugin.py +++ b/alembic/testing/plugin/pytestplugin.py @@ -38,6 +38,26 @@ def pytest_addoption(parser): callback_(option_string, values, parser) kw["action"] = CallableAction + zeroarg_callback = kw.pop("zeroarg_callback", None) + if zeroarg_callback: + class CallableAction(argparse.Action): + def __init__(self, option_strings, + dest, default=False, + required=False, help=None): + super(CallableAction, self).__init__( + option_strings=option_strings, + dest=dest, + nargs=0, + const=True, + default=default, + required=required, + help=help) + + def __call__(self, parser, namespace, + values, option_string=None): + zeroarg_callback(option_string, values, parser) + kw["action"] = CallableAction + group.addoption(name, **kw) plugin_base.setup_options(make_option) diff --git a/alembic/testing/provision.py b/alembic/testing/provision.py index 45289d11..faf437c9 100644 --- a/alembic/testing/provision.py +++ b/alembic/testing/provision.py @@ -163,25 +163,31 @@ def _sqlite_post_configure_engine(url, engine, follower_ident): @_create_db.for_db("postgresql") def _pg_create_db(cfg, eng, ident): + template_db = cfg.options.postgresql_templatedb + with eng.connect().execution_options( isolation_level="AUTOCOMMIT") as conn: try: _pg_drop_db(cfg, conn, ident) except Exception: pass - currentdb = conn.scalar("select current_database()") + if not template_db: + template_db = conn.scalar("select current_database()") for attempt in range(3): try: conn.execute( - "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb)) + "CREATE DATABASE %s TEMPLATE %s" % (ident, template_db)) except exc.OperationalError as err: - if attempt != 2 and "accessed by other users" in str(err): - time.sleep(.2) - continue - else: - raise + if "accessed by other users" in str(err): + log.info( + "Waiting to create %s, URI %r, " + "template DB %s is in use sleeping for .5", + ident, eng.url, template_db) + time.sleep(.5) else: break + else: + raise err @_create_db.for_db("mysql")