From 36efc47a448430c4a658f7ace3bb9aa65694f40e Mon Sep 17 00:00:00 2001 From: Bertrand Chenal Date: Thu, 10 Sep 2009 14:19:10 +0200 Subject: [PATCH] Allow to drop constraint, index and foreign key with custom table name --- CHANGELOG | 1 + trytond/backend/postgresql/table.py | 21 +++++++++++---------- trytond/backend/sqlite/table.py | 9 +++++++-- trytond/backend/table.py | 15 +++++++++++++-- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9fc33fdbec..fff56c8543 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Allow to drop constraint, index and foreign key with custom table name * Added column_rename on TableHandler * Add new tool reduce_ids * Add limit_clause function on cursor diff --git a/trytond/backend/postgresql/table.py b/trytond/backend/postgresql/table.py index 1c890e630d..ba8dc25f29 100644 --- a/trytond/backend/postgresql/table.py +++ b/trytond/backend/postgresql/table.py @@ -136,9 +136,8 @@ class TableHandler(TableHandlerInterface): contype, confdeltype, column, ref, conname = line if contype == 'f': self._fk_deltypes[column] = confdeltype - else: - if conname not in self._constraints: - self._constraints.append(conname) + if conname not in self._constraints: + self._constraints.append(conname) # Fetch indexes defined for the table self.cursor.execute("SELECT cl2.relname "\ @@ -272,9 +271,7 @@ class TableHandler(TableHandlerInterface): if not self.cursor.rowcount: add = True elif self._fk_deltypes.get(column_name) != code: - self.cursor.execute('ALTER TABLE "' + self.table_name + '" ' \ - 'DROP CONSTRAINT "' + self.table_name + '_' + \ - column_name + '_fkey"') + self.drop_fk(column_name) add = True if add: self.cursor.execute('ALTER TABLE "' + self.table_name + '" ' \ @@ -283,10 +280,14 @@ class TableHandler(TableHandlerInterface): 'ON DELETE ' + on_delete) self._update_definitions() - def index_action(self, column_name, action='add'): + def drop_fk(self, column_name, table=None): + self.drop_constraint(column_name + '_fkey', table=table) + + def index_action(self, column_name, action='add', table=None): if isinstance(column_name, basestring): column_name = [column_name] - index_name = self.table_name + "_" + '_'.join(column_name) + "_index" + index_name = (table or self.table_name) + "_" + '_'.join(column_name) \ + + "_index" if action == 'add': if index_name in self._indexes: @@ -366,8 +367,8 @@ class TableHandler(TableHandlerInterface): ident, constraint,)) self._update_definitions() - def drop_constraint(self, ident, exception=False): - ident = self.table_name + "_" + ident + def drop_constraint(self, ident, exception=False, table=None): + ident = (table or self.table_name) + "_" + ident if ident not in self._constraints: return try: diff --git a/trytond/backend/sqlite/table.py b/trytond/backend/sqlite/table.py index 32e9bbb908..e3809b44fe 100644 --- a/trytond/backend/sqlite/table.py +++ b/trytond/backend/sqlite/table.py @@ -196,7 +196,12 @@ class TableHandler(TableHandlerInterface): 'Unable to add foreign key on table %s!' % \ (self.table_name,)) - def index_action(self, column_name, action='add'): + def drop_fk(self, column_name, table=None): + logging.getLogger('init').warning( + 'Unable to drop foreign key on table %s!' % \ + (self.table_name,)) + + def index_action(self, column_name, action='add', table=None): if isinstance(column_name, basestring): column_name = [column_name] index_name = self.table_name + "_" + '_'.join(column_name) + "_index" @@ -243,7 +248,7 @@ class TableHandler(TableHandlerInterface): 'Unable to add constraint on table %s!' % \ (self.table_name,)) - def drop_constraint(self, ident, exception=False): + def drop_constraint(self, ident, exception=False, table=None): logging.getLogger('init').warning( 'Unable to drop constraint on table %s!' % \ (self.table_name,)) diff --git a/trytond/backend/table.py b/trytond/backend/table.py index 5b0fbff709..7401b13579 100644 --- a/trytond/backend/table.py +++ b/trytond/backend/table.py @@ -143,12 +143,22 @@ class TableHandlerInterface(object): ''' raise - def index_action(self, column_name, action='add'): + def drop_fk(self, column_name, table=None): + ''' + Drop a foreign key + + :param column_name: the column name + :param table: optional table name + ''' + raise + + def index_action(self, column_name, action='add', table=None): ''' Add/remove an index :param column_name: the column name or a list of column name :param action: 'add' or 'remove' + :param table: optional table name ''' raise @@ -172,13 +182,14 @@ class TableHandlerInterface(object): ''' raise - def drop_constraint(self, ident, exception=False): + def drop_constraint(self, ident, exception=False, table=None): ''' Remove a constraint :param ident: the name of the constraint :param exception: a boolean to raise or not an exception if it is not possible to remove the constraint + :param table: optional table name ''' raise