/ Hex Artifact Content
Login

Artifact d0133bfa7a0a24aa84c034051410b95217d24a35:


0000: 23 20 32 30 30 35 20 46 65 62 72 75 61 72 79 20  # 2005 February 
0010: 31 38 0a 23 0a 23 20 54 68 65 20 61 75 74 68 6f  18.#.# The autho
0020: 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
0030: 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
0040: 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
0050: 6c 61 63 65 20 6f 66 0a 23 20 61 20 6c 65 67 61  lace of.# a lega
0060: 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
0070: 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 23 0a  s a blessing:.#.
0080: 23 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  #    May you do 
0090: 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
00a0: 6c 2e 0a 23 20 20 20 20 4d 61 79 20 79 6f 75 20  l..#    May you 
00b0: 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
00c0: 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
00d0: 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
00e0: 2e 0a 23 20 20 20 20 4d 61 79 20 79 6f 75 20 73  ..#    May you s
00f0: 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
0100: 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
0110: 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 23 0a  han you give..#.
0120: 23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  #***************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 23 20 54 68 69  **********.# Thi
0170: 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
0180: 73 20 72 65 67 72 65 73 73 69 6f 6e 20 74 65 73  s regression tes
0190: 74 73 20 66 6f 72 20 53 51 4c 69 74 65 20 6c 69  ts for SQLite li
01a0: 62 72 61 72 79 2e 20 20 54 68 65 0a 23 20 66 6f  brary.  The.# fo
01b0: 63 75 73 20 6f 66 20 74 68 69 73 20 73 63 72 69  cus of this scri
01c0: 70 74 20 69 73 20 74 65 73 74 69 6e 67 20 74 68  pt is testing th
01d0: 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 68 61  at SQLite can ha
01e0: 6e 64 6c 65 20 61 20 73 75 62 74 6c 65 20 0a 23  ndle a subtle .#
01f0: 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68 61   file format cha
0200: 6e 67 65 20 74 68 61 74 20 6d 61 79 20 62 65 20  nge that may be 
0210: 75 73 65 64 20 69 6e 20 74 68 65 20 66 75 74 75  used in the futu
0220: 72 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a  re to implement.
0230: 23 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e  # "ALTER TABLE .
0240: 2e 2e 20 41 44 44 20 43 4f 4c 55 4d 4e 22 2e 0a  .. ADD COLUMN"..
0250: 23 0a 23 20 24 49 64 3a 20 61 6c 74 65 72 32 2e  #.# $Id: alter2.
0260: 74 65 73 74 2c 76 20 31 2e 31 34 20 32 30 30 39  test,v 1.14 2009
0270: 2f 30 34 2f 30 37 20 31 34 3a 31 34 3a 32 32 20  /04/07 14:14:22 
0280: 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
0290: 24 0a 23 0a 0a 73 65 74 20 74 65 73 74 64 69 72  $.#..set testdir
02a0: 20 5b 66 69 6c 65 20 64 69 72 6e 61 6d 65 20 24   [file dirname $
02b0: 61 72 67 76 30 5d 0a 73 6f 75 72 63 65 20 24 74  argv0].source $t
02c0: 65 73 74 64 69 72 2f 74 65 73 74 65 72 2e 74 63  estdir/tester.tc
02d0: 6c 0a 0a 23 20 57 65 20 68 61 76 65 20 74 6f 20  l..# We have to 
02e0: 68 61 76 65 20 70 72 61 67 6d 61 73 20 69 6e 20  have pragmas in 
02f0: 6f 72 64 65 72 20 74 6f 20 64 6f 20 74 68 69 73  order to do this
0300: 20 74 65 73 74 0a 69 66 63 61 70 61 62 6c 65 20   test.ifcapable 
0310: 7b 21 70 72 61 67 6d 61 7d 20 72 65 74 75 72 6e  {!pragma} return
0320: 0a 0a 23 20 54 68 65 73 65 20 74 65 73 74 73 20  ..# These tests 
0330: 64 6f 20 6e 6f 74 20 77 6f 72 6b 20 69 66 20 74  do not work if t
0340: 68 65 72 65 20 69 73 20 61 20 63 6f 64 65 63 2e  here is a codec.
0350: 20 0a 23 0a 23 69 66 20 7b 5b 63 61 74 63 68 20   .#.#if {[catch 
0360: 7b 73 71 6c 69 74 65 33 20 2d 68 61 73 5f 63 6f  {sqlite3 -has_co
0370: 64 65 63 7d 20 72 5d 20 7c 7c 20 24 72 7d 20 72  dec} r] || $r} r
0380: 65 74 75 72 6e 0a 0a 23 20 54 68 65 20 66 69 6c  eturn..# The fil
0390: 65 20 66 6f 72 6d 61 74 20 63 68 61 6e 67 65 20  e format change 
03a0: 61 66 66 65 63 74 73 20 74 68 65 20 77 61 79 20  affects the way 
03b0: 72 6f 77 2d 72 65 63 6f 72 64 73 20 73 74 6f 72  row-records stor
03c0: 65 64 20 69 6e 20 74 61 62 6c 65 73 20 28 62 75  ed in tables (bu
03d0: 74 20 0a 23 20 6e 6f 74 20 69 6e 64 69 63 65 73  t .# not indices
03e0: 29 20 61 72 65 20 69 6e 74 65 72 70 72 65 74 65  ) are interprete
03f0: 64 2e 20 42 65 66 6f 72 65 20 76 65 72 73 69 6f  d. Before versio
0400: 6e 20 33 2e 31 2e 33 2c 20 61 20 72 6f 77 2d 72  n 3.1.3, a row-r
0410: 65 63 6f 72 64 20 66 6f 72 20 61 20 0a 23 20 74  ecord for a .# t
0420: 61 62 6c 65 20 77 69 74 68 20 4e 20 63 6f 6c 75  able with N colu
0430: 6d 6e 73 20 77 61 73 20 67 75 61 72 61 6e 74 65  mns was guarante
0440: 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 65 78  ed to contain ex
0450: 61 63 74 6c 79 20 4e 20 66 69 65 6c 64 73 2e 20  actly N fields. 
0460: 41 73 0a 23 20 6f 66 20 76 65 72 73 69 6f 6e 20  As.# of version 
0470: 33 2e 31 2e 33 2c 20 74 68 65 20 72 65 63 6f 72  3.1.3, the recor
0480: 64 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 75 70  d may contain up
0490: 20 74 6f 20 4e 20 66 69 65 6c 64 73 2e 20 49 6e   to N fields. In
04a0: 20 74 68 69 73 20 63 61 73 65 0a 23 20 74 68 65   this case.# the
04b0: 20 4d 20 66 69 65 6c 64 73 20 74 68 61 74 20 61   M fields that a
04c0: 72 65 20 70 72 65 73 65 6e 74 20 61 72 65 20 74  re present are t
04d0: 68 65 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68  he values for th
04e0: 65 20 6c 65 66 74 2d 6d 6f 73 74 20 4d 20 0a 23  e left-most M .#
04f0: 20 63 6f 6c 75 6d 6e 73 2e 20 54 68 65 20 28 4e   columns. The (N
0500: 2d 4d 29 20 72 69 67 68 74 6d 6f 73 74 20 63 6f  -M) rightmost co
0510: 6c 75 6d 6e 73 20 63 6f 6e 74 61 69 6e 20 4e 55  lumns contain NU
0520: 4c 4c 2e 0a 23 0a 23 20 49 66 20 61 6e 79 20 72  LL..#.# If any r
0530: 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 64 61  ecords in the da
0540: 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 20 6c  tabase contain l
0550: 65 73 73 20 66 69 65 6c 64 73 20 74 68 61 6e 20  ess fields than 
0560: 74 68 65 69 72 20 74 61 62 6c 65 0a 23 20 68 61  their table.# ha
0570: 73 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20  s columns, then 
0580: 74 68 65 20 66 69 6c 65 2d 66 6f 72 6d 61 74 20  the file-format 
0590: 6d 65 74 61 20 76 61 6c 75 65 20 73 68 6f 75 6c  meta value shoul
05a0: 64 20 62 65 20 73 65 74 20 74 6f 20 28 61 74 20  d be set to (at 
05b0: 6c 65 61 73 74 29 20 32 2e 20 0a 23 0a 0a 23 20  least) 2. .#..# 
05c0: 54 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 73  This procedure s
05d0: 65 74 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ets the value of
05e0: 20 74 68 65 20 66 69 6c 65 2d 66 6f 72 6d 61 74   the file-format
05f0: 20 69 6e 20 66 69 6c 65 20 27 74 65 73 74 2e 64   in file 'test.d
0600: 62 27 0a 23 20 74 6f 20 24 6e 65 77 76 61 6c 2e  b'.# to $newval.
0610: 20 41 6c 73 6f 2c 20 74 68 65 20 73 63 68 65 6d   Also, the schem
0620: 61 20 63 6f 6f 6b 69 65 20 69 73 20 69 6e 63 72  a cookie is incr
0630: 65 6d 65 6e 74 65 64 2e 0a 23 20 0a 70 72 6f 63  emented..# .proc
0640: 20 73 65 74 5f 66 69 6c 65 5f 66 6f 72 6d 61 74   set_file_format
0650: 20 7b 6e 65 77 76 61 6c 7d 20 7b 0a 20 20 68 65   {newval} {.  he
0660: 78 69 6f 5f 77 72 69 74 65 20 74 65 73 74 2e 64  xio_write test.d
0670: 62 20 34 34 20 5b 68 65 78 69 6f 5f 72 65 6e 64  b 44 [hexio_rend
0680: 65 72 5f 69 6e 74 33 32 20 24 6e 65 77 76 61 6c  er_int32 $newval
0690: 5d 0a 20 20 73 65 74 20 73 63 68 65 6d 61 63 6f  ].  set schemaco
06a0: 6f 6b 69 65 20 5b 68 65 78 69 6f 5f 67 65 74 5f  okie [hexio_get_
06b0: 69 6e 74 20 5b 68 65 78 69 6f 5f 72 65 61 64 20  int [hexio_read 
06c0: 74 65 73 74 2e 64 62 20 34 30 20 34 5d 5d 0a 20  test.db 40 4]]. 
06d0: 20 69 6e 63 72 20 73 63 68 65 6d 61 63 6f 6f 6b   incr schemacook
06e0: 69 65 0a 20 20 68 65 78 69 6f 5f 77 72 69 74 65  ie.  hexio_write
06f0: 20 74 65 73 74 2e 64 62 20 34 30 20 5b 68 65 78   test.db 40 [hex
0700: 69 6f 5f 72 65 6e 64 65 72 5f 69 6e 74 33 32 20  io_render_int32 
0710: 24 73 63 68 65 6d 61 63 6f 6f 6b 69 65 5d 0a 20  $schemacookie]. 
0720: 20 72 65 74 75 72 6e 20 7b 7d 0a 7d 0a 0a 23 20   return {}.}..# 
0730: 54 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 72  This procedure r
0740: 65 74 75 72 6e 73 20 74 68 65 20 76 61 6c 75 65  eturns the value
0750: 20 6f 66 20 74 68 65 20 66 69 6c 65 2d 66 6f 72   of the file-for
0760: 6d 61 74 20 69 6e 20 66 69 6c 65 20 27 74 65 73  mat in file 'tes
0770: 74 2e 64 62 27 2e 0a 23 20 0a 70 72 6f 63 20 67  t.db'..# .proc g
0780: 65 74 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 20 7b  et_file_format {
0790: 7b 66 6e 61 6d 65 20 74 65 73 74 2e 64 62 7d 7d  {fname test.db}}
07a0: 20 7b 0a 20 20 72 65 74 75 72 6e 20 5b 68 65 78   {.  return [hex
07b0: 69 6f 5f 67 65 74 5f 69 6e 74 20 5b 68 65 78 69  io_get_int [hexi
07c0: 6f 5f 72 65 61 64 20 24 66 6e 61 6d 65 20 34 34  o_read $fname 44
07d0: 20 34 5d 5d 0a 7d 0a 0a 23 20 54 68 69 73 20 70   4]].}..# This p
07e0: 72 6f 63 65 64 75 72 65 20 73 65 74 73 20 74 68  rocedure sets th
07f0: 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
0800: 73 74 6f 72 65 64 20 66 6f 72 20 74 61 62 6c 65  stored for table
0810: 20 24 74 62 6c 20 69 6e 20 74 68 65 0a 23 20 73   $tbl in the.# s
0820: 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
0830: 6c 65 20 6f 66 20 66 69 6c 65 20 27 74 65 73 74  le of file 'test
0840: 2e 64 62 27 20 74 6f 20 24 73 71 6c 2e 20 41 6c  .db' to $sql. Al
0850: 73 6f 20 73 65 74 20 74 68 65 20 66 69 6c 65 20  so set the file 
0860: 66 6f 72 6d 61 74 0a 23 20 74 6f 20 74 68 65 20  format.# to the 
0870: 73 75 70 70 6c 69 65 64 20 76 61 6c 75 65 2e 20  supplied value. 
0880: 54 68 69 73 20 69 73 20 32 20 69 66 20 74 68 65  This is 2 if the
0890: 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 20 68 61   added column ha
08a0: 73 20 61 20 64 65 66 61 75 6c 74 20 74 68 61 74  s a default that
08b0: 20 69 73 0a 23 20 4e 55 4c 4c 2c 20 6f 72 20 33   is.# NULL, or 3
08c0: 20 6f 74 68 65 72 77 69 73 65 2e 20 0a 23 0a 70   otherwise. .#.p
08d0: 72 6f 63 20 61 6c 74 65 72 5f 74 61 62 6c 65 20  roc alter_table 
08e0: 7b 74 62 6c 20 73 71 6c 20 7b 66 69 6c 65 5f 66  {tbl sql {file_f
08f0: 6f 72 6d 61 74 20 32 7d 7d 20 7b 0a 20 20 73 71  ormat 2}} {.  sq
0900: 6c 69 74 65 33 20 64 62 61 74 20 74 65 73 74 2e  lite3 dbat test.
0910: 64 62 0a 20 20 73 65 74 20 73 20 5b 73 74 72 69  db.  set s [stri
0920: 6e 67 20 6d 61 70 20 7b 27 20 27 27 7d 20 24 73  ng map {' ''} $s
0930: 71 6c 5d 0a 20 20 73 65 74 20 74 20 5b 73 74 72  ql].  set t [str
0940: 69 6e 67 20 6d 61 70 20 7b 27 20 27 27 7d 20 24  ing map {' ''} $
0950: 74 62 6c 5d 0a 20 20 64 62 61 74 20 65 76 61 6c  tbl].  dbat eval
0960: 20 5b 73 75 62 73 74 20 7b 0a 20 20 20 20 50 52   [subst {.    PR
0970: 41 47 4d 41 20 77 72 69 74 61 62 6c 65 5f 73 63  AGMA writable_sc
0980: 68 65 6d 61 20 3d 20 31 3b 0a 20 20 20 20 55 50  hema = 1;.    UP
0990: 44 41 54 45 20 73 71 6c 69 74 65 5f 6d 61 73 74  DATE sqlite_mast
09a0: 65 72 20 53 45 54 20 73 71 6c 20 3d 20 27 24 73  er SET sql = '$s
09b0: 27 20 57 48 45 52 45 20 6e 61 6d 65 20 3d 20 27  ' WHERE name = '
09c0: 24 74 27 20 41 4e 44 20 74 79 70 65 20 3d 20 27  $t' AND type = '
09d0: 74 61 62 6c 65 27 3b 0a 20 20 20 20 50 52 41 47  table';.    PRAG
09e0: 4d 41 20 77 72 69 74 61 62 6c 65 5f 73 63 68 65  MA writable_sche
09f0: 6d 61 20 3d 20 30 3b 0a 20 20 7d 5d 0a 20 20 64  ma = 0;.  }].  d
0a00: 62 61 74 20 63 6c 6f 73 65 0a 20 20 73 65 74 5f  bat close.  set_
0a10: 66 69 6c 65 5f 66 6f 72 6d 61 74 20 32 0a 7d 0a  file_format 2.}.
0a20: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
0a30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0a40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0a50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0a60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 53 6f 6d 65  ---------.# Some
0a70: 20 62 61 73 69 63 20 74 65 73 74 73 20 74 6f 20   basic tests to 
0a80: 6d 61 6b 65 20 73 75 72 65 20 73 68 6f 72 74 20  make sure short 
0a90: 72 6f 77 73 20 61 72 65 20 68 61 6e 64 6c 65 64  rows are handled
0aa0: 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 61 6c 74 65  ..#.do_test alte
0ab0: 72 32 2d 31 2e 31 20 7b 0a 20 20 65 78 65 63 73  r2-1.1 {.  execs
0ac0: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
0ad0: 54 41 42 4c 45 20 61 62 63 28 61 2c 20 62 29 3b  TABLE abc(a, b);
0ae0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
0af0: 20 61 62 63 20 56 41 4c 55 45 53 28 31 2c 20 32   abc VALUES(1, 2
0b00: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
0b10: 54 4f 20 61 62 63 20 56 41 4c 55 45 53 28 33 2c  TO abc VALUES(3,
0b20: 20 34 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20   4);.    INSERT 
0b30: 49 4e 54 4f 20 61 62 63 20 56 41 4c 55 45 53 28  INTO abc VALUES(
0b40: 35 2c 20 36 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  5, 6);.  }.} {}.
0b50: 64 6f 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 31  do_test alter2-1
0b60: 2e 32 20 7b 0a 20 20 23 20 41 4c 54 45 52 20 54  .2 {.  # ALTER T
0b70: 41 42 4c 45 20 61 62 63 20 41 44 44 20 43 4f 4c  ABLE abc ADD COL
0b80: 55 4d 4e 20 63 3b 0a 20 20 61 6c 74 65 72 5f 74  UMN c;.  alter_t
0b90: 61 62 6c 65 20 61 62 63 20 7b 43 52 45 41 54 45  able abc {CREATE
0ba0: 20 54 41 42 4c 45 20 61 62 63 28 61 2c 20 62 2c   TABLE abc(a, b,
0bb0: 20 63 29 3b 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65   c);}.} {}.do_te
0bc0: 73 74 20 61 6c 74 65 72 32 2d 31 2e 33 20 7b 0a  st alter2-1.3 {.
0bd0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
0be0: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61 62  SELECT * FROM ab
0bf0: 63 3b 0a 20 20 7d 0a 7d 20 7b 31 20 32 20 7b 7d  c;.  }.} {1 2 {}
0c00: 20 33 20 34 20 7b 7d 20 35 20 36 20 7b 7d 7d 0a   3 4 {} 5 6 {}}.
0c10: 64 6f 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 31  do_test alter2-1
0c20: 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .4 {.  execsql {
0c30: 0a 20 20 20 20 55 50 44 41 54 45 20 61 62 63 20  .    UPDATE abc 
0c40: 53 45 54 20 63 20 3d 20 31 30 20 57 48 45 52 45  SET c = 10 WHERE
0c50: 20 61 20 3d 20 31 3b 0a 20 20 20 20 53 45 4c 45   a = 1;.    SELE
0c60: 43 54 20 2a 20 46 52 4f 4d 20 61 62 63 3b 0a 20  CT * FROM abc;. 
0c70: 20 7d 0a 7d 20 7b 31 20 32 20 31 30 20 33 20 34   }.} {1 2 10 3 4
0c80: 20 7b 7d 20 35 20 36 20 7b 7d 7d 0a 64 6f 5f 74   {} 5 6 {}}.do_t
0c90: 65 73 74 20 61 6c 74 65 72 32 2d 31 2e 35 20 7b  est alter2-1.5 {
0ca0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
0cb0: 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 61 62   CREATE INDEX ab
0cc0: 63 5f 69 20 4f 4e 20 61 62 63 28 63 29 3b 0a 20  c_i ON abc(c);. 
0cd0: 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20   }.} {}.do_test 
0ce0: 61 6c 74 65 72 32 2d 31 2e 36 20 7b 0a 20 20 65  alter2-1.6 {.  e
0cf0: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c  xecsql {.    SEL
0d00: 45 43 54 20 63 20 46 52 4f 4d 20 61 62 63 20 4f  ECT c FROM abc O
0d10: 52 44 45 52 20 42 59 20 63 3b 0a 20 20 7d 0a 7d  RDER BY c;.  }.}
0d20: 20 7b 7b 7d 20 7b 7d 20 31 30 7d 0a 64 6f 5f 74   {{} {} 10}.do_t
0d30: 65 73 74 20 61 6c 74 65 72 32 2d 31 2e 37 20 7b  est alter2-1.7 {
0d40: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
0d50: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61   SELECT * FROM a
0d60: 62 63 20 57 48 45 52 45 20 63 20 3d 20 31 30 3b  bc WHERE c = 10;
0d70: 0a 20 20 7d 0a 7d 20 7b 31 20 32 20 31 30 7d 0a  .  }.} {1 2 10}.
0d80: 64 6f 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 31  do_test alter2-1
0d90: 2e 38 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .8 {.  execsql {
0da0: 0a 20 20 20 20 53 45 4c 45 43 54 20 73 75 6d 28  .    SELECT sum(
0db0: 61 29 2c 20 63 20 46 52 4f 4d 20 61 62 63 20 47  a), c FROM abc G
0dc0: 52 4f 55 50 20 42 59 20 63 3b 0a 20 20 7d 0a 7d  ROUP BY c;.  }.}
0dd0: 20 7b 38 20 7b 7d 20 31 20 31 30 7d 0a 64 6f 5f   {8 {} 1 10}.do_
0de0: 74 65 73 74 20 61 6c 74 65 72 32 2d 31 2e 39 20  test alter2-1.9 
0df0: 7b 0a 20 20 23 20 41 4c 54 45 52 20 54 41 42 4c  {.  # ALTER TABL
0e00: 45 20 61 62 63 20 41 44 44 20 43 4f 4c 55 4d 4e  E abc ADD COLUMN
0e10: 20 64 3b 0a 20 20 61 6c 74 65 72 5f 74 61 62 6c   d;.  alter_tabl
0e20: 65 20 61 62 63 20 7b 43 52 45 41 54 45 20 54 41  e abc {CREATE TA
0e30: 42 4c 45 20 61 62 63 28 61 2c 20 62 2c 20 63 2c  BLE abc(a, b, c,
0e40: 20 64 29 3b 7d 0a 20 20 65 78 65 63 73 71 6c 20   d);}.  execsql 
0e50: 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  { SELECT * FROM 
0e60: 61 62 63 3b 20 7d 0a 20 20 65 78 65 63 73 71 6c  abc; }.  execsql
0e70: 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 61 62   {.    UPDATE ab
0e80: 63 20 53 45 54 20 64 20 3d 20 31 31 20 57 48 45  c SET d = 11 WHE
0e90: 52 45 20 63 20 49 53 20 4e 55 4c 4c 20 41 4e 44  RE c IS NULL AND
0ea0: 20 61 3c 34 3b 0a 20 20 20 20 53 45 4c 45 43 54   a<4;.    SELECT
0eb0: 20 2a 20 46 52 4f 4d 20 61 62 63 3b 0a 20 20 7d   * FROM abc;.  }
0ec0: 0a 7d 20 7b 31 20 32 20 31 30 20 7b 7d 20 33 20  .} {1 2 10 {} 3 
0ed0: 34 20 7b 7d 20 31 31 20 35 20 36 20 7b 7d 20 7b  4 {} 11 5 6 {} {
0ee0: 7d 7d 0a 64 6f 5f 74 65 73 74 20 61 6c 74 65 72  }}.do_test alter
0ef0: 32 2d 31 2e 31 30 20 7b 0a 20 20 65 78 65 63 73  2-1.10 {.  execs
0f00: 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20  ql {.    SELECT 
0f10: 74 79 70 65 6f 66 28 64 29 20 46 52 4f 4d 20 61  typeof(d) FROM a
0f20: 62 63 3b 0a 20 20 7d 0a 7d 20 7b 6e 75 6c 6c 20  bc;.  }.} {null 
0f30: 69 6e 74 65 67 65 72 20 6e 75 6c 6c 7d 0a 64 6f  integer null}.do
0f40: 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 31 2e 39  _test alter2-1.9
0f50: 39 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  9 {.  execsql {.
0f60: 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20 61      DROP TABLE a
0f70: 62 63 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d  bc;.  }.} {}..#-
0f80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0fa0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0fb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0fc0: 2d 2d 2d 2d 2d 2d 0a 23 20 54 65 73 74 20 74 68  ------.# Test th
0fd0: 61 74 20 76 69 65 77 73 20 77 6f 72 6b 20 77 68  at views work wh
0fe0: 65 6e 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  en the underlyin
0ff0: 67 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72  g table structur
1000: 65 20 69 73 20 63 68 61 6e 67 65 64 2e 0a 23 0a  e is changed..#.
1010: 69 66 63 61 70 61 62 6c 65 20 76 69 65 77 20 7b  ifcapable view {
1020: 0a 20 20 64 6f 5f 74 65 73 74 20 61 6c 74 65 72  .  do_test alter
1030: 32 2d 32 2e 31 20 7b 0a 20 20 20 20 65 78 65 63  2-2.1 {.    exec
1040: 73 71 6c 20 7b 0a 20 20 20 20 20 20 43 52 45 41  sql {.      CREA
1050: 54 45 20 54 41 42 4c 45 20 61 62 63 32 28 61 2c  TE TABLE abc2(a,
1060: 20 62 2c 20 63 29 3b 0a 20 20 20 20 20 20 49 4e   b, c);.      IN
1070: 53 45 52 54 20 49 4e 54 4f 20 61 62 63 32 20 56  SERT INTO abc2 V
1080: 41 4c 55 45 53 28 31 2c 20 32 2c 20 31 30 29 3b  ALUES(1, 2, 10);
1090: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
10a0: 54 4f 20 61 62 63 32 20 56 41 4c 55 45 53 28 33  TO abc2 VALUES(3
10b0: 2c 20 34 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20  , 4, NULL);.    
10c0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62    INSERT INTO ab
10d0: 63 32 20 56 41 4c 55 45 53 28 35 2c 20 36 2c 20  c2 VALUES(5, 6, 
10e0: 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 43 52 45  NULL);.      CRE
10f0: 41 54 45 20 56 49 45 57 20 61 62 63 32 5f 76 20  ATE VIEW abc2_v 
1100: 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  AS SELECT * FROM
1110: 20 61 62 63 32 3b 0a 20 20 20 20 20 20 53 45 4c   abc2;.      SEL
1120: 45 43 54 20 2a 20 46 52 4f 4d 20 61 62 63 32 5f  ECT * FROM abc2_
1130: 76 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 31 20  v;.    }.  } {1 
1140: 32 20 31 30 20 33 20 34 20 7b 7d 20 35 20 36 20  2 10 3 4 {} 5 6 
1150: 7b 7d 7d 0a 20 20 64 6f 5f 74 65 73 74 20 61 6c  {}}.  do_test al
1160: 74 65 72 32 2d 32 2e 32 20 7b 0a 20 20 20 20 23  ter2-2.2 {.    #
1170: 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61 62 63   ALTER TABLE abc
1180: 20 41 44 44 20 43 4f 4c 55 4d 4e 20 64 3b 0a 20   ADD COLUMN d;. 
1190: 20 20 20 61 6c 74 65 72 5f 74 61 62 6c 65 20 61     alter_table a
11a0: 62 63 32 20 7b 43 52 45 41 54 45 20 54 41 42 4c  bc2 {CREATE TABL
11b0: 45 20 61 62 63 32 28 61 2c 20 62 2c 20 63 2c 20  E abc2(a, b, c, 
11c0: 64 29 3b 7d 0a 20 20 20 20 65 78 65 63 73 71 6c  d);}.    execsql
11d0: 20 7b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20   {.      SELECT 
11e0: 2a 20 46 52 4f 4d 20 61 62 63 32 5f 76 3b 0a 20  * FROM abc2_v;. 
11f0: 20 20 20 7d 0a 20 20 7d 20 7b 31 20 32 20 31 30     }.  } {1 2 10
1200: 20 7b 7d 20 33 20 34 20 7b 7d 20 7b 7d 20 35 20   {} 3 4 {} {} 5 
1210: 36 20 7b 7d 20 7b 7d 7d 0a 20 20 64 6f 5f 74 65  6 {} {}}.  do_te
1220: 73 74 20 61 6c 74 65 72 32 2d 32 2e 33 20 7b 0a  st alter2-2.3 {.
1230: 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20      execsql {.  
1240: 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20 61      DROP TABLE a
1250: 62 63 32 3b 0a 20 20 20 20 20 20 44 52 4f 50 20  bc2;.      DROP 
1260: 56 49 45 57 20 61 62 63 32 5f 76 3b 0a 20 20 20  VIEW abc2_v;.   
1270: 20 7d 0a 20 20 7d 20 7b 7d 0a 7d 0a 0a 23 2d 2d   }.  } {}.}..#--
1280: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1290: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12c0: 2d 2d 2d 2d 2d 0a 23 20 54 65 73 74 20 74 68 61  -----.# Test tha
12d0: 74 20 74 72 69 67 67 65 72 73 20 77 6f 72 6b 20  t triggers work 
12e0: 77 68 65 6e 20 61 20 73 68 6f 72 74 20 72 6f 77  when a short row
12f0: 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68   is copied to th
1300: 65 20 6f 6c 64 2e 2a 0a 23 20 74 72 69 67 67 65  e old.*.# trigge
1310: 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a  r pseudo-table..
1320: 23 0a 69 66 63 61 70 61 62 6c 65 20 74 72 69 67  #.ifcapable trig
1330: 67 65 72 20 7b 0a 20 20 64 6f 5f 74 65 73 74 20  ger {.  do_test 
1340: 61 6c 74 65 72 32 2d 33 2e 31 20 7b 0a 20 20 20  alter2-3.1 {.   
1350: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 20   execsql {.     
1360: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62   CREATE TABLE ab
1370: 63 33 28 61 2c 20 62 29 3b 0a 20 20 20 20 20 20  c3(a, b);.      
1380: 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 6c 6f  CREATE TABLE blo
1390: 67 28 6f 2c 20 6e 29 3b 0a 20 20 20 20 20 20 43  g(o, n);.      C
13a0: 52 45 41 54 45 20 54 52 49 47 47 45 52 20 61 62  REATE TRIGGER ab
13b0: 63 33 5f 74 20 41 46 54 45 52 20 55 50 44 41 54  c3_t AFTER UPDAT
13c0: 45 20 4f 46 20 62 20 4f 4e 20 61 62 63 33 20 42  E OF b ON abc3 B
13d0: 45 47 49 4e 0a 20 20 20 20 20 20 20 20 49 4e 53  EGIN.        INS
13e0: 45 52 54 20 49 4e 54 4f 20 62 6c 6f 67 20 56 41  ERT INTO blog VA
13f0: 4c 55 45 53 28 6f 6c 64 2e 62 2c 20 6e 65 77 2e  LUES(old.b, new.
1400: 62 29 3b 0a 20 20 20 20 20 20 45 4e 44 3b 0a 20  b);.      END;. 
1410: 20 20 20 7d 0a 20 20 7d 20 7b 7d 0a 20 20 64 6f     }.  } {}.  do
1420: 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 33 2e 32  _test alter2-3.2
1430: 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b   {.    execsql {
1440: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
1450: 54 4f 20 61 62 63 33 20 56 41 4c 55 45 53 28 31  TO abc3 VALUES(1
1460: 2c 20 34 29 3b 0a 20 20 20 20 20 20 55 50 44 41  , 4);.      UPDA
1470: 54 45 20 61 62 63 33 20 53 45 54 20 62 20 3d 20  TE abc3 SET b = 
1480: 32 20 57 48 45 52 45 20 62 20 3d 20 34 3b 0a 20  2 WHERE b = 4;. 
1490: 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52       SELECT * FR
14a0: 4f 4d 20 62 6c 6f 67 3b 0a 20 20 20 20 7d 0a 20  OM blog;.    }. 
14b0: 20 7d 20 7b 34 20 32 7d 0a 20 20 64 6f 5f 74 65   } {4 2}.  do_te
14c0: 73 74 20 61 6c 74 65 72 32 2d 33 2e 33 20 7b 0a  st alter2-3.3 {.
14d0: 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20      execsql {.  
14e0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
14f0: 61 62 63 33 20 56 41 4c 55 45 53 28 33 2c 20 34  abc3 VALUES(3, 4
1500: 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  );.      INSERT 
1510: 49 4e 54 4f 20 61 62 63 33 20 56 41 4c 55 45 53  INTO abc3 VALUES
1520: 28 35 2c 20 36 29 3b 0a 20 20 20 20 7d 0a 20 20  (5, 6);.    }.  
1530: 20 20 61 6c 74 65 72 5f 74 61 62 6c 65 20 61 62    alter_table ab
1540: 63 33 20 7b 43 52 45 41 54 45 20 54 41 42 4c 45  c3 {CREATE TABLE
1550: 20 61 62 63 33 28 61 2c 20 62 2c 20 63 29 3b 7d   abc3(a, b, c);}
1560: 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a 20  .    execsql {. 
1570: 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52       SELECT * FR
1580: 4f 4d 20 61 62 63 33 3b 0a 20 20 20 20 7d 0a 20  OM abc3;.    }. 
1590: 20 7d 20 7b 31 20 32 20 7b 7d 20 33 20 34 20 7b   } {1 2 {} 3 4 {
15a0: 7d 20 35 20 36 20 7b 7d 7d 0a 20 20 64 6f 5f 74  } 5 6 {}}.  do_t
15b0: 65 73 74 20 61 6c 74 65 72 32 2d 33 2e 34 20 7b  est alter2-3.4 {
15c0: 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a 20  .    execsql {. 
15d0: 20 20 20 20 20 55 50 44 41 54 45 20 61 62 63 33       UPDATE abc3
15e0: 20 53 45 54 20 62 20 3d 20 62 2a 32 20 57 48 45   SET b = b*2 WHE
15f0: 52 45 20 61 3c 34 3b 0a 20 20 20 20 20 20 53 45  RE a<4;.      SE
1600: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61 62 63 33  LECT * FROM abc3
1610: 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 31 20 34  ;.    }.  } {1 4
1620: 20 7b 7d 20 33 20 38 20 7b 7d 20 35 20 36 20 7b   {} 3 8 {} 5 6 {
1630: 7d 7d 0a 20 20 64 6f 5f 74 65 73 74 20 61 6c 74  }}.  do_test alt
1640: 65 72 32 2d 33 2e 35 20 7b 0a 20 20 20 20 65 78  er2-3.5 {.    ex
1650: 65 63 73 71 6c 20 7b 0a 20 20 20 20 20 20 53 45  ecsql {.      SE
1660: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 62 6c 6f 67  LECT * FROM blog
1670: 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 34 20 32  ;.    }.  } {4 2
1680: 20 32 20 34 20 34 20 38 7d 0a 0a 20 20 64 6f 5f   2 4 4 8}..  do_
1690: 74 65 73 74 20 61 6c 74 65 72 32 2d 33 2e 36 20  test alter2-3.6 
16a0: 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a  {.    execsql {.
16b0: 20 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42        CREATE TAB
16c0: 4c 45 20 63 6c 6f 67 28 6f 2c 20 6e 29 3b 0a 20  LE clog(o, n);. 
16d0: 20 20 20 20 20 43 52 45 41 54 45 20 54 52 49 47       CREATE TRIG
16e0: 47 45 52 20 61 62 63 33 5f 74 32 20 41 46 54 45  GER abc3_t2 AFTE
16f0: 52 20 55 50 44 41 54 45 20 4f 46 20 63 20 4f 4e  R UPDATE OF c ON
1700: 20 61 62 63 33 20 42 45 47 49 4e 0a 20 20 20 20   abc3 BEGIN.    
1710: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
1720: 63 6c 6f 67 20 56 41 4c 55 45 53 28 6f 6c 64 2e  clog VALUES(old.
1730: 63 2c 20 6e 65 77 2e 63 29 3b 0a 20 20 20 20 20  c, new.c);.     
1740: 20 45 4e 44 3b 0a 20 20 20 20 20 20 55 50 44 41   END;.      UPDA
1750: 54 45 20 61 62 63 33 20 53 45 54 20 63 20 3d 20  TE abc3 SET c = 
1760: 61 2a 32 3b 0a 20 20 20 20 20 20 53 45 4c 45 43  a*2;.      SELEC
1770: 54 20 2a 20 46 52 4f 4d 20 63 6c 6f 67 3b 0a 20  T * FROM clog;. 
1780: 20 20 20 7d 0a 20 20 7d 20 7b 7b 7d 20 32 20 7b     }.  } {{} 2 {
1790: 7d 20 36 20 7b 7d 20 31 30 7d 0a 7d 20 65 6c 73  } 6 {} 10}.} els
17a0: 65 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  e {.  execsql { 
17b0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62 63  CREATE TABLE abc
17c0: 33 28 61 2c 20 62 29 3b 20 7d 0a 7d 0a 0a 23 2d  3(a, b); }.}..#-
17d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
17e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
17f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1800: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1810: 2d 2d 2d 2d 0a 23 20 43 68 65 63 6b 20 74 68 61  ----.# Check tha
1820: 74 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  t an error occur
1830: 73 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73  s if the databas
1840: 65 20 69 73 20 75 70 67 72 61 64 65 64 20 74 6f  e is upgraded to
1850: 20 61 20 66 69 6c 65 0a 23 20 66 6f 72 6d 61 74   a file.# format
1860: 20 74 68 61 74 20 53 51 4c 69 74 65 20 64 6f 65   that SQLite doe
1870: 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 28 69  s not support (i
1880: 6e 20 74 68 69 73 20 63 61 73 65 20 35 29 2e 20  n this case 5). 
1890: 4e 6f 74 65 3a 20 54 68 65 20 0a 23 20 66 69 6c  Note: The .# fil
18a0: 65 20 66 6f 72 6d 61 74 20 69 73 20 63 68 65 63  e format is chec
18b0: 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20 74 68  ked each time th
18c0: 65 20 73 63 68 65 6d 61 20 69 73 20 72 65 61 64  e schema is read
18d0: 2c 20 73 6f 20 63 68 61 6e 67 69 6e 67 20 74 68  , so changing th
18e0: 65 0a 23 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  e.# file format 
18f0: 72 65 71 75 69 72 65 73 20 69 6e 63 72 65 6d 65  requires increme
1900: 6e 74 69 6e 67 20 74 68 65 20 73 63 68 65 6d 61  nting the schema
1910: 20 63 6f 6f 6b 69 65 2e 0a 23 0a 64 6f 5f 74 65   cookie..#.do_te
1920: 73 74 20 61 6c 74 65 72 32 2d 34 2e 31 20 7b 0a  st alter2-4.1 {.
1930: 20 20 64 62 20 63 6c 6f 73 65 0a 20 20 73 65 74    db close.  set
1940: 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 20 35 0a 20  _file_format 5. 
1950: 20 73 71 6c 69 74 65 33 20 64 62 20 74 65 73 74   sqlite3 db test
1960: 2e 64 62 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74  .db.} {}.do_test
1970: 20 61 6c 74 65 72 32 2d 34 2e 32 20 7b 0a 20 20   alter2-4.2 {.  
1980: 23 20 57 65 20 68 61 76 65 20 74 6f 20 72 75 6e  # We have to run
1990: 20 74 77 6f 20 71 75 65 72 69 65 73 20 68 65 72   two queries her
19a0: 65 20 62 65 63 61 75 73 65 20 74 68 65 20 54 63  e because the Tc
19b0: 6c 20 69 6e 74 65 72 66 61 63 65 20 75 73 65 73  l interface uses
19c0: 0a 20 20 23 20 73 71 6c 69 74 65 33 5f 70 72 65  .  # sqlite3_pre
19d0: 70 61 72 65 5f 76 32 28 29 2e 20 49 6e 20 74 68  pare_v2(). In th
19e0: 69 73 20 63 61 73 65 2c 20 74 68 65 20 66 69 72  is case, the fir
19f0: 73 74 20 71 75 65 72 79 20 65 6e 63 6f 75 6e 74  st query encount
1a00: 65 72 73 20 61 6e 20 0a 20 20 23 20 53 51 4c 49  ers an .  # SQLI
1a10: 54 45 5f 53 43 48 45 4d 41 20 65 72 72 6f 72 2e  TE_SCHEMA error.
1a20: 20 54 68 65 6e 2c 20 77 68 65 6e 20 74 72 79 69   Then, when tryi
1a30: 6e 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c 65 20  ng to recompile 
1a40: 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  the statement, t
1a50: 68 65 0a 20 20 23 20 22 75 6e 73 75 70 70 6f 72  he.  # "unsuppor
1a60: 74 65 64 20 66 69 6c 65 20 66 6f 72 6d 61 74 22  ted file format"
1a70: 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
1a80: 74 65 72 65 64 2e 20 53 6f 20 74 68 65 20 65 72  tered. So the er
1a90: 72 6f 72 20 63 6f 64 65 0a 20 20 23 20 72 65 74  ror code.  # ret
1aa0: 75 72 6e 65 64 20 69 73 20 53 51 4c 49 54 45 5f  urned is SQLITE_
1ab0: 53 43 48 45 4d 41 2c 20 6e 6f 74 20 53 51 4c 49  SCHEMA, not SQLI
1ac0: 54 45 5f 45 52 52 4f 52 20 61 73 20 72 65 71 75  TE_ERROR as requ
1ad0: 69 72 65 64 20 62 79 20 74 68 65 20 66 6f 6c 6c  ired by the foll
1ae0: 6f 77 69 6e 67 0a 20 20 23 20 74 65 73 74 20 63  owing.  # test c
1af0: 61 73 65 2e 0a 20 20 23 0a 20 20 23 20 57 68 65  ase..  #.  # Whe
1b00: 6e 20 74 68 65 20 71 75 65 72 79 20 69 73 20 61  n the query is a
1b10: 74 74 65 6d 70 74 65 64 20 61 20 73 65 63 6f 6e  ttempted a secon
1b20: 64 20 74 69 6d 65 2c 20 74 68 65 20 73 61 6d 65  d time, the same
1b30: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
1b40: 73 0a 20 20 23 20 72 65 74 75 72 6e 65 64 20 62  s.  # returned b
1b50: 75 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ut the error cod
1b60: 65 20 69 73 20 53 51 4c 49 54 45 5f 45 52 52 4f  e is SQLITE_ERRO
1b70: 52 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 75  R, because the u
1b80: 6e 73 75 70 70 6f 72 74 65 64 0a 20 20 23 20 66  nsupported.  # f
1b90: 69 6c 65 20 66 6f 72 6d 61 74 20 77 61 73 20 64  ile format was d
1ba0: 65 74 65 63 74 65 64 20 64 75 72 69 6e 67 20 61  etected during a
1bb0: 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
1bc0: 5f 70 72 65 70 61 72 65 28 29 2c 20 6e 6f 74 0a  _prepare(), not.
1bd0: 20 20 23 20 73 71 6c 69 74 65 33 5f 73 74 65 70    # sqlite3_step
1be0: 28 29 2e 0a 20 20 23 0a 20 20 63 61 74 63 68 73  ()..  #.  catchs
1bf0: 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52  ql { SELECT * FR
1c00: 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  OM sqlite_master
1c10: 3b 20 7d 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  ; }.  catchsql {
1c20: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 73   SELECT * FROM s
1c30: 71 6c 69 74 65 5f 6d 61 73 74 65 72 3b 20 7d 0a  qlite_master; }.
1c40: 7d 20 7b 31 20 7b 75 6e 73 75 70 70 6f 72 74 65  } {1 {unsupporte
1c50: 64 20 66 69 6c 65 20 66 6f 72 6d 61 74 7d 7d 0a  d file format}}.
1c60: 64 6f 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 34  do_test alter2-4
1c70: 2e 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 65  .3 {.  sqlite3_e
1c80: 72 72 63 6f 64 65 20 64 62 0a 7d 20 7b 53 51 4c  rrcode db.} {SQL
1c90: 49 54 45 5f 45 52 52 4f 52 7d 0a 64 6f 5f 74 65  ITE_ERROR}.do_te
1ca0: 73 74 20 61 6c 74 65 72 32 2d 34 2e 34 20 7b 0a  st alter2-4.4 {.
1cb0: 20 20 73 65 74 20 3a 3a 44 42 20 5b 73 71 6c 69    set ::DB [sqli
1cc0: 74 65 33 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 70  te3_connection_p
1cd0: 6f 69 6e 74 65 72 20 64 62 5d 0a 20 20 63 61 74  ointer db].  cat
1ce0: 63 68 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45  chsql {.    SELE
1cf0: 43 54 20 2a 20 46 52 4f 4d 20 73 71 6c 69 74 65  CT * FROM sqlite
1d00: 5f 6d 61 73 74 65 72 3b 0a 20 20 7d 0a 7d 20 7b  _master;.  }.} {
1d10: 31 20 7b 75 6e 73 75 70 70 6f 72 74 65 64 20 66  1 {unsupported f
1d20: 69 6c 65 20 66 6f 72 6d 61 74 7d 7d 0a 64 6f 5f  ile format}}.do_
1d30: 74 65 73 74 20 61 6c 74 65 72 32 2d 34 2e 35 20  test alter2-4.5 
1d40: 7b 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72 63  {.  sqlite3_errc
1d50: 6f 64 65 20 64 62 0a 7d 20 7b 53 51 4c 49 54 45  ode db.} {SQLITE
1d60: 5f 45 52 52 4f 52 7d 0a 0a 23 2d 2d 2d 2d 2d 2d  _ERROR}..#------
1d70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1da0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
1db0: 23 20 43 68 65 63 6b 20 74 68 61 74 20 65 78 65  # Check that exe
1dc0: 63 75 74 69 6e 67 20 56 41 43 55 55 4d 20 6f 6e  cuting VACUUM on
1dd0: 20 61 20 66 69 6c 65 20 77 69 74 68 20 66 69 6c   a file with fil
1de0: 65 2d 66 6f 72 6d 61 74 20 76 65 72 73 69 6f 6e  e-format version
1df0: 20 32 0a 23 20 72 65 73 65 74 73 20 74 68 65 20   2.# resets the 
1e00: 66 69 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 31  file format to 1
1e10: 2e 0a 23 0a 73 65 74 20 64 65 66 61 75 6c 74 5f  ..#.set default_
1e20: 66 69 6c 65 5f 66 6f 72 6d 61 74 20 5b 65 78 70  file_format [exp
1e30: 72 20 24 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  r $SQLITE_DEFAUL
1e40: 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 3d 3d 34  T_FILE_FORMAT==4
1e50: 20 3f 20 34 20 3a 20 31 5d 0a 69 66 63 61 70 61   ? 4 : 1].ifcapa
1e60: 62 6c 65 20 76 61 63 75 75 6d 20 7b 0a 20 20 64  ble vacuum {.  d
1e70: 6f 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 35 2e  o_test alter2-5.
1e80: 31 20 7b 0a 20 20 20 20 73 65 74 5f 66 69 6c 65  1 {.    set_file
1e90: 5f 66 6f 72 6d 61 74 20 32 0a 20 20 20 20 64 62  _format 2.    db
1ea0: 20 63 6c 6f 73 65 0a 20 20 20 20 73 71 6c 69 74   close.    sqlit
1eb0: 65 33 20 64 62 20 74 65 73 74 2e 64 62 0a 20 20  e3 db test.db.  
1ec0: 20 20 65 78 65 63 73 71 6c 20 7b 53 45 4c 45 43    execsql {SELEC
1ed0: 54 20 31 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f  T 1 FROM sqlite_
1ee0: 6d 61 73 74 65 72 20 4c 49 4d 49 54 20 31 3b 7d  master LIMIT 1;}
1ef0: 0a 20 20 20 20 67 65 74 5f 66 69 6c 65 5f 66 6f  .    get_file_fo
1f00: 72 6d 61 74 0a 20 20 7d 20 7b 32 7d 0a 20 20 64  rmat.  } {2}.  d
1f10: 6f 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 35 2e  o_test alter2-5.
1f20: 32 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20  2 {.    execsql 
1f30: 7b 20 56 41 43 55 55 4d 20 7d 0a 20 20 7d 20 7b  { VACUUM }.  } {
1f40: 7d 0a 20 20 64 6f 5f 74 65 73 74 20 61 6c 74 65  }.  do_test alte
1f50: 72 32 2d 35 2e 33 20 7b 0a 20 20 20 20 67 65 74  r2-5.3 {.    get
1f60: 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 0a 20 20 7d  _file_format.  }
1f70: 20 24 64 65 66 61 75 6c 74 5f 66 69 6c 65 5f 66   $default_file_f
1f80: 6f 72 6d 61 74 0a 7d 0a 20 0a 23 2d 2d 2d 2d 2d  ormat.}. .#-----
1f90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1fa0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1fb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1fc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1fd0: 0a 23 20 54 65 73 74 20 74 68 61 74 20 77 68 65  .# Test that whe
1fe0: 6e 20 61 20 64 61 74 61 62 61 73 65 20 77 69 74  n a database wit
1ff0: 68 20 66 69 6c 65 2d 66 6f 72 6d 61 74 20 32 20  h file-format 2 
2000: 69 73 20 6f 70 65 6e 65 64 2c 20 6e 65 77 20 0a  is opened, new .
2010: 23 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20  # databases are 
2020: 73 74 69 6c 6c 20 63 72 65 61 74 65 64 20 77 69  still created wi
2030: 74 68 20 66 69 6c 65 2d 66 6f 72 6d 61 74 20 31  th file-format 1
2040: 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 61 6c 74 65  ..#.do_test alte
2050: 72 32 2d 36 2e 31 20 7b 0a 20 20 64 62 20 63 6c  r2-6.1 {.  db cl
2060: 6f 73 65 0a 20 20 73 65 74 5f 66 69 6c 65 5f 66  ose.  set_file_f
2070: 6f 72 6d 61 74 20 32 0a 20 20 73 71 6c 69 74 65  ormat 2.  sqlite
2080: 33 20 64 62 20 74 65 73 74 2e 64 62 0a 20 20 67  3 db test.db.  g
2090: 65 74 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 0a 7d  et_file_format.}
20a0: 20 7b 32 7d 0a 69 66 63 61 70 61 62 6c 65 20 61   {2}.ifcapable a
20b0: 74 74 61 63 68 20 7b 0a 20 20 64 6f 5f 74 65 73  ttach {.  do_tes
20c0: 74 20 61 6c 74 65 72 32 2d 36 2e 32 20 7b 0a 20  t alter2-6.2 {. 
20d0: 20 20 20 66 69 6c 65 20 64 65 6c 65 74 65 20 2d     file delete -
20e0: 66 6f 72 63 65 20 74 65 73 74 32 2e 64 62 2d 6a  force test2.db-j
20f0: 6f 75 72 6e 61 6c 0a 20 20 20 20 66 69 6c 65 20  ournal.    file 
2100: 64 65 6c 65 74 65 20 2d 66 6f 72 63 65 20 74 65  delete -force te
2110: 73 74 32 2e 64 62 0a 20 20 20 20 65 78 65 63 73  st2.db.    execs
2120: 71 6c 20 7b 0a 20 20 20 20 20 20 41 54 54 41 43  ql {.      ATTAC
2130: 48 20 27 74 65 73 74 32 2e 64 62 27 20 41 53 20  H 'test2.db' AS 
2140: 61 75 78 3b 0a 20 20 20 20 20 20 43 52 45 41 54  aux;.      CREAT
2150: 45 20 54 41 42 4c 45 20 61 75 78 2e 74 31 28 61  E TABLE aux.t1(a
2160: 2c 20 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , b);.    }.    
2170: 67 65 74 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 20  get_file_format 
2180: 74 65 73 74 32 2e 64 62 0a 20 20 7d 20 24 64 65  test2.db.  } $de
2190: 66 61 75 6c 74 5f 66 69 6c 65 5f 66 6f 72 6d 61  fault_file_forma
21a0: 74 0a 7d 0a 64 6f 5f 74 65 73 74 20 61 6c 74 65  t.}.do_test alte
21b0: 72 32 2d 36 2e 33 20 7b 0a 20 20 65 78 65 63 73  r2-6.3 {.  execs
21c0: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
21d0: 54 41 42 4c 45 20 74 31 28 61 2c 20 62 29 3b 0a  TABLE t1(a, b);.
21e0: 20 20 7d 0a 20 20 67 65 74 5f 66 69 6c 65 5f 66    }.  get_file_f
21f0: 6f 72 6d 61 74 20 0a 7d 20 7b 32 7d 0a 0a 23 2d  ormat .} {2}..#-
2200: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2210: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2240: 2d 2d 2d 2d 0a 23 20 54 65 73 74 20 74 68 61 74  ----.# Test that
2250: 20 74 79 70 65 73 20 61 6e 64 20 76 61 6c 75 65   types and value
2260: 73 20 66 6f 72 20 63 6f 6c 75 6d 6e 73 20 61 64  s for columns ad
2270: 64 65 64 20 77 69 74 68 20 64 65 66 61 75 6c 74  ded with default
2280: 20 76 61 6c 75 65 73 20 0a 23 20 6f 74 68 65 72   values .# other
2290: 20 74 68 61 6e 20 4e 55 4c 4c 20 77 6f 72 6b 20   than NULL work 
22a0: 77 69 74 68 20 53 45 4c 45 43 54 20 73 74 61 74  with SELECT stat
22b0: 65 6d 65 6e 74 73 2e 0a 23 0a 64 6f 5f 74 65 73  ements..#.do_tes
22c0: 74 20 61 6c 74 65 72 32 2d 37 2e 31 20 7b 0a 20  t alter2-7.1 {. 
22d0: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 44   execsql {.    D
22e0: 52 4f 50 20 54 41 42 4c 45 20 74 31 3b 0a 20 20  ROP TABLE t1;.  
22f0: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
2300: 31 28 61 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  1(a);.    INSERT
2310: 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
2320: 31 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  1);.    INSERT I
2330: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 32 29  NTO t1 VALUES(2)
2340: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
2350: 4f 20 74 31 20 56 41 4c 55 45 53 28 33 29 3b 0a  O t1 VALUES(3);.
2360: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
2370: 74 31 20 56 41 4c 55 45 53 28 34 29 3b 0a 20 20  t1 VALUES(4);.  
2380: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
2390: 74 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 32 20 33  t1;.  }.} {1 2 3
23a0: 20 34 7d 0a 64 6f 5f 74 65 73 74 20 61 6c 74 65   4}.do_test alte
23b0: 72 32 2d 37 2e 32 20 7b 0a 20 20 73 65 74 20 73  r2-7.2 {.  set s
23c0: 71 6c 20 7b 43 52 45 41 54 45 20 54 41 42 4c 45  ql {CREATE TABLE
23d0: 20 74 31 28 61 2c 20 62 20 44 45 46 41 55 4c 54   t1(a, b DEFAULT
23e0: 20 27 31 32 33 27 2c 20 63 20 49 4e 54 45 47 45   '123', c INTEGE
23f0: 52 20 44 45 46 41 55 4c 54 20 27 31 32 33 27 29  R DEFAULT '123')
2400: 7d 0a 20 20 61 6c 74 65 72 5f 74 61 62 6c 65 20  }.  alter_table 
2410: 74 31 20 24 73 71 6c 20 33 0a 20 20 65 78 65 63  t1 $sql 3.  exec
2420: 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54  sql {.    SELECT
2430: 20 2a 20 46 52 4f 4d 20 74 31 20 4c 49 4d 49 54   * FROM t1 LIMIT
2440: 20 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 31 32 33   1;.  }.} {1 123
2450: 20 31 32 33 7d 0a 64 6f 5f 74 65 73 74 20 61 6c   123}.do_test al
2460: 74 65 72 32 2d 37 2e 33 20 7b 0a 20 20 65 78 65  ter2-7.3 {.  exe
2470: 63 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43  csql {.    SELEC
2480: 54 20 61 2c 20 74 79 70 65 6f 66 28 61 29 2c 20  T a, typeof(a), 
2490: 62 2c 20 74 79 70 65 6f 66 28 62 29 2c 20 63 2c  b, typeof(b), c,
24a0: 20 74 79 70 65 6f 66 28 63 29 20 46 52 4f 4d 20   typeof(c) FROM 
24b0: 74 31 20 4c 49 4d 49 54 20 31 3b 0a 20 20 7d 0a  t1 LIMIT 1;.  }.
24c0: 7d 20 7b 31 20 69 6e 74 65 67 65 72 20 31 32 33  } {1 integer 123
24d0: 20 74 65 78 74 20 31 32 33 20 69 6e 74 65 67 65   text 123 intege
24e0: 72 7d 0a 64 6f 5f 74 65 73 74 20 61 6c 74 65 72  r}.do_test alter
24f0: 32 2d 37 2e 34 20 7b 0a 20 20 65 78 65 63 73 71  2-7.4 {.  execsq
2500: 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 61  l {.    SELECT a
2510: 2c 20 74 79 70 65 6f 66 28 61 29 2c 20 62 2c 20  , typeof(a), b, 
2520: 74 79 70 65 6f 66 28 62 29 2c 20 63 2c 20 74 79  typeof(b), c, ty
2530: 70 65 6f 66 28 63 29 20 46 52 4f 4d 20 74 31 20  peof(c) FROM t1 
2540: 4c 49 4d 49 54 20 31 3b 0a 20 20 7d 0a 7d 20 7b  LIMIT 1;.  }.} {
2550: 31 20 69 6e 74 65 67 65 72 20 31 32 33 20 74 65  1 integer 123 te
2560: 78 74 20 31 32 33 20 69 6e 74 65 67 65 72 7d 0a  xt 123 integer}.
2570: 64 6f 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 37  do_test alter2-7
2580: 2e 35 20 7b 0a 20 20 73 65 74 20 73 71 6c 20 7b  .5 {.  set sql {
2590: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
25a0: 61 2c 20 62 20 44 45 46 41 55 4c 54 20 2d 31 32  a, b DEFAULT -12
25b0: 33 2e 30 2c 20 63 20 56 41 52 43 48 41 52 28 31  3.0, c VARCHAR(1
25c0: 30 29 20 64 65 66 61 75 6c 74 20 35 29 7d 0a 20  0) default 5)}. 
25d0: 20 61 6c 74 65 72 5f 74 61 62 6c 65 20 74 31 20   alter_table t1 
25e0: 24 73 71 6c 20 33 0a 20 20 65 78 65 63 73 71 6c  $sql 3.  execsql
25f0: 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 61 2c   {.    SELECT a,
2600: 20 74 79 70 65 6f 66 28 61 29 2c 20 62 2c 20 74   typeof(a), b, t
2610: 79 70 65 6f 66 28 62 29 2c 20 63 2c 20 74 79 70  ypeof(b), c, typ
2620: 65 6f 66 28 63 29 20 46 52 4f 4d 20 74 31 20 4c  eof(c) FROM t1 L
2630: 49 4d 49 54 20 31 3b 0a 20 20 7d 0a 7d 20 7b 31  IMIT 1;.  }.} {1
2640: 20 69 6e 74 65 67 65 72 20 2d 31 32 33 20 69 6e   integer -123 in
2650: 74 65 67 65 72 20 35 20 74 65 78 74 7d 0a 0a 23  teger 5 text}..#
2660: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2670: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2680: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2690: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26a0: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 65 73 74 20 74  -------.# Test t
26b0: 68 61 74 20 55 50 44 41 54 45 20 74 72 69 67 67  hat UPDATE trigg
26c0: 65 72 20 74 61 62 6c 65 73 20 77 6f 72 6b 20 77  er tables work w
26d0: 69 74 68 20 64 65 66 61 75 6c 74 20 76 61 6c 75  ith default valu
26e0: 65 73 2c 20 61 6e 64 20 74 68 61 74 20 77 68 65  es, and that whe
26f0: 6e 0a 23 20 61 20 72 6f 77 20 69 73 20 75 70 64  n.# a row is upd
2700: 61 74 65 64 20 74 68 65 20 64 65 66 61 75 6c 74  ated the default
2710: 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 72 72   values are corr
2720: 65 63 74 6c 79 20 74 72 61 6e 73 66 65 72 65 64  ectly transfered
2730: 20 74 6f 20 74 68 65 20 0a 23 20 6e 65 77 20 72   to the .# new r
2740: 6f 77 2e 0a 23 20 0a 69 66 63 61 70 61 62 6c 65  ow..# .ifcapable
2750: 20 74 72 69 67 67 65 72 20 7b 0a 64 62 20 66 75   trigger {.db fu
2760: 6e 63 74 69 6f 6e 20 73 65 74 5f 76 61 6c 20 7b  nction set_val {
2770: 73 65 74 20 3a 3a 76 61 6c 7d 0a 20 20 64 6f 5f  set ::val}.  do_
2780: 74 65 73 74 20 61 6c 74 65 72 32 2d 38 2e 31 20  test alter2-8.1 
2790: 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a  {.    execsql {.
27a0: 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 49        CREATE TRI
27b0: 47 47 45 52 20 74 72 69 67 31 20 42 45 46 4f 52  GGER trig1 BEFOR
27c0: 45 20 55 50 44 41 54 45 20 4f 4e 20 74 31 20 42  E UPDATE ON t1 B
27d0: 45 47 49 4e 0a 20 20 20 20 20 20 53 45 4c 45 43  EGIN.      SELEC
27e0: 54 20 73 65 74 5f 76 61 6c 28 0a 20 20 20 20 20  T set_val(.     
27f0: 20 20 20 20 20 6f 6c 64 2e 62 7c 7c 27 20 27 7c       old.b||' '|
2800: 7c 74 79 70 65 6f 66 28 6f 6c 64 2e 62 29 7c 7c  |typeof(old.b)||
2810: 27 20 27 7c 7c 6f 6c 64 2e 63 7c 7c 27 20 27 7c  ' '||old.c||' '|
2820: 7c 74 79 70 65 6f 66 28 6f 6c 64 2e 63 29 7c 7c  |typeof(old.c)||
2830: 27 20 27 7c 7c 0a 20 20 20 20 20 20 20 20 20 20  ' '||.          
2840: 6e 65 77 2e 62 7c 7c 27 20 27 7c 7c 74 79 70 65  new.b||' '||type
2850: 6f 66 28 6e 65 77 2e 62 29 7c 7c 27 20 27 7c 7c  of(new.b)||' '||
2860: 6e 65 77 2e 63 7c 7c 27 20 27 7c 7c 74 79 70 65  new.c||' '||type
2870: 6f 66 28 6e 65 77 2e 63 29 20 0a 20 20 20 20 20  of(new.c) .     
2880: 20 29 3b 0a 20 20 20 20 20 20 45 4e 44 3b 0a 20   );.      END;. 
2890: 20 20 20 7d 0a 20 20 20 20 6c 69 73 74 0a 20 20     }.    list.  
28a0: 7d 20 7b 7d 0a 7d 0a 64 6f 5f 74 65 73 74 20 61  } {}.}.do_test a
28b0: 6c 74 65 72 32 2d 38 2e 32 20 7b 0a 20 20 65 78  lter2-8.2 {.  ex
28c0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41  ecsql {.    UPDA
28d0: 54 45 20 74 31 20 53 45 54 20 63 20 3d 20 31 30  TE t1 SET c = 10
28e0: 20 57 48 45 52 45 20 61 20 3d 20 31 3b 0a 20 20   WHERE a = 1;.  
28f0: 20 20 53 45 4c 45 43 54 20 61 2c 20 74 79 70 65    SELECT a, type
2900: 6f 66 28 61 29 2c 20 62 2c 20 74 79 70 65 6f 66  of(a), b, typeof
2910: 28 62 29 2c 20 63 2c 20 74 79 70 65 6f 66 28 63  (b), c, typeof(c
2920: 29 20 46 52 4f 4d 20 74 31 20 4c 49 4d 49 54 20  ) FROM t1 LIMIT 
2930: 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 69 6e 74 65  1;.  }.} {1 inte
2940: 67 65 72 20 2d 31 32 33 20 69 6e 74 65 67 65 72  ger -123 integer
2950: 20 31 30 20 74 65 78 74 7d 0a 69 66 63 61 70 61   10 text}.ifcapa
2960: 62 6c 65 20 74 72 69 67 67 65 72 20 7b 0a 20 20  ble trigger {.  
2970: 64 6f 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 38  do_test alter2-8
2980: 2e 33 20 7b 0a 20 20 20 20 73 65 74 20 3a 3a 76  .3 {.    set ::v
2990: 61 6c 0a 20 20 7d 20 7b 2d 31 32 33 20 69 6e 74  al.  } {-123 int
29a0: 65 67 65 72 20 35 20 74 65 78 74 20 2d 31 32 33  eger 5 text -123
29b0: 20 69 6e 74 65 67 65 72 20 31 30 20 74 65 78 74   integer 10 text
29c0: 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }.}..#----------
29d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
29e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
29f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2a00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
2a10: 54 65 73 74 20 74 68 61 74 20 44 45 4c 45 54 45  Test that DELETE
2a20: 20 74 72 69 67 67 65 72 20 74 61 62 6c 65 73 20   trigger tables 
2a30: 77 6f 72 6b 20 77 69 74 68 20 64 65 66 61 75 6c  work with defaul
2a40: 74 20 76 61 6c 75 65 73 2c 20 61 6e 64 20 74 68  t values, and th
2a50: 61 74 20 77 68 65 6e 0a 23 20 61 20 72 6f 77 20  at when.# a row 
2a60: 69 73 20 75 70 64 61 74 65 64 20 74 68 65 20 64  is updated the d
2a70: 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 61 72  efault values ar
2a80: 65 20 63 6f 72 72 65 63 74 6c 79 20 74 72 61 6e  e correctly tran
2a90: 73 66 65 72 65 64 20 74 6f 20 74 68 65 20 0a 23  sfered to the .#
2aa0: 20 6e 65 77 20 72 6f 77 2e 0a 23 20 0a 69 66 63   new row..# .ifc
2ab0: 61 70 61 62 6c 65 20 74 72 69 67 67 65 72 20 7b  apable trigger {
2ac0: 0a 20 20 64 6f 5f 74 65 73 74 20 61 6c 74 65 72  .  do_test alter
2ad0: 32 2d 39 2e 31 20 7b 0a 20 20 20 20 65 78 65 63  2-9.1 {.    exec
2ae0: 73 71 6c 20 7b 0a 20 20 20 20 20 20 43 52 45 41  sql {.      CREA
2af0: 54 45 20 54 52 49 47 47 45 52 20 74 72 69 67 32  TE TRIGGER trig2
2b00: 20 42 45 46 4f 52 45 20 44 45 4c 45 54 45 20 4f   BEFORE DELETE O
2b10: 4e 20 74 31 20 42 45 47 49 4e 0a 20 20 20 20 20  N t1 BEGIN.     
2b20: 20 53 45 4c 45 43 54 20 73 65 74 5f 76 61 6c 28   SELECT set_val(
2b30: 0a 20 20 20 20 20 20 20 20 20 20 6f 6c 64 2e 62  .          old.b
2b40: 7c 7c 27 20 27 7c 7c 74 79 70 65 6f 66 28 6f 6c  ||' '||typeof(ol
2b50: 64 2e 62 29 7c 7c 27 20 27 7c 7c 6f 6c 64 2e 63  d.b)||' '||old.c
2b60: 7c 7c 27 20 27 7c 7c 74 79 70 65 6f 66 28 6f 6c  ||' '||typeof(ol
2b70: 64 2e 63 29 0a 20 20 20 20 20 20 29 3b 0a 20 20  d.c).      );.  
2b80: 20 20 20 20 45 4e 44 3b 0a 20 20 20 20 7d 0a 20      END;.    }. 
2b90: 20 20 20 6c 69 73 74 0a 20 20 7d 20 7b 7d 0a 20     list.  } {}. 
2ba0: 20 64 6f 5f 74 65 73 74 20 61 6c 74 65 72 32 2d   do_test alter2-
2bb0: 39 2e 32 20 7b 0a 20 20 20 20 65 78 65 63 73 71  9.2 {.    execsq
2bc0: 6c 20 7b 0a 20 20 20 20 20 20 44 45 4c 45 54 45  l {.      DELETE
2bd0: 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61   FROM t1 WHERE a
2be0: 20 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 2;.    }.    
2bf0: 73 65 74 20 3a 3a 76 61 6c 0a 20 20 7d 20 7b 2d  set ::val.  } {-
2c00: 31 32 33 20 69 6e 74 65 67 65 72 20 35 20 74 65  123 integer 5 te
2c10: 78 74 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  xt}.}..#--------
2c20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
2c60: 23 20 54 65 73 74 20 63 72 65 61 74 69 6e 67 20  # Test creating 
2c70: 61 6e 20 69 6e 64 65 78 20 6f 6e 20 61 20 63 6f  an index on a co
2c80: 6c 75 6d 6e 20 61 64 64 65 64 20 77 69 74 68 20  lumn added with 
2c90: 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2e  a default value.
2ca0: 20 0a 23 0a 69 66 63 61 70 61 62 6c 65 20 62 6c   .#.ifcapable bl
2cb0: 6f 62 6c 69 74 20 7b 0a 20 20 64 6f 5f 74 65 73  oblit {.  do_tes
2cc0: 74 20 61 6c 74 65 72 32 2d 31 30 2e 31 20 7b 0a  t alter2-10.1 {.
2cd0: 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20      execsql {.  
2ce0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
2cf0: 20 74 32 28 61 29 3b 0a 20 20 20 20 20 20 49 4e   t2(a);.      IN
2d00: 53 45 52 54 20 49 4e 54 4f 20 74 32 20 56 41 4c  SERT INTO t2 VAL
2d10: 55 45 53 28 27 61 27 29 3b 0a 20 20 20 20 20 20  UES('a');.      
2d20: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32 20 56  INSERT INTO t2 V
2d30: 41 4c 55 45 53 28 27 62 27 29 3b 0a 20 20 20 20  ALUES('b');.    
2d40: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32    INSERT INTO t2
2d50: 20 56 41 4c 55 45 53 28 27 63 27 29 3b 0a 20 20   VALUES('c');.  
2d60: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
2d70: 74 32 20 56 41 4c 55 45 53 28 27 64 27 29 3b 0a  t2 VALUES('d');.
2d80: 20 20 20 20 7d 0a 20 20 20 20 61 6c 74 65 72 5f      }.    alter_
2d90: 74 61 62 6c 65 20 74 32 20 7b 43 52 45 41 54 45  table t2 {CREATE
2da0: 20 54 41 42 4c 45 20 74 32 28 61 2c 20 62 20 44   TABLE t2(a, b D
2db0: 45 46 41 55 4c 54 20 58 27 41 42 43 44 27 2c 20  EFAULT X'ABCD', 
2dc0: 63 20 44 45 46 41 55 4c 54 20 4e 55 4c 4c 29 3b  c DEFAULT NULL);
2dd0: 7d 20 33 0a 20 20 20 20 63 61 74 63 68 73 71 6c  } 3.    catchsql
2de0: 20 7b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20   {.      SELECT 
2df0: 2a 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61  * FROM sqlite_ma
2e00: 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ster;.    }.    
2e10: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 20 20  execsql {.      
2e20: 53 45 4c 45 43 54 20 71 75 6f 74 65 28 61 29 2c  SELECT quote(a),
2e30: 20 71 75 6f 74 65 28 62 29 2c 20 71 75 6f 74 65   quote(b), quote
2e40: 28 63 29 20 46 52 4f 4d 20 74 32 20 4c 49 4d 49  (c) FROM t2 LIMI
2e50: 54 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b  T 1;.    }.  } {
2e60: 27 61 27 20 58 27 41 42 43 44 27 20 4e 55 4c 4c  'a' X'ABCD' NULL
2e70: 7d 0a 20 20 64 6f 5f 74 65 73 74 20 61 6c 74 65  }.  do_test alte
2e80: 72 32 2d 31 30 2e 32 20 7b 0a 20 20 20 20 65 78  r2-10.2 {.    ex
2e90: 65 63 73 71 6c 20 7b 0a 20 20 20 20 20 20 43 52  ecsql {.      CR
2ea0: 45 41 54 45 20 49 4e 44 45 58 20 69 31 20 4f 4e  EATE INDEX i1 ON
2eb0: 20 74 32 28 62 29 3b 0a 20 20 20 20 20 20 53 45   t2(b);.      SE
2ec0: 4c 45 43 54 20 61 20 46 52 4f 4d 20 74 32 20 57  LECT a FROM t2 W
2ed0: 48 45 52 45 20 62 20 3d 20 58 27 41 42 43 44 27  HERE b = X'ABCD'
2ee0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 61 20 62  ;.    }.  } {a b
2ef0: 20 63 20 64 7d 0a 20 20 64 6f 5f 74 65 73 74 20   c d}.  do_test 
2f00: 61 6c 74 65 72 32 2d 31 30 2e 33 20 7b 0a 20 20  alter2-10.3 {.  
2f10: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
2f20: 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 32    DELETE FROM t2
2f30: 20 57 48 45 52 45 20 61 20 3d 20 27 63 27 3b 0a   WHERE a = 'c';.
2f40: 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 20 46        SELECT a F
2f50: 52 4f 4d 20 74 32 20 57 48 45 52 45 20 62 20 3d  ROM t2 WHERE b =
2f60: 20 58 27 41 42 43 44 27 3b 0a 20 20 20 20 7d 0a   X'ABCD';.    }.
2f70: 20 20 7d 20 7b 61 20 62 20 64 7d 0a 20 20 64 6f    } {a b d}.  do
2f80: 5f 74 65 73 74 20 61 6c 74 65 72 32 2d 31 30 2e  _test alter2-10.
2f90: 34 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20  4 {.    execsql 
2fa0: 7b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20 63  {.      SELECT c
2fb0: 6f 75 6e 74 28 62 29 20 46 52 4f 4d 20 74 32 20  ount(b) FROM t2 
2fc0: 57 48 45 52 45 20 62 20 3d 20 58 27 41 42 43 44  WHERE b = X'ABCD
2fd0: 27 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 33 7d  ';.    }.  } {3}
2fe0: 0a 7d 0a 0a 66 69 6e 69 73 68 5f 74 65 73 74 0a  .}..finish_test.