os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/alter3.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2005 February 19
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#*************************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this script is testing that SQLite can handle a subtle 
sl@0
    13
# file format change that may be used in the future to implement
sl@0
    14
# "ALTER TABLE ... ADD COLUMN".
sl@0
    15
#
sl@0
    16
# $Id: alter3.test,v 1.11 2008/03/19 00:21:31 drh Exp $
sl@0
    17
#
sl@0
    18
sl@0
    19
set testdir [file dirname $argv0]
sl@0
    20
sl@0
    21
source $testdir/tester.tcl
sl@0
    22
sl@0
    23
# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
sl@0
    24
ifcapable !altertable {
sl@0
    25
  finish_test
sl@0
    26
  return
sl@0
    27
}
sl@0
    28
sl@0
    29
# Determine if there is a codec available on this test.
sl@0
    30
#
sl@0
    31
if {[catch {sqlite3 -has_codec} r] || $r} {
sl@0
    32
  set has_codec 1
sl@0
    33
} else {
sl@0
    34
  set has_codec 0
sl@0
    35
}
sl@0
    36
sl@0
    37
sl@0
    38
# Test Organisation:
sl@0
    39
# ------------------
sl@0
    40
#
sl@0
    41
# alter3-1.*: Test that ALTER TABLE correctly modifies the CREATE TABLE sql.
sl@0
    42
# alter3-2.*: Test error messages.
sl@0
    43
# alter3-3.*: Test adding columns with default value NULL.
sl@0
    44
# alter3-4.*: Test adding columns with default values other than NULL.
sl@0
    45
# alter3-5.*: Test adding columns to tables in ATTACHed databases.
sl@0
    46
# alter3-6.*: Test that temp triggers are not accidentally dropped.
sl@0
    47
# alter3-7.*: Test that VACUUM resets the file-format.
sl@0
    48
#
sl@0
    49
sl@0
    50
# This procedure returns the value of the file-format in file 'test.db'.
sl@0
    51
# 
sl@0
    52
proc get_file_format {{fname test.db}} {
sl@0
    53
  return [hexio_get_int [hexio_read $fname 44 4]]
sl@0
    54
}
sl@0
    55
sl@0
    56
do_test alter3-1.1 {
sl@0
    57
  execsql {
sl@0
    58
    CREATE TABLE abc(a, b, c);
sl@0
    59
    SELECT sql FROM sqlite_master;
sl@0
    60
  }
sl@0
    61
} {{CREATE TABLE abc(a, b, c)}}
sl@0
    62
do_test alter3-1.2 {
sl@0
    63
  execsql {ALTER TABLE abc ADD d INTEGER;}
sl@0
    64
  execsql {
sl@0
    65
    SELECT sql FROM sqlite_master;
sl@0
    66
  }
sl@0
    67
} {{CREATE TABLE abc(a, b, c, d INTEGER)}}
sl@0
    68
do_test alter3-1.3 {
sl@0
    69
  execsql {ALTER TABLE abc ADD e}
sl@0
    70
  execsql {
sl@0
    71
    SELECT sql FROM sqlite_master;
sl@0
    72
  }
sl@0
    73
} {{CREATE TABLE abc(a, b, c, d INTEGER, e)}}
sl@0
    74
do_test alter3-1.4 {
sl@0
    75
  execsql {
sl@0
    76
    CREATE TABLE main.t1(a, b);
sl@0
    77
    ALTER TABLE t1 ADD c;
sl@0
    78
    SELECT sql FROM sqlite_master WHERE tbl_name = 't1';
sl@0
    79
  }
sl@0
    80
} {{CREATE TABLE t1(a, b, c)}}
sl@0
    81
do_test alter3-1.5 {
sl@0
    82
  execsql {
sl@0
    83
    ALTER TABLE t1 ADD d CHECK (a>d);
sl@0
    84
    SELECT sql FROM sqlite_master WHERE tbl_name = 't1';
sl@0
    85
  }
sl@0
    86
} {{CREATE TABLE t1(a, b, c, d CHECK (a>d))}}
sl@0
    87
ifcapable foreignkey {
sl@0
    88
  do_test alter3-1.6 {
sl@0
    89
    execsql {
sl@0
    90
      CREATE TABLE t2(a, b, UNIQUE(a, b));
sl@0
    91
      ALTER TABLE t2 ADD c REFERENCES t1(c)  ;
sl@0
    92
      SELECT sql FROM sqlite_master WHERE tbl_name = 't2' AND type = 'table';
sl@0
    93
    }
sl@0
    94
  } {{CREATE TABLE t2(a, b, c REFERENCES t1(c), UNIQUE(a, b))}}
sl@0
    95
}
sl@0
    96
