Skip to content

Do not change table names #245

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

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jwcrypto = ">=1.4.2"
tornado = ">=6.3.2"
psycopg2 = ">=2.9.3"
libnacl = ">=1.7.1"
pysquril = ">=0.3.6"
pysquril = ">=1.0.0"
tsd-api-client = ">=3.5.2"
SQLAlchemy = ">=1.3.8,<2"
pretty-bad-protocol = ">=3.1.1"
Expand Down
44 changes: 44 additions & 0 deletions scripts/migrate-tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

-- https://github.com/unioslo/tsd-file-api/issues/233
-- https://github.com/unioslo/pysquril/pull/74

drop function if exists migrate_tables(text);
create or replace function migrate_tables(target text)
returns void as $$
declare _schema text;
declare _table_name text;
declare _new_name text;
begin
if target = 'survey' then
for _schema, _table_name in
select table_schema, table_name from information_schema.tables
where table_schema ~ 'p|ec[0-9]+'
and table_name ~ '.+_(audit|metadata)'
loop
raise info 'migrating: %', _table_name;
_new_name := replace(_table_name, '_', '/');
execute format(
'alter table $1."$2" rename to "$3"'
) using _schema, _table_name, _new_name;
end loop;
elsif target = 'apps' then
for _schema, _table_name in
select table_schema, table_name from information_schema.tables
where table_schema ~ 'p|ec[0-9]+_.+'
and table_name ~ '.+_audit' -- for all
or table_name ~ 'persons_.+' -- mvh
or table_name ~ '.+_memos' -- tables | ros
loop
raise info 'migrating: %', _table_name;
_new_name := replace(_table_name, '_', '/');
execute format(
'alter table $1."$2" rename to "$3"',
) using _schema, _table_name, _new_name;
end loop;
end if;
end;
$$ language plpgsql;

select migrate_tables('survey');
select migrate_tables('apps');
drop function if exists migrate_tables(text);
16 changes: 7 additions & 9 deletions tsdfileapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,8 +2165,7 @@ class GenericTableHandler(AuthRequestHandler):
"""

def create_table_name(self, table_name: str) -> str:
name = table_name.replace("/", "_")
return name
return table_name

def get_uri_query(self, uri: str) -> str:
if "?" in uri:
Expand Down Expand Up @@ -2343,9 +2342,9 @@ def set_resource_identifier_info(
def get(self, tenant: str, table_name: str = None) -> None:
try:
if not table_name:
excludes = ["_audit"]
excludes = ["/audit"]
if self.backend == "survey":
excludes.append("_metadata")
excludes.append("/metadata")
tables = self.db.tables_list(
exclude_endswith=excludes,
remove_pattern="_submissions",
Expand Down Expand Up @@ -2375,7 +2374,7 @@ def get(self, tenant: str, table_name: str = None) -> None:
# when broadcasting a query with *
# unless the URI specifies they should be
excludes = []
default_excludes = {"/audit": "_audit", "/metadata": "_metadata"}
default_excludes = {"/audit": "/audit", "/metadata": "/metadata"}
for k, v in default_excludes.items():
if not self.request.uri.split("?")[0].endswith(k):
excludes.append(v)
Expand Down Expand Up @@ -2430,9 +2429,8 @@ def get(self, tenant: str, table_name: str = None) -> None:
self.flush()
self.write("]")
self.flush()
# TODO: raise custom exceptions from pysquril to identify query errors as 4XX
except (psycopg2.errors.UndefinedTable, sqlite3.OperationalError) as e:
if table_name.endswith("_audit"):
if table_name.endswith("/audit"):
# Handle the audit table differently
# it is not created until the first change appears
self.set_status(HTTPStatus.OK.value)
Expand Down Expand Up @@ -2509,11 +2507,11 @@ def patch(self, tenant: str, table_name: str) -> None:
def post(self, tenant: str, table_name: str) -> None:
try:
table_name = self.create_table_name(table_name)
if not table_name.endswith("_audit"):
if not table_name.endswith("/audit"):
raise ClientMethodNotAllowed
query = self.get_uri_query(self.request.uri)
self.restored = self.db.table_restore(
table_name.replace("_audit", ""), query
table_name.replace("/audit", ""), query
)
self.set_status(HTTPStatus.OK.value)
self.write(self.restored)
Expand Down
2 changes: 1 addition & 1 deletion tsdfileapi/test_file_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def test_XXX_query_invalid(self) -> None:
self.assertEqual(resp.status_code, 404)
# Do not throw non exist at audit
resp = requests.get(
f"{self.base_url}/survey/number_audit/submissions?select=count(*)",
f"{self.base_url}/survey/user_number/audit?select=count(*)",
headers=headers,
)
self.assertEqual(resp.status_code, 200)
Expand Down
Loading