Commit 627bf103 authored by Alice Salsé's avatar Alice Salsé
Browse files

alter columns

parent 809f3f81
......@@ -54,13 +54,23 @@ for pathInput in [r'../config/csv/{}'.format(_schemaname)] :
if t.lower() in _existing_tables :
cols, recs= sql.sql_executor_r("select 1 from information_schema.columns where table_schema = '{}' and table_name = '{}' and column_name = '{}';".format(_schemaname, t, row[1]))
if len(recs)>0:
pass
#TODO ALTER COLUMN when exists
_sql.append("ALTER TABLE {}.{} \
ALTER COLUMN {} TYPE {} {} {} {} {};".format(_schemaname, t,
*row[1:5],
['DEFAULT '+[row[5],'\''+row[5]+'\''][type(row[5])==str and '()' not in row[5]],''][row[5] ==''],
['REFERENCES '+_reference+['',' ON DELETE CASCADE'][row[4]=='UNIQUE PRIMARY KEY'],''][row[6] =='']))
# _sql.append("ALTER TABLE {0}.{1} ALTER COLUMN {2} TYPE {3};".format(_schemaname, t, *row[1:3]))
if row[3]=='NOT NULL' :
_sql.append("ALTER TABLE {0}.{1} ALTER COLUMN {2} SET NOT NULL;".format(_schemaname, t, row[1]))
else :
_sql.append("ALTER TABLE {0}.{1} ALTER COLUMN {2} DROP NOT NULL;".format(_schemaname, t, row[1]))
# if row[4]=='UNIQUE PRIMARY KEY' :
# _sql.append("ALTER TABLE {0}.{1} ADD PRIMARY KEY ({2});".format(_schemaname, t, row[1]))
if row[5] == '' :
_sql.append("ALTER TABLE {0}.{1} ALTER COLUMN {2} DROP DEFAULT;".format(_schemaname, t, row[1]))
else :
_sql.append("ALTER TABLE {0}.{1} ALTER COLUMN {2} SET DEFAULT {3};".format(_schemaname, t, row[1], [row[5],'\''+row[5]+'\''][type(row[5])==str and '()' not in row[5]]))
if _reference == '' :
_sql.append("ALTER TABLE {0}.{1} DROP CONSTRAINT IF EXISTS {1}_{2}_fkey;".format(_schemaname, t, row[1]))
else :
_sql.append("ALTER TABLE {0}.{1} DROP CONSTRAINT IF EXISTS {1}_{2}_fkey;".format(_schemaname, t, row[1]))
_sql.append("ALTER TABLE {0}.{1} ADD CONSTRAINT {1}_{2}_fkey FOREIGN KEY ({2}) REFERENCES {3};".format(_schemaname, t, row[1], _reference+['',' ON DELETE CASCADE'][row[4]=='UNIQUE PRIMARY KEY']))
else :
_sql.append("ALTER TABLE {}.{} \
ADD COLUMN {} {} {} {} {} {};".format(_schemaname, t,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment