]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Correct postgresql provisioning
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 6 Jan 2019 18:01:37 +0000 (13:01 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 6 Jan 2019 18:01:37 +0000 (13:01 -0500)
port of a fix from SQLAlchemy that corrects the
retry mechanism when creating databases

Change-Id: Id7ee757c065338ccfe3118203a139fc959ed40d8

alembic/testing/provision.py

index 15d9e1cd5a154f01cf3795e8ded3c6e000f15132..05a21d371718fed7b11c3f0291064bf47c84e776 100644 (file)
@@ -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")