From: Mike Bayer Date: Sun, 6 Jan 2019 18:01:37 +0000 (-0500) Subject: Correct postgresql provisioning X-Git-Tag: rel_1_0_6~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f341ee9f3977034fae76531ad27d586d8985f875;p=thirdparty%2Fsqlalchemy%2Falembic.git Correct postgresql provisioning port of a fix from SQLAlchemy that corrects the retry mechanism when creating databases Change-Id: Id7ee757c065338ccfe3118203a139fc959ed40d8 --- diff --git a/alembic/testing/provision.py b/alembic/testing/provision.py index 15d9e1cd..05a21d37 100644 --- a/alembic/testing/provision.py +++ b/alembic/testing/provision.py @@ -173,21 +173,28 @@ def _pg_create_db(cfg, eng, ident): pass if not template_db: template_db = conn.scalar("select current_database()") - for attempt in range(3): + + attempt = 0 + while True: try: conn.execute( - "CREATE DATABASE %s TEMPLATE %s" % (ident, template_db)) + "CREATE DATABASE %s TEMPLATE %s" % (ident, template_db) + ) except exc.OperationalError as err: + attempt += 1 + if attempt >= 3: + 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) + ident, + eng.url, + template_db, + ) + time.sleep(0.5) else: break - else: - raise err @_create_db.for_db("mysql")