MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _pg_create_db

Function _pg_create_db

lib/sqlalchemy/dialects/postgresql/provision.py:28–59  ·  view source on GitHub ↗
(cfg, eng, ident)

Source from the content-addressed store, hash-verified

26
27@create_db.for_db("postgresql")
28def _pg_create_db(cfg, eng, ident):
29 template_db = cfg.options.postgresql_templatedb
30
31 with eng.execution_options(isolation_level="AUTOCOMMIT").begin() as conn:
32 if not template_db:
33 template_db = conn.exec_driver_sql(
34 "select current_database()"
35 ).scalar()
36
37 attempt = 0
38 while True:
39 try:
40 conn.exec_driver_sql(
41 "CREATE DATABASE %s TEMPLATE %s" % (ident, template_db)
42 )
43 except exc.OperationalError as err:
44 attempt += 1
45 if attempt >= 3:
46 raise
47 if "accessed by other users" in str(err):
48 log.info(
49 "Waiting to create %s, URI %r, "
50 "template DB %s is in use sleeping for .5",
51 ident,
52 eng.url,
53 template_db,
54 )
55 time.sleep(0.5)
56 except:
57 raise
58 else:
59 break
60
61
62@drop_db.for_db("postgresql")

Callers

nothing calls this directly

Calls 5

beginMethod · 0.45
execution_optionsMethod · 0.45
scalarMethod · 0.45
exec_driver_sqlMethod · 0.45
infoMethod · 0.45

Tested by

no test coverage detected