Drop or set NOT NULL if necessary
This commit is contained in:
parent
2da6559fc2
commit
4039a905ae
|
@ -1,6 +1,6 @@
|
||||||
[metadata]
|
[metadata]
|
||||||
name = ProcruSQL
|
name = ProcruSQL
|
||||||
version = 0.0.8
|
version = 0.0.9
|
||||||
author = Peter J. Holzer
|
author = Peter J. Holzer
|
||||||
author_email = hjp@hjp.at
|
author_email = hjp@hjp.at
|
||||||
description = Make a database fit its description
|
description = Make a database fit its description
|
||||||
|
|
|
@ -212,7 +212,18 @@ class HaveColumn(Node):
|
||||||
(self.schema, self.table, self.column, ))
|
(self.schema, self.table, self.column, ))
|
||||||
r = csr.fetchall()
|
r = csr.fetchall()
|
||||||
if len(r) == 1:
|
if len(r) == 1:
|
||||||
# Column exists, all ok
|
# Column exists, check attributes
|
||||||
|
if (r[0]["is_nullable"] == "YES") != self.definition["nullable"]:
|
||||||
|
log_action.info("Changing column %s of %s.%s to %s",
|
||||||
|
self.column, self.schema, self.table,
|
||||||
|
"null" if self.definition["nullable"] else "not null")
|
||||||
|
q = sql.SQL("alter table {schema}.{table} alter {column} {action} not null").format(
|
||||||
|
schema=sql.Identifier(self.schema),
|
||||||
|
table=sql.Identifier(self.table),
|
||||||
|
column=sql.Identifier(self.column),
|
||||||
|
action=sql.SQL("drop" if self.definition["nullable"] else "set")
|
||||||
|
)
|
||||||
|
csr.execute(q)
|
||||||
self.set_order()
|
self.set_order()
|
||||||
self.ok = True
|
self.ok = True
|
||||||
log_state.info("%s is now ok", self.name)
|
log_state.info("%s is now ok", self.name)
|
||||||
|
@ -226,7 +237,7 @@ class HaveColumn(Node):
|
||||||
schema=sql.Identifier(self.schema),
|
schema=sql.Identifier(self.schema),
|
||||||
table=sql.Identifier(self.table),
|
table=sql.Identifier(self.table),
|
||||||
column=sql.Identifier(self.column),
|
column=sql.Identifier(self.column),
|
||||||
definition=sql.SQL(self.definition),
|
definition=sql.SQL(self.definition["text"]),
|
||||||
)
|
)
|
||||||
csr.execute(q)
|
csr.execute(q)
|
||||||
self.set_order()
|
self.set_order()
|
||||||
|
|
|
@ -296,7 +296,9 @@ def parse_column_definition(ps):
|
||||||
if not m:
|
if not m:
|
||||||
ps.record_child_failure(ps2, "expected column definition")
|
ps.record_child_failure(ps2, "expected column definition")
|
||||||
return
|
return
|
||||||
ps2.ast.append(m.group(0))
|
text = m.group(0)
|
||||||
|
nullable = not("not null" in text or "primary key" in text)
|
||||||
|
ps2.ast.append({ "text": text, "nullable": nullable })
|
||||||
return ps2
|
return ps2
|
||||||
|
|
||||||
def parse_dict(ps):
|
def parse_dict(ps):
|
||||||
|
|
Loading…
Reference in New Issue