]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- merge latest CI options from SQLAlchemy
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 22 Aug 2017 17:31:29 +0000 (13:31 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 22 Aug 2017 17:31:29 +0000 (13:31 -0400)
Change-Id: Ia84a7f0779260874137e8d1b3a14b4f6e4778752

alembic/testing/plugin/plugin_base.py
alembic/testing/plugin/pytestplugin.py
alembic/testing/provision.py

index 7b27e69e68ec94afbf13a85967356d17c783ba75..0ed8f847faf9805359598c6e7a03d08e6806e17f 100644 (file)
@@ -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 - "
index d3d39ba31e12c503a049506e887096f49c88b9cc..15c7e078897ea59a5a71e02da1c29505eafc321e 100644 (file)
@@ -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)
index 45289d11479479ee3e2d4ab5631ea405152d9419..faf437c9be3f596e634988c2f18836e619ff8f07 100644 (file)
@@ -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")