xref: /titanic_52/usr/src/lib/libsqlite/test/vacuum.test (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292
2*c5c4113dSnw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292# 2001 September 15
5*c5c4113dSnw141292#
6*c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
7*c5c4113dSnw141292# a legal notice, here is a blessing:
8*c5c4113dSnw141292#
9*c5c4113dSnw141292#    May you do good and not evil.
10*c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
11*c5c4113dSnw141292#    May you share freely, never taking more than you give.
12*c5c4113dSnw141292#
13*c5c4113dSnw141292#***********************************************************************
14*c5c4113dSnw141292# This file implements regression tests for SQLite library.  The
15*c5c4113dSnw141292# focus of this file is testing the VACUUM statement.
16*c5c4113dSnw141292#
17*c5c4113dSnw141292# $Id: vacuum.test,v 1.15 2004/02/14 16:31:04 drh Exp $
18*c5c4113dSnw141292
19*c5c4113dSnw141292set testdir [file dirname $argv0]
20*c5c4113dSnw141292source $testdir/tester.tcl
21*c5c4113dSnw141292
22*c5c4113dSnw141292proc cksum {{db db}} {
23*c5c4113dSnw141292  set txt [$db eval {SELECT name, type, sql FROM sqlite_master}]\n
24*c5c4113dSnw141292  foreach tbl [$db eval {SELECT name FROM sqlite_master WHERE type='table'}] {
25*c5c4113dSnw141292    append txt [$db eval "SELECT * FROM $tbl"]\n
26*c5c4113dSnw141292  }
27*c5c4113dSnw141292  foreach prag {default_synchronous default_cache_size} {
28*c5c4113dSnw141292    append txt $prag-[$db eval "PRAGMA $prag"]\n
29*c5c4113dSnw141292  }
30*c5c4113dSnw141292  set cksum [string length $txt]-[md5 $txt]
31*c5c4113dSnw141292  # puts $cksum-[file size test.db]
32*c5c4113dSnw141292  return $cksum
33*c5c4113dSnw141292}
34*c5c4113dSnw141292do_test vacuum-1.1 {
35*c5c4113dSnw141292  execsql {
36*c5c4113dSnw141292    BEGIN;
37*c5c4113dSnw141292    CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
38*c5c4113dSnw141292    INSERT INTO t1 VALUES(NULL,randstr(10,100),randstr(5,50));
39*c5c4113dSnw141292    INSERT INTO t1 VALUES(123456,randstr(10,100),randstr(5,50));
40*c5c4113dSnw141292    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
41*c5c4113dSnw141292    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
42*c5c4113dSnw141292    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
43*c5c4113dSnw141292    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
44*c5c4113dSnw141292    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
45*c5c4113dSnw141292    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
46*c5c4113dSnw141292    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
47*c5c4113dSnw141292    CREATE INDEX i1 ON t1(b,c);
48*c5c4113dSnw141292    CREATE TABLE t2 AS SELECT * FROM t1;
49*c5c4113dSnw141292    COMMIT;
50*c5c4113dSnw141292    DROP TABLE t2;
51*c5c4113dSnw141292  }
52*c5c4113dSnw141292  set ::size1 [file size test.db]
53*c5c4113dSnw141292  set ::cksum [cksum]
54*c5c4113dSnw141292  expr {$::cksum!=""}
55*c5c4113dSnw141292} {1}
56*c5c4113dSnw141292do_test vacuum-1.2 {
57*c5c4113dSnw141292  execsql {
58*c5c4113dSnw141292    VACUUM;
59*c5c4113dSnw141292  }
60*c5c4113dSnw141292  cksum
61*c5c4113dSnw141292} $cksum
62*c5c4113dSnw141292do_test vacuum-1.3 {
63*c5c4113dSnw141292  expr {[file size test.db]<$::size1}
64*c5c4113dSnw141292} {1}
65*c5c4113dSnw141292do_test vacuum-1.4 {
66*c5c4113dSnw141292  execsql {
67*c5c4113dSnw141292    BEGIN;
68*c5c4113dSnw141292    CREATE TABLE t2 AS SELECT * FROM t1;
69*c5c4113dSnw141292    CREATE TABLE t3 AS SELECT * FROM t1;
70*c5c4113dSnw141292    CREATE VIEW v1 AS SELECT b, c FROM t3;
71*c5c4113dSnw141292    CREATE TRIGGER r1 AFTER DELETE ON t2 BEGIN
72*c5c4113dSnw141292      SELECT 1;
73*c5c4113dSnw141292    END;
74*c5c4113dSnw141292    COMMIT;
75*c5c4113dSnw141292    DROP TABLE t2;
76*c5c4113dSnw141292  }
77*c5c4113dSnw141292  set ::size1 [file size test.db]
78*c5c4113dSnw141292  set ::cksum [cksum]
79*c5c4113dSnw141292  expr {$::cksum!=""}
80*c5c4113dSnw141292} {1}
81*c5c4113dSnw141292do_test vacuum-1.5 {
82*c5c4113dSnw141292  execsql {
83*c5c4113dSnw141292    VACUUM;
84*c5c4113dSnw141292  }
85*c5c4113dSnw141292  cksum
86*c5c4113dSnw141292} $cksum
87*c5c4113dSnw141292do_test vacuum-1.6 {
88*c5c4113dSnw141292  expr {[file size test.db]<$::size1}
89*c5c4113dSnw141292} {1}
90*c5c4113dSnw141292
91*c5c4113dSnw141292do_test vacuum-2.1 {
92*c5c4113dSnw141292  catchsql {
93*c5c4113dSnw141292    BEGIN;
94*c5c4113dSnw141292    VACUUM;
95*c5c4113dSnw141292    COMMIT;
96*c5c4113dSnw141292  }
97*c5c4113dSnw141292} {1 {cannot VACUUM from within a transaction}}
98*c5c4113dSnw141292catch {db eval COMMIT}
99*c5c4113dSnw141292do_test vacuum-2.2 {
100*c5c4113dSnw141292  sqlite db2 test.db
101*c5c4113dSnw141292  execsql {
102*c5c4113dSnw141292    BEGIN;
103*c5c4113dSnw141292    CREATE TABLE t4 AS SELECT * FROM t1;
104*c5c4113dSnw141292    CREATE TABLE t5 AS SELECT * FROM t1;
105*c5c4113dSnw141292    COMMIT;
106*c5c4113dSnw141292    DROP TABLE t4;
107*c5c4113dSnw141292    DROP TABLE t5;
108*c5c4113dSnw141292  } db2
109*c5c4113dSnw141292  set ::cksum [cksum db2]
110*c5c4113dSnw141292  catchsql {
111*c5c4113dSnw141292    VACUUM
112*c5c4113dSnw141292  }
113*c5c4113dSnw141292} {0 {}}
114*c5c4113dSnw141292do_test vacuum-2.3 {
115*c5c4113dSnw141292  cksum
116*c5c4113dSnw141292} $cksum
117*c5c4113dSnw141292do_test vacuum-2.4 {
118*c5c4113dSnw141292  catch {db2 eval {SELECT count(*) FROM sqlite_master}}
119*c5c4113dSnw141292  cksum db2
120*c5c4113dSnw141292} $cksum
121*c5c4113dSnw141292
122*c5c4113dSnw141292# Ticket #427.  Make sure VACUUM works when the EMPTY_RESULT_CALLBACKS
123*c5c4113dSnw141292# pragma is turned on.
124*c5c4113dSnw141292#
125*c5c4113dSnw141292do_test vacuum-3.1 {
126*c5c4113dSnw141292  db close
127*c5c4113dSnw141292  db2 close
128*c5c4113dSnw141292  file delete test.db
129*c5c4113dSnw141292  sqlite db test.db
130*c5c4113dSnw141292  execsql {
131*c5c4113dSnw141292    PRAGMA empty_result_callbacks=on;
132*c5c4113dSnw141292    VACUUM;
133*c5c4113dSnw141292  }
134*c5c4113dSnw141292} {}
135*c5c4113dSnw141292
136*c5c4113dSnw141292# Ticket #464.  Make sure VACUUM works with the sqlite_compile() API.
137*c5c4113dSnw141292#
138*c5c4113dSnw141292do_test vacuum-4.1 {
139*c5c4113dSnw141292  db close
140*c5c4113dSnw141292  set DB [sqlite db test.db]
141*c5c4113dSnw141292  set VM [sqlite_compile $DB {VACUUM} TAIL]
142*c5c4113dSnw141292  sqlite_step $VM N VALUES COLNAMES
143*c5c4113dSnw141292} {SQLITE_DONE}
144*c5c4113dSnw141292do_test vacuum-4.2 {
145*c5c4113dSnw141292  sqlite_finalize $VM
146*c5c4113dSnw141292} {}
147*c5c4113dSnw141292
148*c5c4113dSnw141292# Ticket #515.  VACUUM after deleting and recreating the table that
149*c5c4113dSnw141292# a view refers to.
150*c5c4113dSnw141292#
151*c5c4113dSnw141292do_test vacuum-5.1 {
152*c5c4113dSnw141292  db close
153*c5c4113dSnw141292  file delete -force test.db
154*c5c4113dSnw141292  sqlite db test.db
155*c5c4113dSnw141292  catchsql {
156*c5c4113dSnw141292    CREATE TABLE Test (TestID int primary key);
157*c5c4113dSnw141292    INSERT INTO Test VALUES (NULL);
158*c5c4113dSnw141292    CREATE VIEW viewTest AS SELECT * FROM Test;
159*c5c4113dSnw141292
160*c5c4113dSnw141292    BEGIN;
161*c5c4113dSnw141292    CREATE TEMP TABLE tempTest (TestID int primary key, Test2 int NULL);
162*c5c4113dSnw141292    INSERT INTO tempTest SELECT TestID, 1 FROM Test;
163*c5c4113dSnw141292    DROP TABLE Test;
164*c5c4113dSnw141292    CREATE TABLE Test(TestID int primary key, Test2 int NULL);
165*c5c4113dSnw141292    INSERT INTO Test SELECT * FROM tempTest;
166*c5c4113dSnw141292    COMMIT;
167*c5c4113dSnw141292    VACUUM;
168*c5c4113dSnw141292  }
169*c5c4113dSnw141292} {0 {}}
170*c5c4113dSnw141292do_test vacuum-5.2 {
171*c5c4113dSnw141292  catchsql {
172*c5c4113dSnw141292    VACUUM;
173*c5c4113dSnw141292  }
174*c5c4113dSnw141292} {0 {}}
175*c5c4113dSnw141292
176*c5c4113dSnw141292# finish_test
177