From f341ee9f3977034fae76531ad27d586d8985f875 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 6 Jan 2019 13:01:37 -0500 Subject: [PATCH] Correct postgresql provisioning port of a fix from SQLAlchemy that corrects the retry mechanism when creating databases Change-Id: Id7ee757c065338ccfe3118203a139fc959ed40d8 --- alembic/testing/provision.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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") -- 2.47.2