changeset e775cf82255f in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=e775cf82255f
description:
Allow column sql types to be tested from the table handler
issue9645
review312421002
diffstat:
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 deletions(-)
diffs (83 lines):
diff -r ee23aca64b26 -r e775cf82255f CHANGELOG
--- a/CHANGELOG Mon Apr 12 20:54:03 2021 +0200
+++ b/CHANGELOG Mon Apr 12 20:56:08 2021 +0200
@@ -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 -r ee23aca64b26 -r e775cf82255f trytond/backend/postgresql/table.py
--- a/trytond/backend/postgresql/table.py Mon Apr 12 20:54:03 2021 +0200
+++ b/trytond/backend/postgresql/table.py Mon Apr 12 20:56:08 2021 +0200
@@ -221,6 +221,18 @@
'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 -r ee23aca64b26 -r e775cf82255f trytond/backend/sqlite/table.py
--- a/trytond/backend/sqlite/table.py Mon Apr 12 20:54:03 2021 +0200
+++ b/trytond/backend/sqlite/table.py Mon Apr 12 20:56:08 2021 +0200
@@ -138,7 +138,7 @@
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 @@
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 -r ee23aca64b26 -r e775cf82255f trytond/backend/table.py
--- a/trytond/backend/table.py Mon Apr 12 20:54:03 2021 +0200
+++ b/trytond/backend/table.py Mon Apr 12 20:56:08 2021 +0200
@@ -84,6 +84,18 @@
'''
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