-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate
executable file
·94 lines (70 loc) · 2.85 KB
/
migrate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
# pylint: disable=print-used
from click_odoo_contrib.update import main
from odoo.modules.migration import MigrationManager
from odoo.modules.registry import Registry
from odoo import sql_db
from datetime import datetime
import os
import logging
_logger = logging.getLogger(__name__)
def get_version_last_digit(version):
return ".".join(version.split(".")[-3:])
def get_new_version(current_version):
version = datetime.now().strftime("%y%m.%d.0")
# increment last digit if needed
if current_version >= version:
year_month, day, inc = current_version.split(".")
inc = str(int(inc) + 1)
version = ".".join([year_month, day, inc])
return version
# Patch odoo native migration manager to always run migration script in the
# directory "migrations/0.0.0"
# Indeed odoo will only run the migration script if the version have been bump
# inside the module.
# So if an migration directory 0.0.0 exist "dynamically" increment the version
# for odoo/local-src modules
def have_pending_migration(self, pkg):
return bool(self.migrations[pkg.name].get("module", {}).get("0.0.0"))
def update_version(pkg):
current_version = pkg.data["version"]
pkg.data["version"] = get_new_version(current_version)
ori_migrate_module = MigrationManager.migrate_module
def migrate_module(self, pkg, stage):
if have_pending_migration(self, pkg):
update_version(pkg)
return ori_migrate_module(self, pkg, stage)
MigrationManager.migrate_module = migrate_module
# Process before.sql script in custom_all/migrations/{version}/before.sql
def get_before_request(cr):
cr.execute(
"SELECT latest_version FROM ir_module_module WHERE name='custom_all'"
)
todo = []
current_version = cr.fetchone()
if not current_version:
_logger.error("No version found for custom_all, skip begin script")
current_version = current_version[0]
migr_path = "/odoo/local-src/custom_all/migrations"
if os.path.exists(migr_path):
for version in os.listdir(migr_path):
if version == "0.0.0" or version > current_version:
file_path = f"{migr_path}/{version}/before.sql"
if os.path.exists(file_path):
todo.append((file_path, open(file_path, "r").read()))
return todo
ori_new = Registry.new
@classmethod
def new(cls, db_name, force_demo=False, status=None, update_module=False):
conn = sql_db.db_connect(db_name)
with conn.cursor() as cr:
for file_path, requests in get_before_request(cr):
_logger.info("Execute before sql request \n===\n%s===\n", requests)
cr.execute(requests)
return ori_new(
db_name, force_demo=force_demo, status=status,
update_module=update_module)
Registry.new = new
# Call native click-odoo-update
if __name__ == "__main__": # pragma: no cover
main()