do_test alter3-1.7 {
sl@0
    97
  execsql {
sl@0
    98
    CREATE TABLE t3(a, b, UNIQUE(a, b));
sl@0
    99
    ALTER TABLE t3 ADD COLUMN c VARCHAR(10, 20);
sl@0
   100
    SELECT sql FROM sqlite_master WHERE tbl_name = 't3' AND type = 'table';
sl@0
   101
  }
sl@0
   102
} {{CREATE TABLE t3(a, b, c VARCHAR(10, 20), UNIQUE(a, b))}}
sl@0
   103
do_test alter3-1.99 {
sl@0
   104
  catchsql {
sl@0
   105
    # May not exist if foriegn-keys are omitted at compile time.
sl@0
   106
    DROP TABLE t2; 
sl@0
   107
  }
sl@0
   108
  execsql {
sl@0
   109
    DROP TABLE abc; 
sl@0
   110
    DROP TABLE t1; 
sl@0
   111
    DROP TABLE t3; 
sl@0
   112
  }
sl@0
   113
} {}
sl@0
   114
sl@0
   115
do_test alter3-2.1 {
sl@0
   116
  execsql {
sl@0
   117
    CREATE TABLE t1(a, b);
sl@0
   118
  }
sl@0
   119
  catchsql {
sl@0
   120
    ALTER TABLE t1 ADD c PRIMARY KEY;
sl@0
   121
  }
sl@0
   122
} {1 {Cannot add a PRIMARY KEY column}}
sl@0
   123
do_test alter3-2.2 {
sl@0
   124
  catchsql {
sl@0
   125
    ALTER TABLE t1 ADD c UNIQUE
sl@0
   126
  }
sl@0
   127
} {1 {Cannot add a UNIQUE column}}
sl@0
   128
do_test alter3-2.3 {
sl@0
   129
  catchsql {
sl@0
   130
    ALTER TABLE t1 ADD b VARCHAR(10)
sl@0
   131
  }
sl@0
   132
} {1 {duplicate column name: b}}
sl@0
   133
do_test alter3-2.3 {
sl@0
   134
  catchsql {
sl@0
   135
    ALTER TABLE t1 ADD c NOT NULL;
sl@0
   136
  }
sl@0
   137
} {1 {Cannot add a NOT NULL column with default value NULL}}
sl@0
   138
do_test alter3-2.4 {
sl@0
   139
  catchsql {
sl@0
   140
    ALTER TABLE t1 ADD c NOT NULL DEFAULT 10;
sl@0
   141
  }
sl@0
   142
} {0 {}}
sl@0
   143
ifcapable view {
sl@0
   144
  do_test alter3-2.5 {
sl@0
   145
    execsql {
sl@0
   146
      CREATE VIEW v1 AS SELECT * FROM t1;
sl@0
   147
    }
sl@0
   148
    catchsql {
sl@0
   149
      alter table v1 add column d;
sl@0
   150
    }
sl@0
   151
  } {1 {Cannot add a column to a view}}
sl@0
   152
}
sl@0
   153
do_test alter3-2.6 {
sl@0
   154
  catchsql {
sl@0
   155
    alter table t1 add column d DEFAULT CURRENT_TIME;
sl@0
   156
  }
sl@0
   157
} {1 {Cannot add a column with non-constant default}}
sl@0
   158
do_test alter3-2.99 {
sl@0
   159
  execsql {
sl@0
   160
    DROP TABLE t1;
sl@0
   161
  }
sl@0
   162
} {}
sl@0
   163
sl@0
   164
do_test alter3-3.1 {
sl@0
   165
  execsql {
sl@0
   166
    CREATE TABLE t1(a, b);
sl@0
   167
    INSERT INTO t1 VALUES(1, 100);
sl@0
   168
    INSERT INTO t1 VALUES(2, 300);
sl@0
   169
    SELECT * FROM t1;
sl@0
   170
  }
sl@0
   171
} {1 100 2 300}
sl@0
   172
do_test alter3-3.1 {
sl@0
   173
  execsql {
sl@0
   174
    PRAGMA schema_version = 10;
sl@0
   175
  }
sl@0
   176
} {}
sl@0
   177
do_test alter3-3.2 {
sl@0
   178
  execsql {
sl@0
   179
    ALTER TABLE t1 ADD c;
sl@0
   180
    SELECT * FROM t1;
sl@0
   181
  }
sl@0
   182
} {1 100 {} 2 300 {}}
sl@0
   183
if {!$has_codec} {
sl@0
   184
  do_test alter3-3.3 {
sl@0
   185
    get_file_format
sl@0
   186
  } {3}
sl@0
   187
}
sl@0
   188
ifcapable schema_version {
sl@0
   189
  do_test alter3-3.4 {
sl@0
   190
    execsql {
sl@0
   191
      PRAGMA schema_version;
sl@0
   192
    }
sl@0
   193
  } {11}
sl@0
   194
}
sl@0
   195
