sl@0: # 2001 September 15
sl@0: #
sl@0: # The author disclaims copyright to this source code.  In place of
sl@0: # a legal notice, here is a blessing:
sl@0: #
sl@0: #    May you do good and not evil.
sl@0: #    May you find forgiveness for yourself and forgive others.
sl@0: #    May you share freely, never taking more than you give.
sl@0: #
sl@0: #***********************************************************************
sl@0: # This file implements regression tests for SQLite library.  The
sl@0: # focus of this file is testing the SELECT statement.
sl@0: #
sl@0: # $Id: autovacuum.test,v 1.28 2008/09/10 10:57:28 danielk1977 Exp $
sl@0: 
sl@0: set testdir [file dirname $argv0]
sl@0: source $testdir/tester.tcl
sl@0: 
sl@0: # If this build of the library does not support auto-vacuum, omit this
sl@0: # whole file.
sl@0: ifcapable {!autovacuum || !pragma} {
sl@0:   finish_test
sl@0:   return
sl@0: }
sl@0: 
sl@0: # Return a string $len characters long. The returned string is $char repeated
sl@0: # over and over. For example, [make_str abc 8] returns "abcabcab".
sl@0: proc make_str {char len} {
sl@0:   set str [string repeat $char. $len]
sl@0:   return [string range $str 0 [expr $len-1]]
sl@0: }
sl@0: 
sl@0: # Return the number of pages in the file test.db by looking at the file system.
sl@0: proc file_pages {} {
sl@0:   return [expr [file size test.db] / 1024]
sl@0: }
sl@0: 
sl@0: #-------------------------------------------------------------------------
sl@0: # Test cases autovacuum-1.* work as follows:
sl@0: #
sl@0: # 1. A table with a single indexed field is created.
sl@0: # 2. Approximately 20 rows are inserted into the table. Each row is long 
sl@0: #    enough such that it uses at least 2 overflow pages for both the table 
sl@0: #    and index entry.
sl@0: # 3. The rows are deleted in a psuedo-random order. Sometimes only one row
sl@0: #    is deleted per transaction, sometimes more than one.
sl@0: # 4. After each transaction the table data is checked to ensure it is correct
sl@0: #    and a "PRAGMA integrity_check" is executed.
sl@0: # 5. Once all the rows are deleted the file is checked to make sure it 
sl@0: #    consists of exactly 4 pages.
sl@0: #
sl@0: # Steps 2-5 are repeated for a few different psuedo-random delete patterns 
sl@0: # (defined by the $delete_orders list).
sl@0: set delete_orders [list]
sl@0: lappend delete_orders {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
sl@0: lappend delete_orders {20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1} 
sl@0: lappend delete_orders {8 18 2 4 14 11 13 3 10 7 9 5 12 17 19 15 20 6 16 1}
sl@0: lappend delete_orders {10 3 11 17 19 20 7 4 13 6 1 14 16 12 9 18 8 15 5 2}
sl@0: lappend delete_orders {{1 2 3 4 5 6 7 8 9 10} {11 12 13 14 15 16 17 18 19 20}}
sl@0: lappend delete_orders {{19 8 17 15} {16 11 9 14} {18 5 3 1} {13 20 7 2} {6 12}}
sl@0: 
sl@0: # The length of each table entry. 
sl@0: # set ENTRY_LEN 3500
sl@0: set ENTRY_LEN 3500
sl@0: 
sl@0: do_test autovacuum-1.1 {
sl@0:   execsql {
sl@0:     PRAGMA auto_vacuum = 1;
sl@0:     CREATE TABLE av1(a);
sl@0:     CREATE INDEX av1_idx ON av1(a);
sl@0:   }
sl@0: } {}
sl@0: 
sl@0: set tn 0
sl@0: foreach delete_order $delete_orders {
sl@0:   incr tn
sl@0: 
sl@0:   # Set up the table.
sl@0:   set ::tbl_data [list]
sl@0:   foreach i [lsort -integer [eval concat $delete_order]] {
sl@0:     execsql "INSERT INTO av1 (oid, a) VALUES($i, '[make_str $i $ENTRY_LEN]')"
sl@0:     lappend ::tbl_data [make_str $i $ENTRY_LEN]
sl@0:   }
sl@0: 
sl@0:   # Make sure the integrity check passes with the initial data.
sl@0:   ifcapable {integrityck} {
sl@0:     do_test autovacuum-1.$tn.1 {
sl@0:       execsql {
sl@0:         pragma integrity_check
sl@0:       }
sl@0:     } {ok}
sl@0:   }
sl@0: 
sl@0:   foreach delete $delete_order {
sl@0:     # Delete one set of rows from the table.
sl@0:     do_test autovacuum-1.$tn.($delete).1 {
sl@0:       execsql "
sl@0:         DELETE FROM av1 WHERE oid = [join $delete " OR oid = "]
sl@0:       "
sl@0:     } {}
sl@0: 
sl@0:     # Do the integrity check.
sl@0:     ifcapable {integrityck} {
sl@0:       do_test autovacuum-1.$tn.($delete).2 {
sl@0:         execsql {
sl@0:           pragma integrity_check
sl@0:         }
sl@0:       } {ok}
sl@0:     }
sl@0:     # Ensure the data remaining in the table is what was expected.
sl@0:     foreach d $delete {
sl@0:       set idx [lsearch $::tbl_data [make_str $d $ENTRY_LEN]]
sl@0:       set ::tbl_data [lreplace $::tbl_data $idx $idx]
sl@0:     }
sl@0:     do_test autovacuum-1.$tn.($delete).3 {
sl@0:       execsql {
sl@0:         select a from av1
sl@0:       }
sl@0:     } $::tbl_data
sl@0:   }
sl@0: 
sl@0:   # All rows have been deleted. Ensure the file has shrunk to 4 pages.
sl@0:   do_test autovacuum-1.$tn.3 {
sl@0:     file_pages
sl@0:   } {4}
sl@0: }
sl@0: 
sl@0: #---------------------------------------------------------------------------
sl@0: # Tests cases autovacuum-2.* test that root pages are allocated 
sl@0: # and deallocated correctly at the start of the file. Operation is roughly as
sl@0: # follows:
sl@0: #
sl@0: # autovacuum-2.1.*: Drop the tables that currently exist in the database.
sl@0: # autovacuum-2.2.*: Create some tables. Ensure that data pages can be
sl@0: #                   moved correctly to make space for new root-pages.
sl@0: # autovacuum-2.3.*: Drop one of the tables just created (not the last one),
sl@0: #                   and check that one of the other tables is moved to
sl@0: #                   the free root-page location.
sl@0: # autovacuum-2.4.*: Check that a table can be created correctly when the
sl@0: #                   root-page it requires is on the free-list.
sl@0: # autovacuum-2.5.*: Check that a table with indices can be dropped. This
sl@0: #                   is slightly tricky because dropping one of the
sl@0: #                   indices/table btrees could move the root-page of another.
sl@0: #                   The code-generation layer of SQLite overcomes this problem
sl@0: #                   by dropping the btrees in descending order of root-pages.
sl@0: #                   This test ensures that this actually happens.
sl@0: #
sl@0: do_test autovacuum-2.1.1 {
sl@0:   execsql {
sl@0:     DROP TABLE av1;
sl@0:   }
sl@0: } {}
sl@0: do_test autovacuum-2.1.2 {
sl@0:   file_pages
sl@0: } {1}
sl@0: 
sl@0: # Create a table and put some data in it.
sl@0: do_test autovacuum-2.2.1 {
sl@0:   execsql {
sl@0:     CREATE TABLE av1(x);
sl@0:     SELECT rootpage FROM sqlite_master ORDER BY rootpage;
sl@0:   }
sl@0: } {3}
sl@0: do_test autovacuum-2.2.2 {
sl@0:   execsql "
sl@0:     INSERT INTO av1 VALUES('[make_str abc 3000]');
sl@0:     INSERT INTO av1 VALUES('[make_str def 3000]');
sl@0:     INSERT INTO av1 VALUES('[make_str ghi 3000]');
sl@0:     INSERT INTO av1 VALUES('[make_str jkl 3000]');
sl@0:   "
sl@0:   set ::av1_data [db eval {select * from av1}]
sl@0:   file_pages
sl@0: } {15}
sl@0: 
sl@0: # Create another table. Check it is located immediately after the first.
sl@0: # This test case moves the second page in an over-flow chain.
sl@0: do_test autovacuum-2.2.3 {
sl@0:   execsql {
sl@0:     CREATE TABLE av2(x);
sl@0:     SELECT rootpage FROM sqlite_master ORDER BY rootpage;
sl@0:   }
sl@0: } {3 4}
sl@0: do_test autovacuum-2.2.4 {
sl@0:   file_pages
sl@0: } {16}
sl@0: 
sl@0: # Create another table. Check it is located immediately after the second.
sl@0: # This test case moves the first page in an over-flow chain.
sl@0: do_test autovacuum-2.2.5 {
sl@0:   execsql {
sl@0:     CREATE TABLE av3(x);
sl@0:     SELECT rootpage FROM sqlite_master ORDER BY rootpage;
sl@0:   }
sl@0: } {3 4 5}
sl@0: do_test autovacuum-2.2.6 {
sl@0:   file_pages
sl@0: } {17}
sl@0: 
sl@0: # Create another table. Check it is located immediately after the second.
sl@0: # This test case moves a btree leaf page.
sl@0: do_test autovacuum-2.2.7 {
sl@0:   execsql {
sl@0:     CREATE TABLE av4(x);
sl@0:     SELECT rootpage FROM sqlite_master ORDER BY rootpage;
sl@0:   }
sl@0: } {3 4 5 6}
sl@0: do_test autovacuum-2.2.8 {
sl@0:   file_pages
sl@0: } {18}
sl@0: do_test autovacuum-2.2.9 {
sl@0:   execsql {
sl@0:     select * from av1
sl@0:   }
sl@0: } $av1_data
sl@0: 
sl@0: do_test autovacuum-2.3.1 {
sl@0:   execsql {
sl@0:     INSERT INTO av2 SELECT 'av1' || x FROM av1;
sl@0:     INSERT INTO av3 SELECT 'av2' || x FROM av1;
sl@0:     INSERT INTO av4 SELECT 'av3' || x FROM av1;
sl@0:   }
sl@0:   set ::av2_data [execsql {select x from av2}]
sl@0:   set ::av3_data [execsql {select x from av3}]
sl@0:   set ::av4_data [execsql {select x from av4}]
sl@0:   file_pages
sl@0: } {54}
sl@0: do_test autovacuum-2.3.2 {
sl@0:   execsql {
sl@0:     DROP TABLE av2;
sl@0:     SELECT rootpage FROM sqlite_master ORDER BY rootpage;
sl@0:   }
sl@0: } {3 4 5}
sl@0: do_test autovacuum-2.3.3 {
sl@0:   file_pages
sl@0: } {41}
sl@0: do_test autovacuum-2.3.4 {
sl@0:   execsql {
sl@0:     SELECT x FROM av3;
sl@0:   }
sl@0: } $::av3_data
sl@0: do_test autovacuum-2.3.5 {
sl@0:   execsql {
sl@0:     SELECT x FROM av4;
sl@0:   }
sl@0: } $::av4_data
sl@0: 
sl@0: # Drop all the tables in the file. This puts all pages except the first 2
sl@0: # (the sqlite_master root-page and the first pointer map page) on the 
sl@0: # free-list.
sl@0: do_test autovacuum-2.4.1 {
sl@0:   execsql {
sl@0:     DROP TABLE av1;
sl@0:     DROP TABLE av3;
sl@0:     BEGIN;
sl@0:     DROP TABLE av4;
sl@0:   }
sl@0:   file_pages
sl@0: } {15}
sl@0: do_test autovacuum-2.4.2 {
sl@0:   for {set i 3} {$i<=10} {incr i} {
sl@0:     execsql "CREATE TABLE av$i (x)"
sl@0:   }
sl@0:   file_pages
sl@0: } {15}
sl@0: do_test autovacuum-2.4.3 {
sl@0:   execsql {
sl@0:     SELECT rootpage FROM sqlite_master ORDER by rootpage
sl@0:   }
sl@0: } {3 4 5 6 7 8 9 10}
sl@0: 
sl@0: # Right now there are 5 free pages in the database. Consume and then free
sl@0: # a 520 pages. Then create 520 tables. This ensures that at least some of the
sl@0: # desired root-pages reside on the second free-list trunk page, and that the
sl@0: # trunk itself is required at some point.
sl@0: do_test autovacuum-2.4.4 {
sl@0:   execsql "
sl@0:     INSERT INTO av3 VALUES ('[make_str abcde [expr 1020*520 + 500]]');
sl@0:     DELETE FROM av3;
sl@0:   "
sl@0: } {}
sl@0: set root_page_list [list]
sl@0: set pending_byte_page [expr ($::sqlite_pending_byte / 1024) + 1]
sl@0: for {set i 3} {$i<=532} {incr i} {
sl@0:   # 207 and 412 are pointer-map pages.
sl@0:   if { $i!=207 && $i!=412 && $i != $pending_byte_page} {
sl@0:     lappend root_page_list $i
sl@0:   }
sl@0: }
sl@0: if {$i >= $pending_byte_page} {
sl@0:   lappend root_page_list $i
sl@0: }
sl@0: do_test autovacuum-2.4.5 {
sl@0:   for {set i 11} {$i<=530} {incr i} {
sl@0:     execsql "CREATE TABLE av$i (x)"
sl@0:   }
sl@0:   execsql {
sl@0:     SELECT rootpage FROM sqlite_master ORDER by rootpage
sl@0:   }
sl@0: } $root_page_list
sl@0: 
sl@0: # Just for fun, delete all those tables and see if the database is 1 page.
sl@0: do_test autovacuum-2.4.6 {
sl@0:   execsql COMMIT;
sl@0:   file_pages
sl@0: } [expr 561 + (($i >= $pending_byte_page)?1:0)]
sl@0: integrity_check autovacuum-2.4.6
sl@0: do_test autovacuum-2.4.7 {
sl@0:   execsql BEGIN
sl@0:   for {set i 3} {$i<=530} {incr i} {
sl@0:     execsql "DROP TABLE av$i"
sl@0:   }
sl@0:   execsql COMMIT
sl@0:   file_pages
sl@0: } 1
sl@0: 
sl@0: # Create some tables with indices to drop.
sl@0: do_test autovacuum-2.5.1 {
sl@0:   execsql {
sl@0:     CREATE TABLE av1(a PRIMARY KEY, b, c);
sl@0:     INSERT INTO av1 VALUES('av1 a', 'av1 b', 'av1 c');
sl@0: 
sl@0:     CREATE TABLE av2(a PRIMARY KEY, b, c);
sl@0:     CREATE INDEX av2_i1 ON av2(b);
sl@0:     CREATE INDEX av2_i2 ON av2(c);
sl@0:     INSERT INTO av2 VALUES('av2 a', 'av2 b', 'av2 c');
sl@0: 
sl@0:     CREATE TABLE av3(a PRIMARY KEY, b, c);
sl@0:     CREATE INDEX av3_i1 ON av3(b);
sl@0:     INSERT INTO av3 VALUES('av3 a', 'av3 b', 'av3 c');
sl@0: 
sl@0:     CREATE TABLE av4(a, b, c);
sl@0:     CREATE INDEX av4_i1 ON av4(a);
sl@0:     CREATE INDEX av4_i2 ON av4(b);
sl@0:     CREATE INDEX av4_i3 ON av4(c);
sl@0:     CREATE INDEX av4_i4 ON av4(a, b, c);
sl@0:     INSERT INTO av4 VALUES('av4 a', 'av4 b', 'av4 c');
sl@0:   }
sl@0: } {}
sl@0: 
sl@0: do_test autovacuum-2.5.2 {
sl@0:   execsql {
sl@0:     SELECT name, rootpage FROM sqlite_master;
sl@0:   }
sl@0: } [list av1 3  sqlite_autoindex_av1_1 4 \
sl@0:         av2 5  sqlite_autoindex_av2_1 6 av2_i1 7 av2_i2 8 \
sl@0:         av3 9 sqlite_autoindex_av3_1 10 av3_i1 11 \
sl@0:         av4 12 av4_i1 13 av4_i2 14 av4_i3 15 av4_i4 16 \
sl@0: ]
sl@0: 
sl@0: # The following 4 tests are SELECT queries that use the indices created.
sl@0: # If the root-pages in the internal schema are not updated correctly when
sl@0: # a table or indice is moved, these queries will fail. They are repeated
sl@0: # after each table is dropped (i.e. as test cases 2.5.*.[1..4]).
sl@0: do_test autovacuum-2.5.2.1 {
sl@0:   execsql {
sl@0:     SELECT * FROM av1 WHERE a = 'av1 a';
sl@0:   }
sl@0: } {{av1 a} {av1 b} {av1 c}}
sl@0: do_test autovacuum-2.5.2.2 {
sl@0:   execsql {
sl@0:     SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
sl@0:   }
sl@0: } {{av2 a} {av2 b} {av2 c}}
sl@0: do_test autovacuum-2.5.2.3 {
sl@0:   execsql {
sl@0:     SELECT * FROM av3 WHERE a = 'av3 a' AND b = 'av3 b';
sl@0:   }
sl@0: } {{av3 a} {av3 b} {av3 c}}
sl@0: do_test autovacuum-2.5.2.4 {
sl@0:   execsql {
sl@0:     SELECT * FROM av4 WHERE a = 'av4 a' AND b = 'av4 b' AND c = 'av4 c';
sl@0:   }
sl@0: } {{av4 a} {av4 b} {av4 c}}
sl@0: 
sl@0: # Drop table av3. Indices av4_i2, av4_i3 and av4_i4 are moved to fill the two
sl@0: # root pages vacated. The operation proceeds as:
sl@0: # Step 1: Delete av3_i1 (root-page 11). Move root-page of av4_i4 to page 11.
sl@0: # Step 2: Delete av3 (root-page 10). Move root-page of av4_i3 to page 10.
sl@0: # Step 3: Delete sqlite_autoindex_av1_3 (root-page 9). Move av4_i2 to page 9.
sl@0: do_test autovacuum-2.5.3 {
sl@0:   execsql {
sl@0:     DROP TABLE av3;
sl@0:     SELECT name, rootpage FROM sqlite_master;
sl@0:   }
sl@0: } [list av1 3  sqlite_autoindex_av1_1 4 \
sl@0:         av2 5  sqlite_autoindex_av2_1 6 av2_i1 7 av2_i2 8 \
sl@0:         av4 12 av4_i1 13 av4_i2 9 av4_i3 10 av4_i4 11 \
sl@0: ]
sl@0: do_test autovacuum-2.5.3.1 {
sl@0:   execsql {
sl@0:     SELECT * FROM av1 WHERE a = 'av1 a';
sl@0:   }
sl@0: } {{av1 a} {av1 b} {av1 c}}
sl@0: do_test autovacuum-2.5.3.2 {
sl@0:   execsql {
sl@0:     SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
sl@0:   }
sl@0: } {{av2 a} {av2 b} {av2 c}}
sl@0: do_test autovacuum-2.5.3.3 {
sl@0:   execsql {
sl@0:     SELECT * FROM av4 WHERE a = 'av4 a' AND b = 'av4 b' AND c = 'av4 c';
sl@0:   }
sl@0: } {{av4 a} {av4 b} {av4 c}}
sl@0: 
sl@0: # Drop table av1:
sl@0: # Step 1: Delete av1 (root page 4). Root-page of av4_i1 fills the gap.
sl@0: # Step 2: Delete sqlite_autoindex_av1_1 (root page 3). Move av4 to the gap.
sl@0: do_test autovacuum-2.5.4 {
sl@0:   execsql {
sl@0:     DROP TABLE av1;
sl@0:     SELECT name, rootpage FROM sqlite_master;
sl@0:   }
sl@0: } [list av2 5  sqlite_autoindex_av2_1 6 av2_i1 7 av2_i2 8 \
sl@0:         av4 3 av4_i1 4 av4_i2 9 av4_i3 10 av4_i4 11 \
sl@0: ]
sl@0: do_test autovacuum-2.5.4.2 {
sl@0:   execsql {
sl@0:     SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
sl@0:   }
sl@0: } {{av2 a} {av2 b} {av2 c}}
sl@0: do_test autovacuum-2.5.4.4 {
sl@0:   execsql {
sl@0:     SELECT * FROM av4 WHERE a = 'av4 a' AND b = 'av4 b' AND c = 'av4 c';
sl@0:   }
sl@0: } {{av4 a} {av4 b} {av4 c}}
sl@0: 
sl@0: # Drop table av4:
sl@0: # Step 1: Delete av4_i4.
sl@0: # Step 2: Delete av4_i3.
sl@0: # Step 3: Delete av4_i2.
sl@0: # Step 4: Delete av4_i1. av2_i2 replaces it.
sl@0: # Step 5: Delete av4. av2_i1 replaces it.
sl@0: do_test autovacuum-2.5.5 {
sl@0:   execsql {
sl@0:     DROP TABLE av4;
sl@0:     SELECT name, rootpage FROM sqlite_master;
sl@0:   }
sl@0: } [list av2 5 sqlite_autoindex_av2_1 6 av2_i1 3 av2_i2 4]
sl@0: do_test autovacuum-2.5.5.2 {
sl@0:   execsql {
sl@0:     SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
sl@0:   }
sl@0: } {{av2 a} {av2 b} {av2 c}}
sl@0: 
sl@0: #--------------------------------------------------------------------------
sl@0: # Test cases autovacuum-3.* test the operation of the "PRAGMA auto_vacuum"
sl@0: # command.
sl@0: #
sl@0: do_test autovacuum-3.1 {
sl@0:   execsql {
sl@0:     PRAGMA auto_vacuum;
sl@0:   }
sl@0: } {1}
sl@0: do_test autovacuum-3.2 {
sl@0:   db close
sl@0:   sqlite3 db test.db
sl@0:   execsql {
sl@0:     PRAGMA auto_vacuum;
sl@0:   }
sl@0: } {1}
sl@0: do_test autovacuum-3.3 {
sl@0:   execsql {
sl@0:     PRAGMA auto_vacuum = 0;
sl@0:     PRAGMA auto_vacuum;
sl@0:   }
sl@0: } {1}
sl@0: 
sl@0: do_test autovacuum-3.4 {
sl@0:   db close
sl@0:   file delete -force test.db
sl@0:   sqlite3 db test.db
sl@0:   execsql {
sl@0:     PRAGMA auto_vacuum;
sl@0:   }
sl@0: } $AUTOVACUUM
sl@0: do_test autovacuum-3.5 {
sl@0:   execsql {
sl@0:     CREATE TABLE av1(x);
sl@0:     PRAGMA auto_vacuum;
sl@0:   }
sl@0: } $AUTOVACUUM
sl@0: do_test autovacuum-3.6 {
sl@0:   execsql {
sl@0:     PRAGMA auto_vacuum = 1;
sl@0:     PRAGMA auto_vacuum;
sl@0:   }
sl@0: } [expr $AUTOVACUUM ? 1 : 0]
sl@0: do_test autovacuum-3.7 {
sl@0:   execsql {
sl@0:     DROP TABLE av1;
sl@0:   }
sl@0:   file_pages
sl@0: } [expr $AUTOVACUUM?1:2]
sl@0: 
sl@0: 
sl@0: #-----------------------------------------------------------------------
sl@0: # Test that if a statement transaction around a CREATE INDEX statement is
sl@0: # rolled back no corruption occurs.
sl@0: #
sl@0: do_test autovacuum-4.0 {
sl@0:   # The last round of tests may have left the db in non-autovacuum mode.
sl@0:   # Reset everything just in case.
sl@0:   #
sl@0:   db close
sl@0:   file delete -force test.db test.db-journal
sl@0:   sqlite3 db test.db
sl@0:   execsql {
sl@0:     PRAGMA auto_vacuum = 1;
sl@0:     PRAGMA auto_vacuum;
sl@0:   }
sl@0: } {1}
sl@0: do_test autovacuum-4.1 {
sl@0:   execsql {
sl@0:     CREATE TABLE av1(a, b);
sl@0:     BEGIN;
sl@0:   }
sl@0:   for {set i 0} {$i<100} {incr i} {
sl@0:     execsql "INSERT INTO av1 VALUES($i, '[string repeat X 200]');"
sl@0:   }
sl@0:   execsql "INSERT INTO av1 VALUES(99, '[string repeat X 200]');"
sl@0:   execsql {
sl@0:     SELECT sum(a) FROM av1;
sl@0:   }
sl@0: } {5049}
sl@0: do_test autovacuum-4.2 {
sl@0:   catchsql {
sl@0:     CREATE UNIQUE INDEX av1_i ON av1(a);
sl@0:   }
sl@0: } {1 {indexed columns are not unique}}
sl@0: do_test autovacuum-4.3 {
sl@0:   execsql {
sl@0:     SELECT sum(a) FROM av1;
sl@0:   }
sl@0: } {5049}
sl@0: do_test autovacuum-4.4 {
sl@0:   execsql {
sl@0:     COMMIT;
sl@0:   }
sl@0: } {}
sl@0: 
sl@0: ifcapable integrityck {
sl@0: 
sl@0: # Ticket #1727
sl@0: do_test autovacuum-5.1 {
sl@0:   db close
sl@0:   sqlite3 db :memory:
sl@0:   db eval {
sl@0:     PRAGMA auto_vacuum=1;
sl@0:     CREATE TABLE t1(a);
sl@0:     CREATE TABLE t2(a);
sl@0:     DROP TABLE t1;
sl@0:     PRAGMA integrity_check;
sl@0:   }
sl@0: } ok
sl@0: 
sl@0: }
sl@0: 
sl@0: # Ticket #1728.
sl@0: #
sl@0: # In autovacuum mode, when tables or indices are deleted, the rootpage
sl@0: # values in the symbol table have to be updated.  There was a bug in this
sl@0: # logic so that if an index/table was moved twice, the second move might
sl@0: # not occur.  This would leave the internal symbol table in an inconsistent
sl@0: # state causing subsequent statements to fail.
sl@0: #
sl@0: # The problem is difficult to reproduce.  The sequence of statements in
sl@0: # the following test are carefully designed make it occur and thus to
sl@0: # verify that this very obscure bug has been resolved.
sl@0: # 
sl@0: ifcapable integrityck&&memorydb {
sl@0: 
sl@0: do_test autovacuum-6.1 {
sl@0:   db close
sl@0:   sqlite3 db :memory:
sl@0:   db eval {
sl@0:     PRAGMA auto_vacuum=1;
sl@0:     CREATE TABLE t1(a, b);
sl@0:     CREATE INDEX i1 ON t1(a);
sl@0:     CREATE TABLE t2(a);
sl@0:     CREATE INDEX i2 ON t2(a);
sl@0:     CREATE TABLE t3(a);
sl@0:     CREATE INDEX i3 ON t2(a);
sl@0:     CREATE INDEX x ON t1(b);
sl@0:     DROP TABLE t3;
sl@0:     PRAGMA integrity_check;
sl@0:     DROP TABLE t2;
sl@0:     PRAGMA integrity_check;
sl@0:     DROP TABLE t1;
sl@0:     PRAGMA integrity_check;
sl@0:   }
sl@0: } {ok ok ok}
sl@0: 
sl@0: }
sl@0: 
sl@0: #---------------------------------------------------------------------
sl@0: # Test cases autovacuum-7.X test the case where a page must be moved
sl@0: # and the destination location collides with at least one other
sl@0: # entry in the page hash-table (internal to the pager.c module. 
sl@0: #
sl@0: do_test autovacuum-7.1 {
sl@0:   db close
sl@0:   file delete -force test.db
sl@0:   file delete -force test.db-journal
sl@0:   sqlite3 db test.db
sl@0: 
sl@0:   execsql {
sl@0:     PRAGMA auto_vacuum=1;
sl@0:     CREATE TABLE t1(a, b, PRIMARY KEY(a, b));
sl@0:     INSERT INTO t1 VALUES(randstr(400,400),randstr(400,400));
sl@0:     INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
sl@0:     INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 4
sl@0:     INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 8
sl@0:     INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 16
sl@0:     INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 32
sl@0:   }
sl@0: 
sl@0:   expr {[file size test.db] / 1024}
sl@0: } {73}
sl@0: 
sl@0: do_test autovacuum-7.2 {
sl@0:   execsql {
sl@0:     CREATE TABLE t2(a, b, PRIMARY KEY(a, b));
sl@0:     INSERT INTO t2 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
sl@0:     CREATE TABLE t3(a, b, PRIMARY KEY(a, b));
sl@0:     INSERT INTO t3 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
sl@0:     CREATE TABLE t4(a, b, PRIMARY KEY(a, b));
sl@0:     INSERT INTO t4 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
sl@0:     CREATE TABLE t5(a, b, PRIMARY KEY(a, b));
sl@0:     INSERT INTO t5 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
sl@0:   }
sl@0:   expr {[file size test.db] / 1024}
sl@0: } {354}
sl@0: 
sl@0: do_test autovacuum-7.3 {
sl@0:   db close
sl@0:   sqlite3 db test.db
sl@0:   execsql {
sl@0:     BEGIN;
sl@0:     DELETE FROM t4;
sl@0:     COMMIT;
sl@0:     SELECT count(*) FROM t1;
sl@0:   }
sl@0:   expr {[file size test.db] / 1024}
sl@0: } {286}
sl@0: 
sl@0: #------------------------------------------------------------------------
sl@0: # Additional tests.
sl@0: #
sl@0: # Try to determine the autovacuum setting for a database that is locked.
sl@0: #
sl@0: do_test autovacuum-8.1 {
sl@0:   db close
sl@0:   sqlite3 db test.db
sl@0:   sqlite3 db2 test.db
sl@0:   db eval {PRAGMA auto_vacuum}
sl@0: } {1}
sl@0: do_test autovacuum-8.2 {
sl@0:   db eval {BEGIN EXCLUSIVE}
sl@0:   catchsql {PRAGMA auto_vacuum} db2
sl@0: } {1 {database is locked}}
sl@0: catch {db2 close}
sl@0: catch {db eval {COMMIT}}
sl@0:     
sl@0: 
sl@0: finish_test