From b013738b4c9a6e5598a4d9bdbc6b2629ddd9628d Mon Sep 17 00:00:00 2001 From: dan Date: Mon, 20 Aug 2018 20:01:01 +0000 Subject: [PATCH] Add further tests for RENAME COLUMN. FossilOrigin-Name: 82c4c10a96db76f39edf5f6d027294d449d75b5a18da70da3667786da90e184d --- manifest | 15 ++--- manifest.uuid | 2 +- src/alter.c | 70 +++++++++++------------ test/altercol.test | 125 ++++++++++++++++++++++++++++++++++++++++- test/altermalloc2.test | 75 +++++++++++++++++++++++++ 5 files changed, 243 insertions(+), 44 deletions(-) create mode 100644 test/altermalloc2.test diff --git a/manifest b/manifest index a142c7a7af..d0f7e8beb9 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Ensure\sthat\sit\sis\snot\spossible\sto\srename\scolumns\sof\ssystem\stables,\sviews\sor\nvirtual\stables. -D 2018-08-20T16:16:05.616 +C Add\sfurther\stests\sfor\sRENAME\sCOLUMN. +D 2018-08-20T20:01:01.997 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 0a3a6c81e6fcb969ff9106e882f0a08547014ba463cb6beca4c4efaecc924ee6 @@ -432,7 +432,7 @@ F spec.template 86a4a43b99ebb3e75e6b9a735d5fd293a24e90ca F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b F sqlite3.1 fc7ad8990fc8409983309bb80de8c811a7506786 F sqlite3.pc.in 48fed132e7cb71ab676105d2a4dc77127d8c1f3a -F src/alter.c bfd9803d8ec46b1465a1455e26087b5c2b5d74cf528d9fb8dee75d757915d296 +F src/alter.c efe002e3fda0b33e7f802da03149f014f6150f4974e83a695a1a3948ac0e5e9a F src/analyze.c 3dc6b98cf007b005af89df165c966baaa48e8124f38c87b4d2b276fe7f0b9eb9 F src/attach.c 4bd5b92633671d3e8ce431153ebb1893b50335818423b5373f3f27969f79769a F src/auth.c 32a5bbe3b755169ab6c66311c5225a3cd4f75a46c041f7fb117e0cbb68055114 @@ -599,8 +599,9 @@ F test/alter.test b820ab9dcf85f8e3a65bc8326accb2f0c7be64ef F test/alter2.test 7ea05c7d92ac99349a802ef7ada17294dd647060 F test/alter3.test 4d79934d812eaeacc6f22781a080f8cfe012fdc3 F test/alter4.test b6d7b86860111864f6cddb54af313f5862dda23b -F test/altercol.test 692c15f8b8a37d6ef4bce62497e9e880f54548c3ecb9186cc2dfb8da05d06142 +F test/altercol.test 9d3e8257f8ec12f7586378a01689001aa008906755e9ae2022acfa060049a543 F test/altermalloc.test e81ac9657ed25c6c5bb09bebfa5a047cd8e4acfc +F test/altermalloc2.test 0231398534c494401a70a1d06a63d7849cb5b317fcc14228cbdb53039eba7eae F test/amatch1.test b5ae7065f042b7f4c1c922933f4700add50cdb9f F test/analyze.test b3a9c67d00e1df7588a5b7be9a0292899f94fe8cac1f94a017277474ca2e59df F test/analyze3.test ff62d9029e6deb2c914490c6b00caf7fae47cc85cdc046e4a0d0a4d4b87c71d8 @@ -1757,7 +1758,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 9d8e73bf71e996b810959ffc0e60de69b5e8ca3301df52f9c35d5e9075921798 -R 39372b826854afa8a81496c6e8dc9eab +P 786b5991dc0bb6ba13327a3ac9d04efbf8b591dedc736b719b206ffd865b1918 +R e196aa51fc6cca2fe63b8709f6076f0b U dan -Z 3c780ac9a6e33ed49d86a6810b26da66 +Z bcfa062522347c444c4d3162f9d6cbe2 diff --git a/manifest.uuid b/manifest.uuid index ca241a8318..062ab41c74 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -786b5991dc0bb6ba13327a3ac9d04efbf8b591dedc736b719b206ffd865b1918 \ No newline at end of file +82c4c10a96db76f39edf5f6d027294d449d75b5a18da70da3667786da90e184d \ No newline at end of file diff --git a/src/alter.c b/src/alter.c index 9748477946..b4723d21df 100644 --- a/src/alter.c +++ b/src/alter.c @@ -878,7 +878,7 @@ void sqlite3AlterRenameColumn( bQuote = sqlite3Isquote(pNew->z[0]); sqlite3NestedParse(pParse, "UPDATE \"%w\".%s SET " - "sql = sqlite_rename_column(sql, %Q, %Q, %d, %Q, %d) " + "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d) " "WHERE name NOT LIKE 'sqlite_%%' AND (type != 'index' OR tbl_name = %Q)" " AND sql NOT LIKE 'create virtual%%'", zDb, MASTER_NAME, @@ -957,7 +957,7 @@ void sqlite3RenameToken(Parse *pParse, void *pPtr, Token *pToken){ */ void sqlite3MoveRenameToken(Parse *pParse, void *pTo, void *pFrom){ RenameToken *p; - for(p=pParse->pRename; p; p=p->pNext){ + for(p=pParse->pRename; ALWAYS(p); p=p->pNext){ if( p->p==pFrom ){ p->p = pTo; break; @@ -1064,21 +1064,16 @@ static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){ ** sub-routine is currently stored in pParse->zErrMsg. This function ** adds context to the error message and then stores it in pCtx. */ -static void renameColumnParseError(sqlite3_context *pCtx, Parse *pParse){ - const char *zT; - const char *zN; +static void renameColumnParseError( + sqlite3_context *pCtx, + sqlite3_value *pType, + sqlite3_value *pObject, + Parse *pParse +){ + const char *zT = sqlite3_value_text(pType); + const char *zN = sqlite3_value_text(pObject); char *zErr; - if( pParse->pNewTable ){ - zT = pParse->pNewTable->pSelect ? "view" : "table"; - zN = pParse->pNewTable->zName; - }else if( pParse->pNewIndex ){ - zT = "index"; - zN = pParse->pNewIndex->zName; - }else{ - assert( pParse->pNewTrigger ); - zT = "trigger"; - zN = pParse->pNewTrigger->zName; - } + zErr = sqlite3_mprintf("error processing %s %s: %s", zT, zN, pParse->zErrMsg); sqlite3_result_error(pCtx, zErr, -1); sqlite3_free(zErr); @@ -1090,11 +1085,13 @@ static void renameColumnParseError(sqlite3_context *pCtx, Parse *pParse){ ** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld) ** ** 0. zSql: SQL statement to rewrite -** 1. Database: Database name (e.g. "main") -** 2. Table: Table name -** 3. iCol: Index of column to rename -** 4. zNew: New column name -** 5. bQuote: True if the new column name should be quoted +** 1. type: Type of object ("table", "view" etc.) +** 2. object: Name of object +** 3. Database: Database name (e.g. "main") +** 4. Table: Table name +** 5. iCol: Index of column to rename +** 6. zNew: New column name +** 7. bQuote: True if the new column name should be quoted ** ** Do a column rename operation on the CREATE statement given in zSql. ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol @@ -1120,12 +1117,12 @@ static void renameColumnFunc( RenameCtx sCtx; const char *zSql = (const char*)sqlite3_value_text(argv[0]); int nSql = sqlite3_value_bytes(argv[0]); - const char *zDb = (const char*)sqlite3_value_text(argv[1]); - const char *zTable = (const char*)sqlite3_value_text(argv[2]); - int iCol = sqlite3_value_int(argv[3]); - const char *zNew = (const char*)sqlite3_value_text(argv[4]); - int nNew = sqlite3_value_bytes(argv[4]); - int bQuote = sqlite3_value_int(argv[5]); + const char *zDb = (const char*)sqlite3_value_text(argv[3]); + const char *zTable = (const char*)sqlite3_value_text(argv[4]); + int iCol = sqlite3_value_int(argv[5]); + const char *zNew = (const char*)sqlite3_value_text(argv[6]); + int nNew = sqlite3_value_bytes(argv[6]); + int bQuote = sqlite3_value_int(argv[7]); const char *zOld; int rc; @@ -1142,8 +1139,8 @@ static void renameColumnFunc( UNUSED_PARAMETER(NotUsed); if( zSql==0 ) return; - if( zNew==0 ) return; if( zTable==0 ) return; + if( zNew==0 ) return; if( iCol<0 ) return; pTab = sqlite3FindTable(db, zTable, zDb); if( pTab==0 || iCol>=pTab->nCol ) return; @@ -1272,8 +1269,11 @@ static void renameColumnFunc( rc==SQLITE_OK && pStep; pStep=pStep->pNext ){ - if( pStep->pSelect ) sqlite3SelectPrep(&sParse, pStep->pSelect, &sNC); - if( pStep->zTarget ){ + if( pStep->pSelect ){ + sqlite3SelectPrep(&sParse, pStep->pSelect, &sNC); + if( sParse.nErr ) rc = sParse.rc; + } + if( rc==SQLITE_OK && pStep->zTarget ){ Table *pTarget = sqlite3FindTable(db, pStep->zTarget, zDb); if( pTarget==0 ){ rc = SQLITE_ERROR; @@ -1290,11 +1290,11 @@ static void renameColumnFunc( if( rc==SQLITE_OK ){ rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList); } + assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) ); if( pStep->pUpsert ){ Upsert *pUpsert = pStep->pUpsert; - if( rc==SQLITE_OK ){ - rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget); - } + assert( rc==SQLITE_OK ); + rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget); if( rc==SQLITE_OK && pUpsert->pUpsertSet){ ExprList *pUpsertSet = pUpsert->pUpsertSet; rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet); @@ -1413,7 +1413,7 @@ static void renameColumnFunc( renameColumnFunc_done: if( rc!=SQLITE_OK ){ if( sParse.zErrMsg ){ - renameColumnParseError(context, &sParse); + renameColumnParseError(context, argv[1], argv[2], &sParse); }else{ sqlite3_result_error_code(context, rc); } @@ -1438,7 +1438,7 @@ renameColumnFunc_done: void sqlite3AlterFunctions(void){ static FuncDef aAlterTableFuncs[] = { FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc), - FUNCTION(sqlite_rename_column, 6, 0, 0, renameColumnFunc), + FUNCTION(sqlite_rename_column, 8, 0, 0, renameColumnFunc), #ifndef SQLITE_OMIT_TRIGGER FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc), #endif diff --git a/test/altercol.test b/test/altercol.test index 348ccaa025..733f7d60cc 100644 --- a/test/altercol.test +++ b/test/altercol.test @@ -405,6 +405,14 @@ foreach {tn old new lSchema} { ON CONFLICT (_x_) WHERE _x_>10 DO UPDATE SET _x_ = _x_+1; END } } + + 4 _x_ _xxx_ { + { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) } + { CREATE TRIGGER ttt AFTER UPDATE ON t1 BEGIN + INSERT INTO t1 VALUES(new.a, new.b, new._x_) + ON CONFLICT (_x_) WHERE _x_>10 DO NOTHING; + END } + } } { do_rename_column_test 9.$tn $old $new $lSchema } @@ -472,6 +480,8 @@ do_catchsql_test 11.3 { # 1. Renaming a column of a system table, # 2. Renaming a column of a VIEW, # 3. Renaming a column of a virtual table. +# 4. Renaming a column that does not exist. +# 5. Renaming a column of a table that does not exist. # reset_db do_execsql_test 12.1.1 { @@ -502,10 +512,123 @@ ifcapable fts5 { do_execsql_test 12.3.1 { CREATE VIRTUAL TABLE ft USING fts5(a, b, c); } - do_catchsql_test 12.2.2 { + do_catchsql_test 12.3.2 { ALTER TABLE ft RENAME a TO z; } {1 {columns of virtual table ft may not be renamed}} } +do_execsql_test 12.4.1 { + CREATE TABLE t2(x, y, z); +} +do_catchsql_test 12.4.2 { + ALTER TABLE t2 RENAME COLUMN a TO b; +} {1 {no such column: "a"}} + +do_catchsql_test 12.5.1 { + ALTER TABLE t3 RENAME COLUMN a TO b; +} {1 {no such table: t3}} + +#------------------------------------------------------------------------- +# Test the effect of some parse/resolve errors. +# +reset_db +do_execsql_test 13.1.1 { + CREATE TABLE x1(i INTEGER, t TEXT UNIQUE); + CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN + SELECT * FROM nosuchtable; + END; +} + +do_catchsql_test 13.1.2 { + ALTER TABLE x1 RENAME COLUMN t TO ttt; +} {1 {error processing trigger tr1: no such table: main.nosuchtable}} + +do_execsql_test 13.1.3 { + DROP TRIGGER tr1; + CREATE INDEX x1i ON x1(i); + SELECT sql FROM sqlite_master WHERE name='x1i'; +} {{CREATE INDEX x1i ON x1(i)}} + +do_execsql_test 13.1.4 { + PRAGMA writable_schema = 1; + UPDATE sqlite_master SET sql = 'CREATE INDEX x1i ON x1(j)' WHERE name='x1i'; +} {} + +do_catchsql_test 13.1.5 { + ALTER TABLE x1 RENAME COLUMN t TO ttt; +} {1 {error processing index x1i: no such column: j}} + +do_execsql_test 13.1.6 { + UPDATE sqlite_master SET sql = '' WHERE name='x1i'; +} {} + +do_catchsql_test 13.1.7 { + ALTER TABLE x1 RENAME COLUMN t TO ttt; +} {1 {database disk image is malformed}} + +do_execsql_test 13.1.8 { + DELETE FROM sqlite_master WHERE name = 'x1i'; +} + +do_execsql_test 13.2.0 { + CREATE TABLE data(x UNIQUE, y, z); +} +foreach {tn trigger error} { + 1 { + CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN + UPDATE data SET x=x+1 WHERE zzz=new.i; + END; + } {no such column: zzz} + + 2 { + CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN + INSERT INTO data(x, y) VALUES(new.i, new.t, 1) + ON CONFLICT (x) DO UPDATE SET z=zz+1; + END; + } {no such column: zz} + + 3 { + CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN + INSERT INTO x1(i, t) VALUES(new.i+1, new.t||'1') + ON CONFLICT (tttttt) DO UPDATE SET t=i+1; + END; + } {no such column: tttttt} + + +} { + do_execsql_test 13.2.$tn.1 " + DROP TRIGGER IF EXISTS tr1; + $trigger + " + + do_catchsql_test 13.2.$tn.2 { + ALTER TABLE x1 RENAME COLUMN t TO ttt; + } "1 {error processing trigger tr1: $error}" +} + +#------------------------------------------------------------------------- +# Passing invalid parameters directly to sqlite_rename_column(). +# +do_execsql_test 14.1 { + CREATE TABLE ddd(sql, type, object, db, tbl, icol, znew, bquote); + INSERT INTO ddd VALUES( + 'CREATE TABLE x1(i INTEGER, t TEXT)', + 'table', 'x1', 'main', 'x1', -1, 'zzz', 0 + ), ( + 'CREATE TABLE x1(i INTEGER, t TEXT)', + 'table', 'x1', 'main', 'x1', 2, 'zzz', 0 + ), ( + 'CREATE TABLE x1(i INTEGER, t TEXT)', + 'table', 'x1', 'main', 'notable', 0, 'zzz', 0 + ); +} {} + +do_execsql_test 14.2 { + SELECT + sqlite_rename_column(sql, type, object, db, tbl, icol, znew, bquote) + FROM ddd; +} {{} {} {}} + + finish_test diff --git a/test/altermalloc2.test b/test/altermalloc2.test new file mode 100644 index 0000000000..b23d1c7170 --- /dev/null +++ b/test/altermalloc2.test @@ -0,0 +1,75 @@ +# 2018 August 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix altermalloc2 + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(abcd, efgh); +} +faultsim_save_and_close + +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + ALTER TABLE t1 RENAME abcd TO dcba + } +} -test { + faultsim_test_result {0 {}} +} + +catch {db close} +forcedelete test.db +sqlite3 db test.db +do_execsql_test 2.0 { + PRAGMA encoding = 'utf-16'; + CREATE TABLE t1(abcd, efgh); +} +faultsim_save_and_close + +do_faultsim_test 2 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + ALTER TABLE t1 RENAME abcd TO dcba + } +} -test { + faultsim_test_result {0 {}} +} + + +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(abcd, efgh); + CREATE VIEW v1 AS SELECT * FROM t1 WHERE abcd>efgh; +} +faultsim_save_and_close + +do_faultsim_test 3 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + ALTER TABLE t1 RENAME abcd TO dcba + } +} -test { + faultsim_test_result {0 {}} +} +finish_test