142 database |
142 database |
143 """ |
143 """ |
144 table = column = None # make pylint happy |
144 table = column = None # make pylint happy |
145 def precommit_event(self): |
145 def precommit_event(self): |
146 session, table, column = self.session, self.table, self.column |
146 session, table, column = self.session, self.table, self.column |
|
147 source = session.repo.system_source |
147 # drop index if any |
148 # drop index if any |
148 session.pool.source('system').drop_index(session, table, column) |
149 source.drop_index(session, table, column) |
149 try: |
150 if source.dbhelper.alter_column_support: |
150 session.system_sql('ALTER TABLE %s DROP COLUMN %s' |
151 session.system_sql('ALTER TABLE %s DROP COLUMN %s' |
151 % (table, column), rollback_on_failure=False) |
152 % (table, column), rollback_on_failure=False) |
152 self.info('dropped column %s from table %s', column, table) |
153 self.info('dropped column %s from table %s', column, table) |
153 except Exception, ex: |
154 else: |
154 # not supported by sqlite for instance |
155 # not supported by sqlite for instance |
155 self.error('error while altering table %s: %s', table, ex) |
156 self.error('dropping column not supported by the backend, handle ' |
|
157 'it yourself (%s.%s)', table, column) |
156 |
158 |
157 |
159 |
158 # base operations for in-memory schema synchronization ######################## |
160 # base operations for in-memory schema synchronization ######################## |
159 |
161 |
160 class MemSchemaNotifyChanges(hook.SingleLastOperation): |
162 class MemSchemaNotifyChanges(hook.SingleLastOperation): |
281 for etype in rschema.subjects(): |
283 for etype in rschema.subjects(): |
282 table = SQL_PREFIX + str(etype) |
284 table = SQL_PREFIX + str(etype) |
283 sqlexec('INSERT INTO %s_relation SELECT %s, %s FROM %s WHERE NOT %s IS NULL' |
285 sqlexec('INSERT INTO %s_relation SELECT %s, %s FROM %s WHERE NOT %s IS NULL' |
284 % (rtype, eidcolumn, column, table, column)) |
286 % (rtype, eidcolumn, column, table, column)) |
285 # drop existant columns |
287 # drop existant columns |
286 if session.repo.system_source.dbhelper.alter_column_support: |
288 #if session.repo.system_source.dbhelper.alter_column_support: |
287 for etype in rschema.subjects(): |
289 for etype in rschema.subjects(): |
288 DropColumn(session, table=SQL_PREFIX + str(etype), |
290 DropColumn(session, table=SQL_PREFIX + str(etype), |
289 column=SQL_PREFIX + rtype) |
291 column=SQL_PREFIX + rtype) |
290 else: |
292 else: |
291 for etype in rschema.subjects(): |
293 for etype in rschema.subjects(): |
292 try: |
294 try: |
293 add_inline_relation_column(session, str(etype), rtype) |
295 add_inline_relation_column(session, str(etype), rtype) |
294 except Exception, ex: |
296 except Exception, ex: |
1013 'R eid %%(x)s, X from_entity E, E name %%(name)s' |
1015 'R eid %%(x)s, X from_entity E, E name %%(name)s' |
1014 % rdeftype, {'x': self.eidto, 'name': str(subjschema)}) |
1016 % rdeftype, {'x': self.eidto, 'name': str(subjschema)}) |
1015 if rset[0][0] == 0 and not subjschema.eid in pendings: |
1017 if rset[0][0] == 0 and not subjschema.eid in pendings: |
1016 ptypes = session.transaction_data.setdefault('pendingrtypes', set()) |
1018 ptypes = session.transaction_data.setdefault('pendingrtypes', set()) |
1017 ptypes.add(rschema.type) |
1019 ptypes.add(rschema.type) |
1018 if session.repo.system_source.dbhelper.alter_column_support: |
1020 DropColumn(session, table=SQL_PREFIX + subjschema.type, |
1019 DropColumn(session, table=SQL_PREFIX + subjschema.type, |
1021 column=SQL_PREFIX + rschema.type) |
1020 column=SQL_PREFIX + rschema.type) |
|
1021 elif lastrel: |
1022 elif lastrel: |
1022 DropRelationTable(session, rschema.type) |
1023 DropRelationTable(session, rschema.type) |
1023 # if this is the last instance, drop associated relation type |
1024 # if this is the last instance, drop associated relation type |
1024 if lastrel and not self.eidto in pendings: |
1025 if lastrel and not self.eidto in pendings: |
1025 execute('DELETE CWRType X WHERE X eid %(x)s', {'x': self.eidto}) |
1026 execute('DELETE CWRType X WHERE X eid %(x)s', {'x': self.eidto}) |