Browse Source

migration handler SQL: use basic_type and fieldtypes method to create column definitions

ArnAud 9 years ago
parent
commit
5babe2f6d7
1 changed files with 27 additions and 4 deletions
  1. 27
    4
      EditorialModel/migrationhandler/sql.py

+ 27
- 4
EditorialModel/migrationhandler/sql.py View File

@@ -8,6 +8,7 @@
8 8
 
9 9
 import EditorialModel
10 10
 from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
11
+from EditorialModel.fieldtypes.generic import GenericFieldType
11 12
 from EditorialModel.model import Model
12 13
 from mosql.db import Database
13 14
 from Lodel.utils.mosql import create, alter_add
@@ -95,18 +96,40 @@ class SQLMigrationHandler(DummyMigrationHandler):
95 96
         else:
96 97
             table_name = self._class_table_name_from_field(model, new_state)
97 98
 
98
-        field_definition = SQLMigrationHandler.fieldtype_to_sql[new_state['fieldtype']]
99
+        field_definition = self._fieldtype_definition(new_state['fieldtype'], new_state)
99 100
         self._query_bd(
100 101
             alter_add(table=table_name, column=new_state['name'] + ' ' + field_definition)
101 102
         )
102 103
 
103
-    # Test if internal tables must be created, create it if it must
104
+    ## convert fieldtype name to SQL definition
105
+    def _fieldtype_definition(self, fieldtype, options):
106
+        basic_type = GenericFieldType.from_name(fieldtype).ftype
107
+        if basic_type == 'int':
108
+            return 'INT'
109
+        elif basic_type == 'char':
110
+            max_length = options['max_length'] if 'max_length' in options else 255
111
+            return 'CHAR(%s)' % max_length
112
+        elif basic_type == 'text':
113
+            return 'TEXT'
114
+        elif basic_type == 'bool':
115
+            return 'BOOLEAN'
116
+        elif basic_type == 'datetime':
117
+            definition = 'DATETIME'
118
+            if 'now_on_create' in options and options['now_on_create']:
119
+                definition += ' DEFAULT CURRENT_TIMESTAMP'
120
+            if 'now_on_update' in options and options['now_on_update']:
121
+                definition += ' ON UPDATE CURRENT_TIMESTAMP'
122
+            return definition
123
+
124
+        raise EditorialModel.exceptions.MigrationHandlerChangeError("Basic type '%s' of fieldtype '%s' is not compatible with SQL migration Handler" % basic_type, fieldtype)
125
+
126
+    ## Test if internal tables must be created, create it if it must
104 127
     def _install_tables(self):
105 128
         # create common fields definition
106 129
         common_fields = [self._pk_column]
107 130
         for name, options in EditorialModel.classtypes.common_fields.items():
108 131
             if options['fieldtype'] != 'pk':
109
-                common_fields.append(name + ' ' + SQLMigrationHandler.fieldtype_to_sql[options['fieldtype']])
132
+                common_fields.append(name + ' ' + self._fieldtype_definition(options['fieldtype'], options))
110 133
 
111 134
         # create common tables
112 135
         self._query_bd(
@@ -117,7 +140,7 @@ class SQLMigrationHandler(DummyMigrationHandler):
117 140
     def _query_bd(self, *queries):
118 141
         with self.db as cur:
119 142
             for query in queries:
120
-                print(query)
143
+                #print(query)
121 144
                 cur.execute(query)
122 145
 
123 146
     def _class_table_name(self, class_name):

Loading…
Cancel
Save