[IMP] use sqlparse also to determine which ddl to update

This commit is contained in:
Holger Brunn
2022-05-17 10:42:16 +02:00
parent 377aa43832
commit d431ee796c

View File

@@ -41,16 +41,21 @@ def post_load():
def execute(self, query, params=None, log_exceptions=None): def execute(self, query, params=None, log_exceptions=None):
"""Wrap DDL in pglogical.replicate_ddl_command""" """Wrap DDL in pglogical.replicate_ddl_command"""
parsed_queries = [x.get_type() for x in sqlparse.parse(query)] # short circuit statements that must be as fast as possible
if any(q in ["CREATE", "ALTER", "DROP"] for q in parsed_queries) and ( if query[:6] != "SELECT":
# don't replicate constraints, triggers, indexes parsed_queries = sqlparse.parse(query)
"CONSTRAINT" not in query if any(
and "TRIGGER" not in query parsed_query.get_type() in ("CREATE", "ALTER", "DROP")
and "INDEX" not in query for parsed_query in parsed_queries
): ) and not any(
mogrified = self.mogrify(query, params).decode("utf8") token.is_keyword and token.normalized in
query = "SELECT pglogical.replicate_ddl_command(%s, %s)" # don't replicate constraints, triggers, indexes
params = (mogrified, execute.replication_sets) ("CONSTRAINT", "TRIGGER", "INDEX")
for parsed in parsed_queries for token in parsed.tokens[1:]
):
mogrified = self.mogrify(query, params).decode("utf8")
query = "SELECT pglogical.replicate_ddl_command(%s, %s)"
params = (mogrified, execute.replication_sets)
return execute.origin(self, query, params=params, log_exceptions=log_exceptions) return execute.origin(self, query, params=params, log_exceptions=log_exceptions)
execute.origin = execute_org execute.origin = execute_org