# HG changeset patch # User Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr> # Date 1694176806 -7200 # Fri Sep 08 14:40:06 2023 +0200 # Node ID 365bd5d87b3032cac88b4e7b03686e524f24a8b4 # Parent d73fdff4a934438b8b3ff835c727ef154ce91c5a docker_dev_start: create databases when they do not exists diff --git a/NEWS.rst b/NEWS.rst --- a/NEWS.rst +++ b/NEWS.rst @@ -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 ------ diff --git a/odoo_scripts/docker_dev_start.py b/odoo_scripts/docker_dev_start.py --- a/odoo_scripts/docker_dev_start.py +++ b/odoo_scripts/docker_dev_start.py @@ -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,9 +939,26 @@ 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 - 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,18 +985,18 @@ _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, - ) 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)