[IMP] core: optimize model._auto_init() on a new model

Create the table with all the columns from scratch, with the NOT NULL
constraint when required.  Also do not call `_check_removed_columns()`
on a new table.

This saves 0.5% of the total installation time.

X-original-commit: 0727cacf5194a143b15ab4cb9893f3035a67be1f
This commit is contained in:
Raphael Collet
2020-04-03 11:40:28 +00:00
parent 892e3df701
commit 3ecb6fdcb0
2 changed files with 26 additions and 5 deletions
+10 -2
View File
@@ -2486,14 +2486,22 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})):
if self._auto:
if must_create_table:
tools.create_model_table(cr, self._table, self._description)
def make_type(field):
return field.column_type[1] + (" NOT NULL" if field.required else "")
tools.create_model_table(cr, self._table, self._description, [
(name, make_type(field), field.string)
for name, field in self._fields.items()
if name != 'id' and field.store and field.column_type
])
if self._parent_store:
if not tools.column_exists(cr, self._table, 'parent_path'):
self._create_parent_columns()
parent_path_compute = True
self._check_removed_columns(log=False)
if not must_create_table:
self._check_removed_columns(log=False)
# update the database schema for fields
columns = tools.table_columns(cr, self._table)
+16 -3
View File
@@ -48,11 +48,24 @@ def table_kind(cr, tablename):
cr.execute(query, (tablename,))
return cr.fetchone()[0] if cr.rowcount else None
def create_model_table(cr, tablename, comment=None):
def create_model_table(cr, tablename, comment=None, columns=()):
""" Create the table for a model. """
cr.execute('CREATE TABLE "{}" (id SERIAL NOT NULL, PRIMARY KEY(id))'.format(tablename))
colspecs = ['id SERIAL NOT NULL'] + [
'"{}" {}'.format(columnname, columntype)
for columnname, columntype, columncomment in columns
]
cr.execute('CREATE TABLE "{}" ({}, PRIMARY KEY(id))'.format(tablename, ", ".join(colspecs)))
queries, params = [], []
if comment:
cr.execute('COMMENT ON TABLE "{}" IS %s'.format(tablename), (comment,))
queries.append('COMMENT ON TABLE "{}" IS %s'.format(tablename))
params.append(comment)
for columnname, columntype, columncomment in columns:
queries.append('COMMENT ON COLUMN "{}"."{}" IS %s'.format(tablename, columnname))
params.append(columncomment)
if queries:
cr.execute("; ".join(queries), params)
_schema.debug("Table %r: created", tablename)
def table_columns(cr, tablename):