Files
tryton/trytond/backend/postgresql/table.py
T

470 lines
20 KiB
Python
Raw Normal View History

2014-12-02 12:54:01 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.transaction import Transaction
from trytond.backend.table import TableHandlerInterface
import logging
2013-09-06 22:32:37 +02:00
__all__ = ['TableHandler']
2015-03-02 23:39:15 +01:00
logger = logging.getLogger(__name__)
class TableHandler(TableHandlerInterface):
def __init__(self, model, module_name=None, history=False):
super(TableHandler, self).__init__(model,
2009-03-18 18:08:41 +01:00
module_name=module_name, history=history)
self._columns = {}
self._constraints = []
self._fk_deltypes = {}
self._indexes = []
self._field2module = {}
transaction = Transaction()
cursor = transaction.connection.cursor()
# Create sequence if necessary
if not self.sequence_exist(self.sequence_name):
cursor.execute('CREATE SEQUENCE "%s"' % self.sequence_name)
# Create new table if necessary
if not self.table_exist(self.table_name):
cursor.execute('CREATE TABLE "%s" ()' % self.table_name)
self.table_schema = transaction.database.get_table_schema(
transaction.connection, self.table_name)
cursor.execute('SELECT tableowner = current_user FROM pg_tables '
2015-09-04 11:16:09 +02:00
'WHERE tablename = %s AND schemaname = %s',
(self.table_name, self.table_schema))
self.is_owner, = cursor.fetchone()
2015-09-04 11:16:09 +02:00
if model.__doc__ and self.is_owner:
cursor.execute('COMMENT ON TABLE "%s" IS \'%s\'' %
2012-09-24 22:16:53 +02:00
(self.table_name, model.__doc__.replace("'", "''")))
self._update_definitions()
2009-03-05 14:46:53 +01:00
if 'id' not in self._columns:
if not self.history:
cursor.execute('ALTER TABLE "%s" '
2012-04-01 23:08:15 +02:00
'ADD COLUMN id INTEGER '
'DEFAULT nextval(\'"%s"\') NOT NULL'
% (self.table_name, self.sequence_name))
cursor.execute('ALTER TABLE "%s" '
2012-04-01 23:08:15 +02:00
'ADD PRIMARY KEY(id)' % self.table_name)
else:
cursor.execute('ALTER TABLE "%s" '
2012-04-01 23:08:15 +02:00
'ADD COLUMN id INTEGER' % self.table_name)
2009-03-05 14:46:53 +01:00
self._update_definitions()
2014-12-02 12:54:01 +01:00
if self.history and '__id' not in self._columns:
cursor.execute('ALTER TABLE "%s" '
2012-04-01 23:08:15 +02:00
'ADD COLUMN __id INTEGER '
'DEFAULT nextval(\'"%s"\') NOT NULL' %
(self.table_name, self.sequence_name))
cursor.execute('ALTER TABLE "%s" '
2012-04-01 23:08:15 +02:00
'ADD PRIMARY KEY(__id)' % self.table_name)
else:
2015-09-04 11:16:09 +02:00
default = "nextval('%s'::regclass)" % self.sequence_name
if self.history:
if self._columns['__id']['default'] != default:
cursor.execute('ALTER TABLE "%s" '
2015-09-04 11:16:09 +02:00
'ALTER __id SET DEFAULT %s'
% (self.table_name, default))
if self._columns['id']['default'] != default:
cursor.execute('ALTER TABLE "%s" '
2015-09-04 11:16:09 +02:00
'ALTER id SET DEFAULT %s'
% (self.table_name, default))
self._update_definitions()
@staticmethod
def table_exist(table_name):
transaction = Transaction()
return bool(transaction.database.get_table_schema(
transaction.connection, table_name))
@staticmethod
def table_rename(old_name, new_name):
cursor = Transaction().connection.cursor()
2014-12-02 12:54:01 +01:00
# Rename table
if (TableHandler.table_exist(old_name)
and not TableHandler.table_exist(new_name)):
2012-04-01 23:08:15 +02:00
cursor.execute('ALTER TABLE "%s" RENAME TO "%s"'
% (old_name, new_name))
# Rename sequence
old_sequence = old_name + '_id_seq'
new_sequence = new_name + '_id_seq'
TableHandler.sequence_rename(old_sequence, new_sequence)
2014-12-02 12:54:01 +01:00
# Rename history table
old_history = old_name + "__history"
new_history = new_name + "__history"
if (TableHandler.table_exist(old_history)
and not TableHandler.table_exist(new_history)):
2012-04-01 23:08:15 +02:00
cursor.execute('ALTER TABLE "%s" RENAME TO "%s"'
% (old_history, new_history))
2015-09-04 11:16:09 +02:00
@classmethod
def sequence_schema(cls, sequence_name):
transaction = Transaction()
cursor = transaction.connection.cursor()
for schema in transaction.database.search_path:
2015-09-04 11:16:09 +02:00
cursor.execute('SELECT 1 '
'FROM information_schema.sequences '
'WHERE sequence_name = %s AND sequence_schema = %s',
(sequence_name, schema))
if cursor.rowcount:
return schema
@classmethod
def sequence_exist(cls, sequence_name):
return bool(cls.sequence_schema(sequence_name))
@staticmethod
def sequence_rename(old_name, new_name):
cursor = Transaction().connection.cursor()
if (TableHandler.sequence_exist(old_name)
and not TableHandler.sequence_exist(new_name)):
2012-04-01 23:08:15 +02:00
cursor.execute('ALTER TABLE "%s" RENAME TO "%s"'
% (old_name, new_name))
def column_exist(self, column_name):
return column_name in self._columns
2009-09-10 14:09:59 +02:00
def column_rename(self, old_name, new_name, exception=False):
cursor = Transaction().connection.cursor()
2012-04-01 23:08:15 +02:00
if (self.column_exist(old_name)
and not self.column_exist(new_name)):
cursor.execute('ALTER TABLE "%s" '
2012-04-01 23:08:15 +02:00
'RENAME COLUMN "%s" TO "%s"'
% (self.table_name, old_name, new_name))
2016-02-15 10:07:18 +01:00
self._update_definitions()
2009-09-10 14:09:59 +02:00
elif exception and self.column_exist(new_name):
2012-04-01 23:08:15 +02:00
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))
2009-09-10 14:09:59 +02:00
def _update_definitions(self):
cursor = Transaction().connection.cursor()
self._columns = {}
2015-09-04 11:16:09 +02:00
# Fetch columns definitions from the table
cursor.execute('SELECT '
2015-09-04 11:16:09 +02:00
'column_name, udt_name, is_nullable, character_maximum_length, '
'column_default '
'FROM information_schema.columns '
'WHERE table_name = %s AND table_schema = %s',
(self.table_name, self.table_schema))
for column, typname, nullable, size, default in cursor.fetchall():
self._columns[column] = {
2015-09-04 11:16:09 +02:00
'typname': typname,
'notnull': True if nullable == 'NO' else False,
'size': size,
'default': default,
}
2010-02-08 09:36:32 +01:00
# fetch constraints for the table
cursor.execute('SELECT constraint_name '
2015-09-04 11:16:09 +02:00
'FROM information_schema.table_constraints '
'WHERE table_name = %s AND table_schema = %s',
(self.table_name, self.table_schema))
self._constraints = [c for c, in cursor.fetchall()]
2015-09-04 11:16:09 +02:00
cursor.execute('SELECT k.column_name, r.delete_rule '
2015-09-04 11:16:09 +02:00
'FROM information_schema.key_column_usage AS k '
'JOIN information_schema.referential_constraints AS r '
'ON r.constraint_schema = k.constraint_schema '
'AND r.constraint_name = k.constraint_name '
'WHERE k.table_name = %s AND k.table_schema = %s',
(self.table_name, self.table_schema))
self._fk_deltypes = dict(cursor.fetchall())
# Fetch indexes defined for the table
cursor.execute("SELECT cl2.relname "
2012-04-01 23:08:15 +02:00
"FROM pg_index ind "
"JOIN pg_class cl on (cl.oid = ind.indrelid) "
2015-09-04 11:16:09 +02:00
"JOIN pg_namespace n ON (cl.relnamespace = n.oid) "
2012-04-01 23:08:15 +02:00
"JOIN pg_class cl2 on (cl2.oid = ind.indexrelid) "
2015-09-04 11:16:09 +02:00
"WHERE cl.relname = %s AND n.nspname = %s",
(self.table_name, self.table_schema))
self._indexes = [l[0] for l in cursor.fetchall()]
# Keep track of which module created each field
self._field2module = {}
if self.object_name is not None:
cursor.execute('SELECT f.name, f.module '
2012-04-01 23:08:15 +02:00
'FROM ir_model_field f '
'JOIN ir_model m on (f.model=m.id) '
'WHERE m.model = %s',
(self.object_name,))
for line in cursor:
self._field2module[line[0]] = line[1]
def alter_size(self, column_name, column_type):
cursor = Transaction().connection.cursor()
cursor.execute("ALTER TABLE \"%s\" "
2012-04-01 23:08:15 +02:00
"RENAME COLUMN \"%s\" TO _temp_change_size"
% (self.table_name, column_name))
cursor.execute("ALTER TABLE \"%s\" "
2012-04-01 23:08:15 +02:00
"ADD COLUMN \"%s\" %s"
% (self.table_name, column_name, column_type))
cursor.execute("UPDATE \"%s\" "
2012-04-01 23:08:15 +02:00
"SET \"%s\" = _temp_change_size::%s"
% (self.table_name, column_name, column_type))
cursor.execute("ALTER TABLE \"%s\" "
2012-04-01 23:08:15 +02:00
"DROP COLUMN _temp_change_size"
% (self.table_name,))
self._update_definitions()
def alter_type(self, column_name, column_type):
cursor = Transaction().connection.cursor()
cursor.execute('ALTER TABLE "' + self.table_name + '" '
2012-04-01 23:08:15 +02:00
'ALTER "' + column_name + '" TYPE ' + column_type)
self._update_definitions()
def db_default(self, column_name, value):
2015-09-04 11:16:09 +02:00
if value in [True, False]:
test = str(value).lower()
else:
test = value
if self._columns[column_name]['default'] != test:
cursor = Transaction().connection.cursor()
cursor.execute('ALTER TABLE "' + self.table_name + '" '
2015-09-04 11:16:09 +02:00
'ALTER COLUMN "' + column_name + '" SET DEFAULT %s',
(value,))
def add_raw_column(self, column_name, column_type, column_format,
default_fun=None, field_size=None, migrate=True, string=''):
cursor = Transaction().connection.cursor()
def comment():
2015-09-04 11:16:09 +02:00
if self.is_owner:
cursor.execute('COMMENT ON COLUMN "%s"."%s" IS \'%s\'' %
(self.table_name, column_name, string.replace("'", "''")))
if self.column_exist(column_name):
if (column_name in ('create_date', 'write_date')
and column_type[1].lower() != 'timestamp(6)'):
2014-12-02 12:54:01 +01:00
# Migrate dates from timestamp(0) to timestamp
cursor.execute('ALTER TABLE "' + self.table_name + '" '
2012-04-01 23:08:15 +02:00
'ALTER COLUMN "' + column_name + '" TYPE timestamp')
comment()
if not migrate:
return
base_type = column_type[0].lower()
if base_type != self._columns[column_name]['typname']:
if (self._columns[column_name]['typname'], base_type) in [
('varchar', 'text'),
('text', 'varchar'),
('date', 'timestamp'),
('int4', 'float8'),
]:
self.alter_type(column_name, base_type)
else:
2015-03-02 23:39:15 +01:00
logger.warning(
2012-04-01 23:08:15 +02:00
'Unable to migrate column %s on table %s '
2015-03-02 23:39:15 +01:00
'from %s to %s.',
2015-03-05 11:32:18 +01:00
column_name, self.table_name,
self._columns[column_name]['typname'], base_type)
2012-04-01 23:08:15 +02:00
if (base_type == 'varchar'
and self._columns[column_name]['typname'] == 'varchar'):
# Migrate size
2013-02-27 22:50:31 +01:00
if field_size is None:
2016-02-07 16:46:16 +01:00
if self._columns[column_name]['size']:
self.alter_size(column_name, base_type)
elif self._columns[column_name]['size'] == field_size:
pass
2016-02-07 16:46:16 +01:00
elif (self._columns[column_name]['size']
2013-02-27 22:50:31 +01:00
and self._columns[column_name]['size'] < field_size):
self.alter_size(column_name, column_type[1])
else:
2015-03-02 23:39:15 +01:00
logger.warning(
2012-04-01 23:08:15 +02:00
'Unable to migrate column %s on table %s '
2015-03-02 23:39:15 +01:00
'from varchar(%s) to varchar(%s).',
2015-03-05 11:32:18 +01:00
column_name, self.table_name,
self._columns[column_name]['size'] > 0 and
self._columns[column_name]['size'] or "",
field_size)
return
column_type = column_type[1]
cursor.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s'
2012-04-01 23:08:15 +02:00
% (self.table_name, column_name, column_type))
comment()
2009-03-12 15:52:44 +01:00
if column_format:
# check if table is non-empty:
cursor.execute('SELECT 1 FROM "%s" limit 1' % self.table_name)
if cursor.rowcount:
2009-03-12 15:52:44 +01:00
# Populate column with default values:
default = None
if default_fun is not None:
2010-08-19 22:01:12 +02:00
default = default_fun()
cursor.execute('UPDATE "' + self.table_name + '" '
2012-04-01 23:08:15 +02:00
'SET "' + column_name + '" = %s',
(column_format(default),))
self._update_definitions()
def add_fk(self, column_name, reference, on_delete=None):
2012-04-01 23:08:15 +02:00
if on_delete is not None:
on_delete = on_delete.upper()
else:
on_delete = 'SET NULL'
cursor = Transaction().connection.cursor()
2015-09-04 11:16:09 +02:00
name = self.table_name + '_' + column_name + '_fkey'
cursor.execute('SELECT 1 '
2015-09-04 11:16:09 +02:00
'FROM information_schema.key_column_usage '
'WHERE table_name = %s AND table_schema = %s '
'AND constraint_name = %s',
(self.table_name, self.table_schema, name))
add = False
if not cursor.rowcount:
add = True
2015-09-04 11:16:09 +02:00
elif self._fk_deltypes.get(column_name) != on_delete:
self.drop_fk(column_name)
add = True
if add:
cursor.execute('ALTER TABLE "' + self.table_name + '" '
2015-09-04 11:16:09 +02:00
'ADD CONSTRAINT "' + name + '" '
'FOREIGN KEY ("' + column_name + '") '
2012-04-01 23:08:15 +02:00
'REFERENCES "' + reference + '" '
'ON DELETE ' + on_delete)
self._update_definitions()
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]
2013-02-27 22:50:31 +01:00
index_name = ((table or self.table_name) + "_" + '_'.join(column_name)
+ "_index")
2011-09-28 18:09:30 +02:00
if self._indexes:
test_index_name = index_name[:max(map(len, self._indexes))]
else:
test_index_name = index_name
with Transaction().connection.cursor() as cursor:
if action == 'add':
if test_index_name in self._indexes:
return
cursor.execute('CREATE INDEX "' + index_name + '" '
'ON "' + self.table_name + '" ( '
+ ','.join(['"' + x + '"' for x in column_name]) + ')')
self._update_definitions()
elif action == 'remove':
if len(column_name) == 1:
if (self._field2module.get(column_name[0],
self.module_name) != self.module_name):
return
if test_index_name in self._indexes:
cursor.execute('DROP INDEX "%s" ' % (index_name,))
self._update_definitions()
else:
raise Exception('Index action not supported!')
def not_null_action(self, column_name, action='add'):
if not self.column_exist(column_name):
return
with Transaction().connection.cursor() as cursor:
if action == 'add':
if self._columns[column_name]['notnull']:
return
cursor.execute('SELECT id FROM "%s" '
'WHERE "%s" IS NULL'
% (self.table_name, column_name))
if not cursor.rowcount:
cursor.execute('ALTER TABLE "' + self.table_name + '" '
'ALTER COLUMN "' + column_name + '" SET NOT NULL')
self._update_definitions()
else:
logger.warning(
'Unable to set column %s '
'of table %s not null !\n'
'Try to re-run: '
'trytond.py --update=module\n'
'If it doesn\'t work, update records '
'and execute manually:\n'
'ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL',
column_name, self.table_name, self.table_name,
column_name)
elif action == 'remove':
if not self._columns[column_name]['notnull']:
return
if (self._field2module.get(column_name, self.module_name)
!= self.module_name):
return
cursor.execute('ALTER TABLE "%s" '
'ALTER COLUMN "%s" DROP NOT NULL'
% (self.table_name, column_name))
self._update_definitions()
else:
raise Exception('Not null action not supported!')
def add_constraint(self, ident, constraint, exception=False):
ident = self.table_name + "_" + ident
if ident in self._constraints:
# This constrain already exist
return
cursor = Transaction().connection.cursor()
try:
cursor.execute('ALTER TABLE "%s" '
2012-04-01 23:08:15 +02:00
'ADD CONSTRAINT "%s" %s'
% (self.table_name, ident, constraint), constraint.params)
2010-07-08 19:53:39 +02:00
except Exception:
if exception:
raise
2015-03-02 23:39:15 +01:00
logger.warning(
2012-04-01 23:08:15 +02:00
'unable to add \'%s\' constraint on table %s !\n'
'If you want to have it, you should update the records '
'and execute manually:\n'
2015-03-02 23:39:15 +01:00
'ALTER table "%s" ADD CONSTRAINT "%s" %s',
2015-03-05 11:32:18 +01:00
constraint, self.table_name, self.table_name, ident,
constraint)
self._update_definitions()
def drop_constraint(self, ident, exception=False, table=None):
ident = (table or self.table_name) + "_" + ident
if ident not in self._constraints:
return
cursor = Transaction().connection.cursor()
try:
cursor.execute('ALTER TABLE "%s" '
2012-04-01 23:08:15 +02:00
'DROP CONSTRAINT "%s"'
% (self.table_name, ident))
2010-07-08 19:53:39 +02:00
except Exception:
if exception:
raise
2015-03-02 23:39:15 +01:00
logger.warning(
'unable to drop \'%s\' constraint on table %s!',
2015-03-05 11:32:18 +01:00
ident, self.table_name)
self._update_definitions()
def drop_column(self, column_name, exception=False):
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))
2010-07-08 19:53:39 +02:00
except Exception:
if exception:
raise
2015-03-02 23:39:15 +01:00
logger.warning(
'unable to drop \'%s\' column on table %s!',
2015-03-05 11:32:18 +01:00
column_name, self.table_name, exc_info=True)
self._update_definitions()
@staticmethod
2010-02-18 18:50:35 +01:00
def drop_table(cursor, model, table, cascade=False):
cursor = Transaction().connection.cursor()
2012-04-01 23:08:15 +02:00
cursor.execute('DELETE FROM ir_model_data '
'WHERE model = \'%s\'' % model)
query = 'DROP TABLE "%s"' % table
if cascade:
query = query + ' CASCADE'
cursor.execute(query)