Skip to content

Commit 2402249

Browse files
authored
apply alter column command only to the table (#1532)
* add alter column test * apply alter column only to the table and ignore indices/triggers/views etc * fix tests * update bundle * add test in rust_suite
1 parent 7f731b8 commit 2402249

File tree

6 files changed

+51
-23
lines changed

6 files changed

+51
-23
lines changed

libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21663,7 +21663,7 @@ SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
2166321663
SQLITE_PRIVATE void sqlite3AlterFunctions(void);
2166421664
SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
2166521665
SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
21666-
void libsqlAlterAlterColumn(Parse*, SrcList*, Token*, Token*);
21666+
void libsqlAlterAlterColumn(Parse*, SrcList*, Token*, Token*, int);
2166721667
SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
2166821668
SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
2166921669
SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
@@ -116496,7 +116496,8 @@ void libsqlAlterAlterColumn(
116496116496
Parse *pParse, /* Parsing context */
116497116497
SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
116498116498
Token *pOld, /* Name of column being changed */
116499-
Token *pNew /* New column declaration */
116499+
Token *pNew, /* New column declaration */
116500+
int nNewSqlLength /* New column declaration SQL string length (pNew.z is the start of the declaration) */
116500116501
){
116501116502
sqlite3 *db = pParse->db; /* Database connection */
116502116503
Table *pTab; /* Table being updated */
@@ -116554,9 +116555,7 @@ void libsqlAlterAlterColumn(
116554116555
}
116555116556
// NOTICE: this is the main difference in ALTER COLUMN compared to RENAME COLUMN,
116556116557
// we just take the whole new column declaration as it is.
116557-
// FIXME: the semicolon can also appear in the middle of the declaration when it's quoted,
116558-
// so we should check from the end.
116559-
pNew->n = sqlite3Strlen30(pNew->z);
116558+
pNew->n = nNewSqlLength;
116560116559
while (pNew->n > 0 && pNew->z[pNew->n - 1] == ';') pNew->n--;
116561116560
zNew = sqlite3DbStrNDup(db, pNew->z, pNew->n);
116562116561
if( !zNew ) goto exit_update_column;
@@ -116565,7 +116564,7 @@ void libsqlAlterAlterColumn(
116565116564
sqlite3NestedParse(pParse,
116566116565
"UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
116567116566
"sql = libsql_alter_column(sql, %Q, %Q, %d, %Q, %d, %d, %d) "
116568-
"WHERE tbl_name = %Q",
116567+
"WHERE name = %Q AND type = 'table'",
116569116568
zDb,
116570116569
zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1, pTab->aCol[iCol].colFlags,
116571116570
pTab->zName
@@ -117577,9 +117576,11 @@ static void renameColumnFunc(
117577117576
*/
117578117577
static void alterColumnFunc(
117579117578
sqlite3_context *context,
117580-
int NotUsed,
117579+
int argc,
117581117580
sqlite3_value **argv
117582117581
){
117582+
UNUSED_PARAMETER(argc);
117583+
117583117584
sqlite3 *db = sqlite3_context_db_handle(context);
117584117585
RenameCtx sCtx;
117585117586
const char *zSql = (const char*)sqlite3_value_text(argv[0]);
@@ -117602,7 +117603,6 @@ static void alterColumnFunc(
117602117603
sqlite3_xauth xAuth = db->xAuth;
117603117604
#endif
117604117605

117605-
UNUSED_PARAMETER(NotUsed);
117606117606
if( zSql==0 ) return;
117607117607
if( zTable==0 ) return;
117608117608
if( zNew==0 ) return;
@@ -117655,9 +117655,10 @@ static void alterColumnFunc(
117655117655
}
117656117656
} else {
117657117657
rc = SQLITE_ERROR;
117658-
sParse.zErrMsg = sqlite3MPrintf(sParse.db, "Only ordinary tables can be altered, not ", IsView(sParse.pNewTable) ? "views" : "virtual tables");
117659-
goto alterColumnFunc_done; }
117660-
} else if (sParse.pNewIndex) {
117658+
sParse.zErrMsg = sqlite3MPrintf(sParse.db, "Only ordinary tables can be altered, not %s", IsView(sParse.pNewTable) ? "views" : "virtual tables");
117659+
goto alterColumnFunc_done;
117660+
}
117661+
} else if( sParse.pNewIndex ){
117661117662
rc = SQLITE_ERROR;
117662117663
sParse.zErrMsg = sqlite3MPrintf(sParse.db, "Only ordinary tables can be altered, not indexes");
117663117664
goto alterColumnFunc_done;
@@ -176759,7 +176760,8 @@ static YYACTIONTYPE yy_reduce(
176759176760
break;
176760176761
case 301: /* cmd ::= ALTER TABLE fullname ALTER COLUMNKW columnname TO columnname carglist */
176761176762
{
176762-
libsqlAlterAlterColumn(pParse, yymsp[-6].minor.yy203, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy0);
176763+
int definitionLength = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
176764+
libsqlAlterAlterColumn(pParse, yymsp[-6].minor.yy203, &yymsp[-3].minor.yy0, &yymsp[-1].minor.yy0, definitionLength);
176763176765
}
176764176766
break;
176765176767
case 302: /* cmd ::= create_vtab */

libsql-ffi/bundled/bindings/bindgen.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C" {
2323
) -> ::std::os::raw::c_int;
2424
}
2525

26+
pub const __GNUC_VA_LIST: i32 = 1;
2627
pub const SQLITE_VERSION: &[u8; 7] = b"3.44.0\0";
2728
pub const SQLITE_VERSION_NUMBER: i32 = 3044000;
2829
pub const SQLITE_SOURCE_ID: &[u8; 85] =
@@ -499,8 +500,8 @@ pub const FTS5_TOKENIZE_DOCUMENT: i32 = 4;
499500
pub const FTS5_TOKENIZE_AUX: i32 = 8;
500501
pub const FTS5_TOKEN_COLOCATED: i32 = 1;
501502
pub const WAL_SAVEPOINT_NDATA: i32 = 4;
502-
pub type __gnuc_va_list = __builtin_va_list;
503503
pub type va_list = __builtin_va_list;
504+
pub type __gnuc_va_list = __builtin_va_list;
504505
extern "C" {
505506
pub static sqlite3_version: [::std::os::raw::c_char; 0usize];
506507
}
@@ -937,7 +938,7 @@ extern "C" {
937938
extern "C" {
938939
pub fn sqlite3_vmprintf(
939940
arg1: *const ::std::os::raw::c_char,
940-
arg2: va_list,
941+
arg2: *mut __va_list_tag,
941942
) -> *mut ::std::os::raw::c_char;
942943
}
943944
extern "C" {
@@ -953,7 +954,7 @@ extern "C" {
953954
arg1: ::std::os::raw::c_int,
954955
arg2: *mut ::std::os::raw::c_char,
955956
arg3: *const ::std::os::raw::c_char,
956-
arg4: va_list,
957+
arg4: *mut __va_list_tag,
957958
) -> *mut ::std::os::raw::c_char;
958959
}
959960
extern "C" {
@@ -2500,7 +2501,7 @@ extern "C" {
25002501
pub fn sqlite3_str_vappendf(
25012502
arg1: *mut sqlite3_str,
25022503
zFormat: *const ::std::os::raw::c_char,
2503-
arg2: va_list,
2504+
arg2: *mut __va_list_tag,
25042505
);
25052506
}
25062507
extern "C" {
@@ -3503,4 +3504,12 @@ extern "C" {
35033504
extern "C" {
35043505
pub static sqlite3_wal_manager: libsql_wal_manager;
35053506
}
3506-
pub type __builtin_va_list = *mut ::std::os::raw::c_char;
3507+
pub type __builtin_va_list = [__va_list_tag; 1usize];
3508+
#[repr(C)]
3509+
#[derive(Debug, Copy, Clone)]
3510+
pub struct __va_list_tag {
3511+
pub gp_offset: ::std::os::raw::c_uint,
3512+
pub fp_offset: ::std::os::raw::c_uint,
3513+
pub overflow_arg_area: *mut ::std::os::raw::c_void,
3514+
pub reg_save_area: *mut ::std::os::raw::c_void,
3515+
}

libsql-ffi/bundled/src/sqlite3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116564,7 +116564,7 @@ void libsqlAlterAlterColumn(
116564116564
sqlite3NestedParse(pParse,
116565116565
"UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
116566116566
"sql = libsql_alter_column(sql, %Q, %Q, %d, %Q, %d, %d, %d) "
116567-
"WHERE tbl_name = %Q",
116567+
"WHERE name = %Q AND type = 'table'",
116568116568
zDb,
116569116569
zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1, pTab->aCol[iCol].colFlags,
116570116570
pTab->zName

libsql-sqlite3/src/alter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ void libsqlAlterAlterColumn(
758758
sqlite3NestedParse(pParse,
759759
"UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
760760
"sql = libsql_alter_column(sql, %Q, %Q, %d, %Q, %d, %d, %d) "
761-
"WHERE tbl_name = %Q",
761+
"WHERE name = %Q AND type = 'table'",
762762
zDb,
763763
zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1, pTab->aCol[iCol].colFlags,
764764
pTab->zName

libsql-sqlite3/test/libsql_alter.test

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,35 @@ source $testdir/tester.tcl
1212
do_test libsql_alter-ok.1 {
1313
execsql {CREATE TABLE t1(x);}
1414
execsql {ALTER TABLE t1 ALTER COLUMN x TO x;}
15-
execsql {SELECT sql FROM sqlite_master WHERE tbl_name = 't1';}
15+
execsql {SELECT sql FROM sqlite_master WHERE name = 't1';}
1616
} {{CREATE TABLE t1(x)}}
1717

1818
do_test libsql_alter-ok.2 {
1919
execsql {CREATE TABLE t2(x);}
2020
execsql {ALTER TABLE t2 ALTER COLUMN x TO x INTEGER DEFAULT(-1);}
21-
execsql {SELECT sql FROM sqlite_master WHERE tbl_name = 't2';}
21+
execsql {SELECT sql FROM sqlite_master WHERE name = 't2';}
2222
} {{CREATE TABLE t2(x INTEGER DEFAULT(-1))}}
2323

2424
do_test libsql_alter-ok.3 {
2525
execsql {CREATE TABLE t3(x);}
2626
# NOTE: extra spaces in the end of ALTER COLUMN command
2727
execsql { ALTER TABLE t3 ALTER COLUMN x TO x INTEGER DEFAULT(-1); }
28-
execsql {SELECT sql FROM sqlite_master WHERE tbl_name = 't3';}
28+
execsql {SELECT sql FROM sqlite_master WHERE name = 't3';}
2929
} {{CREATE TABLE t3(x INTEGER DEFAULT(-1))}}
3030

3131
do_test libsql_alter-ok.4 {
3232
execsql {CREATE TABLE t4(x);}
3333
execsql { ALTER TABLE t4 ALTER COLUMN x TO x INTEGER DEFAULT(-1); -- explain alter command }
34-
execsql {SELECT sql FROM sqlite_master WHERE tbl_name = 't4';}
34+
execsql {SELECT sql FROM sqlite_master WHERE name = 't4';}
3535
} {{CREATE TABLE t4(x INTEGER DEFAULT(-1))}}
3636

37+
do_test libsql_alter-ok.5 {
38+
execsql {CREATE TABLE t5(x);}
39+
execsql {CREATE INDEX t5_idx ON t5(x);}
40+
execsql {ALTER TABLE t5 ALTER COLUMN x TO x TEXT}
41+
execsql {SELECT sql FROM sqlite_master WHERE name = 't5';}
42+
} {{CREATE TABLE t5(x TEXT)}}
43+
3744
reset_db
3845
do_test libsql_alter-err.1 {
3946
execsql { CREATE TABLE t1(x); }

libsql-sqlite3/test/rust_suite/src/alter_column.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,13 @@ fn test_comment_in_the_end() {
196196
)
197197
.unwrap();
198198
}
199+
200+
#[test]
201+
fn test_table_with_index() {
202+
let conn = Connection::open_in_memory().unwrap();
203+
204+
conn.execute("CREATE TABLE t(id)", ()).unwrap();
205+
conn.execute("CREATE INDEX i ON t(id)", ()).unwrap();
206+
conn.execute("ALTER TABLE t ALTER COLUMN id TO id TEXT", ())
207+
.unwrap();
208+
}

0 commit comments

Comments
 (0)