# 2019-01-11
#
# 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
set testprefix altercorrupt

# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
ifcapable !altertable {
  finish_test
  return
}

database_may_be_corrupt

#--------------------------------------------------------------------------
reset_db
do_test 1.0 {
  sqlite3 db {}
  db deserialize [decode_hexdb {
.open --hexdb
| size 24576 pagesize 4096 filename crash-685346d89b5e5f.db
| page 1 offset 0
|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06   .....@  ........
|     32: 00 00 63 00 00 05 f0 00 00 00 00 04 10 00 00 04   ..c.............
|     48: 00 00 00 00 00 00 0f f0 00 00 00 01 00 00 00 00   ................
|     64: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
|     96: 00 00 00 00 0d 0f f8 00 05 0e cf 00 0f 79 0f d3   .............y..
|    112: 0f 2e 0e f3 0e cf 00 00 00 00 00 00 00 00 00 00   ................
|   3776: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22   ................
|   3792: 05 06 17 11 11 01 31 74 61 62 6c 65 74 34 74 34   ......1tablet4t4
|   3808: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 34   .CREATE TABLE t4
|   3824: 28 7a 29 39 04 06 17 11 11 01 5f 74 61 62 6c 65   (z)9......_table
|   3840: 74 33 74 33 05 43 52 45 41 54 45 20 54 41 42 4c   t3t3.CREATE TABL
|   3856: 45 20 74 33 28 78 20 49 4e 54 45 47 45 52 20 50   E t3(x INTEGER P
|   3872: 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 49 03   RIMARY KEY, y)I.
|   3888: 06 17 11 11 01 7f 74 61 62 6c 65 74 32 74 32 04   ......tablet2t2.
|   3904: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 28   CREATE TABLE t2(
|   3920: 61 2c 62 2c 63 20 50 52 49 4d 41 52 59 20 4b 45   a,b,c PRIMARY KE
|   3936: 59 2c 20 64 2c 20 65 2c 20 66 29 20 57 49 54 48   Y, d, e, f) WITH
|   3952: 4f 55 54 20 52 4f 57 49 44 58 03 07 17 11 11 01   OUT ROWIDX......
|   3968: 81 1b 74 61 62 6c 65 74 31 74 31 02 43 52 45 41   ..tablet1t1.CREA
|   3984: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 2c   TE TABLE t1(a,b,
|   4000: 63 20 41 53 20 28 2d 62 29 20 56 49 52 54 55 41   c AS (-b) VIRTUA
|   4016: 4c 2c 64 20 43 48 45 43 4b 28 64 3e 35 29 2c 65   L,d CHECK(d>5),e
|   4032: 20 55 4e 49 51 55 45 2c 20 66 20 41 53 20 28 2b    UNIQUE, f AS (+
|   4048: 62 29 29 23 02 06 17 37 11 01 00 69 6e 64 65 78   b))#...7...index
|   4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78   sqlite_autoindex
|   4080: 5f 74 31 5f 31 74 31 03 00 00 00 08 00 00 00 00   _t1_1t1.........
| page 2 offset 4096
|      0: 0d 00 00 00 0a 0f 93 00 0f f6 0f eb 0f e0 0f d5   ................
|     16: 0f ca 0f 8f 0f b4 0f a9 0f 9e 0f 93 00 00 00 00   ................
|   3984: 00 00 00 09 0a 05 01 01 01 01 0a 64 6e 14 09 09   ...........dn...
|   4000: 05 01 01 01 01 09 5a 6d 12 09 08 05 01 01 01 01   ......Zm........
|   4016: 08 50 6c 10 09 07 05 01 01 01 01 07 46 6b 0e 09   .Pl.........Fk..
|   4032: 06 05 01 01 01 01 06 3c 6a 0c 09 05 05 01 01 01   .......<j.......
|   4048: 01 05 32 69 0a 09 04 05 01 01 01 01 04 28 68 08   ..2i.........(h.
|   4064: 09 03 05 01 01 01 01 03 1e 67 06 09 02 05 01 01   .........g......
|   4080: 01 01 02 14 66 04 08 01 05 09 01 01 01 0a 65 02   ....f.........e.
| page 3 offset 8192
|      0: 0a 00 00 00 0a 0f c5 00 0f fb 0f f5 0f ef 0f e9   ................
|     16: 0f e3 0f dd 0f d7 0f d1 0f cb 0f c5 00 00 00 00   ................
|   4032: 00 00 00 00 00 05 03 01 01 14 0a 05 03 01 01 12   ................
|   4048: 09 05 03 01 01 10 08 05 03 01 01 0e 07 05 03 01   ................
|   4064: 01 0c 06 05 03 01 01 0a 05 05 03 01 01 08 04 05   ................
|   4080: 03 01 01 06 03 05 03 01 01 04 02 04 03 01 09 02   ................
| page 4 offset 12288
|      0: 0a 00 00 00 0a 0f 75 00 0f 75 0f 83 0f 91 0f 9f   ......u..u......
|     16: 0f ad 0f bb 0f 00 00 00 00 00 00 00 00 00 00 00   ................
|   3952: 00 00 00 00 00 0d 07 01 01 01 01 01 01 9c 0a 64   ...............d
|   3968: 6e 14 64 0d 07 02 01 01 01 01 01 a6 09 5a 6d 12   n.d..........Zm.
|   3984: 5a 0d 07 01 01 01 01 01 01 b0 08 50 6c 10 50 0d   Z..........Pl.P.
|   4000: 07 01 01 01 01 01 01 ba 07 46 6b 0e 46 0d 07 01   .........Fk.F...
|   4016: 01 01 01 01 01 c4 06 3c 6a 0c 3c 0d 07 01 01 01   .......<j.<.....
|   4032: 01 01 01 ce 05 32 69 0a 32 0d 07 01 01 01 01 01   .....2i.2.......
|   4048: 01 d8 04 28 68 08 28 0d 07 01 01 01 01 01 01 e2   ...(h.(.........
|   4064: 03 1e 67 06 1e 0d 07 01 01 01 01 01 01 ec 02 14   ..g.............
|   4080: 66 04 14 0c 07 01 09 01 01 01 01 f6 0a 65 02 0a   f............e..
| page 5 offset 16384
|      0: 0d 00 00 00 03 0f e9 00 0f e9 0f fb 0f f6 00 00   ................
|     16: 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
|   4064: 00 00 00 00 00 00 00 00 00 03 ff ff ff ff ff ff   ................
|   4080: ff ff 9c 03 00 00 03 64 03 00 00 03 01 03 00 00   .......d........
| page 6 offset 20480
|      0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00   ................
| end crash-685346d89b5e5f.db
}]} {}

do_catchsql_test 1.1 {
  ALTER TABLE t2 DROP COLUMN e;
  ALTER TABLE t1 DROP COLUMN f;
} {1 {database disk image is malformed}}


#--------------------------------------------------------------------------
reset_db
do_test 2.0 {
  sqlite3 db {}
  db deserialize [decode_hexdb {
.open --hexdb
| size 24576 pagesize 4096 filename crash-0572db8f391431.db
| page 1 offset 0
|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06   .....@  ........
|     32: 00 00 63 00 10 05 f0 00 00 00 00 04 10 00 00 04   ..c.............
|     48: 00 00 00 00 00 00 0f f0 00 00 00 00 00 00 00 00   ................
|     96: 00 00 00 00 0d 0f f8 00 05 0e cf 00 0f 79 0f d3   .............y..
|    112: 0f 2e 0e f3 0e cf 00 00 00 00 00 00 00 00 00 00   ................
|   3776: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22   ................
|   3792: 05 06 17 11 11 01 31 74 61 62 6c 65 74 34 74 34   ......1tablet4t4
|   3808: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 34   .CREATE TABLE t4
|   3824: 28 7a 29 39 04 06 17 11 11 01 5f 74 61 62 6c 65   (z)9......_table
|   3840: 74 33 74 33 05 43 52 45 41 54 45 20 54 41 42 4c   t3t3.CREATE TABL
|   3856: 45 20 74 33 28 78 20 49 4e 54 55 47 45 52 20 50   E t3(x INTUGER P
|   3872: 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 49 03   RIMARY KEY, y)I.
|   3888: 06 17 11 11 01 7f 74 61 62 6c 65 74 32 74 32 04   ......tablet2t2.
|   3904: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 28   CREATE TABLE t2(
|   3920: 61 2c 62 2c 63 20 50 52 49 4d 41 52 59 20 4b 45   a,b,c PRIMARY KE
|   3936: 59 2c 20 64 2c 20 65 2c 20 66 29 20 57 49 54 48   Y, d, e, f) WITH
|   3952: 4f 55 54 20 52 4f 57 49 44 58 05 07 17 11 11 01   OUT ROWIDX......
|   3968: 81 1b 74 61 62 6c 65 74 31 74 31 02 43 52 45 41   ..tablet1t1.CREA
|   3984: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 2c   TE TABLE t1(a,b,
|   4000: 63 20 41 53 20 28 2d 62 29 20 56 49 52 54 55 41   c AS (-b) VIRTUA
|   4016: 4c 2c 64 20 43 48 45 43 4b 28 64 3e 35 29 2c 65   L,d CHECK(d>5),e
|   4032: 20 55 4e 49 51 55 45 2c 20 66 20 41 53 20 28 2b    UNIQUE, f AS (+
|   4048: 62 29 29 23 02 06 17 37 11 01 00 69 6e 64 65 78   b))#...7...index
|   4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78   sqlite_autoindex
|   4080: 5f 74 31 5f 31 84 31 03 01 00 00 08 00 00 00 00   _t1_1.1.........
| page 2 offset 4096
|      0: 0d 00 00 00 0a 0f 93 00 0f f6 0f eb 0f e0 0f d5   ................
|     16: 0f ca 0f 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
|   3984: 00 00 00 09 0a 05 01 01 01 01 0a 64 6e 14 09 09   ...........dn...
|   4000: 05 01 01 01 01 09 5a 6d 12 09 08 05 01 00 f1 01   ......Zm........
|   4016: 08 50 6c 10 09 07 05 01 01 01 01 07 46 6b 0e 09   .Pl.........Fk..
|   4032: 06 05 01 00 f1 01 06 3c 6a 0c 09 05 05 01 01 01   .......<j.......
|   4048: 01 05 32 69 0a 09 04 05 01 01 01 01 04 28 68 08   ..2i.........(h.
|   4064: 57 03 05 01 01 01 01 03 1e 67 06 09 02 05 01 01   W........g......
|   4080: 01 01 02 14 66 04 08 01 05 09 01 01 01 0a 65 02   ....f.........e.
| page 3 offset 8192
|      0: 09 ff ff ff fa 0f c5 00 0f fb 0f f5 0f ef 0f e9   ................
|     16: 0f e3 0f dd 0f d7 00 00 00 00 00 00 00 00 00 00   ................
|   4032: 00 00 00 00 00 05 03 01 01 14 0a 05 03 01 01 12   ................
|   4048: 09 05 03 01 01 10 08 05 03 01 01 0e 07 05 03 01   ................
|   4064: 01 0c 06 05 03 01 01 0a 05 05 03 01 01 08 04 05   ................
|   4080: 03 01 01 06 03 05 03 01 01 04 02 04 03 01 09 02   ................
| page 4 offset 12288
|      0: 0a 00 00 00 0a 0f 75 00 0f 75 0f 83 0f 91 0f 9f   ......u..u......
|     16: 0f ad 0f bb 0f 00 00 00 00 00 01 00 00 00 00 00   ................
|   3952: 00 00 00 00 00 0d 07 01 01 01 01 01 01 9c 0a 64   ...............d
|   3968: 6e 14 64 0d 07 02 01 01 01 01 01 a6 09 5a 6d 12   n.d..........Zm.
|   3984: 5a 0d 07 01 01 01 01 d4 01 b0 08 50 6c 10 50 0d   Z..........Pl.P.
|   4000: 07 01 01 01 01 01 01 ba 07 46 6b 0e 46 0d 07 00   .........Fk.F...
|   4016: 01 01 01 01 01 c4 06 3c 6a 0c 3c 0d 07 01 01 01   .......<j.<.....
|   4032: 01 01 01 ce 05 32 69 0a 32 0d 07 01 01 01 01 01   .....2i.2.......
|   4048: 01 d8 04 28 68 08 28 0d 07 01 01 01 01 01 01 e2   ...(h.(.........
|   4064: 03 1e 67 06 1e 0d 07 01 01 01 01 01 01 ec 02 14   ..g.............
|   4080: 66 04 14 0c 07 01 09 01 01 00 f1 f6 0a 65 02 0a   f............e..
| page 5 offset 16384
|      0: 0d 00 00 00 03 0f e9 00 0f e9 0f fb 0f f6 00 00   ................
|   4064: 00 00 00 00 00 00 00 00 00 03 ff ff ff ff ff ff   ................
|   4080: ff ff 9c 03 00 00 03 64 03 00 01 03 01 03 00 00   .......d........
| page 6 offset 20480
|      0: 0d 00 10 00 00 10 01 00 00 00 00 00 00 00 00 00   ................
| end crash-0572db8f391431.db
}]} {}

do_catchsql_test 2.1 {
  ALTER TABLE t1 DROP COLUMN a;
} {1 {database disk image is malformed}}

finish_test