Skip to content
Snippets Groups Projects
Commit 365bd5d87b30 authored by Vincent Hatakeyama's avatar Vincent Hatakeyama
Browse files

docker_dev_start: create databases when they do not exists

parent d73fdff4a934
No related branches found
No related tags found
1 merge request!198docker_dev_start: create databases when they do not exists
......@@ -2,6 +2,11 @@
History
=======
20.8.0
------
docker_dev_start: create database when it does not exists (avoid issue with anomization when creating new databases)
20.7.1
------
......
......@@ -848,6 +848,8 @@
arg.append(dbport)
db_host = nmspc.db_host
create_database = False
# Check that connection can be done, try to create user if asked to
# TODO: handle the case where the database is still starting up
# TODO: (and remove the sleep)
......@@ -937,5 +939,22 @@
arg.append("--db_host")
arg.append(db_host)
# --- restore ---
# Create database if it does not exists
if database:
with connect(
user=user,
password=password,
database="postgres",
host=socket_path if start_postgresql else db_host,
port=dbport,
) as connection:
with connection.cursor() as cursor:
cursor.execute(
"SELECT count(*) FROM pg_database WHERE datname=%s", (database,)
)
data = cursor.fetchall()
create_database = data[0][0] == 0
# --- restore / create database ---
# TODO find out why odoo_help would stop us from restoring
......@@ -941,5 +960,5 @@
# TODO find out why odoo_help would stop us from restoring
if start_postgresql and not odoo_help and restore_filename:
if start_postgresql and not odoo_help and (restore_filename or create_database):
_logger.info("Creating database %s for %s", database, user)
pg.exec_run(["createdb", "-U", user, database])
if start_postgresql and not odoo_help and restore_filename:
......@@ -966,10 +985,10 @@
_logger.info("Removing dump file in docker")
pg.exec_run(["rm", inside_restore_filename])
if not start_postgresql and not odoo_help and restore_filename:
with connect(
if not start_postgresql and not odoo_help and (restore_filename or create_database):
connection = connect(
user=user,
password=password,
database="postgres",
host=db_host,
port=dbport,
......@@ -971,13 +990,13 @@
user=user,
password=password,
database="postgres",
host=db_host,
port=dbport,
) as connection:
connection.autocommit = True
_logger.info("Creating database %s", database)
with connection.cursor() as cursor:
cursor.execute(f'CREATE DATABASE "{database}"')
)
connection.autocommit = True
_logger.info("Creating database %s", database)
with connection.cursor() as cursor:
cursor.execute(f'CREATE DATABASE "{database}"')
connection.close()
if not start_postgresql and not odoo_help and restore_filename:
_logger.info("Restoring database %s", database)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment