SQLite

Check-in [76d2d2ad3b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix handling of schema changes mid-session.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 76d2d2ad3b2a5171393b7894f35f463ff284e53b
User & Date: dan 2011-03-24 16:53:57.000
Context
2011-03-25
10:52
Improve coverage of session module code. (check-in: 666123c8d0 user: dan tags: sessions)
2011-03-24
16:53
Fix handling of schema changes mid-session. (check-in: 76d2d2ad3b user: dan tags: sessions)
16:04
Fix handling of schema mismatches in sqlite3session.c so that it matches the docs in sqlite3session.h. (check-in: 506a0d7a71 user: dan tags: sessions)
Changes
Unified Diff Ignore Whitespace Patch
Changes to ext/session/session3.test.
15
16
17
18
19
20
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  set testdir [file join [file dirname [info script]] .. .. test]
} 
source [file join [file dirname [info script]] session_common.tcl]
source $testdir/tester.tcl

set testprefix session3


# These tests - session3-1.* - verify that the session module behaves
# correctly when confronted with a schema mismatch when applying a 
# changeset (in function sqlite3changeset_apply()).
#
#   session3-1.1.*: Table does not exist in target db.
#   session3-1.2.*: Table has wrong number of columns in target db.
#   session3-1.3.*: Table has wrong PK columns in target db.
#

db close
sqlite3_shutdown
test_sqlite3_log log
sqlite3 db test.db

proc log {code msg} { lappend ::log $code $msg }








>








<







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

31
32
33
34
35
36
37
  set testdir [file join [file dirname [info script]] .. .. test]
} 
source [file join [file dirname [info script]] session_common.tcl]
source $testdir/tester.tcl

set testprefix session3

#-------------------------------------------------------------------------
# These tests - session3-1.* - verify that the session module behaves
# correctly when confronted with a schema mismatch when applying a 
# changeset (in function sqlite3changeset_apply()).
#
#   session3-1.1.*: Table does not exist in target db.
#   session3-1.2.*: Table has wrong number of columns in target db.
#   session3-1.3.*: Table has wrong PK columns in target db.
#

db close
sqlite3_shutdown
test_sqlite3_log log
sqlite3 db test.db

proc log {code msg} { lappend ::log $code $msg }

72
73
74
75
76
77
78
79




80






















































































81
82
83
84
85
86
87
88
  set ::log {}
  do_then_apply_sql {
    INSERT INTO t1 VALUES(9, 10);
    INSERT INTO t1 VALUES(11, 12);
  }
  set ::log
} {SQLITE_SCHEMA {sqlite3changeset_apply(): primary key mismatch for table t1}}




























































































catch { db close }
catch { db2 close }
sqlite3_shutdown
test_sqlite3_log
sqlite3_initialize

finish_test









>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  set ::log {}
  do_then_apply_sql {
    INSERT INTO t1 VALUES(9, 10);
    INSERT INTO t1 VALUES(11, 12);
  }
  set ::log
} {SQLITE_SCHEMA {sqlite3changeset_apply(): primary key mismatch for table t1}}

#-------------------------------------------------------------------------
# These tests - session3-2.* - verify that the session module behaves
# correctly when the schema of an attached table is modified during the
# session.
#
#   session3-2.1.*: Table is dropped midway through the session.
#   session3-2.2.*: Table is dropped and recreated with a different # cols.
#   session3-2.3.*: Table is dropped and recreated with a different PK.
#
# In all of these scenarios, the call to sqlite3session_changeset() will
# return SQLITE_SCHEMA. Also:
#   
#   session3-2.4.*: Table is dropped and recreated with an identical schema.
#                   In this case sqlite3session_changeset() returns SQLITE_OK.
#

do_test 2.1 {
  execsql { CREATE TABLE t2(a, b PRIMARY KEY) }
  sqlite3session S db main
  S attach t2
  execsql {
    INSERT INTO t2 VALUES(1, 2);
    DROP TABLE t2;
  }
  list [catch { S changeset } msg] $msg
} {1 SQLITE_SCHEMA}

do_test 2.2.1 {
  S delete
  sqlite3session S db main
  execsql { CREATE TABLE t2(a, b PRIMARY KEY, c) }
  S attach t2
  execsql {
    INSERT INTO t2 VALUES(1, 2, 3);
    DROP TABLE t2;
    CREATE TABLE t2(a, b PRIMARY KEY);
  }
  list [catch { S changeset } msg] $msg
} {1 SQLITE_SCHEMA}

do_test 2.2.2 {
  S delete
  sqlite3session S db main
  execsql { 
    DROP TABLE t2;
    CREATE TABLE t2(a, b PRIMARY KEY, c);
  }
  S attach t2
  execsql {
    INSERT INTO t2 VALUES(1, 2, 3);
    DROP TABLE t2;
    CREATE TABLE t2(a, b PRIMARY KEY, c, d);
  }
  list [catch { S changeset } msg] $msg
} {1 SQLITE_SCHEMA}

