Package trac :: Package db :: Module schema

Source Code for Module trac.db.schema

 1  # -*- coding: utf-8 -*- 
 2  # 
 3  # Copyright (C) 2005-2020 Edgewall Software 
 4  # Copyright (C) 2005 Christopher Lenz <[email protected]> 
 5  # All rights reserved. 
 6  # 
 7  # This software is licensed as described in the file COPYING, which 
 8  # you should have received as part of this distribution. The terms 
 9  # are also available at https://trac.edgewall.org/wiki/TracLicense. 
10  # 
11  # This software consists of voluntary contributions made by many 
12  # individuals. For the exact contribution history, see the revision 
13  # history and logs, available at https://trac.edgewall.org/log/. 
14  # 
15  # Author: Christopher Lenz <[email protected]> 
16   
17   
18 -class Table(object):
19 """Declare a table in a database schema.""" 20
21 - def __init__(self, name, key=[]):
22 self.name = name 23 self.columns = [] 24 self.indices = [] 25 self.key = [key] if isinstance(key, basestring) else key
26
27 - def __getitem__(self, objs):
28 self.columns = [o for o in objs if isinstance(o, Column)] 29 self.indices = [o for o in objs if isinstance(o, Index)] 30 return self
31
32 - def remove_columns(self, column_names):
33 """Remove columns specified in the list or tuple `column_names`.""" 34 if not isinstance(column_names, (list, tuple)): 35 column_names = [column_names] 36 if any(c in column_names for c in self.key): 37 self.key = [] 38 self.columns = [col for col in self.columns 39 if col.name not in column_names] 40 self.indices = [idx for idx in self.indices 41 if all(c not in column_names for c in idx.columns)]
42 43
44 -class Column(object):
45 """Declare a table column in a database schema.""" 46
47 - def __init__(self, name, type='text', size=None, key_size=None, 48 auto_increment=False):
49 self.name = name 50 self.type = type 51 self.size = size 52 self.key_size = key_size 53 self.auto_increment = auto_increment
54 55
56 -class Index(object):
57 """Declare an index for a database schema.""" 58
59 - def __init__(self, columns, unique=False):
60 self.columns = columns 61 self.unique = unique
62