mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-27 20:34:12 +02:00
Use quoted SQL identifiers in convert --dry-run, closes #829
This commit is contained in:
parent
25c632fbbc
commit
c5063f67b1
2 changed files with 33 additions and 5 deletions
|
|
@ -3283,12 +3283,12 @@ def convert(
|
||||||
db.conn.create_function("preview_transform", 1, preview)
|
db.conn.create_function("preview_transform", 1, preview)
|
||||||
sql = """
|
sql = """
|
||||||
select
|
select
|
||||||
[{column}] as value,
|
{column} as value,
|
||||||
preview_transform([{column}]) as preview
|
preview_transform({column}) as preview
|
||||||
from [{table}]{where} limit 10
|
from {table}{where} limit 10
|
||||||
""".format(
|
""".format(
|
||||||
column=columns[0],
|
column=quote_identifier(columns[0]),
|
||||||
table=table,
|
table=quote_identifier(table),
|
||||||
where=f" where {where}" if where is not None else "",
|
where=f" where {where}" if where is not None else "",
|
||||||
)
|
)
|
||||||
for row in db.conn.execute(sql, where_args).fetchall():
|
for row in db.conn.execute(sql, where_args).fetchall():
|
||||||
|
|
|
||||||
|
|
@ -181,6 +181,34 @@ def test_convert_dryrun(test_db_and_path):
|
||||||
assert result.output.strip().split("\n")[-1] == "Would affect 1 row"
|
assert result.output.strip().split("\n")[-1] == "Would affect 1 row"
|
||||||
|
|
||||||
|
|
||||||
|
def test_convert_dryrun_table_and_column_names_containing_closing_bracket(
|
||||||
|
fresh_db_and_path,
|
||||||
|
):
|
||||||
|
db, db_path = fresh_db_and_path
|
||||||
|
table_name = "table]name"
|
||||||
|
column_name = "column]name"
|
||||||
|
db[table_name].insert({column_name: "hello"})
|
||||||
|
|
||||||
|
result = CliRunner().invoke(
|
||||||
|
cli.cli,
|
||||||
|
[
|
||||||
|
"convert",
|
||||||
|
db_path,
|
||||||
|
table_name,
|
||||||
|
column_name,
|
||||||
|
"value.upper()",
|
||||||
|
"--dry-run",
|
||||||
|
],
|
||||||
|
catch_exceptions=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert result.output.strip() == (
|
||||||
|
"hello\n --- becomes:\nHELLO\n\nWould affect 1 row"
|
||||||
|
)
|
||||||
|
assert list(db[table_name].rows) == [{column_name: "hello"}]
|
||||||
|
|
||||||
|
|
||||||
def test_convert_multi_dryrun(test_db_and_path):
|
def test_convert_multi_dryrun(test_db_and_path):
|
||||||
db_path = test_db_and_path[1]
|
db_path = test_db_and_path[1]
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue