Compare commits

..

No commits in common. "8f93c2e2da7c22f84bb6f0d3511cef535ea06be6" and "f6c64a50abe37daf3e921fa9cdf23aac7aebee0e" have entirely different histories.

3 changed files with 18 additions and 72 deletions

View File

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

View File

@ -48,12 +48,11 @@ class Node:
self.order = order
class HaveData(Node):
def __init__(self, name, depends, table, key, extra, schema="public"):
def __init__(self, name, depends, table, key, extra):
super().__init__(name, depends)
self.table = table
self.key = key
self.extra = extra
self.schema = schema
def check(self):
log_check.info("Checking %s", self.name)
@ -64,9 +63,8 @@ class HaveData(Node):
]
key_check = sql.SQL(" and ").join(key_checks)
q = sql.SQL(
"select * from {schema}.{table} where {key_check}"
"select * from {table} where {key_check}"
).format(
schema=sql.Identifier(self.schema),
table=sql.Identifier(self.table),
key_check=key_check
)
@ -80,9 +78,8 @@ class HaveData(Node):
if self.result[0][c] != self.extra[c]:
log_action.info("Updating %s: %s <- %s", key_values, c, self.extra[c])
q = sql.SQL(
"update {schema}.{table} set {column}={placeholder} where {key_check}"
"update {table} set {column}={placeholder} where {key_check}"
).format(
schema=sql.Identifier(self.schema),
table=sql.Identifier(self.table),
column=sql.Identifier(c),
placeholder=sql.Placeholder(),
@ -99,9 +96,8 @@ class HaveData(Node):
columns = list(self.key.keys()) + list(self.extra.keys())
values = key_values + extra_values
q = sql.SQL(
"insert into {schema}.{table}({columns}) values({placeholders}) returning *"
"insert into {table}({columns}) values({placeholders}) returning *"
).format(
schema=sql.Identifier(self.schema),
table=sql.Identifier(self.table),
columns=sql.SQL(", ").join([sql.Identifier(x) for x in columns]),
placeholders=sql.SQL(", ").join([sql.Placeholder() for x in columns]),
@ -154,7 +150,7 @@ class HaveTable(Node):
def __init__(self, name, depends, table, schema="public"):
super().__init__(name, depends)
self.table = table
self.schema = schema
self.schema = "public"
def check(self):
log_check.info("Checking %s", self.name)
@ -202,7 +198,7 @@ class HaveColumn(Node):
self.table = table
self.column = column
self.definition = definition
self.schema = schema
self.schema = "public"
def check(self):
log_check.info("Checking %s", self.name)
@ -356,22 +352,16 @@ def main():
ap = argparse.ArgumentParser()
ap.add_argument("--dbname")
ap.add_argument("--dbuser")
ap.add_argument("--schema", default="public")
ap.add_argument("files", nargs="+")
args = ap.parse_args()
db = psycopg2.connect(dbname=args.dbname, user=args.dbuser)
csr = db.cursor()
csr.execute("show search_path")
search_path = csr.fetchone()[0]
search_path = args.schema + ", " + search_path
csr.execute(f"set search_path to {search_path}")
rules = []
for f in args.files:
with open(f) as rf:
text = rf.read()
ps = parser.ParseState(text, schema=args.schema)
ps = parser.ParseState(text)
ps2 = parser.parse_ruleset(ps)

View File

@ -21,14 +21,13 @@ class Failure:
class ParseState:
def __init__(self, text, position=0, schema="public"):
def __init__(self, text, position=0):
self.text = text
self.position = position
self.schema = schema # the default schema. probably doesn't belong into the parser state
self.child_failure = None
def clone(self):
ps = ParseState(self.text, self.position, self.schema)
ps = ParseState(self.text, self.position)
return ps
@property
@ -43,7 +42,7 @@ class ParseState:
linesbefore = self.text[:position].split("\n")
linesafter = self.text[position:].split("\n")
good = "\x1B[40;32m"
bad = "\x1B[40;31;1m"
bad = "\x1B[40;31m"
reset = "\x1B[0m"
s = reset + message + "\n"
lines = []
@ -99,16 +98,8 @@ def parse_table_rule(ps):
if not ps3:
ps.record_child_failure(ps2, "expected table name")
return
if len(ps3.ast) == 2:
schema_name = ps3.ast[0]
table_name = ps3.ast[1]
elif len(ps3.ast) == 1:
schema_name = ps3.schema
table_name = ps3.ast[0]
else:
assert(False)
ps2.ast = procrusql.HaveTable(rulename(), [], ps3.ast[0], schema=schema_name)
ps2.ast = procrusql.HaveTable(rulename(), [], ps3.ast[0])
ps2.position = ps3.position
return ps2
@ -128,14 +119,7 @@ def parse_column_rule(ps):
if not ps3:
ps.record_child_failure(ps2, "expected table name")
return
if len(ps3.ast) == 2:
schema_name = ps3.ast[0]
table_name = ps3.ast[1]
elif len(ps3.ast) == 1:
schema_name = ps3.schema
table_name = ps3.ast[0]
else:
assert(False)
table_name = ps3.ast[0]
ps2.position = ps3.position
ps3 = parse_column_name(ps2)
@ -152,9 +136,7 @@ def parse_column_rule(ps):
column_definition = ps3.ast[0]
ps2.position = ps3.position
ps2.ast = procrusql.HaveColumn(
rulename(), [],
table_name, column_name, column_definition, schema=schema_name)
ps2.ast = procrusql.HaveColumn(rulename(), [], table_name, column_name, column_definition)
ps2.match_newlines()
@ -171,14 +153,7 @@ def parse_data_rule(ps):
if not ps3:
ps.record_child_failure(ps2, "expected table name")
return
if len(ps3.ast) == 2:
schema_name = ps3.ast[0]
table_name = ps3.ast[1]
elif len(ps3.ast) == 1:
schema_name = ps3.schema
table_name = ps3.ast[0]
else:
assert(False)
table_name = ps3.ast[0]
ps2.position = ps3.position
if ps3 := parse_dict(ps2):
@ -199,9 +174,7 @@ def parse_data_rule(ps):
else:
label = rulename()
ps2.ast = procrusql.HaveData(
label, [],
table_name, key_data, extra_data, schema=schema_name)
ps2.ast = procrusql.HaveData(label, [], table_name, key_data, extra_data)
ps2.match_newlines()
elif ps3 := parse_init_query(ps2):
@ -242,14 +215,7 @@ def parse_index_rule(ps):
if not ps3:
ps.record_child_failure(ps2, "expected table name")
return
if len(ps3.ast) == 2:
schema_name = ps3.ast[0]
table_name = ps3.ast[1]
elif len(ps3.ast) == 1:
schema_name = ps3.schema
table_name = ps3.ast[0]
else:
assert(False)
table_name = ps3.ast[0]
ps2.position = ps3.position
m = ps2.match(r"\s*(using\b|\([\w, ]+\))[^>\n]*")
@ -264,10 +230,7 @@ def parse_index_rule(ps):
else:
label = rulename()
ps2.ast = procrusql.HaveIndex(
label, [],
table_name, index_name,
index_type, index_definition, schema=schema_name)
ps2.ast = procrusql.HaveIndex(label, [], table_name, index_name, index_type, index_definition)
ps2.match_newlines()
@ -284,12 +247,6 @@ def parse_table_name(ps):
if ps2.rest[0].isalpha():
m = ps2.match(r"\w+") # always succeeds since we already checked the first character
ps2.ast.append(m.group(0))
if ps2.rest[0] == ".":
m = ps2.match(r"\w+")
if not m:
ps.record_child_failure(ps2, "expected table name after schema")
return
ps2.ast.append(m.group(0))
else:
ps.record_child_failure(ps2, "expected table name")
return ps2
@ -340,7 +297,6 @@ def parse_column_definition(ps):
"json", "jsonb",
"uuid",
r"integer\[\]", r"int\[\]", r"bigint\[\]",
"bytea",
),
key=lambda x: -len(x) # longest match first
)