Skip to content

Commit

Permalink
Support existing-but-not-initialized databases in init script
Browse files Browse the repository at this point in the history
Since odoo/odoo@cb2862a (v12+), Odoo will not autoinitialize modules in a database that already exists.

However, in v8-, Odoo will not start if the database doesn't exist.

To make this difference easier to handle, with this change, `click-odoo-initdb` will treat non-existing databases the same as non-initialized ones (which is, for Odoo, the same).

@Tecnativa TT20713
  • Loading branch information
yajo committed Dec 2, 2019
1 parent 89d8023 commit 8eaf3d7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
11 changes: 9 additions & 2 deletions click_odoo_contrib/_dbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@


@contextmanager
def pg_connect():
conn = odoo.sql_db.db_connect("postgres")
def pg_connect(dbname="postgres"):
conn = odoo.sql_db.db_connect(dbname)
cr = conn.cursor()
cr.autocommit(True)
try:
Expand All @@ -28,6 +28,13 @@ def db_exists(dbname):
return bool(cr.fetchone())


def db_initialized(dbname):
if not db_exists(dbname):
return False
with pg_connect(dbname) as cr:
return odoo.modules.db.is_initialized(cr)


def terminate_connections(dbname):
with pg_connect() as cr:
cr.execute(
Expand Down
19 changes: 17 additions & 2 deletions click_odoo_contrib/initdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import click_odoo
from click_odoo import odoo

from ._dbutils import db_exists
from ._dbutils import db_exists, db_initialized
from .manifest import expand_dependencies
from .update import _save_installed_checksums

Expand Down Expand Up @@ -67,7 +67,12 @@ def _db_storage(self):

def odoo_createdb(dbname, demo, module_names, force_db_storage):
with _patch_ir_attachment_store(force_db_storage):
odoo.service.db._create_empty_database(dbname)
try:
odoo.service.db._create_empty_database(dbname)
except odoo.service.db.DatabaseExists:
if db_initialized(dbname):
# Why are you creating a DB that exists and is initialized?
raise
odoo.tools.config["init"] = dict.fromkeys(module_names, 1)
odoo.tools.config["without_demo"] = not demo
if _odoo_version < odoo.tools.parse_version("10"):
Expand Down Expand Up @@ -391,6 +396,11 @@ def trim_age(self, max_age):
is_flag=True,
help="Don't report error if database already exists.",
)
@click.option(
"--unless-initialized",
is_flag=True,
help="Don't report error if database is already initialized.",
)
def main(
env,
new_database,
Expand All @@ -401,6 +411,7 @@ def main(
cache_max_age,
cache_max_size,
unless_exists,
unless_initialized,
):
""" Create an Odoo database with pre-installed modules.
Expand All @@ -419,6 +430,10 @@ def main(
msg = "Database already exists: {}".format(new_database)
click.echo(click.style(msg, fg="yellow"))
return
if unless_initialized and db_initialized(new_database):
msg = "Database already initialized: {}".format(new_database)
click.echo(click.style(msg, fg="yellow"))
return
module_names = [m.strip() for m in modules.split(",")]
if not cache:
if new_database:
Expand Down

0 comments on commit 8eaf3d7

Please sign in to comment.