|
9 | 9 |
|
10 | 10 |
|
11 | 11 | def _get_server_env_mixin_fnames(cr, model: str) -> set[str]: |
| 12 | + """Return the core mixin field names present on the model.""" |
12 | 13 | possible_fnames = ("server_env_defaults", "tech_name") |
13 | 14 | cr.execute( |
14 | 15 | SQL( |
15 | 16 | """ |
16 | 17 | SELECT name |
17 | 18 | FROM ir_model_fields |
18 | 19 | WHERE model = %(model)s |
19 | | - AND name IN %(possible_fnames)s |
| 20 | + AND name IN %(possible_fnames)s |
20 | 21 | """, |
21 | 22 | model=model, |
22 | 23 | possible_fnames=possible_fnames, |
23 | 24 | ) |
24 | 25 | ) |
25 | | - return set(row[0] for row in cr.fetchall()) |
| 26 | + return {row[0] for row in cr.fetchall()} |
26 | 27 |
|
27 | 28 |
|
28 | | -def _get_server_env_mixin_magic_fnames(cr, model: str) -> set[str]: |
29 | | - """Find all the magic server environment field names for a given model""" |
30 | | - # Get all fields created on-the-fly by the mixin. Following the name patterns: |
31 | | - # - default fields: x_<field_name>_env_default |
32 | | - # - is editable fields: x_<field_name>_env_is_editable |
| 29 | +def _get_server_env_mixin_magic_fnames(cr, model): |
| 30 | + """Return the dynamic server_environment helper fields for the model. |
| 31 | +
|
| 32 | + These are the fields created on the fly by server.env.mixin: |
| 33 | + - x_<field_name>_env_default |
| 34 | + - x_<field_name>_env_is_editable |
| 35 | + """ |
33 | 36 | cr.execute( |
34 | 37 | SQL( |
35 | 38 | """ |
36 | 39 | SELECT name |
37 | 40 | FROM ir_model_fields |
38 | 41 | WHERE model = %(model)s |
39 | | - AND name LIKE 'x_%%_env_default' |
40 | | - OR name LIKE 'x_%%_env_is_editable' |
| 42 | + AND ( |
| 43 | + name LIKE 'x_%%_env_default' |
| 44 | + OR name LIKE 'x_%%_env_is_editable' |
| 45 | + ) |
41 | 46 | """, |
42 | 47 | model=model, |
43 | 48 | ) |
44 | 49 | ) |
45 | | - return set(row[0] for row in cr.fetchall()) |
| 50 | + return {row[0] for row in cr.fetchall()} |
| 51 | + |
| 52 | + |
| 53 | +def _get_existing_columns(cr, table_name): |
| 54 | + """Return the current physical columns of a PostgreSQL table.""" |
| 55 | + cr.execute( |
| 56 | + SQL( |
| 57 | + """ |
| 58 | + SELECT column_name |
| 59 | + FROM information_schema.columns |
| 60 | + WHERE table_schema = 'public' |
| 61 | + AND table_name = %(table_name)s |
| 62 | + ORDER BY ordinal_position |
| 63 | + """, |
| 64 | + table_name=table_name, |
| 65 | + ) |
| 66 | + ) |
| 67 | + return [row[0] for row in cr.fetchall()] |
46 | 68 |
|
47 | 69 |
|
48 | 70 | def migrate(cr, version): |
| 71 | + """Move XMLIDs to the glue module and snapshot the original schema. |
| 72 | +
|
| 73 | + This keeps the server_environment ir.model.fields metadata attached to the |
| 74 | + new module and preserves field values. |
| 75 | + In the end, the original fs_storage schema is preserved at post cleanup. |
| 76 | + """ |
| 77 | + |
49 | 78 | if not version: |
50 | 79 | return |
| 80 | + |
51 | 81 | model = "fs.storage" |
| 82 | + old_module = "fs_storage" |
| 83 | + new_module = "fs_storage_environment" |
| 84 | + |
52 | 85 | mixin_fnames = _get_server_env_mixin_fnames(cr, model) |
53 | 86 | magic_fnames = _get_server_env_mixin_magic_fnames(cr, model) |
54 | 87 | to_move_fnames = mixin_fnames | magic_fnames |
55 | | - old_module = "fs_storage" |
56 | | - new_module = "fs_storage_environment" |
| 88 | + |
57 | 89 | rename_specs = [ |
58 | 90 | (field_xmlid(old_module, model, fname), field_xmlid(new_module, model, fname)) |
59 | 91 | for fname in to_move_fnames |
60 | 92 | ] |
61 | 93 | openupgrade.rename_xmlids(cr, rename_specs, allow_merge=True) |
| 94 | + |
62 | 95 | # Add noupdate to the magic_fnames, to prevent Odoo from deleting them in upgrade |
| 96 | + if magic_fnames: |
| 97 | + openupgrade.logged_query( |
| 98 | + cr, |
| 99 | + """ |
| 100 | + UPDATE ir_model_data SET noupdate = TRUE |
| 101 | + WHERE module = %(module)s |
| 102 | + AND name IN %(names)s |
| 103 | + """, |
| 104 | + dict( |
| 105 | + module=new_module, |
| 106 | + names=tuple( |
| 107 | + field_xmlid(new_module, model, fname).split(".")[1] |
| 108 | + for fname in magic_fnames |
| 109 | + ), |
| 110 | + ), |
| 111 | + ) |
| 112 | + |
| 113 | + # Snapshot the original physical schema. |
| 114 | + # Post cleanup hook in `fs_storage_environment` will compare |
| 115 | + # the final table with this snapshot and drop every created extra column. |
| 116 | + original_columns = _get_existing_columns(cr, "fs_storage") |
63 | 117 | openupgrade.logged_query( |
64 | 118 | cr, |
65 | 119 | """ |
66 | | - UPDATE ir_model_data SET noupdate = TRUE |
67 | | - WHERE module = %(module)s |
68 | | - AND name IN %(names)s |
| 120 | + CREATE TABLE IF NOT EXISTS fs_storage_original_columns ( |
| 121 | + column_name varchar PRIMARY KEY |
| 122 | + ) |
69 | 123 | """, |
70 | | - dict( |
71 | | - module=new_module, |
72 | | - names=tuple( |
73 | | - field_xmlid(new_module, model, fname).split(".")[1] |
74 | | - for fname in magic_fnames |
75 | | - ), |
76 | | - ), |
77 | 124 | ) |
| 125 | + openupgrade.logged_query(cr, "DELETE FROM fs_storage_original_columns") |
| 126 | + |
| 127 | + for column_name in original_columns: |
| 128 | + cr.execute( |
| 129 | + """ |
| 130 | + INSERT INTO fs_storage_original_columns (column_name) |
| 131 | + VALUES (%s) |
| 132 | + ON CONFLICT (column_name) DO NOTHING |
| 133 | + """, |
| 134 | + (column_name,), |
| 135 | + ) |
0 commit comments