vacuum2.test 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # 2005 February 15
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the VACUUM statement.
  13. #
  14. # $Id: vacuum2.test,v 1.10 2009/02/18 20:31:18 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Do not use a codec for tests in this file, as the database file is
  18. # manipulated directly using tcl scripts (using the [hexio_write] command).
  19. #
  20. do_not_use_codec
  21. # If the VACUUM statement is disabled in the current build, skip all
  22. # the tests in this file.
  23. #
  24. ifcapable {!vacuum||!autoinc} {
  25. finish_test
  26. return
  27. }
  28. if $AUTOVACUUM {
  29. finish_test
  30. return
  31. }
  32. # Ticket #1121 - make sure vacuum works if all autoincrement tables
  33. # have been deleted.
  34. #
  35. do_test vacuum2-1.1 {
  36. execsql {
  37. CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
  38. DROP TABLE t1;
  39. VACUUM;
  40. }
  41. } {}
  42. # Ticket #2518. Make sure vacuum increments the change counter
  43. # in the database header.
  44. #
  45. do_test vacuum2-2.1 {
  46. execsql {
  47. CREATE TABLE t1(x);
  48. CREATE TABLE t2(y);
  49. INSERT INTO t1 VALUES(1);
  50. }
  51. hexio_get_int [hexio_read test.db 24 4]
  52. } [expr {[hexio_get_int [hexio_read test.db 24 4]]+3}]
  53. do_test vacuum2-2.1 {
  54. execsql {
  55. VACUUM
  56. }
  57. hexio_get_int [hexio_read test.db 24 4]
  58. } [expr {[hexio_get_int [hexio_read test.db 24 4]]+1}]
  59. ############################################################################
  60. # Verify that we can use the auto_vacuum pragma to request a new
  61. # autovacuum setting, do a VACUUM, and the new setting takes effect.
  62. # Make sure this happens correctly even if there are multiple open
  63. # connections to the same database file.
  64. #
  65. sqlite3 db2 test.db
  66. set pageSize [db eval {pragma page_size}]
  67. # We are currently not autovacuuming so the database should be 3 pages
  68. # in size. 1 page for each of sqlite_master, t1, and t2.
  69. #
  70. do_test vacuum2-3.1 {
  71. execsql {
  72. INSERT INTO t1 VALUES('hello');
  73. INSERT INTO t2 VALUES('out there');
  74. }
  75. expr {[file size test.db]/$pageSize}
  76. } {3}
  77. set cksum [cksum]
  78. do_test vacuum2-3.2 {
  79. cksum db2
  80. } $cksum
  81. # Convert the database to an autovacuumed database.
  82. ifcapable autovacuum {
  83. do_test vacuum2-3.3 {
  84. execsql {
  85. PRAGMA auto_vacuum=FULL;
  86. VACUUM;
  87. }
  88. expr {[file size test.db]/$pageSize}
  89. } {4}
  90. }
  91. do_test vacuum2-3.4 {
  92. cksum db2
  93. } $cksum
  94. do_test vacuum2-3.5 {
  95. cksum
  96. } $cksum
  97. do_test vacuum2-3.6 {
  98. execsql {PRAGMA integrity_check} db2
  99. } {ok}
  100. do_test vacuum2-3.7 {
  101. execsql {PRAGMA integrity_check} db
  102. } {ok}
  103. # Convert the database back to a non-autovacuumed database.
  104. do_test vacuum2-3.13 {
  105. execsql {
  106. PRAGMA auto_vacuum=NONE;
  107. VACUUM;
  108. }
  109. expr {[file size test.db]/$pageSize}
  110. } {3}
  111. do_test vacuum2-3.14 {
  112. cksum db2
  113. } $cksum
  114. do_test vacuum2-3.15 {
  115. cksum
  116. } $cksum
  117. do_test vacuum2-3.16 {
  118. execsql {PRAGMA integrity_check} db2
  119. } {ok}
  120. do_test vacuum2-3.17 {
  121. execsql {PRAGMA integrity_check} db
  122. } {ok}
  123. db2 close
  124. ifcapable autovacuum {
  125. do_test vacuum2-4.1 {
  126. db close
  127. forcedelete test.db
  128. sqlite3 db test.db
  129. execsql {
  130. pragma auto_vacuum=1;
  131. create table t(a, b);
  132. insert into t values(1, 2);
  133. insert into t values(1, 2);
  134. pragma auto_vacuum=0;
  135. vacuum;
  136. pragma auto_vacuum;
  137. }
  138. } {0}
  139. do_test vacuum2-4.2 {
  140. execsql {
  141. pragma auto_vacuum=1;
  142. vacuum;
  143. pragma auto_vacuum;
  144. }
  145. } {1}
  146. do_test vacuum2-4.3 {
  147. execsql {
  148. pragma integrity_check
  149. }
  150. } {ok}
  151. do_test vacuum2-4.4 {
  152. db close
  153. sqlite3 db test.db
  154. execsql {
  155. pragma auto_vacuum;
  156. }
  157. } {1}
  158. do_test vacuum2-4.5 { # Ticket #3663
  159. execsql {
  160. pragma auto_vacuum=2;
  161. vacuum;
  162. pragma auto_vacuum;
  163. }
  164. } {2}
  165. do_test vacuum2-4.6 {
  166. execsql {
  167. pragma integrity_check
  168. }
  169. } {ok}
  170. do_test vacuum2-4.7 {
  171. db close
  172. sqlite3 db test.db
  173. execsql {
  174. pragma auto_vacuum;
  175. }
  176. } {2}
  177. }
  178. #-------------------------------------------------------------------------
  179. # The following block of tests verify the behaviour of the library when
  180. # a database is VACUUMed when there are one or more unfinalized SQL
  181. # statements reading the same database using the same db handle.
  182. #
  183. db close
  184. forcedelete test.db
  185. sqlite3 db test.db
  186. do_execsql_test vacuum2-5.1 {
  187. CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
  188. INSERT INTO t1 VALUES(1, randomblob(500));
  189. INSERT INTO t1 SELECT a+1, randomblob(500) FROM t1; -- 2
  190. INSERT INTO t1 SELECT a+2, randomblob(500) FROM t1; -- 4
  191. INSERT INTO t1 SELECT a+4, randomblob(500) FROM t1; -- 8
  192. INSERT INTO t1 SELECT a+8, randomblob(500) FROM t1; -- 16
  193. } {}
  194. do_test vacuum2-5.2 {
  195. list [catch {
  196. db eval {SELECT a, b FROM t1} { if {$a == 8} { execsql VACUUM } }
  197. } msg] $msg
  198. } {1 {cannot VACUUM - SQL statements in progress}}
  199. do_test vacuum2-5.3 {
  200. list [catch {
  201. db eval {SELECT 1, 2, 3} { execsql VACUUM }
  202. } msg] $msg
  203. } {1 {cannot VACUUM - SQL statements in progress}}
  204. do_test vacuum2-5.4 {
  205. set res ""
  206. set res2 ""
  207. db eval {SELECT a, b FROM t1 WHERE a<=10} {
  208. if {$a==6} { set res [catchsql VACUUM] }
  209. lappend res2 $a
  210. }
  211. lappend res2 $res
  212. } {1 2 3 4 5 6 7 8 9 10 {1 {cannot VACUUM - SQL statements in progress}}}
  213. finish_test