Always raise exception in table handler

issue7175
review40161002
This commit is contained in:
Cédric Krier
2018-03-15 00:43:32 +01:00
parent 57248f9655
commit 036ca6d6a9
5 changed files with 42 additions and 92 deletions
+1
View File
@@ -1,3 +1,4 @@
* Always raise exception in table handler
* Simplify API for Session: new, remove, check and reset
* Ensure that all buttons are registered in ir.model.button
* Make trytond-admin ask for admin email
+20 -50
View File
@@ -110,19 +110,18 @@ class TableHandler(TableHandlerInterface):
def column_exist(self, column_name):
return column_name in self._columns
def column_rename(self, old_name, new_name, exception=False):
def column_rename(self, old_name, new_name):
cursor = Transaction().connection.cursor()
if (self.column_exist(old_name)
and not self.column_exist(new_name)):
cursor.execute('ALTER TABLE "%s" '
'RENAME COLUMN "%s" TO "%s"'
% (self.table_name, old_name, new_name))
self._update_definitions(columns=True)
elif exception and self.column_exist(new_name):
raise Exception('Unable to rename column %s.%s to %s.%s: '
'%s.%s already exist!'
% (self.table_name, old_name, self.table_name, new_name,
self.table_name, new_name))
if self.column_exist(old_name):
if not self.column_exist(new_name):
cursor.execute('ALTER TABLE "%s" '
'RENAME COLUMN "%s" TO "%s"'
% (self.table_name, old_name, new_name))
self._update_definitions(columns=True)
else:
logger.warning(
'Unable to rename column %s on table %s to %s.',
old_name, self.table_name, self.table_name, new_name)
def _update_definitions(self,
columns=None, constraints=None, indexes=None):
@@ -381,60 +380,31 @@ class TableHandler(TableHandlerInterface):
else:
raise Exception('Not null action not supported!')
def add_constraint(self, ident, constraint, exception=False):
def add_constraint(self, ident, constraint):
ident = self.convert_name(self.table_name + "_" + ident)
if ident in self._constraints:
# This constrain already exist
return
cursor = Transaction().connection.cursor()
try:
cursor.execute('ALTER TABLE "%s" '
'ADD CONSTRAINT "%s" %s'
% (self.table_name, ident, constraint), constraint.params)
except Exception:
if exception:
raise
logger.warning(
'unable to add \'%s\' constraint on table %s !\n'
'If you want to have it, you should update the records '
'and execute manually:\n'
'ALTER table "%s" ADD CONSTRAINT "%s" %s',
constraint, self.table_name, self.table_name, ident,
constraint)
cursor.execute('ALTER TABLE "%s" ADD CONSTRAINT "%s" %s'
% (self.table_name, ident, constraint), constraint.params)
self._update_definitions(constraints=True)
def drop_constraint(self, ident, exception=False, table=None):
def drop_constraint(self, ident, table=None):
ident = self.convert_name((table or self.table_name) + "_" + ident)
if ident not in self._constraints:
return
cursor = Transaction().connection.cursor()
try:
cursor.execute('ALTER TABLE "%s" '
'DROP CONSTRAINT "%s"'
% (self.table_name, ident))
except Exception:
if exception:
raise
logger.warning(
'unable to drop \'%s\' constraint on table %s!',
ident, self.table_name)
cursor.execute('ALTER TABLE "%s" DROP CONSTRAINT "%s"'
% (self.table_name, ident))
self._update_definitions(constraints=True)
def drop_column(self, column_name, exception=False):
def drop_column(self, column_name):
if not self.column_exist(column_name):
return
cursor = Transaction().connection.cursor()
try:
cursor.execute(
'ALTER TABLE "%s" DROP COLUMN "%s"' %
(self.table_name, column_name))
except Exception:
if exception:
raise
logger.warning(
'unable to drop \'%s\' column on table %s!',
column_name, self.table_name, exc_info=True)
cursor.execute('ALTER TABLE "%s" DROP COLUMN "%s"'
% (self.table_name, column_name))
self._update_definitions(columns=True)
@staticmethod
+13 -26
View File
@@ -114,15 +114,14 @@ class TableHandler(TableHandlerInterface):
cursor.execute('DROP TABLE "%s"' % temp_table)
self._update_definitions()
def column_rename(self, old_name, new_name, exception=False):
if self.column_exist(old_name) and \
not self.column_exist(new_name):
self._recreate_table({old_name: {'name': new_name}})
elif exception and self.column_exist(new_name):
raise Exception('Unable to rename column %s.%s to %s.%s: '
'%s.%s already exist!'
% (self.table_name, old_name, self.table_name, new_name,
self.table_name, new_name))
def column_rename(self, old_name, new_name):
if self.column_exist(old_name):
if self.column_exist(new_name):
self._recreate_table({old_name: {'name': new_name}})
else:
logger.warning(
'Unable to rename column %s on table %s to %s.',
old_name, self.table_name, new_name)
def _update_definitions(self, columns=None, indexes=None):
if columns is None and indexes is None:
@@ -282,26 +281,20 @@ class TableHandler(TableHandlerInterface):
else:
raise Exception('Not null action not supported!')
def add_constraint(self, ident, constraint, exception=False):
def add_constraint(self, ident, constraint):
warnings.warn('Unable to add constraint with SQLite backend')
def drop_constraint(self, ident, exception=False, table=None):
def drop_constraint(self, ident, table=None):
warnings.warn('Unable to drop constraint with SQLite backend')
def drop_column(self, column_name, exception=False):
def drop_column(self, column_name):
if not self.column_exist(column_name):
return
transaction = Transaction()
database = transaction.database
cursor = transaction.connection.cursor()
temp_table = '_temp_%s' % self.table_name
try:
TableHandler.table_rename(self.table_name, temp_table)
except Exception:
if exception:
raise
logger.warning('Unable to drop column: Error renaming '
'table %s to temp!', self.table_name)
TableHandler.table_rename(self.table_name, temp_table)
new_table = TableHandler(self._model, history=self.history)
for name, values in self._columns.iteritems():
if name != column_name:
@@ -315,13 +308,7 @@ class TableHandler(TableHandlerInterface):
') SELECT ' +
','.join('"%s"' % c for c in columns_name) + ' ' +
'FROM "%s"') % (self.table_name, temp_table))
try:
cursor.execute('DROP TABLE "%s"' % temp_table)
except Exception:
if exception:
raise
logger.warning('Unable to drop temporary table %s',
self.temp_table)
cursor.execute('DROP TABLE "%s"' % temp_table)
self._update_definitions()
@staticmethod
+5 -13
View File
@@ -57,14 +57,12 @@ class TableHandlerInterface(object):
'''
raise NotImplementedError
def column_rename(self, old_name, new_name, exception=False):
def column_rename(self, old_name, new_name):
'''
Rename column
Rename column if exists
:param old_name: the name of the existing column
:param new_name: the new name of the column
:param exception: a boolean to raise or not an exception
if it is not possible to rename the column.
'''
raise NotImplementedError
@@ -144,35 +142,29 @@ class TableHandlerInterface(object):
'''
raise NotImplementedError
def add_constraint(self, ident, constraint, exception=False):
def add_constraint(self, ident, constraint):
'''
Add a constraint
:param ident: the name of the constraint
:param constraint: the definition of the constraint
:param exception: a boolean to raise or not an exception
if it is not possible to add the constraint
'''
raise NotImplementedError
def drop_constraint(self, ident, exception=False, table=None):
def drop_constraint(self, ident, 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 NotImplementedError
def drop_column(self, column_name, exception=False):
def drop_column(self, column_name):
'''
Remove a column
:param column_name: the column name
:param exception: a boolean to raise or not an exception
if it is not possible to remove the column
'''
raise NotImplementedError
+3 -3
View File
@@ -211,7 +211,7 @@ class User(ModelSQL, ModelView):
table.not_null_action('menu', action='remove')
# Migration from 2.6
table.drop_column('login_try', exception=True)
table.drop_column('login_try')
# Migration from 3.0
if table.column_exist('password') and table.column_exist('salt'):
@@ -221,8 +221,8 @@ class User(ModelSQL, ModelView):
cursor.execute(*sqltable.update(
columns=[sqltable.password_hash],
values=[password_hash_new]))
table.drop_column('password', exception=True)
table.drop_column('salt', exception=True)
table.drop_column('password')
table.drop_column('salt')
# Migration from 4.2: Remove required on name
table.not_null_action('name', action='remove')