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, "
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 - "
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)
@_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")