Add __table_handler__ to ModelSQL

issue7581
review48531002
This commit is contained in:
Cédric Krier committed 2018-07-24 22:01:08 +02:00
1 parent 3547690d8b
commit 7a2ce7905a
16 files changed
+44 -64

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
* Add __table_handler__ to ModelSQL
* Support partial index
* Allow to use SQL expression for index action
* Add cache clean timeout configuration
+4
View File
@@ -411,6 +411,10 @@ Class methods:
Return a SQL Table instance for the history of Model.
.. classmethod:: ModelSQL.__table_handler__([module_name[, history]])
Return a TableHandler for the Model.
.. classmethod:: ModelSQL.table_query()
Could be defined to use a custom SQL query instead of a table of the
+4 -7
View File
@@ -120,10 +120,9 @@ class ActionKeyword(ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
super(ActionKeyword, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
table.index_action(['keyword', 'model'], 'add')
def get_groups(self, name):
@@ -504,7 +503,7 @@ class ActionReport(ActionMixin, ModelSQL, ModelView):
transaction = Transaction()
cursor = transaction.connection.cursor()
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
action_report = cls.__table__()
# Migration from 1.0 report_name_uniq has been removed
@@ -743,11 +742,10 @@ class ActionActWindow(ActionMixin, ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
cursor = Transaction().connection.cursor()
TableHandler = backend.get('TableHandler')
act_window = cls.__table__()
super(ActionActWindow, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 2.0: new search_value format
cursor.execute(*act_window.update(
@@ -933,9 +931,8 @@ class ActionActWindowView(DeactivableMixin, ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
super(ActionActWindowView, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 1.0 remove multi
table.drop_column('multi')
+1 -3
View File
@@ -4,7 +4,6 @@ from sql import Null
from sql.operators import Concat
from ..model import ModelView, ModelSQL, fields, Unique
from .. import backend
from ..transaction import Transaction
from ..pyson import Eval
from .resource import ResourceMixin
@@ -64,12 +63,11 @@ class Attachment(ResourceMixin, ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
cursor = Transaction().connection.cursor()
super(Attachment, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
attachment = cls.__table__()
# Migration from 1.4 res_model and res_id merged into resource
+1 -3
View File
@@ -12,7 +12,6 @@ from ast import literal_eval
from ..model import ModelView, ModelSQL, DeactivableMixin, fields, dualmethod
from ..transaction import Transaction
from ..pool import Pool
from .. import backend
from ..config import config
from ..sendmail import sendmail
@@ -76,12 +75,11 @@ class Cron(DeactivableMixin, ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
cursor = Transaction().connection.cursor()
cron = cls.__table__()
# Migration from 2.0: rename numbercall, doall and nextcall
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
table.column_rename('numbercall', 'number_calls')
table.column_rename('doall', 'repeat_missed')
table.column_rename('nextcall', 'next_call')
+3 -9
View File
@@ -20,7 +20,6 @@ from ..cache import Cache
from ..pool import Pool
from ..pyson import Bool, Eval, PYSONDecoder
from ..rpc import RPC
from .. import backend
from ..protocols.jsonrpc import JSONDecoder, JSONEncoder
from ..tools import is_instance_method, cursor_dict, grouped_slice
try:
@@ -435,11 +434,9 @@ class ModelAccess(ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
super(ModelAccess, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 2.6 (model, group) no more unique
table.drop_constraint('model_group_uniq')
@@ -619,11 +616,9 @@ class ModelFieldAccess(ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
super(ModelFieldAccess, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 2.6 (field, group) no more unique
table.drop_constraint('field_group_uniq')
@@ -1121,13 +1116,12 @@ class ModelData(ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
cursor = Transaction().connection.cursor()
model_data = cls.__table__()
super(ModelData, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 2.6: remove inherit
if table.column_exist('inherit'):
+2 -2
View File
@@ -415,10 +415,10 @@ class ModuleConfigWizardItem(ModelSQL, ModelView):
where=(model_data.model ==
'ir.module.module.config_wizard.item')))
table = TableHandler(cls, module_name)
table_h = cls.__table_handler__(module_name)
# Migrate from 2.2 remove name
table.drop_column('name')
table_h.drop_column('name')
super(ModuleConfigWizardItem, cls).__register__(module_name)
+1 -3
View File
@@ -4,7 +4,6 @@ from ..model import ModelView, ModelSQL, fields, EvalEnvironment, Check
from ..transaction import Transaction
from ..cache import Cache
from ..pool import Pool
from .. import backend
from ..pyson import PYSONDecoder
__all__ = [
@@ -108,9 +107,8 @@ class Rule(ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
super(Rule, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 2.6: replace field, operator and operand by domain
table.not_null_action('field', action='remove')
+2 -3
View File
@@ -101,12 +101,11 @@ class Sequence(DeactivableMixin, ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
table = TableHandler(cls, module_name)
table_h = cls.__table_handler__(module_name)
transaction = Transaction()
# Migration from 2.0 rename number_next into number_next_internal
table.column_rename('number_next', 'number_next_internal')
table_h.column_rename('number_next', 'number_next_internal')
super(Sequence, cls).__register__(module_name)
+1 -3
View File
@@ -14,7 +14,6 @@ except ImportError:
from trytond.model import ModelSQL, fields
from trytond.config import config
from .. import backend
__all__ = [
'Session', 'SessionWizard',
@@ -35,10 +34,9 @@ class Session(ModelSQL):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
super(Session, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
table.index_action('create_uid', 'add')
@classmethod
+3 -5
View File
@@ -22,7 +22,6 @@ from ..model import ModelView, ModelSQL, fields, Unique
from ..wizard import Wizard, StateView, StateTransition, StateAction, \
Button
from ..tools import file_open, reduce_ids, grouped_slice, cursor_dict
from .. import backend
from ..pyson import PYSONEncoder, Eval
from ..transaction import Transaction
from ..pool import Pool
@@ -97,11 +96,10 @@ class Translation(ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
transaction = Transaction()
cursor = transaction.connection.cursor()
ir_translation = cls.__table__()
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 1.8: new field src_md5
src_md5_exist = table.column_exist('src_md5')
if not src_md5_exist:
@@ -124,7 +122,7 @@ class Translation(ModelSQL, ModelView):
cls.write([translation], {
'src_md5': src_md5,
})
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
table.not_null_action('src_md5', action='add')
# Migration from 2.2 and 2.8
@@ -138,7 +136,7 @@ class Translation(ModelSQL, ModelView):
['report'],
where=ir_translation.type == 'odt'))
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
table.index_action(['lang', 'type', 'name'], 'add')
@classmethod
+2 -5
View File
@@ -9,7 +9,6 @@ from ..model import (
ModelView, ModelSQL, DeactivableMixin, fields, EvalEnvironment, Check)
from ..pyson import Eval, PYSONDecoder
from ..tools import grouped_slice
from .. import backend
from ..tools import reduce_ids
from ..transaction import Transaction
from ..cache import Cache
@@ -73,9 +72,8 @@ class Trigger(DeactivableMixin, ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
cursor = Transaction().connection.cursor()
table = TableHandler(cls, module_name)
table = cls.__table_handler__(cls, module_name)
sql_table = cls.__table__()
super(Trigger, cls).__register__(module_name)
@@ -300,8 +298,7 @@ class TriggerLog(ModelSQL):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
super(TriggerLog, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(cls, module_name)
table.index_action(['trigger', 'record_id'], 'add')
+4 -7
View File
@@ -6,7 +6,6 @@ import json
from lxml import etree
from trytond.model import ModelView, ModelSQL, fields
from trytond import backend
from trytond.pyson import Eval, Bool, PYSONDecoder, If
from trytond.tools import file_open
from trytond.transaction import Transaction
@@ -81,8 +80,7 @@ class View(ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 2.4 arch moved into data
if table.column_exist('arch'):
@@ -91,7 +89,7 @@ class View(ModelSQL, ModelView):
super(View, cls).__register__(module_name)
# New instance to refresh definition
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 1.0 arch no more required
table.not_null_action('arch', action='remove')
@@ -331,15 +329,14 @@ class ViewTreeState(ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 2.8: table name changed
table.table_rename('ir_ui_view_tree_expanded_state', cls._table)
super(ViewTreeState, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
table.index_action(['model', 'domain', 'user', 'child_name'], 'add')
@staticmethod
+8 -4
View File
@@ -190,6 +190,11 @@ class ModelSQL(ModelStorage):
raise ValueError('No history table')
return Table(cls._table + '__history')
@classmethod
def __table_handler__(cls, module_name=None, history=False):
TableHandler = backend.get('TableHandler')
return TableHandler(cls, module_name, history=history)
@classmethod
def __register__(cls, module_name):
sql_table = cls.__table__()
@@ -203,9 +208,9 @@ class ModelSQL(ModelStorage):
pool = Pool()
# create/update table in the database
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
if cls._history:
history_table = TableHandler(cls, module_name, history=True)
history_table = cls.__table_handler__(module_name, history=True)
history_table.index_action('id', action='add')
for field_name, field in cls._fields.items():
@@ -303,9 +308,8 @@ class ModelSQL(ModelStorage):
@classmethod
def _update_history_table(cls):
TableHandler = backend.get('TableHandler')
if cls._history:
history_table = TableHandler(cls, history=True)
history_table = cls.__table_handler__(history=True)
for field_name, field in cls._fields.items():
if not field.sql_type():
continue
+4 -5
View File
@@ -31,7 +31,7 @@ class UIMenuGroup(ModelSQL):
transaction.database.sequence_rename(transaction.connection,
'ir_ui_menu_group_rel_id_seq', cls._table + '_id_seq')
# Migration from 2.0 menu_id and gid renamed into menu group
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
table.column_rename('menu_id', 'menu')
table.column_rename('gid', 'group')
super(UIMenuGroup, cls).__register__(module_name)
@@ -73,7 +73,7 @@ class ActionGroup(ModelSQL):
transaction.database.sequence_rename(transaction.connection,
'ir_action_group_rel_id_seq', cls._table + '_id_seq')
# Migration from 2.0 action_id and gid renamed into action and group
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
table.column_rename('action_id', 'action')
table.column_rename('gid', 'group')
super(ActionGroup, cls).__register__(module_name)
@@ -122,13 +122,12 @@ class ModelFieldGroup(ModelSQL):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
database = Transaction().database
transaction = Transaction()
# Migration from 1.0 table name change
TableHandler.table_rename('ir_model_field_group_rel', cls._table)
transaction.database.sequence_rename(transaction.connection,
'ir_model_field_group_rel_id_seq', cls._table + '_id_seq')
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 2.6: field_id and group_id renamed to field and group
table.column_rename('field_id', 'field')
table.column_rename('group_id', 'group')
@@ -194,7 +193,7 @@ class RuleGroupGroup(ModelSQL):
'group_rule_group_rel_id_seq', cls._table + '_id_seq')
# Migration from 2.0 rule_group_id and group_id renamed into rule_group
# and group
table = TableHandler(cls, module_name)
table = cls.__table_handler__(cls, module_name)
table.column_rename('rule_group_id', 'rule_group')
table.column_rename('group_id', 'group')
super(RuleGroupGroup, cls).__register__(module_name)
+3 -5
View File
@@ -187,10 +187,9 @@ class User(DeactivableMixin, ModelSQL, ModelView):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
cursor = Transaction().connection.cursor()
super(User, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 1.6
@@ -732,9 +731,8 @@ class LoginAttempt(ModelSQL):
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
super(LoginAttempt, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
# Migration from 2.8: remove user
table.drop_column('user')
@@ -858,7 +856,7 @@ class UserGroup(ModelSQL):
transaction.database.sequence_rename(transaction.connection,
'res_group_user_rel_id_seq', cls._table + '_id_seq')
# Migration from 2.0 uid and gid rename into user and group
table = TableHandler(cls, module_name)
table = cls.__table_handler__(module_name)
table.column_rename('uid', 'user')
table.column_rename('gid', 'group')
super(UserGroup, cls).__register__(module_name)