982 |
982 |
run_query(db, 'ALTER TABLE '+table+' ADD COLUMN '+row_num_col
|
983 |
983 |
+' serial NOT NULL PRIMARY KEY', log_level=3)
|
984 |
984 |
|
985 |
|
def create_table(db, table, cols, has_pkey=True):
|
|
985 |
def create_table(db, table, cols, has_pkey=True, col_indexes=True):
|
986 |
986 |
'''Creates a table.
|
987 |
987 |
@param cols [sql_gen.TypedCol,...] The column names and types
|
988 |
988 |
@param has_pkey If set, the first column becomes the primary key.
|
|
989 |
@param col_indexes bool|[ref]
|
|
990 |
* If True, indexes will be added on all non-pkey columns.
|
|
991 |
* If a list reference, [0] will be set to a function to do this.
|
|
992 |
This can be used to delay index creation until the table is populated.
|
989 |
993 |
'''
|
990 |
994 |
table = sql_gen.as_Table(table)
|
991 |
995 |
|
... | ... | |
997 |
1001 |
str_ += '\n, '.join(v.to_str(db) for v in cols)
|
998 |
1002 |
str_ += '\n);\n'
|
999 |
1003 |
run_query(db, str_, cacheable=True, log_level=2)
|
|
1004 |
|
|
1005 |
# Add indexes
|
|
1006 |
if has_pkey: index_cols = cols[1:]
|
|
1007 |
else: index_cols = cols
|
|
1008 |
def add_indexes():
|
|
1009 |
for col in index_cols: add_index(db, col.to_Col(), table)
|
|
1010 |
if isinstance(col_indexes, list): col_indexes[0] = add_indexes # defer
|
|
1011 |
elif col_indexes: add_indexes() # add now
|
1000 |
1012 |
|
1001 |
1013 |
def vacuum(db, table):
|
1002 |
1014 |
table = sql_gen.as_Table(table)
|
sql.py: create_table(): Add indexes on all non-pkey columns, unless turned off or deferred using new param col_indexes