2001-09-16 04:13:26 +04:00
|
|
|
# 2001 September 15
|
2000-06-03 22:06:52 +04:00
|
|
|
#
|
2001-09-16 04:13:26 +04:00
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
2000-06-03 22:06:52 +04:00
|
|
|
#
|
2001-09-16 04:13:26 +04:00
|
|
|
# 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.
|
2000-06-03 22:06:52 +04:00
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
# This file implements regression tests for SQLite library. The
|
|
|
|
# focus of this file is testing the VACUUM statement.
|
|
|
|
#
|
2009-01-31 17:54:06 +03:00
|
|
|
# $Id: vacuum.test,v 1.43 2009/01/31 14:54:07 danielk1977 Exp $
|
2000-06-03 22:06:52 +04:00
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
|
2004-11-04 07:42:28 +03:00
|
|
|
# If the VACUUM statement is disabled in the current build, skip all
|
|
|
|
# the tests in this file.
|
|
|
|
#
|
|
|
|
ifcapable {!vacuum} {
|
2008-04-15 06:36:33 +04:00
|
|
|
omit_test vacuum.test {Compiled with SQLITE_OMIT_VACUUM}
|
2004-11-04 07:42:28 +03:00
|
|
|
finish_test
|
|
|
|
return
|
|
|
|
}
|
2004-11-10 18:27:38 +03:00
|
|
|
if $AUTOVACUUM {
|
2008-04-15 06:36:33 +04:00
|
|
|
omit_test vacuum.test {Auto-vacuum is enabled}
|
2004-11-10 18:27:38 +03:00
|
|
|
finish_test
|
|
|
|
return
|
|
|
|
}
|
2004-11-04 07:42:28 +03:00
|
|
|
|
2004-07-27 03:07:51 +04:00
|
|
|
set fcnt 1
|
2003-04-25 06:43:08 +04:00
|
|
|
do_test vacuum-1.1 {
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
|
|
|
|
INSERT INTO t1 VALUES(NULL,randstr(10,100),randstr(5,50));
|
|
|
|
INSERT INTO t1 VALUES(123456,randstr(10,100),randstr(5,50));
|
|
|
|
INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
|
|
|
|
INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
|
|
|
|
INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
|
|
|
|
INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
|
|
|
|
INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
|
|
|
|
INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
|
|
|
|
INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
|
|
|
|
CREATE INDEX i1 ON t1(b,c);
|
2004-07-27 03:07:51 +04:00
|
|
|
CREATE UNIQUE INDEX i2 ON t1(c,a);
|
2003-04-25 06:43:08 +04:00
|
|
|
CREATE TABLE t2 AS SELECT * FROM t1;
|
|
|
|
COMMIT;
|
|
|
|
DROP TABLE t2;
|
|
|
|
}
|
|
|
|
set ::size1 [file size test.db]
|
2008-01-17 05:36:28 +03:00
|
|
|
set ::cksum [cksum]
|
2003-04-25 06:43:08 +04:00
|
|
|
expr {$::cksum!=""}
|
|
|
|
} {1}
|
2010-04-24 18:02:59 +04:00
|
|
|
|
|
|
|
# Create bogus application-defined functions for functions used
|
|
|
|
# internally by VACUUM, to ensure that VACUUM falls back
|
|
|
|
# to the built-in functions.
|
|
|
|
#
|
|
|
|
proc failing_app_func {args} {error "bad function"}
|
|
|
|
do_test vacuum-1.1b {
|
|
|
|
db func substr failing_app_func
|
|
|
|
db func like failing_app_func
|
|
|
|
db func quote failing_app_func
|
|
|
|
catchsql {SELECT substr(name,1,3) FROM sqlite_master}
|
|
|
|
} {1 {bad function}}
|
|
|
|
|
2003-04-25 06:43:08 +04:00
|
|
|
do_test vacuum-1.2 {
|
|
|
|
execsql {
|
|
|
|
VACUUM;
|
|
|
|
}
|
2008-01-17 05:36:28 +03:00
|
|
|
cksum
|
2003-04-25 06:43:08 +04:00
|
|
|
} $cksum
|
2004-10-31 00:23:09 +04:00
|
|
|
ifcapable vacuum {
|
|
|
|
do_test vacuum-1.3 {
|
|
|
|
expr {[file size test.db]<$::size1}
|
|
|
|
} {1}
|
|
|
|
}
|
2003-04-25 06:43:08 +04:00
|
|
|
do_test vacuum-1.4 {
|
2004-11-22 11:43:32 +03:00
|
|
|
set sql_script {
|
2003-04-25 06:43:08 +04:00
|
|
|
BEGIN;
|
|
|
|
CREATE TABLE t2 AS SELECT * FROM t1;
|
|
|
|
CREATE TABLE t3 AS SELECT * FROM t1;
|
|
|
|
CREATE VIEW v1 AS SELECT b, c FROM t3;
|
2004-11-22 14:51:13 +03:00
|
|
|
CREATE TRIGGER r1 AFTER DELETE ON t2 BEGIN SELECT 1; END;
|
2003-04-25 06:43:08 +04:00
|
|
|
COMMIT;
|
|
|
|
DROP TABLE t2;
|
|
|
|
}
|
2004-11-22 11:43:32 +03:00
|
|
|
# If the library was compiled to omit view support, comment out the
|
2004-11-22 14:51:13 +03:00
|
|
|
# create view in the script $sql_script before executing it. Similarly,
|
|
|
|
# if triggers are not supported, comment out the trigger definition.
|
2004-11-22 11:43:32 +03:00
|
|
|
ifcapable !view {
|
|
|
|
regsub {CREATE VIEW} $sql_script {-- CREATE VIEW} sql_script
|
|
|
|
}
|
2004-11-22 14:51:13 +03:00
|
|
|
ifcapable !trigger {
|
|
|
|
regsub {CREATE TRIGGER} $sql_script {-- CREATE TRIGGER} sql_script
|
|
|
|
}
|
2004-11-22 11:43:32 +03:00
|
|
|
execsql $sql_script
|
2003-04-25 06:43:08 +04:00
|
|
|
set ::size1 [file size test.db]
|
2008-01-17 05:36:28 +03:00
|
|
|
set ::cksum [cksum]
|
2003-04-25 06:43:08 +04:00
|
|
|
expr {$::cksum!=""}
|
|
|
|
} {1}
|
|
|
|
do_test vacuum-1.5 {
|
|
|
|
execsql {
|
|
|
|
VACUUM;
|
|
|
|
}
|
2008-01-17 05:36:28 +03:00
|
|
|
cksum
|
2003-04-25 06:43:08 +04:00
|
|
|
} $cksum
|
2004-11-22 11:43:32 +03:00
|
|
|
|
2004-10-31 00:23:09 +04:00
|
|
|
ifcapable vacuum {
|
|
|
|
do_test vacuum-1.6 {
|
|
|
|
expr {[file size test.db]<$::size1}
|
|
|
|
} {1}
|
|
|
|
}
|
|
|
|
ifcapable vacuum {
|
2009-01-23 02:04:45 +03:00
|
|
|
do_test vacuum-2.1.1 {
|
2004-10-31 00:23:09 +04:00
|
|
|
catchsql {
|
|
|
|
BEGIN;
|
|
|
|
VACUUM;
|
|
|
|
}
|
|
|
|
} {1 {cannot VACUUM from within a transaction}}
|
2009-01-23 02:04:45 +03:00
|
|
|
do_test vacuum-2.1.2 {
|
|
|
|
sqlite3_get_autocommit db
|
|
|
|
} {0}
|
|
|
|
do_test vacuum-2.1.3 {
|
|
|
|
db eval {COMMIT}
|
|
|
|
} {}
|
2004-10-31 00:23:09 +04:00
|
|
|
}
|
2003-04-25 17:22:51 +04:00
|
|
|
do_test vacuum-2.2 {
|
2004-06-19 04:16:31 +04:00
|
|
|
sqlite3 db2 test.db
|
2003-04-25 17:22:51 +04:00
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
CREATE TABLE t4 AS SELECT * FROM t1;
|
|
|
|
CREATE TABLE t5 AS SELECT * FROM t1;
|
|
|
|
COMMIT;
|
|
|
|
DROP TABLE t4;
|
|
|
|
DROP TABLE t5;
|
|
|
|
} db2
|
2008-01-17 05:36:28 +03:00
|
|
|
set ::cksum [cksum db2]
|
2003-04-25 17:22:51 +04:00
|
|
|
catchsql {
|
|
|
|
VACUUM
|
|
|
|
}
|
2004-02-14 19:31:02 +03:00
|
|
|
} {0 {}}
|
2003-04-25 17:22:51 +04:00
|
|
|
do_test vacuum-2.3 {
|
2008-01-17 05:36:28 +03:00
|
|
|
cksum
|
2003-04-25 17:22:51 +04:00
|
|
|
} $cksum
|
|
|
|
do_test vacuum-2.4 {
|
|
|
|
catch {db2 eval {SELECT count(*) FROM sqlite_master}}
|
2008-01-17 05:36:28 +03:00
|
|
|
cksum db2
|
2003-04-25 17:22:51 +04:00
|
|
|
} $cksum
|
|
|
|
|
2005-02-12 03:19:30 +03:00
|
|
|
# Make sure the schema cookie is incremented by vacuum.
|
|
|
|
#
|
|
|
|
do_test vacuum-2.5 {
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
CREATE TABLE t6 AS SELECT * FROM t1;
|
|
|
|
CREATE TABLE t7 AS SELECT * FROM t1;
|
|
|
|
COMMIT;
|
|
|
|
}
|
|
|
|
sqlite3 db3 test.db
|
|
|
|
execsql {
|
2006-01-11 17:09:31 +03:00
|
|
|
-- The "SELECT * FROM sqlite_master" statement ensures that this test
|
|
|
|
-- works when shared-cache is enabled. If shared-cache is enabled, then
|
|
|
|
-- db3 shares a cache with db2 (but not db - it was opened as
|
|
|
|
-- "./test.db").
|
|
|
|
SELECT * FROM sqlite_master;
|
2005-02-12 03:19:30 +03:00
|
|
|
SELECT * FROM t7 LIMIT 1
|
|
|
|
} db3
|
|
|
|
execsql {
|
|
|
|
VACUUM;
|
|
|
|
}
|
|
|
|
execsql {
|
|
|
|
INSERT INTO t7 VALUES(1234567890,'hello','world');
|
|
|
|
} db3
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM t7 WHERE a=1234567890
|
|
|
|
}
|
|
|
|
} {1234567890 hello world}
|
|
|
|
integrity_check vacuum-2.6
|
|
|
|
do_test vacuum-2.7 {
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM t7 WHERE a=1234567890
|
|
|
|
} db3
|
|
|
|
} {1234567890 hello world}
|
|
|
|
do_test vacuum-2.8 {
|
|
|
|
execsql {
|
|
|
|
INSERT INTO t7 SELECT * FROM t6;
|
|
|
|
SELECT count(*) FROM t7;
|
|
|
|
}
|
|
|
|
} 513
|
|
|
|
integrity_check vacuum-2.9
|
|
|
|
do_test vacuum-2.10 {
|
|
|
|
execsql {
|
|
|
|
DELETE FROM t7;
|
|
|
|
SELECT count(*) FROM t7;
|
|
|
|
} db3
|
|
|
|
} 0
|
|
|
|
integrity_check vacuum-2.11
|
|
|
|
db3 close
|
|
|
|
|
|
|
|
|
2003-08-15 17:24:51 +04:00
|
|
|
# Ticket #427. Make sure VACUUM works when the EMPTY_RESULT_CALLBACKS
|
|
|
|
# pragma is turned on.
|
|
|
|
#
|
|
|
|
do_test vacuum-3.1 {
|
|
|
|
db close
|
2003-08-26 15:18:19 +04:00
|
|
|
db2 close
|
2011-08-02 04:57:34 +04:00
|
|
|
delete_file test.db
|
2004-06-19 04:16:31 +04:00
|
|
|
sqlite3 db test.db
|
2003-08-15 17:24:51 +04:00
|
|
|
execsql {
|
|
|
|
PRAGMA empty_result_callbacks=on;
|
|
|
|
VACUUM;
|
|
|
|
}
|
|
|
|
} {}
|
2003-04-25 17:22:51 +04:00
|
|
|
|
2004-05-21 05:47:26 +04:00
|
|
|
# Ticket #464. Make sure VACUUM works with the sqlite3_prepare() API.
|
2003-09-27 05:08:37 +04:00
|
|
|
#
|
|
|
|
do_test vacuum-4.1 {
|
|
|
|
db close
|
2006-01-03 03:33:50 +03:00
|
|
|
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
|
2004-05-21 05:47:26 +04:00
|
|
|
set VM [sqlite3_prepare $DB {VACUUM} -1 TAIL]
|
2004-05-26 14:11:05 +04:00
|
|
|
sqlite3_step $VM
|
2003-09-27 05:08:37 +04:00
|
|
|
} {SQLITE_DONE}
|
|
|
|
do_test vacuum-4.2 {
|
2004-05-21 14:08:53 +04:00
|
|
|
sqlite3_finalize $VM
|
2004-05-26 14:11:05 +04:00
|
|
|
} SQLITE_OK
|
2003-09-27 05:08:37 +04:00
|
|
|
|
2003-12-07 03:24:35 +03:00
|
|
|
# Ticket #515. VACUUM after deleting and recreating the table that
|
2004-11-22 11:43:32 +03:00
|
|
|
# a view refers to. Omit this test if the library is not view-enabled.
|
2003-12-07 03:24:35 +03:00
|
|
|
#
|
2004-11-22 11:43:32 +03:00
|
|
|
ifcapable view {
|
2003-12-07 03:24:35 +03:00
|
|
|
do_test vacuum-5.1 {
|
|
|
|
db close
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db
|
2004-06-19 04:16:31 +04:00
|
|
|
sqlite3 db test.db
|
2003-12-07 03:24:35 +03:00
|
|
|
catchsql {
|
|
|
|
CREATE TABLE Test (TestID int primary key);
|
|
|
|
INSERT INTO Test VALUES (NULL);
|
|
|
|
CREATE VIEW viewTest AS SELECT * FROM Test;
|
|
|
|
|
|
|
|
BEGIN;
|
2005-03-29 07:10:59 +04:00
|
|
|
CREATE TABLE tempTest (TestID int primary key, Test2 int NULL);
|
2003-12-07 03:24:35 +03:00
|
|
|
INSERT INTO tempTest SELECT TestID, 1 FROM Test;
|
|
|
|
DROP TABLE Test;
|
|
|
|
CREATE TABLE Test(TestID int primary key, Test2 int NULL);
|
|
|
|
INSERT INTO Test SELECT * FROM tempTest;
|
2005-03-29 07:10:59 +04:00
|
|
|
DROP TABLE tempTest;
|
2003-12-07 03:24:35 +03:00
|
|
|
COMMIT;
|
|
|
|
VACUUM;
|
|
|
|
}
|
|
|
|
} {0 {}}
|
|
|
|
do_test vacuum-5.2 {
|
|
|
|
catchsql {
|
|
|
|
VACUUM;
|
|
|
|
}
|
|
|
|
} {0 {}}
|
2004-11-22 11:43:32 +03:00
|
|
|
} ;# ifcapable view
|
2001-09-14 07:24:23 +04:00
|
|
|
|
2004-05-29 14:43:06 +04:00
|
|
|
# Ensure vacuum works with complicated tables names.
|
|
|
|
do_test vacuum-6.1 {
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE "abc abc"(a, b, c);
|
|
|
|
INSERT INTO "abc abc" VALUES(1, 2, 3);
|
|
|
|
VACUUM;
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
do_test vacuum-6.2 {
|
|
|
|
execsql {
|
|
|
|
select * from "abc abc";
|
|
|
|
}
|
|
|
|
} {1 2 3}
|
|
|
|
|
|
|
|
# Also ensure that blobs survive a vacuum.
|
2004-11-03 16:59:04 +03:00
|
|
|
ifcapable {bloblit} {
|
|
|
|
do_test vacuum-6.3 {
|
|
|
|
execsql {
|
|
|
|
DELETE FROM "abc abc";
|
|
|
|
INSERT INTO "abc abc" VALUES(X'00112233', NULL, NULL);
|
|
|
|
VACUUM;
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
do_test vacuum-6.4 {
|
|
|
|
execsql {
|
|
|
|
select count(*) from "abc abc" WHERE a = X'00112233';
|
|
|
|
}
|
|
|
|
} {1}
|
|
|
|
}
|
2004-05-29 14:43:06 +04:00
|
|
|
|
2004-11-22 08:26:27 +03:00
|
|
|
# Check what happens when an in-memory database is vacuumed. The
|
2011-08-02 04:57:34 +04:00
|
|
|
# [delete_file] command covers us in case the library was compiled
|
2004-11-22 08:26:27 +03:00
|
|
|
# without in-memory database support.
|
|
|
|
#
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete :memory:
|
2004-06-30 13:49:22 +04:00
|
|
|
do_test vacuum-7.0 {
|
|
|
|
sqlite3 db2 :memory:
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE t1(t);
|
|
|
|
VACUUM;
|
|
|
|
} db2
|
2010-11-15 14:35:50 +03:00
|
|
|
} {}
|
|
|
|
do_test vacuum-7.1 {
|
2009-01-31 17:54:06 +03:00
|
|
|
execsql {
|
|
|
|
CREATE TABLE t2(t);
|
|
|
|
CREATE TABLE t3(t);
|
|
|
|
DROP TABLE t2;
|
2010-11-15 14:35:50 +03:00
|
|
|
PRAGMA freelist_count;
|
|
|
|
}
|
|
|
|
} {1}
|
|
|
|
do_test vacuum-7.2 {
|
|
|
|
execsql {
|
2009-01-31 17:54:06 +03:00
|
|
|
VACUUM;
|
|
|
|
pragma integrity_check;
|
|
|
|
} db2
|
|
|
|
} {ok}
|
2010-11-15 14:35:50 +03:00
|
|
|
do_test vacuum-7.3 {
|
|
|
|
execsql { PRAGMA freelist_count; } db2
|
|
|
|
} {0}
|
|
|
|
ifcapable autovacuum {
|
|
|
|
do_test vacuum-7.4 {
|
|
|
|
execsql { PRAGMA auto_vacuum } db2
|
|
|
|
} {0}
|
|
|
|
do_test vacuum-7.5 {
|
|
|
|
execsql { PRAGMA auto_vacuum = 1} db2
|
|
|
|
execsql { PRAGMA auto_vacuum } db2
|
|
|
|
} {0}
|
|
|
|
do_test vacuum-7.6 {
|
|
|
|
execsql { PRAGMA auto_vacuum = 1} db2
|
|
|
|
execsql { VACUUM } db2
|
|
|
|
execsql { PRAGMA auto_vacuum } db2
|
|
|
|
} {1}
|
|
|
|
}
|
2004-06-30 14:54:29 +04:00
|
|
|
db2 close
|
2004-06-30 13:49:22 +04:00
|
|
|
|
2004-09-02 19:27:41 +04:00
|
|
|
# Ticket #873. VACUUM a database that has ' in its name.
|
|
|
|
#
|
|
|
|
do_test vacuum-8.1 {
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete a'z.db
|
|
|
|
forcedelete a'z.db-journal
|
2004-09-02 19:27:41 +04:00
|
|
|
sqlite3 db2 a'z.db
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE t1(t);
|
|
|
|
VACUUM;
|
|
|
|
} db2
|
|
|
|
} {}
|
|
|
|
db2 close
|
|
|
|
|
2005-02-03 04:08:20 +03:00
|
|
|
# Ticket #1095: Vacuum a table that uses AUTOINCREMENT
|
|
|
|
#
|
|
|
|
ifcapable {autoinc} {
|
|
|
|
do_test vacuum-9.1 {
|
|
|
|
execsql {
|
|
|
|
DROP TABLE 'abc abc';
|
|
|
|
CREATE TABLE autoinc(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
|
|
|
|
INSERT INTO autoinc(b) VALUES('hi');
|
|
|
|
INSERT INTO autoinc(b) VALUES('there');
|
|
|
|
DELETE FROM autoinc;
|
|
|
|
}
|
2008-01-17 05:36:28 +03:00
|
|
|
set ::cksum [cksum]
|
2005-02-03 04:08:20 +03:00
|
|
|
expr {$::cksum!=""}
|
|
|
|
} {1}
|
|
|
|
do_test vacuum-9.2 {
|
|
|
|
execsql {
|
|
|
|
VACUUM;
|
|
|
|
}
|
2008-01-17 05:36:28 +03:00
|
|
|
cksum
|
2005-02-03 04:08:20 +03:00
|
|
|
} $::cksum
|
|
|
|
do_test vacuum-9.3 {
|
|
|
|
execsql {
|
|
|
|
INSERT INTO autoinc(b) VALUES('one');
|
|
|
|
INSERT INTO autoinc(b) VALUES('two');
|
|
|
|
}
|
2008-01-17 05:36:28 +03:00
|
|
|
set ::cksum [cksum]
|
2005-02-03 04:08:20 +03:00
|
|
|
expr {$::cksum!=""}
|
|
|
|
} {1}
|
|
|
|
do_test vacuum-9.4 {
|
|
|
|
execsql {
|
|
|
|
VACUUM;
|
|
|
|
}
|
2008-01-17 05:36:28 +03:00
|
|
|
cksum
|
2005-02-03 04:08:20 +03:00
|
|
|
} $::cksum
|
|
|
|
}
|
|
|
|
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete {a'z.db}
|
2005-02-12 03:19:30 +03:00
|
|
|
|
2011-09-30 16:01:01 +04:00
|
|
|
# Test that "PRAGMA count_changes" does not interfere with VACUUM or cause
|
|
|
|
# it to return any rows to the user.
|
|
|
|
#
|
|
|
|
do_test vacuum-10.1 {
|
|
|
|
db close
|
|
|
|
forcedelete test.db
|
|
|
|
sqlite3 db test.db
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE t8(a, b);
|
|
|
|
INSERT INTO t8 VALUES('a', 'b');
|
|
|
|
INSERT INTO t8 VALUES('c', 'd');
|
|
|
|
PRAGMA count_changes = 1;
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
do_test vacuum-10.2 { execsql VACUUM } {}
|
|
|
|
|
2005-02-12 03:19:30 +03:00
|
|
|
finish_test
|