Skip to content

Commit

Permalink
Fix for foreign keys with strange characters, closes #62
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 24, 2024
1 parent 06c26bc commit 1bd4555
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
17 changes: 11 additions & 6 deletions datasette_edit_schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,8 @@ def get_columns_and_schema_and_fks_and_pks_and_indexes(conn):
info["foreign_key"].other_column,
),
"value": "{}.{}".format(
info["foreign_key"].other_table,
info["foreign_key"].other_column,
tilde_encode(info["foreign_key"].other_table),
tilde_encode(info["foreign_key"].other_column),
),
"selected": True,
}
Expand All @@ -569,7 +569,10 @@ def get_columns_and_schema_and_fks_and_pks_and_indexes(conn):
"name": "{}.{} (suggested)".format(
suggested_table, suggested_column
),
"value": "{}.{}".format(suggested_table, suggested_column),
"value": "{}.{}".format(
tilde_encode(suggested_table),
tilde_encode(suggested_column),
),
"selected": False,
}
)
Expand All @@ -581,7 +584,9 @@ def get_columns_and_schema_and_fks_and_pks_and_indexes(conn):
options.append(
{
"name": "{}.{}".format(rest_table, rest_column),
"value": "{}.{}".format(rest_table, rest_column),
"value": "{}.{}".format(
tilde_encode(rest_table), tilde_encode(rest_column)
),
"selected": False,
}
)
Expand Down Expand Up @@ -782,8 +787,8 @@ async def update_foreign_keys(request, datasette, database, table, formdata):
fks.append(
(
column,
split[0],
split[1],
tilde_decode(split[0]),
tilde_decode(split[1]),
)
)

Expand Down
22 changes: 20 additions & 2 deletions tests/test_edit_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,24 @@ async def test_edit_form_for_empty_table(db_path):
["id"],
"Foreign keys updated to distraction_id → cities.id",
),
# Same again for tables with weird characters in their names
(
"animal.name/with/slashes",
{
"action": "update_foreign_keys",
"fk.species": "table~2Ename~2Fwith~2Fslashes~2Ecategories.id",
},
[
(
"animal.name/with/slashes",
"species",
"table.name/with/slashes.categories",
"id",
)
],
["id"],
"Foreign keys updated to species → table.name/with/slashes.categories.id",
),
# Change primary key in a way that works
(
"museums",
Expand All @@ -845,13 +863,13 @@ async def test_edit_keys(
# Grab a csrftoken
cookies = {"ds_actor": ds.sign({"a": {"id": "root"}}, "actor")}
csrftoken_r = await ds.client.get(
"/-/edit-schema/data/{}".format(table), cookies=cookies
"/-/edit-schema/data/{}".format(tilde_encode(table)), cookies=cookies
)
csrftoken = csrftoken_r.cookies["ds_csrftoken"]
cookies["ds_csrftoken"] = csrftoken
post_data["csrftoken"] = csrftoken
response = await ds.client.post(
"/-/edit-schema/data/{}".format(table),
"/-/edit-schema/data/{}".format(tilde_encode(table)),
data=post_data,
cookies=cookies,
)
Expand Down

0 comments on commit 1bd4555

Please sign in to comment.