sl@0
   196
do_test alter3-4.1 {
sl@0
   197
  db close
sl@0
   198
  file delete -force test.db
sl@0
   199
  set ::DB [sqlite3 db test.db]
sl@0
   200
  execsql {
sl@0
   201
    CREATE TABLE t1(a, b);
sl@0
   202
    INSERT INTO t1 VALUES(1, 100);
sl@0
   203
    INSERT INTO t1 VALUES(2, 300);
sl@0
   204
    SELECT * FROM t1;
sl@0
   205
  }
sl@0
   206
} {1 100 2 300}
sl@0
   207
do_test alter3-4.1 {
sl@0
   208
  execsql {
sl@0
   209
    PRAGMA schema_version = 20;
sl@0
   210
  }
sl@0
   211
} {}
sl@0
   212
do_test alter3-4.2 {
sl@0
   213
  execsql {
sl@0
   214
    ALTER TABLE t1 ADD c DEFAULT 'hello world';
sl@0
   215
    SELECT * FROM t1;
sl@0
   216
  }
sl@0
   217
} {1 100 {hello world} 2 300 {hello world}}
sl@0
   218
if {!$has_codec} {
sl@0
   219
  do_test alter3-4.3 {
sl@0
   220
    get_file_format
sl@0
   221
  } {3}
sl@0
   222
}
sl@0
   223
ifcapable schema_version {
sl@0
   224
  do_test alter3-4.4 {
sl@0
   225
    execsql {
sl@0
   226
      PRAGMA schema_version;
sl@0
   227
    }
sl@0
   228
  } {21}
sl@0
   229
}
sl@0
   230
do_test alter3-4.99 {
sl@0
   231
  execsql {
sl@0
   232
    DROP TABLE t1;
sl@0
   233
  }
sl@0
   234
} {}
sl@0
   235
sl@0
   236
ifcapable attach {
sl@0
   237
  do_test alter3-5.1 {
sl@0
   238
    file delete -force test2.db
sl@0
   239
    file delete -force test2.db-journal
sl@0
   240
    execsql {
sl@0
   241
      CREATE TABLE t1(a, b);
sl@0
   242
      INSERT INTO t1 VALUES(1, 'one');
sl@0
   243
      INSERT INTO t1 VALUES(2, 'two');
sl@0
   244
      ATTACH 'test2.db' AS aux;
sl@0
   245
      CREATE TABLE aux.t1 AS SELECT * FROM t1;
sl@0
   246
      PRAGMA aux.schema_version = 30;
sl@0
   247
      SELECT sql FROM aux.sqlite_master;
sl@0
   248
    } 
sl@0
   249
  } {{CREATE TABLE t1(a,b)}}
sl@0
   250
  do_test alter3-5.2 {
sl@0
   251
    execsql {
sl@0
   252
      ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128);
sl@0
   253
      SELECT sql FROM aux.sqlite_master;
sl@0
   254
    }
sl@0
   255
  } {{CREATE TABLE t1(a,b, c VARCHAR(128))}}
sl@0
   256
  do_test alter3-5.3 {
sl@0
   257
    execsql {
sl@0
   258
      SELECT * FROM aux.t1;
sl@0
   259
    }
sl@0
   260
  } {1 one {} 2 two {}}
sl@0
   261
  ifcapable schema_version {
sl@0
   262
    do_test alter3-5.4 {
sl@0
   263
      execsql {
sl@0
   264
        PRAGMA aux.schema_version;
sl@0
   265
      }
sl@0
   266
    } {31}
sl@0
   267
  }
sl@0
   268
  if {!$has_codec} {
sl@0
   269
    do_test alter3-5.5 {
sl@0
   270
      list [get_file_format test2.db] [get_file_format]
sl@0
   271
    } {2 3}
sl@0
   272
  }
sl@0
   273
  do_test alter3-5.6 {
sl@0
   274
    execsql {
sl@0
   275
      ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
sl@0
   276
      SELECT sql FROM aux.sqlite_master;
sl@0
   277
    }
sl@0
   278
  } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
sl@0
   279
  do_test alter3-5.7 {
sl@0
   280
    execsql {
sl@0
   281
      SELECT * FROM aux.t1;
sl@0
   282
    }
sl@0
   283
  } {1 one {} 1000 2 two {} 1000}
sl@0
   284
  ifcapable schema_version {
sl@0
   285
    do_test alter3-5.8 {
sl@0
   286
      execsql {
sl@0
   287
        PRAGMA aux.schema_version;
sl@0
   288
      }
sl@0
   289
    } {32}
sl@0
   290
  }
