|
@@ -18,14 +18,17 @@ class SQLMigrationHandler(DummyMigrationHandler):
|
18
|
18
|
|
19
|
19
|
fieldtype_to_sql = {
|
20
|
20
|
'char': "CHAR(255)",
|
21
|
|
- 'int': 'INT'
|
|
21
|
+ 'integer': 'INT'
|
22
|
22
|
}
|
23
|
23
|
|
24
|
24
|
def __init__(self, module=None, *conn_args, **conn_kargs):
|
25
|
|
- self.db = Database(module, *conn_args, **conn_kargs)
|
26
|
25
|
super(SQLMigrationHandler, self).__init__(False)
|
|
26
|
+
|
|
27
|
+ self.db = Database(module, *conn_args, **conn_kargs)
|
27
|
28
|
self._pk_column = EditorialModel.classtypes.pk_name() + ' INT PRIMARY_KEY AUTOINCREMENT NOT NULL'
|
28
|
|
- # @todo vérification de l'existance de la table objects et de la table relation
|
|
29
|
+ self._main_table_name = 'object'
|
|
30
|
+ self._relation_table_name = 'relation'
|
|
31
|
+
|
29
|
32
|
self._install_tables()
|
30
|
33
|
|
31
|
34
|
## @brief Record a change in the EditorialModel and indicate wether or not it is possible to make it
|
|
@@ -36,10 +39,7 @@ class SQLMigrationHandler(DummyMigrationHandler):
|
36
|
39
|
# @param new_state dict | None : dict with field name as key and field value as value. Representing the new state. None mean component deletion
|
37
|
40
|
# @throw EditorialModel.exceptions.MigrationHandlerChangeError if the change was refused
|
38
|
41
|
def register_change(self, model, uid, initial_state, new_state):
|
39
|
|
- #print(uid, initial_state, new_state)
|
40
|
42
|
# find type of component change
|
41
|
|
- component = Model.name_from_emclass(type(model.component(uid)))
|
42
|
|
- #print ("ça", component, type(model.component(uid)))
|
43
|
43
|
if initial_state is None:
|
44
|
44
|
state_change = 'new'
|
45
|
45
|
elif new_state is None:
|
|
@@ -47,34 +47,25 @@ class SQLMigrationHandler(DummyMigrationHandler):
|
47
|
47
|
else:
|
48
|
48
|
state_change = 'upgrade'
|
49
|
49
|
|
50
|
|
- if component == 'EmType' and len(new_state) == 1:
|
51
|
|
- if 'superiors_list' in new_state:
|
52
|
|
- what = 'superiors_list'
|
53
|
|
- elif 'fields_list' in new_state:
|
54
|
|
- what = 'fields_list'
|
55
|
|
- else:
|
56
|
|
- what = component
|
57
|
|
-
|
58
|
|
- handler_func = what + '_' + state_change
|
59
|
|
- #print (handler_func)
|
|
50
|
+ # call method to handle the database change
|
|
51
|
+ component_name = Model.name_from_emclass(type(model.component(uid)))
|
|
52
|
+ handler_func = component_name.lower() + '_' + state_change
|
60
|
53
|
if hasattr(self, handler_func):
|
61
|
54
|
getattr(self, handler_func)(model, uid, initial_state, new_state)
|
62
|
|
- #print(handler_func, uid, initial_state, new_state)
|
63
|
55
|
|
64
|
56
|
# New Class, a table must be created
|
65
|
|
- def EmClass_new(self, model, uid, initial_state, new_state):
|
|
57
|
+ def emclass_new(self, model, uid, initial_state, new_state):
|
66
|
58
|
class_table_name = self._class_table_name(new_state['name'])
|
67
|
59
|
self._query_bd(
|
68
|
60
|
create(table=class_table_name, column=self._pk_column)
|
69
|
61
|
)
|
70
|
62
|
|
71
|
63
|
# New Field, must create a column in Class table or in Class_Type relational attribute table
|
72
|
|
- def EmField_new(self, model, uid, initial_state, new_state):
|
73
|
|
- # field is internal, create a column in the objects table
|
74
|
|
- if new_state['internal']:
|
75
|
|
- return
|
76
|
|
- # field is of type rel2type, create the relational class_type table
|
77
|
|
- elif new_state['fieldtype'] == 'rel2type':
|
|
64
|
+ # @todo common fields creation does not allow to add new common fields. It should
|
|
65
|
+ def emfield_new(self, model, uid, initial_state, new_state):
|
|
66
|
+
|
|
67
|
+ # field is of type rel2type, create the relational class_type table and return
|
|
68
|
+ if new_state['fieldtype'] == 'rel2type':
|
78
|
69
|
# find relational_type name, and class name of the field
|
79
|
70
|
class_name = self._class_table_name_from_field(model, new_state)
|
80
|
71
|
type_name = model.component(new_state['rel_to_type_id']).name
|
|
@@ -82,30 +73,45 @@ class SQLMigrationHandler(DummyMigrationHandler):
|
82
|
73
|
self._query_bd(
|
83
|
74
|
create(table=table_name, column=self._pk_column),
|
84
|
75
|
)
|
85
|
|
- #print('create rel2type table', class_name, type_name)
|
86
|
76
|
return
|
|
77
|
+
|
|
78
|
+ # Column creation
|
|
79
|
+ #
|
|
80
|
+ # field is internal, create a column in the objects table
|
|
81
|
+ if new_state['internal']:
|
|
82
|
+ if new_state['fieldtype'] == 'pk': # this column has already beeen created by self._install_tables()
|
|
83
|
+ return
|
|
84
|
+ if new_state['name'] in EditorialModel.classtypes.common_fields: # this column has already beeen created by self._install_tables()
|
|
85
|
+ return
|
|
86
|
+
|
87
|
87
|
# field is relational (rel_field_id), create a column in the class_type table
|
88
|
88
|
elif new_state['rel_field_id']:
|
89
|
89
|
class_name = self._class_table_name_from_field(model, new_state)
|
90
|
90
|
rel_type_id = model.component(new_state['rel_field_id']).rel_to_type_id
|
91
|
91
|
type_name = model.component(rel_type_id).name
|
92
|
92
|
table_name = class_name + '_' + type_name
|
93
|
|
- #print('create field in rel2type table', new_state, class_name, rel_type_id, type_name)
|
|
93
|
+
|
94
|
94
|
# else create a column in the class table
|
95
|
95
|
else:
|
96
|
|
- # find name of the class table, and type of the field
|
97
|
96
|
table_name = self._class_table_name_from_field(model, new_state)
|
98
|
97
|
|
99
|
|
- fieldtype = SQLMigrationHandler.fieldtype_to_sql[new_state['fieldtype']]
|
|
98
|
+ field_definition = SQLMigrationHandler.fieldtype_to_sql[new_state['fieldtype']]
|
100
|
99
|
self._query_bd(
|
101
|
|
- alter_add(table=table_name, column=new_state['name'] + ' ' + fieldtype)
|
|
100
|
+ alter_add(table=table_name, column=new_state['name'] + ' ' + field_definition)
|
102
|
101
|
)
|
103
|
102
|
|
104
|
103
|
# Test if internal tables must be created, create it if it must
|
105
|
104
|
def _install_tables(self):
|
|
105
|
+ # create common fields definition
|
|
106
|
+ common_fields = [self._pk_column]
|
|
107
|
+ for name, options in EditorialModel.classtypes.common_fields.items():
|
|
108
|
+ if options['fieldtype'] != 'pk':
|
|
109
|
+ common_fields.append(name + ' ' + SQLMigrationHandler.fieldtype_to_sql[options['fieldtype']])
|
|
110
|
+
|
|
111
|
+ # create common tables
|
106
|
112
|
self._query_bd(
|
107
|
|
- create(table='object', column=self._pk_column),
|
108
|
|
- create(table='relation', column=('id_relation INT PRIMARY_KEY AUTOINCREMENT NOT NULL', 'id_superior INT', 'id_subdordinate INT', 'nature CHAR(255)', 'depth INT', 'rank INT'))
|
|
113
|
+ create(table=self._main_table_name, column=common_fields),
|
|
114
|
+ create(table=self._relation_table_name, column=('relation_id INT PRIMARY_KEY AUTOINCREMENT NOT NULL', 'superior_id INT', 'subdordinate_id INT', 'nature CHAR(255)', 'depth INT', 'rank INT'))
|
109
|
115
|
)
|
110
|
116
|
|
111
|
117
|
def _query_bd(self, *queries):
|