Views should be double quoted too

This commit is contained in:
Simon Willison 2025-11-23 20:06:44 -08:00
commit 98dd284f6b
3 changed files with 20 additions and 13 deletions

View file

@ -1177,7 +1177,9 @@ class Database:
assert not ( assert not (
ignore and replace ignore and replace
), "Use one or the other of ignore/replace, not both" ), "Use one or the other of ignore/replace, not both"
create_sql = "CREATE VIEW {name} AS {sql}".format(name=name, sql=sql) create_sql = "CREATE VIEW {name} AS {sql}".format(
name=quote_identifier(name), sql=sql
)
if ignore or replace: if ignore or replace:
# Does view exist already? # Does view exist already?
if name in self.view_names(): if name in self.view_names():

View file

@ -62,8 +62,8 @@ def test_views(db_path):
result = CliRunner().invoke(cli.cli, ["views", db_path, "--table", "--schema"]) result = CliRunner().invoke(cli.cli, ["views", db_path, "--table", "--schema"])
assert ( assert (
"view schema\n" "view schema\n"
"------ --------------------------------------------\n" "------ ----------------------------------------------\n"
"hello CREATE VIEW hello AS select sqlite_version()" 'hello CREATE VIEW "hello" AS select sqlite_version()'
) == result.output.strip() ) == result.output.strip()
@ -1294,7 +1294,9 @@ def test_create_view():
cli.cli, ["create-view", "test.db", "version", "select sqlite_version()"] cli.cli, ["create-view", "test.db", "version", "select sqlite_version()"]
) )
assert result.exit_code == 0 assert result.exit_code == 0
assert "CREATE VIEW version AS select sqlite_version()" == db["version"].schema assert (
'CREATE VIEW "version" AS select sqlite_version()' == db["version"].schema
)
def test_create_view_error_if_view_exists(): def test_create_view_error_if_view_exists():
@ -1329,7 +1331,8 @@ def test_create_view_ignore():
) )
assert result.exit_code == 0 assert result.exit_code == 0
assert ( assert (
"CREATE VIEW version AS select sqlite_version() + 1" == db["version"].schema 'CREATE VIEW "version" AS select sqlite_version() + 1'
== db["version"].schema
) )
@ -1349,7 +1352,9 @@ def test_create_view_replace():
], ],
) )
assert result.exit_code == 0 assert result.exit_code == 0
assert "CREATE VIEW version AS select sqlite_version()" == db["version"].schema assert (
'CREATE VIEW "version" AS select sqlite_version()' == db["version"].schema
)
def test_drop_table(): def test_drop_table():

View file

@ -172,8 +172,8 @@ def test_memory_dump(extra_args):
");\n" ");\n"
"INSERT INTO \"stdin\" VALUES(1,'Cleo');\n" "INSERT INTO \"stdin\" VALUES(1,'Cleo');\n"
"INSERT INTO \"stdin\" VALUES(2,'Bants');\n" "INSERT INTO \"stdin\" VALUES(2,'Bants');\n"
'CREATE VIEW t1 AS select * from "stdin";\n' 'CREATE VIEW "t1" AS select * from "stdin";\n'
'CREATE VIEW t AS select * from "stdin";\n' 'CREATE VIEW "t" AS select * from "stdin";\n'
"COMMIT;" "COMMIT;"
) )
# Using sqlite-dump it won't have IF NOT EXISTS # Using sqlite-dump it won't have IF NOT EXISTS
@ -194,8 +194,8 @@ def test_memory_schema(extra_args):
' "id" INTEGER,\n' ' "id" INTEGER,\n'
' "name" TEXT\n' ' "name" TEXT\n'
");\n" ");\n"
'CREATE VIEW t1 AS select * from "stdin";\n' 'CREATE VIEW "t1" AS select * from "stdin";\n'
'CREATE VIEW t AS select * from "stdin";' 'CREATE VIEW "t" AS select * from "stdin";'
) )
@ -288,13 +288,13 @@ def test_memory_two_files_with_same_stem(tmpdir):
' "id" INTEGER,\n' ' "id" INTEGER,\n'
' "name" TEXT\n' ' "name" TEXT\n'
");\n" ");\n"
'CREATE VIEW t1 AS select * from "data";\n' 'CREATE VIEW "t1" AS select * from "data";\n'
'CREATE VIEW t AS select * from "data";\n' 'CREATE VIEW "t" AS select * from "data";\n'
'CREATE TABLE "data_2" (\n' 'CREATE TABLE "data_2" (\n'
' "id" INTEGER,\n' ' "id" INTEGER,\n'
' "name" TEXT\n' ' "name" TEXT\n'
");\n" ");\n"
'CREATE VIEW t2 AS select * from "data_2";\n' 'CREATE VIEW "t2" AS select * from "data_2";\n'
) )