diff --git a/trytond/backend/postgresql/table.py b/trytond/backend/postgresql/table.py index 6683110cb0..21d5615fa5 100644 --- a/trytond/backend/postgresql/table.py +++ b/trytond/backend/postgresql/table.py @@ -8,31 +8,34 @@ import logging class TableHandler(TableHandlerInterface): - def __init__(self, cursor, table_name, object_name=None, module_name=None, - history=False): - super(TableHandler, self).__init__(cursor, table_name, - object_name=object_name, module_name=module_name) + def __init__(self, cursor, model, module_name=None, history=False): + super(TableHandler, self).__init__(cursor, model, + module_name=module_name) self._columns = {} self._constraints = [] self._fk_deltypes = {} self._indexes = {} self._field2module = {} + # Create sequence if necessary + if not self.history and \ + not self.sequence_exist(self.cursor, self.sequence_name): + self.cursor.execute('CREATE SEQUENCE "%s"' % self.sequence_name) + # Create new table if necessary if not self.table_exist(self.cursor, self.table_name): - if not history: - self.cursor.execute('CREATE TABLE "%s" ' \ - "(id SERIAL NOT NULL, " \ - "PRIMARY KEY(id))"% self.table_name) - else: - self.cursor.execute('CREATE TABLE "%s" ' \ - '(id INTEGER)' % self.table_name) + self.cursor.execute('CREATE TABLE "%s" ' \ + '(id INTEGER NOT NULL)' % self.table_name) + if not self.history: + self.cursor.execute('ALTER TABLE "%s" ' \ + 'ADD PRIMARY KEY(id)' % self.table_name) self._update_definitions() if 'id' not in self._columns: self.cursor.execute('ALTER TABLE "%s" ' \ - 'ADD COLUMN id SERIAL NOT NULL' % self.table_name) - self.cursor.execute('ALTER TABLE "%s" ' \ - 'ADD PRIMARY KEY(id)' % self.table_name) + 'ADD COLUMN id INTEGER NOT NULL' % self.table_name) + if not self.history: + self.cursor.execute('ALTER TABLE "%s" ' \ + 'ADD PRIMARY KEY(id)' % self.table_name) self._update_definitions() @staticmethod diff --git a/trytond/backend/table.py b/trytond/backend/table.py index 12f26b4f20..f86bcb79bc 100644 --- a/trytond/backend/table.py +++ b/trytond/backend/table.py @@ -7,13 +7,23 @@ class TableHandlerInterface(object): Define generic interface to handle database table ''' - def __init__(self, cursor, table_name, object_name=None, module_name=None, - history=False): + def __init__(self, cursor, model, module_name=None, history=False): + ''' + :param cursor: the database cursor + :param model: the Model linked to the table + :param module_name: the module name + :param history: a boolean to define if it is a history table + ''' super(TableHandlerInterface, self).__init__() self.cursor = cursor - self.table_name = table_name - self.object_name = object_name + if history: + self.table_name = model._table + '__history' + else: + self.table_name = model._table + self.object_name = model._name + self.sequence_name = model._sequence self.module_name = module_name + self.history = history @staticmethod def table_exist(cursor, table_name): diff --git a/trytond/ir/action.py b/trytond/ir/action.py index 7bbca21cba..164d20b289 100644 --- a/trytond/ir/action.py +++ b/trytond/ir/action.py @@ -246,7 +246,7 @@ class ActionReport(ModelSQL, ModelView): def init(self, cursor, module_name): super(ActionReport, self).init(cursor, module_name) - table = TableHandler(cursor, self._table, self._name, module_name) + table = TableHandler(cursor, self, module_name) # Migration from 1.0 report_name_uniq has been removed table.drop_constraint('report_name_uniq') @@ -442,7 +442,7 @@ class ActionActWindowView(ModelSQL, ModelView): def init(self, cursor, module_name): super(ActionActWindowView, self).init(cursor, module_name) - table = TableHandler(cursor, self._table, self._name, module_name) + table = TableHandler(cursor, self, module_name) # Migration from 1.0 remove multi table.drop_column('multi') diff --git a/trytond/ir/translation.py b/trytond/ir/translation.py index ef7617d7be..88774e9769 100644 --- a/trytond/ir/translation.py +++ b/trytond/ir/translation.py @@ -66,7 +66,7 @@ class Translation(ModelSQL, ModelView, Cacheable): def init(self, cursor, module_name): super(Translation, self).init(cursor, module_name) - table = TableHandler(cursor, self._table, self._name, module_name) + table = TableHandler(cursor, self, module_name) table.index_action(['lang', 'type', 'name'], 'add') table.index_action(['lang', 'type', 'name', 'src'], 'add') diff --git a/trytond/ir/ui/view.py b/trytond/ir/ui/view.py index 925cc8375f..01ce305000 100644 --- a/trytond/ir/ui/view.py +++ b/trytond/ir/ui/view.py @@ -46,7 +46,7 @@ class View(ModelSQL, ModelView): def init(self, cursor, module_name): super(View, self).init(cursor, module_name) - table = TableHandler(cursor, self._table, self._name, module_name) + table = TableHandler(cursor, self, module_name) # Migration from 1.0 arch no more required table.not_null_action('arch', action='remove') diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index e5848e74ca..4d07a03f3a 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -59,10 +59,9 @@ class ModelSQL(ModelStorage): return # create/update table in the database - table = TableHandler(cursor, self._table, self._name, module_name) + table = TableHandler(cursor, self, module_name) if self._history: - history_table = TableHandler(cursor, self._table + '__history', - None, module_name, history=True) + history_table = TableHandler(cursor, self, module_name, history=True) datetime_field = FIELDS['datetime'] integer_field = FIELDS['integer'] logs = ( diff --git a/trytond/workflow/workflow.py b/trytond/workflow/workflow.py index bd453b47cd..3c11445649 100644 --- a/trytond/workflow/workflow.py +++ b/trytond/workflow/workflow.py @@ -144,7 +144,7 @@ class WorkflowInstance(ModelSQL, ModelView): def init(self, cursor, module_name): super(WorkflowInstance, self).init(cursor, module_name) - table = TableHandler(cursor, self._table, self._name, module_name) + table = TableHandler(cursor, self, module_name) table.index_action(['res_id', 'res_type', 'state'], 'add') table.index_action(['res_id', 'workflow'], 'add') @@ -454,7 +454,7 @@ class WorkflowTrigger(ModelSQL, ModelView): def init(self, cursor, module_name): super(WorkflowTrigger, self).init(cursor, module_name) - table = TableHandler(cursor, self._table, self._name, module_name) + table = TableHandler(cursor, self, module_name) table.index_action(['res_id', 'model'], 'add') WorkflowTrigger()