Skip to content

Commit

Permalink
fix: support for indexes with quotes in the name #652 (#653)
Browse files Browse the repository at this point in the history
* fix: support for indexes with quotes in the name #652

* doc: extra comment
  • Loading branch information
vjeeva authored Jan 6, 2025
1 parent a7907ee commit 1b7d90c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
7 changes: 5 additions & 2 deletions pgbelt/util/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def dump_source_tables(
[
"pg_dump",
"--data-only",
f"--table={config.schema_name}.{table}",
f'--table={config.schema_name}."{table}"',
"-Fc",
"-f",
table_file(config.db, config.dc, table),
Expand Down Expand Up @@ -401,13 +401,16 @@ async def create_target_indexes(
for c in create_index_statements.split(";"):
# Get the Index Name
regex_matches = search(
r"CREATE [UNIQUE ]*INDEX (?P<index>[a-zA-Z0-9._]+)+.*",
r"CREATE [UNIQUE ]*INDEX (?P<index>[a-zA-Z0-9._\"]+)+.*",
c,
)
if not regex_matches:
continue
index = regex_matches.groupdict()["index"]

# Sometimes the index name is quoted, so remove the quotes
index = index.replace('"', "")

# Create the index
# Note that the host DSN must have a statement timeout of 0.
# Example DSN: `host=server-hostname user=user dbname=db_name options='-c statement_timeout=3600000'`
Expand Down
2 changes: 1 addition & 1 deletion pgbelt/util/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ async def table_empty(pool: Pool, table: str, schema: str, logger: Logger) -> bo
return true if the table is empty
"""
logger.info(f"Checking if table {table} is empty...")
result = await pool.fetch(f"SELECT * FROM {schema}.{table} LIMIT 1;")
result = await pool.fetch(f'SELECT * FROM {schema}."{table}" LIMIT 1;')
return len(result) == 0


Expand Down
4 changes: 3 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ async def _create_dbupgradeconfigs() -> dict[str, DbupgradeConfig]:
db_upgrade_config_kwargs["schema_name"] = (
"non_public_schema" if "nonpublic" in s else "public"
)
db_upgrade_config_kwargs["tables"] = ["UsersCapital"] if "exodus" in s else None
db_upgrade_config_kwargs["tables"] = (
["UsersCapital", "existingSomethingIds"] if "exodus" in s else None
)
db_upgrade_config_kwargs["sequences"] = (
["userS_id_seq"] if "exodus" in s else None
)
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/files/test_schema_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ CREATE INDEX users2_idx ON public."UsersCapital" (
hash_lastname
);

-- Addressing the following index statement style: CREATE INDEX "existingEmailIds_email_id_idx" ON public."existingEmailIds" USING btree ("projectId", "emailId");
-- Issue #652
-- Did not add a primary key, helped iron out related quoting issues in the dump and load code.

CREATE TABLE public."existingSomethingIds" (
"thingId" integer NOT NULL,
"somethingId" character varying(255) NOT NULL
);

CREATE INDEX "existingSomethingIds_something_id_idx" ON public."existingSomethingIds" USING btree ("thingId", "somethingId");

--
-- Name: userS_id_seq; Type: SEQUENCE; Schema: public; Owner: owner
--
Expand Down Expand Up @@ -130,6 +141,17 @@ INSERT INTO public."UsersCapital2" (id, "hash_firstName", hash_lastname, gender)
INSERT INTO public.another_test_table ("someThingIDontKnow", "anotherThing")
VALUES ('0e095b60-ab7d-4892-9a92-6175497fe0f9', '0e095b60-ab7d-4892-9a92-6175497fe0f9');

--
-- Data for Name: existingSomethingIds; Type: TABLE DATA; Schema: public; Owner: owner
--

INSERT INTO public."existingSomethingIds" ("thingId", "somethingId")
VALUES (1, 'something1'),
(2, 'something2'),
(3, 'something3'),
(4, 'something4');


--
-- Name: userS_id_seq; Type: SEQUENCE SET; Schema: public; Owner: owner
--
Expand Down

0 comments on commit 1b7d90c

Please sign in to comment.