Skip to content

Commit

Permalink
Merge pull request #205 from Tecnativa/add_fields-default
Browse files Browse the repository at this point in the history
[IMP] add_fields: Allow to set an initialization value
  • Loading branch information
pedrobaeza authored Apr 27, 2020
2 parents 1028a8a + fc8da00 commit 5651e38
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -2192,6 +2192,8 @@ def add_fields(env, field_spec):
(from the valid PostgreSQL types):
https://www.postgresql.org/docs/9.6/static/datatype.html
* module name: for adding the XML-ID entry.
* (optional) initialization value: if included in the tuple, it is set
in the column for existing records.
"""
sql_type_mapping = {
'binary': 'bytea', # If there's attachment, no SQL. Force it manually
Expand All @@ -2212,20 +2214,35 @@ def add_fields(env, field_spec):
'serialized': 'text',
}
for vals in field_spec:
field_name, model_name, table_name, field_type, sql_type, module = vals
field_name = vals[0]
model_name = vals[1]
table_name = vals[2]
field_type = vals[3]
sql_type = vals[4]
module = vals[5]
init_value = vals[6] if len(vals) > 6 else False
# Add SQL column
if not table_name:
table_name = env[model_name]._table
sql_type = sql_type or sql_type_mapping.get(field_type)
if sql_type:
logged_query(
env.cr,
sql.SQL("ALTER TABLE {} ADD COLUMN {} {}").format(
sql.Identifier(table_name),
sql.Identifier(field_name),
sql.SQL(sql_type),
),
query = sql.SQL("ALTER TABLE {} ADD COLUMN {} {}").format(
sql.Identifier(table_name),
sql.Identifier(field_name),
sql.SQL(sql_type),
)
args = []
if init_value:
query += sql.SQL(" DEFAULT %s")
args.append(init_value)
logged_query(env.cr, query, args)
if init_value:
logged_query(env.cr, sql.SQL(
"ALTER TABLE {} ALTER COLUMN {} DROP DEFAULT").format(
sql.Identifier(table_name),
sql.Identifier(field_name),
)
)
# Add ir.model.fields entry
env.cr.execute(
"SELECT id FROM ir_model WHERE model = %s", (model_name, ),
Expand Down

0 comments on commit 5651e38

Please sign in to comment.