xref: /sqlite-3.40.0/test/vacuum5.test (revision 75ba6767)
19ef5e770Sdrh# 2016-08-19
29ef5e770Sdrh#
39ef5e770Sdrh# The author disclaims copyright to this source code.  In place of
49ef5e770Sdrh# a legal notice, here is a blessing:
59ef5e770Sdrh#
69ef5e770Sdrh#    May you do good and not evil.
79ef5e770Sdrh#    May you find forgiveness for yourself and forgive others.
89ef5e770Sdrh#    May you share freely, never taking more than you give.
99ef5e770Sdrh#
109ef5e770Sdrh#***********************************************************************
119ef5e770Sdrh#
129ef5e770Sdrh# This file implements a test for VACUUM on attached databases.
139ef5e770Sdrh#
149ef5e770Sdrh
159ef5e770Sdrhset testdir [file dirname $argv0]
169ef5e770Sdrhsource $testdir/tester.tcl
17*75ba6767Sdanset testprefix vacuum5
189ef5e770Sdrh
199ef5e770Sdrh# If the VACUUM statement is disabled in the current build, skip all
209ef5e770Sdrh# the tests in this file.
219ef5e770Sdrh#
229ef5e770Sdrhifcapable !vacuum {
239ef5e770Sdrh  finish_test
249ef5e770Sdrh  return
259ef5e770Sdrh}
269ef5e770Sdrh
279ef5e770Sdrhforcedelete test2.db test3.db
289ef5e770Sdrhdo_execsql_test vacuum5-1.1 {
29b6ea12ccSdan  PRAGMA auto_vacuum = 0;
309ef5e770Sdrh  CREATE TABLE main.t1(a,b);
319ef5e770Sdrh  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000)
329ef5e770Sdrh    INSERT INTO t1(a,b) SELECT x, randomblob(1000) FROM c;
339ef5e770Sdrh  CREATE TEMP TABLE ttemp(x,y);
349ef5e770Sdrh  INSERT INTO ttemp SELECT * FROM t1;
359ef5e770Sdrh  ATTACH 'test2.db' AS x2;
369ef5e770Sdrh  ATTACH 'test3.db' AS x3;
379ef5e770Sdrh  CREATE TABLE x2.t2(c,d);
389ef5e770Sdrh  INSERT INTO t2 SELECT * FROM t1;
399ef5e770Sdrh  CREATE TABLE x3.t3(e,f);
409ef5e770Sdrh  INSERT INTO t3 SELECT * FROM t1;
419ef5e770Sdrh  DELETE FROM t1 WHERE (rowid%3)!=0;
429ef5e770Sdrh  DELETE FROM t2 WHERE (rowid%4)!=0;
439ef5e770Sdrh  DELETE FROM t3 WHERE (rowid%5)!=0;
449ef5e770Sdrh  PRAGMA main.integrity_check;
459ef5e770Sdrh  PRAGMA x2.integrity_check;
469ef5e770Sdrh  PRAGMA x3.integrity_check;
479ef5e770Sdrh} {ok ok ok}
489ef5e770Sdrhset size1 [file size test.db]
499ef5e770Sdrhset size2 [file size test2.db]
509ef5e770Sdrhset size3 [file size test3.db]
519ef5e770Sdrh
529ef5e770Sdrhdo_execsql_test vacuum5-1.2.1 {
539ef5e770Sdrh  VACUUM main;
549ef5e770Sdrh} {}
559ef5e770Sdrhdo_test vacuum5-1.2.2 {
569ef5e770Sdrh  expr {[file size test.db]<$size1}
579ef5e770Sdrh} {1}
589ef5e770Sdrhdo_test vacuum5-1.2.3 {
599ef5e770Sdrh  file size test2.db
609ef5e770Sdrh} $size2
619ef5e770Sdrhdo_test vacuum5-1.2.4 {
629ef5e770Sdrh  file size test3.db
639ef5e770Sdrh} $size3
649ef5e770Sdrhset size1 [file size test.db]
659ef5e770Sdrhdo_execsql_test vacuum-1.2.5 {
669ef5e770Sdrh  DELETE FROM t1;
679ef5e770Sdrh  PRAGMA main.integrity_check;
689ef5e770Sdrh} {ok}
699ef5e770Sdrh
709ef5e770Sdrhdo_execsql_test vacuum5-1.3.1 {
719ef5e770Sdrh  VACUUM x2;
729ef5e770Sdrh} {}
739ef5e770Sdrhdo_test vacuum5-1.3.2 {
749ef5e770Sdrh  file size test.db
759ef5e770Sdrh} $size1
769ef5e770Sdrhdo_test vacuum5-1.3.3 {
779ef5e770Sdrh  expr {[file size test2.db]<$size2}
789ef5e770Sdrh} 1
799ef5e770Sdrhdo_test vacuum5-1.3.4 {
809ef5e770Sdrh  file size test3.db
819ef5e770Sdrh} $size3
829ef5e770Sdrhset size2 [file size test2.db]
839ef5e770Sdrhdo_execsql_test vacuum-1.3.5 {
849ef5e770Sdrh  DELETE FROM t2;
859ef5e770Sdrh  PRAGMA x2.integrity_check;
869ef5e770Sdrh} {ok}
879ef5e770Sdrh
889ef5e770Sdrhdo_execsql_test vacuum5-1.4.1 {
899ef5e770Sdrh  VACUUM x3;
909ef5e770Sdrh} {}
919ef5e770Sdrhdo_test vacuum5-1.3.2 {
929ef5e770Sdrh  file size test.db
939ef5e770Sdrh} $size1
949ef5e770Sdrhdo_test vacuum5-1.3.3 {
959ef5e770Sdrh  file size test2.db
969ef5e770Sdrh} $size2
979ef5e770Sdrhdo_test vacuum5-1.3.4 {
989ef5e770Sdrh  expr {[file size test3.db]<$size3}
999ef5e770Sdrh} 1
1009ef5e770Sdrh
1019ef5e770Sdrh# VACUUM is a no-op on the TEMP table
1029ef5e770Sdrh#
1039ef5e770Sdrhset sizeTemp [db one {PRAGMA temp.page_count}]
1049ef5e770Sdrhdo_execsql_test vacuum5-1.4.1 {
1059ef5e770Sdrh  VACUUM temp;
1069ef5e770Sdrh} {}
1079ef5e770Sdrhdo_execsql_test vacuum5-1.4.2 {
1089ef5e770Sdrh  PRAGMA temp.page_count;
1099ef5e770Sdrh} $sizeTemp
1109ef5e770Sdrh
1119ef5e770Sdrhdo_catchsql_test vacuum5-2.0 {
1129ef5e770Sdrh  VACUUM olaf;
1139ef5e770Sdrh} {1 {unknown database olaf}}
114ff02e200Sdan
115*75ba6767Sdan#-------------------------------------------------------------------------
116*75ba6767Sdan# Test that a temp file is opened as part of VACUUM.
117*75ba6767Sdan#
118*75ba6767Sdanif {$::TEMP_STORE<3} {
119*75ba6767Sdan  db close
120*75ba6767Sdan  testvfs tvfs
121*75ba6767Sdan  tvfs filter xOpen
122*75ba6767Sdan  tvfs script open_cb
123*75ba6767Sdan  forcedelete test.db
124*75ba6767Sdan
125*75ba6767Sdan  set ::openfiles [list]
126*75ba6767Sdan  proc open_cb {method args} {
127*75ba6767Sdan    lappend ::openfiles [file tail [lindex $args 0]]
128*75ba6767Sdan  }
129*75ba6767Sdan  sqlite3 db test.db -vfs tvfs
130*75ba6767Sdan
131*75ba6767Sdan  do_execsql_test 3.0 {
132*75ba6767Sdan    PRAGMA page_size = 1024;
133*75ba6767Sdan    PRAGMA cache_size = 50;
134*75ba6767Sdan    CREATE TABLE t1(i INTEGER PRIMARY KEY, j UNIQUE);
135*75ba6767Sdan    WITH s(i) AS (
136*75ba6767Sdan      VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000
137*75ba6767Sdan    )
138*75ba6767Sdan    INSERT INTO t1 SELECT NULL, randomblob(100) FROM s;
139*75ba6767Sdan  }
140*75ba6767Sdan
141*75ba6767Sdan  do_execsql_test 3.1 { VACUUM }
142*75ba6767Sdan
143*75ba6767Sdan  db close
144*75ba6767Sdan  tvfs delete
145*75ba6767Sdan  do_test 3.2 {
146*75ba6767Sdan    set ::openfiles
147*75ba6767Sdan  } {test.db test.db-journal test.db-journal {} test.db-journal}
148*75ba6767Sdan}
149*75ba6767Sdan
150*75ba6767Sdan
151*75ba6767Sdan
152ff02e200Sdanfinish_test
153