/usr/lib/python2.7/dist-packages/trytond/backend/mysql/table.py is in tryton-server 3.0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | #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.backend.table import TableHandlerInterface
import logging
class TableHandler(TableHandlerInterface):
def __init__(self, cursor, model, module_name=None, history=False):
super(TableHandler, self).__init__(cursor, model,
module_name=module_name, history=history)
self._columns = {}
self._constraints = []
self._fkeys = []
self._indexes = []
self._field2module = {}
self._model = model
# Create new table if necessary
if not self.table_exist(self.cursor, self.table_name):
if not self.history:
self.cursor.execute('CREATE TABLE `%s` ('
'id BIGINT AUTO_INCREMENT NOT NULL, '
'PRIMARY KEY(id)'
') ENGINE=InnoDB;' % self.table_name)
else:
self.cursor.execute('CREATE TABLE `%s` ('
'__id BIGINT AUTO_INCREMENT NOT NULL, '
'id BIGINT, '
'PRIMARY KEY(__id)'
') ENGINE=InnoDB;' % self.table_name)
self._update_definitions()
if 'id' not in self._columns:
if not self.history:
self.cursor.execute('ALTER TABLE `%s` '
'ADD COLUMN id BIGINT AUTO_INCREMENT '
'NOT NULL PRIMARY KEY' % self.table_name)
else:
self.cursor.execute('ALTER TABLE `%s` '
'ADD COLUMN id BIGINT' % self.table_name)
self._update_definitions()
if self.history and not '__id' in self._columns:
self.cursor.execute('ALTER TABLE `%s` '
'ADD COLUMN __id BIGINT AUTO_INCREMENT '
'NOT NULL PRIMARY KEY' % self.table_name)
self._update_definitions()
@staticmethod
def table_exist(cursor, table_name):
cursor.execute("SELECT table_name FROM information_schema.tables "
"WHERE table_schema = %s AND table_name = %s",
(cursor.database_name, table_name))
return bool(cursor.rowcount)
@staticmethod
def table_rename(cursor, old_name, new_name):
#Rename table
if (TableHandler.table_exist(cursor, old_name)
and not TableHandler.table_exist(cursor, new_name)):
cursor.execute('ALTER TABLE `%s` RENAME TO `%s`'
% (old_name, new_name))
#Rename history table
old_history = old_name + '__history'
new_history = new_name + '__history'
if (TableHandler.table_exist(cursor, old_history)
and not TableHandler.table_exist(cursor, new_history)):
cursor.execute('ALTER TABLE `%s` RENAME TO `%s`'
% (old_history, new_history))
@staticmethod
def sequence_exist(cursor, sequence_name):
return True
@staticmethod
def sequence_rename(cursor, old_name, new_name):
pass
def column_exist(self, column_name):
return column_name in self._columns
def column_rename(self, old_name, new_name, exception=False):
if (self.column_exist(old_name)
and not self.column_exist(new_name)):
self.cursor.execute('ALTER TABLE `%s` '
'RENAME COLUMN `%s` TO `%s`'
% (self.table_name, old_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 _update_definitions(self):
# Fetch columns definitions from the table
self.cursor.execute("SELECT column_name, character_maximum_length, "
"data_type, is_nullable, column_default "
"FROM information_schema.columns "
"WHERE table_schema = %s AND table_name = %s",
(self.cursor.database_name, self.table_name))
self._columns = {}
for line in self.cursor.fetchall():
column, size, typname, nullable, default = line
self._columns[column] = {
'size': size,
'typname': typname,
'nullable': nullable == 'YES' and True or False,
'default': default,
}
# fetch constraints for the table
self.cursor.execute("SELECT constraint_name, constraint_type "
"FROM information_schema.table_constraints "
"WHERE table_schema = %s AND table_name = %s",
(self.cursor.database_name, self.table_name))
self._constraints = []
self._fkeys = []
for line in self.cursor.fetchall():
conname, contype = line
if contype not in ('PRIMARY KEY', 'FOREIGN KEY'):
self._constraints.append(conname)
elif contype == 'FOREIGN KEY':
self._fkeys.append(conname)
# Fetch indexes defined for the table
self.cursor.execute('SHOW INDEXES FROM `%s`' % self.table_name)
self._indexes = list(set(x[2] for x in self.cursor.fetchall()
if x[2] != 'PRIMARY'))
# Keep track of which module created each field
self._field2module = {}
if self.object_name is not None:
self.cursor.execute('SELECT f.name, f.module '
'FROM ir_model_field f '
'JOIN ir_model m on (f.model=m.id) '
'WHERE m.model = %s',
(self.object_name,))
for line in self.cursor.fetchall():
self._field2module[line[0]] = line[1]
def alter_size(self, column_name, column_type):
self.cursor.execute('ALTER TABLE `%s` '
'MODIFY COLUMN `%s` %s'
% (self.table_name, column_name,
self._column_definition(column_name)))
self._update_definitions()
def alter_type(self, column_name, column_type):
self.cursor.execute('ALTER TABLE `%s` '
'MODIFY COLUMN `%s` %s'
% (self.table_name, column_name,
self._column_definition(column_name, typname=column_type)))
self._update_definitions()
def db_default(self, column_name, value):
self.cursor.execute('ALTER TABLE `%s` '
'MODIFY COLUMN `%s` %s'
% (self.table_name, column_name,
self._column_definition(column_name, default=value)))
self._update_definitions()
def add_raw_column(self, column_name, column_type, column_format,
default_fun=None, field_size=None, migrate=True, string=''):
if self.column_exist(column_name):
if not migrate:
return
base_type = column_type[0].lower()
convert = {
'char': 'varchar',
'signed integer': 'bigint',
}
base_type = convert.get(base_type, base_type)
if base_type != self._columns[column_name]['typname']:
if (self._columns[column_name]['typname'], base_type) in (
('varchar', 'text'),
('text', 'varchar'),
('date', 'timestamp'),
('bigint', 'double'),
('int', 'bigint'),
('tinyint', 'bool'),
('decimal', 'numeric'),
):
self.alter_type(column_name, base_type)
else:
logging.getLogger('init').warning(
'Unable to migrate column %s on table %s '
'from %s to %s.'
% (column_name, self.table_name,
self._columns[column_name]['typname'], base_type))
if (base_type == 'varchar'
and self._columns[column_name]['typname'] == 'varchar'):
# Migrate size
if field_size is None:
if self._columns[column_name]['size'] != 255:
self.alter_size(column_name, base_type)
elif self._columns[column_name]['size'] == field_size:
pass
else:
logging.getLogger('init').warning(
'Unable to migrate column %s on table %s '
'from varchar(%s) to varchar(%s).'
% (column_name, self.table_name,
self._columns[column_name]['size'] > 0
and self._columns[column_name]['size'] or 255,
field_size))
return
column_type = column_type[1]
self.cursor.execute('ALTER TABLE `%s` ADD COLUMN `%s` %s' %
(self.table_name, column_name, column_type))
if column_format:
# check if table is non-empty:
self.cursor.execute('SELECT 1 FROM `%s` limit 1' % self.table_name)
if self.cursor.rowcount:
# Populate column with default values:
default = None
if default_fun is not None:
default = default_fun()
self.cursor.execute('UPDATE `' + self.table_name + '` '
'SET `' + column_name + '` = %s',
(column_format(default),))
self._update_definitions()
def add_fk(self, column_name, reference, on_delete=None):
if on_delete is None:
on_delete = 'SET NULL'
conname = '%s_%s_fkey' % (self.table_name, column_name)
if conname in self._fkeys:
self.drop_fk(column_name)
self.cursor.execute('ALTER TABLE `%s` '
'ADD CONSTRAINT `%s` FOREIGN KEY (`%s`) '
'REFERENCES `%s` (id) ON DELETE %s'
% (self.table_name, conname, column_name, reference, on_delete))
self._update_definitions()
def drop_fk(self, column_name, table=None):
conname = '%s_%s_fkey' % (self.table_name, column_name)
if conname not in self._fkeys:
return
self.cursor.execute('ALTER TABLE `%s` '
'DROP FOREIGN KEY `%s`' % (self.table_name, conname))
self._update_definitions()
def index_action(self, column_name, action='add', table=None):
if isinstance(column_name, basestring):
column_name = [column_name]
index_name = ((table or self.table_name) + "_" + '_'.join(column_name)
+ "_index")
# Index name length is limited to 64
index_name = index_name[:64]
for k in column_name:
if k in self._columns:
if self._columns[k]['typname'] in ('text', 'blob'):
return
if action == 'add':
if index_name in self._indexes:
return
self.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 index_name in self._indexes:
self.cursor.execute('DROP INDEX `%s` ON `%s`'
% (index_name, self.table_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
if action == 'add':
if not self._columns[column_name]['nullable']:
return
self.cursor.execute('SELECT id FROM `%s` '
'WHERE `%s` IS NULL'
% (self.table_name, column_name))
if not self.cursor.rowcount:
self.cursor.execute('ALTER TABLE `%s` '
'MODIFY COLUMN `%s` %s'
% (self.table_name, column_name,
self._column_definition(column_name, nullable=False)))
self._update_definitions()
else:
logging.getLogger('init').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` MODIFY COLUMN `%s` %s'
% (column_name, self.table_name, self.table_name,
column_name, self._column_definition(column_name,
nullable=False)))
elif action == 'remove':
if self._columns[column_name]['nullable']:
return
if (self._field2module.get(column_name, self.module_name)
!= self.module_name):
return
self.cursor.execute('ALTER TABLE `%s` '
'MODIFY COLUMN `%s` %s'
% (self.table_name, column_name,
self._column_definition(column_name, nullable=True)))
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 exists
return
try:
self.cursor.execute('ALTER TABLE `%s` '
'ADD CONSTRAINT `%s` %s'
% (self.table_name, ident, constraint,))
except Exception:
if exception:
raise
logging.getLogger('init').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,))
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
try:
self.cursor.execute('ALTER TABLE `%s` '
'DROP CONSTRAINT `%s`'
% (self.table_name, ident))
except Exception:
if exception:
raise
logging.getLogger('init').warning(
'unable to drop \'%s\' constraint on table %s!'
% (ident, self.table_name))
self._update_definitions()
def drop_column(self, column_name, exception=False):
if not self.column_exist(column_name):
return
try:
self.cursor.execute(
'ALTER TABLE `%s` DROP COLUMN `%s`' %
(self.table_name, column_name))
except Exception:
if exception:
raise
logging.getLogger('init').warning(
'unable to drop \'%s\' column on table %s!'
% (column_name, self.table_name))
self._update_definitions()
@staticmethod
def drop_table(cursor, model, table, cascade=False):
cursor.execute('DELETE from ir_model_data where '
'model = %s', model)
query = 'DROP TABLE `%s`' % table
if cascade:
query = query + ' CASCADE'
cursor.execute(query)
def _column_definition(self, column_name, typname=None, nullable=None,
size=None, default=None):
if typname is None:
typname = self._columns[column_name]['typname']
if nullable is None:
nullable = self._columns[column_name]['nullable']
if size is None:
size = self._columns[column_name]['size']
if default is None:
default = self._columns[column_name]['default']
res = ''
if typname == 'varchar':
if int(size) > 255:
size = 255
res = 'varchar(%s)' % str(size)
elif typname == 'decimal':
res = 'decimal(65, 30)'
elif typname == 'double':
res = 'double(255, 15)'
else:
res = typname
# Default value for timestamp doesn't work
if typname == 'timestamp' and not nullable:
nullable = True
if nullable:
res += ' NULL'
else:
res += ' NOT NULL'
if default is not None:
res += ' DEFAULT %s' % default
return res
|