sl@0
   291
  do_test alter3-5.9 {
sl@0
   292
    execsql {
sl@0
   293
      SELECT * FROM t1;
sl@0
   294
    }
sl@0
   295
  } {1 one 2 two}
sl@0
   296
  do_test alter3-5.99 {
sl@0
   297
    execsql {
sl@0
   298
      DROP TABLE aux.t1;
sl@0
   299
      DROP TABLE t1;
sl@0
   300
    }
sl@0
   301
  } {}
sl@0
   302
}
sl@0
   303
sl@0
   304
#----------------------------------------------------------------
sl@0
   305
# Test that the table schema is correctly reloaded when a column
sl@0
   306
# is added to a table.
sl@0
   307
#
sl@0
   308
ifcapable trigger&&tempdb {
sl@0
   309
  do_test alter3-6.1 {
sl@0
   310
    execsql {
sl@0
   311
      CREATE TABLE t1(a, b);
sl@0
   312
      CREATE TABLE log(trig, a, b);
sl@0
   313
sl@0
   314
      CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN
sl@0
   315
        INSERT INTO log VALUES('a', new.a, new.b);
sl@0
   316
      END;
sl@0
   317
      CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN
sl@0
   318
        INSERT INTO log VALUES('b', new.a, new.b);
sl@0
   319
      END;
sl@0
   320
  
sl@0
   321
      INSERT INTO t1 VALUES(1, 2);
sl@0
   322
      SELECT * FROM log;
sl@0
   323
    }
sl@0
   324
  } {b 1 2 a 1 2}
sl@0
   325
  do_test alter3-6.2 {
sl@0
   326
    execsql {
sl@0
   327
      ALTER TABLE t1 ADD COLUMN c DEFAULT 'c';
sl@0
   328
      INSERT INTO t1(a, b) VALUES(3, 4);
sl@0
   329
      SELECT * FROM log;
sl@0
   330
    }
sl@0
   331
  } {b 1 2 a 1 2 b 3 4 a 3 4}
sl@0
   332
}
sl@0
   333
sl@0
   334
if {!$has_codec} {
sl@0
   335
  ifcapable vacuum {
sl@0
   336
    do_test alter3-7.1 {
sl@0
   337
      execsql {
sl@0
   338
        VACUUM;
sl@0
   339
      }
sl@0
   340
      get_file_format
sl@0
   341
    } {1}
sl@0
   342
    do_test alter3-7.2 {
sl@0
   343
      execsql {
sl@0
   344
        CREATE TABLE abc(a, b, c);
sl@0
   345
        ALTER TABLE abc ADD d DEFAULT NULL;
sl@0
   346
      }
sl@0
   347
      get_file_format
sl@0
   348
    } {2}
sl@0
   349
    do_test alter3-7.3 {
sl@0
   350
      execsql {
sl@0
   351
        ALTER TABLE abc ADD e DEFAULT 10;
sl@0
   352
      }
sl@0
   353
      get_file_format
sl@0
   354
    } {3}
sl@0
   355
    do_test alter3-7.4 {
sl@0
   356
      execsql {
sl@0
   357
        ALTER TABLE abc ADD f DEFAULT NULL;
sl@0
   358
      }
sl@0
   359
      get_file_format
sl@0
   360
    } {3}
sl@0
   361
    do_test alter3-7.5 {
sl@0
   362
      execsql {
sl@0
   363
        VACUUM;
sl@0
   364
      }
sl@0
   365
      get_file_format
sl@0
   366
    } {1}
sl@0
   367
  }
sl@0
   368
}
sl@0
   369
sl@0
   370
# Ticket #1183 - Make sure adding columns to large tables does not cause
sl@0
   371
# memory corruption (as was the case before this bug was fixed).
sl@0
   372
do_test alter3-8.1 {
sl@0
   373
  execsql {
sl@0
   374
    CREATE TABLE t4(c1);
sl@0
   375
  }
sl@0
   376
} {}
sl@0
   377
set ::sql ""
sl@0
   378
do_test alter3-8.2 {
sl@0
   379
  set cols c1
sl@0
   380
  for {set i 2} {$i < 100} {incr i} {
sl@0
   381
    execsql "
sl@0
   382
      ALTER TABLE t4 ADD c$i
sl@0
   383
    "
sl@0
   384
    lappend cols c$i
sl@0
   385
  }
sl@0
   386
  set ::sql "CREATE TABLE t4([join $cols {, }])"
sl@0
   387
  list 
sl@0
   388
} {}
sl@0
   389
do_test alter3-8.2 {
sl@0
   390
  execsql {
sl@0
   391
    SELECT sql FROM sqlite_master WHERE name = 't4';
sl@0
   392
  }
sl@0
   393
} [list $::sql]
sl@0
   394
sl@0
   395
finish_test