do_test 2.3 {
  S delete
  sqlite3session S db main
  execsql { 
    DROP TABLE t2;
    CREATE TABLE t2(a, b PRIMARY KEY);
  }
  S attach t2
  execsql {
    INSERT INTO t2 VALUES(1, 2);
    DROP TABLE t2;
    CREATE TABLE t2(a PRIMARY KEY, b, c);
  }
  list [catch { S changeset } msg] $msg
} {1 SQLITE_SCHEMA}

do_test 2.4 {
  S delete
  sqlite3session S db main
  execsql { 
    DROP TABLE t2;
    CREATE TABLE t2(a, b PRIMARY KEY);
  }
  S attach t2
  execsql {
    INSERT INTO t2 VALUES(1, 2);
    DROP TABLE t2;
    CREATE TABLE t2(a, b PRIMARY KEY);
  }
  list [catch { S changeset } msg] $msg
} {0 {}}

S delete


catch { db close }
catch { db2 close }
sqlite3_shutdown
test_sqlite3_log
sqlite3_initialize

finish_test

Changes to ext/session/sqlite3session.c.
1345
1346
1347
1348
1349
1350
1351
1352
1353

1354
1355
1356
1357






1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
  *pnChangeset = 0;
  *ppChangeset = 0;
  rc = pSession->rc;

  for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
    if( pTab->nEntry ){
      const char *zName = pTab->zName;
      int nCol = pTab->nCol;      /* Local copy of member variable */
      u8 *abPK = pTab->abPK;      /* Local copy of member variable */

      int i;                      /* Used to iterate through hash buckets */
      sqlite3_stmt *pSel = 0;     /* SELECT statement to query table pTab */
      int nRewind = buf.nBuf;     /* Initial size of write buffer */
      int nNoop;                  /* Size of buffer after writing tbl header */







      /* Write a table header */
      sessionAppendByte(&buf, 'T', &rc);
      sessionAppendVarint(&buf, nCol, &rc);
      sessionAppendBlob(&buf, pTab->abPK, nCol, &rc);
      sessionAppendBlob(&buf, (u8 *)zName, strlen(zName)+1, &rc);

      /* Build and compile a statement to execute: */
      if( rc==SQLITE_OK ){
        rc = sessionSelectStmt(
            db, pSession->zDb, zName, nCol, pTab->azCol, abPK, &pSel);
      }

      if( rc==SQLITE_OK && nCol!=sqlite3_column_count(pSel) ){
        rc = SQLITE_SCHEMA;
      }

      nNoop = buf.nBuf;







|
|
>




>
>
>
>
>
>










|







1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
  *pnChangeset = 0;
  *ppChangeset = 0;
  rc = pSession->rc;

  for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
    if( pTab->nEntry ){
      const char *zName = pTab->zName;
      int nCol;                   /* Number of columns in table */
      u8 *abPK;                   /* Primary key array */
      const char **azCol = 0;     /* Table columns */
      int i;                      /* Used to iterate through hash buckets */
      sqlite3_stmt *pSel = 0;     /* SELECT statement to query table pTab */
      int nRewind = buf.nBuf;     /* Initial size of write buffer */
      int nNoop;                  /* Size of buffer after writing tbl header */

      /* Check the table schema is still Ok. */
      rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
      if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
        rc = SQLITE_SCHEMA;
      }

      /* Write a table header */
      sessionAppendByte(&buf, 'T', &rc);
      sessionAppendVarint(&buf, nCol, &rc);
      sessionAppendBlob(&buf, pTab->abPK, nCol, &rc);
      sessionAppendBlob(&buf, (u8 *)zName, strlen(zName)+1, &rc);

      /* Build and compile a statement to execute: */
      if( rc==SQLITE_OK ){
        rc = sessionSelectStmt(
            db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
      }

      if( rc==SQLITE_OK && nCol!=sqlite3_column_count(pSel) ){
        rc = SQLITE_SCHEMA;
      }

      nNoop = buf.nBuf;
1403
1404
1405
1406
1407
1408
1409

1410
1411
1412
1413
1414
1415
1416
        }
      }

      sqlite3_finalize(pSel);
      if( buf.nBuf==nNoop ){
        buf.nBuf = nRewind;
      }

    }
  }

  if( rc==SQLITE_OK ){
    *pnChangeset = buf.nBuf;
    *ppChangeset = buf.aBuf;
  }else{







>







1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
        }
      }

      sqlite3_finalize(pSel);
      if( buf.nBuf==nNoop ){
        buf.nBuf = nRewind;
      }
      sqlite3_free(azCol);
    }
  }

  if( rc==SQLITE_OK ){
    *pnChangeset = buf.nBuf;
    *ppChangeset = buf.aBuf;
  }else{