diff --git a/pglogical/hooks.py b/pglogical/hooks.py index 9f6ed8c1..3d9fa492 100644 --- a/pglogical/hooks.py +++ b/pglogical/hooks.py @@ -10,7 +10,9 @@ except ImportError: sqlparse = None 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"): @@ -22,13 +24,10 @@ def schema_qualify(parsed_query, temp_tables, schema="public"): Name = sqlparse.tokens.Name Punctuation = sqlparse.tokens.Punctuation Symbol = sqlparse.tokens.String.Symbol - is_qualified = False is_temp_table = False for token in token_iterator: yield token - if token.is_keyword and token.normalized == "INHERITS": - is_qualified = False - if not is_qualified and token.is_keyword and token.normalized in DDL_KEYWORDS: + if token.is_keyword and token.normalized in QUALIFY_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 next_token = False @@ -43,13 +42,17 @@ def schema_qualify(parsed_query, temp_tables, schema="public"): 'TEMP', 'TEMPORARY' ): # don't qualify CREATE TEMP TABLE statements - is_qualified = True is_temp_table = True 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): break yield next_token if is_temp_table: + is_temp_table = False yield next_token while True: try: @@ -90,8 +93,6 @@ def schema_qualify(parsed_query, temp_tables, schema="public"): if next_token: yield next_token - is_qualified = True - def post_load(): """ diff --git a/pglogical/tests/test_pglogical.py b/pglogical/tests/test_pglogical.py index 170c5976..c8ac7c06 100644 --- a/pglogical/tests/test_pglogical.py +++ b/pglogical/tests/test_pglogical.py @@ -114,7 +114,7 @@ class TestPglogical(TransactionCase): 'alter table testtable', '''create table testtable - (col1 int, col2 int); select * from test''', + (col1 int, col2 int); select * from testtable''', 'alter table testschema.test drop column somecol', ' DROP view if exists testtable', 'truncate table testtable', @@ -128,6 +128,8 @@ class TestPglogical(TransactionCase): 'ALTER TABLE "testtable" ADD COLUMN "test_field" double precision', 'CREATE TEMP TABLE "temptable" (col1 char) INHERITS (ir_translation)', 'DROP TABLE "temptable"', + 'create view testtable as select col1, col2 from testtable join ' + 'testtable test1 on col3=test1.col4)', ): qualified_query = ''.join( ''.join(str(token) for token in schema_qualify(parsed_query, temp_tables))