Skip to content

Commit 85e050d

Browse files
authored
Fix quoting of schema name (#54) (#100)
* function that adds quotes to schema name if the schema name contains upper case letter * needs string with " if upper case letters, psycopg2 takes care of adding ' * add workspace * parametrize tests, so that they run on two project names - adding problematic project name with upper case letter * add test info about workspace * update test name * fix tests to properly escape schema names * add to avoid accidental commits * escape if schema contains other characters then a-z 0-9 and _, use double quote if necessary * rename tests to avoid potential issues on Windows * do not parameterize all the tests * create simple parametrized test, that contains just init, push and pull for couple of different project names --------- Co-authored-by: Jan Caha <[email protected]>
1 parent e025433 commit 85e050d

File tree

4 files changed

+122
-55
lines changed

4 files changed

+122
-55
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ config.ini
44
mergin
55
.coverage
66
htmlcov
7+
.vscode
8+
.env

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ To run automatic tests:
140140
export TEST_MERGIN_URL=<url> # testing server
141141
export TEST_API_USERNAME=<username>
142142
export TEST_API_PASSWORD=<pwd>
143+
export TEST_API_WORKSPACE=<workspace>
143144
pytest-3 test/
144145
145146

dbsync.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import tempfile
1717
import random
1818
import uuid
19+
import re
1920

2021
import psycopg2
2122
from itertools import chain
@@ -34,6 +35,14 @@ class DbSyncError(Exception):
3435
pass
3536

3637

38+
def _add_quotes_to_schema_name(schema: str) -> str:
39+
matches = re.findall(r"[^a-z0-9_]", schema)
40+
if len(matches) != 0:
41+
schema = schema.replace("\"", "\"\"")
42+
schema = f'"{schema}"'
43+
return schema
44+
45+
3746
def _tables_list_to_string(tables):
3847
return ";".join(tables)
3948

@@ -230,6 +239,7 @@ def _set_db_project_comment(conn, schema, project_name, version, project_id=None
230239
def _get_db_project_comment(conn, schema):
231240
""" Get Mergin Maps project name and its current version in db schema"""
232241
cur = conn.cursor()
242+
schema = _add_quotes_to_schema_name(schema)
233243
cur.execute("SELECT obj_description(%s::regnamespace, 'pg_namespace')", (schema, ))
234244
res = cur.fetchone()[0]
235245
try:

0 commit comments

Comments
 (0)