Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove double quotes from filenames #22

Merged
merged 2 commits into from
Apr 14, 2024

Conversation

mihuman
Copy link
Contributor

@mihuman mihuman commented Apr 6, 2024

This pull request addresses an issue with table names that include double quotes.

Previously, the split_sql_file function in pg_split_schema_dump.py would generate filenames that included double quotes if the table name in the SQL dump file was double-quoted (using a reserved keyword as table name, for example 'user'). This could cause an error when trying to write the file, as double quotes are not allowed in filenames on Windows. Example:

OSError: [Errno 22] Invalid argument: '.\\split-dump\\my_schema.TABLE_"user".ACL'

The change in this pull request modifies the split_sql_file function to remove double quotes from table names when generating filenames, and adds a test case.

@akaihola akaihola self-requested a review April 13, 2024 08:00
@akaihola
Copy link
Owner

Thanks @mihuman, this looks great!

Could you just fix the code formatting to conform to Black? I'm running Darker to enforce that on modified lines of each PR, and this is what we now get from the build run:

--- pgtricks/pg_split_schema_dump.py	2024-04-13 07:59:41.699312 +0000
+++ pgtricks/pg_split_schema_dump.py	2024-04-13 07:59:55.328318 +0000
@@ -62,11 +62,11 @@
                 "{chunk}\n"
                 "{divider}".format(sqlpath=sqlpath, chunk=part, divider=77 * '=')
             )
             print(part)
             continue
-        name = match.group(1).replace(" ", "_").replace('"', '')
+        name = match.group(1).replace(" ", "_").replace('"', "")
         type_ = match.group(2).replace(" ", "_")
         schema = match.group(3)
         if schema == "-":
             schema = "no_schema"
         if name == "id":
--- pgtricks/tests/test_pg_split_schema_dump.py	2024-04-13 07:59:41.699312 +0000
+++ pgtricks/tests/test_pg_split_schema_dump.py	2024-04-13 07:59:55.389746 +0000
@@ -150,38 +150,39 @@
 
         (information for the unidentified content goes here)
         ============================================================================="""
     )
 
+
 def test_split_sql_file_with_quotes_in_name(tmpdir):
-    target_directory = tmpdir / 'target'
-    sqlpath = tmpdir / 'test.sql'
+    target_directory = tmpdir / "target"
+    sqlpath = tmpdir / "test.sql"
     sqlpath.write(
         dedent(
-            '''
+            """
 
             --
             -- Name: "user"; Type: TABLE; Schema: public; Owner:
             --
 
             (information for user table goes here)
-            '''
+            """
         )
     )
 
     split_sql_file(str(sqlpath), str(target_directory))
 
     assert {path.basename for path in target_directory.listdir()} == {
-        'public.user.TABLE'
+        "public.user.TABLE"
     }
-    assert (target_directory / 'public.user.TABLE').readlines(cr=False) == [
-        'SET search_path = public;',
-        '',
-        '',
-        '',
-        '--',
+    assert (target_directory / "public.user.TABLE").readlines(cr=False) == [
+        "SET search_path = public;",
+        "",
+        "",
+        "",
+        "--",
         '-- Name: "user"; Type: TABLE; Schema: public; Owner:',
-        '--',
-        '',
-        '(information for user table goes here)',
-        '',
+        "--",
+        "",
+        "(information for user table goes here)",
+        "",
     ]
pgtricks/tests/test_pg_split_schema_dump.py:155:1: E302 expected 2 blank lines, found 1 [flake8]

@mihuman
Copy link
Contributor Author

mihuman commented Apr 14, 2024

Hi @akaihola , sorry about that. The formatting should be fixed now.

@akaihola akaihola merged commit 1f92ae3 into akaihola:master Apr 14, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants