From ffe62f46e8740afefb7e80117ae1a3d0bfd4a373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20=C3=89vrard?= Date: Mon, 12 Apr 2021 20:56:08 +0200 Subject: [PATCH] Allow column sql types to be tested from the table handler issue9645 review312421002 --- CHANGELOG | 1 + trytond/backend/postgresql/table.py | 12 ++++++++++++ trytond/backend/sqlite/table.py | 14 +++++++++++++- trytond/backend/table.py | 12 ++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 01c81d2e44..2479ef5322 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Allow column sql types to be tested from the table handler * Add support for database connection parameters to configuration URI * Use immutable datastructures for Dict and MultiSelection fields * Skip warnings for non-interactive operations diff --git a/trytond/backend/postgresql/table.py b/trytond/backend/postgresql/table.py index ec48b7bf14..ef9dd5170e 100644 --- a/trytond/backend/postgresql/table.py +++ b/trytond/backend/postgresql/table.py @@ -221,6 +221,18 @@ class TableHandler(TableHandlerInterface): 'ALTER "' + column_name + '" TYPE ' + column_type) self._update_definitions(columns=True) + def column_is_type(self, column_name, type_, *, size=-1): + db_type = self._columns[column_name]['typname'].upper() + + database = Transaction().database + base_type = database.sql_type(type_).base.upper() + if base_type == 'VARCHAR': + same_size = self._columns[column_name]['size'] == size + else: + same_size = True + + return base_type == db_type and same_size + def db_default(self, column_name, value): if value in [True, False]: test = str(value).lower() diff --git a/trytond/backend/sqlite/table.py b/trytond/backend/sqlite/table.py index 5a8e270500..3122a38216 100644 --- a/trytond/backend/sqlite/table.py +++ b/trytond/backend/sqlite/table.py @@ -138,7 +138,7 @@ class TableHandler(TableHandlerInterface): size = match.group(3) and int(match.group(3)) or 0 else: typname = type_.upper() - size = -1 + size = None self._columns[column] = { 'notnull': notnull, 'hasdef': hasdef, @@ -170,6 +170,18 @@ class TableHandler(TableHandlerInterface): def alter_type(self, column_name, column_type): self._recreate_table({column_name: {'typname': column_type}}) + def column_is_type(self, column_name, type_, *, size=-1): + db_type = self._columns[column_name]['typname'].upper() + + database = Transaction().database + base_type = database.sql_type(type_).base.upper() + if base_type == 'VARCHAR': + same_size = self._columns[column_name]['size'] == size + else: + same_size = True + + return base_type == db_type and same_size + def db_default(self, column_name, value): warnings.warn('Unable to set default on column with SQLite backend') diff --git a/trytond/backend/table.py b/trytond/backend/table.py index b80a5ccb4b..4c7fb1012a 100644 --- a/trytond/backend/table.py +++ b/trytond/backend/table.py @@ -84,6 +84,18 @@ class TableHandlerInterface(object): ''' raise NotImplementedError + def column_is_type(self, column_name, type_, *, size=-1): + ''' + Return True if the column is of type type_ + + :param column_name: the column name + :param type_: the generic name of the type + :param size: if `type` is VARCHAR you can specify its size. + Defaults to -1 which will won't match any size + :return: a boolean + ''' + raise NotImplementedError + def db_default(self, column_name, value): ''' Set a default on a column