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

This commit is contained in:
Holger Brunn
2022-07-12 20:20:54 +02:00
parent 38ae4a696c
commit b99468dec9
2 changed files with 12 additions and 9 deletions

View File

@@ -10,7 +10,9 @@ except ImportError:
sqlparse = None sqlparse = None
SECTION_NAME = "pglogical" SECTION_NAME = "pglogical"
DDL_KEYWORDS = ("CREATE", "ALTER", "DROP", "TRUNCATE", "INHERITS") DDL_KEYWORDS = ("CREATE", "ALTER", "DROP", "TRUNCATE")
QUALIFY_KEYWORDS = DDL_KEYWORDS + ("INHERITS", "FROM", "JOIN")
NO_QUALIFY_KEYWORDS = ("COLUMN",)
def schema_qualify(parsed_query, temp_tables, schema="public"): def schema_qualify(parsed_query, temp_tables, schema="public"):
@@ -22,13 +24,10 @@ def schema_qualify(parsed_query, temp_tables, schema="public"):
Name = sqlparse.tokens.Name Name = sqlparse.tokens.Name
Punctuation = sqlparse.tokens.Punctuation Punctuation = sqlparse.tokens.Punctuation
Symbol = sqlparse.tokens.String.Symbol Symbol = sqlparse.tokens.String.Symbol
is_qualified = False
is_temp_table = False is_temp_table = False
for token in token_iterator: for token in token_iterator:
yield token yield token
if token.is_keyword and token.normalized == "INHERITS": if token.is_keyword and token.normalized in QUALIFY_KEYWORDS:
is_qualified = False
if not is_qualified and token.is_keyword and token.normalized in DDL_KEYWORDS:
# we check if the name coming after {create,drop,alter} object keywords # we check if the name coming after {create,drop,alter} object keywords
# is schema qualified, and if not, add the schema we got passed # is schema qualified, and if not, add the schema we got passed
next_token = False next_token = False
@@ -43,13 +42,17 @@ def schema_qualify(parsed_query, temp_tables, schema="public"):
'TEMP', 'TEMPORARY' 'TEMP', 'TEMPORARY'
): ):
# don't qualify CREATE TEMP TABLE statements # don't qualify CREATE TEMP TABLE statements
is_qualified = True
is_temp_table = True is_temp_table = True
break break
if next_token.is_keyword and next_token.normalized in NO_QUALIFY_KEYWORDS:
yield next_token
next_token = False
break
if not (next_token.is_whitespace or next_token.is_keyword): if not (next_token.is_whitespace or next_token.is_keyword):
break break
yield next_token yield next_token
if is_temp_table: if is_temp_table:
is_temp_table = False
yield next_token yield next_token
while True: while True:
try: try:
@@ -90,8 +93,6 @@ def schema_qualify(parsed_query, temp_tables, schema="public"):
if next_token: if next_token:
yield next_token yield next_token
is_qualified = True
def post_load(): def post_load():
""" """

View File

@@ -114,7 +114,7 @@ class TestPglogical(TransactionCase):
'alter table testtable', 'alter table testtable',
'''create table '''create table
testtable testtable
(col1 int, col2 int); select * from test''', (col1 int, col2 int); select * from testtable''',
'alter table testschema.test drop column somecol', 'alter table testschema.test drop column somecol',
' DROP view if exists testtable', ' DROP view if exists testtable',
'truncate table testtable', 'truncate table testtable',
@@ -128,6 +128,8 @@ class TestPglogical(TransactionCase):
'ALTER TABLE "testtable" ADD COLUMN "test_field" double precision', 'ALTER TABLE "testtable" ADD COLUMN "test_field" double precision',
'CREATE TEMP TABLE "temptable" (col1 char) INHERITS (ir_translation)', 'CREATE TEMP TABLE "temptable" (col1 char) INHERITS (ir_translation)',
'DROP TABLE "temptable"', 'DROP TABLE "temptable"',
'create view testtable as select col1, col2 from testtable join '
'testtable test1 on col3=test1.col4)',
): ):
qualified_query = ''.join( qualified_query = ''.join(
''.join(str(token) for token in schema_qualify(parsed_query, temp_tables)) ''.join(str(token) for token in schema_qualify(parsed_query, temp_tables))