Drop or set NOT NULL if necessary

This commit is contained in:
Peter J. Holzer 2022-09-19 14:37:29 +02:00
parent 2da6559fc2
commit 4039a905ae
3 changed files with 17 additions and 4 deletions

View File

@ -1,6 +1,6 @@
[metadata]
name = ProcruSQL
version = 0.0.8
version = 0.0.9
author = Peter J. Holzer
author_email = hjp@hjp.at
description = Make a database fit its description

View File

@ -212,7 +212,18 @@ class HaveColumn(Node):
(self.schema, self.table, self.column, ))
r = csr.fetchall()
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.ok = True
log_state.info("%s is now ok", self.name)
@ -226,7 +237,7 @@ class HaveColumn(Node):
schema=sql.Identifier(self.schema),
table=sql.Identifier(self.table),
column=sql.Identifier(self.column),
definition=sql.SQL(self.definition),
definition=sql.SQL(self.definition["text"]),
)
csr.execute(q)
self.set_order()

View File

@ -296,7 +296,9 @@ def parse_column_definition(ps):
if not m:
ps.record_child_failure(ps2, "expected column definition")
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
def parse_dict(ps):