rbu3.test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # 2014 August 30
  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. #
  12. source [file join [file dirname [info script]] rbu_common.tcl]
  13. if_no_rbu_support { finish_test ; return }
  14. set ::testprefix rbu3
  15. # Run the RBU in file $rbu on target database $target until completion.
  16. #
  17. proc run_rbu {target rbu} {
  18. sqlite3rbu rbu $target $rbu
  19. while { [rbu step]=="SQLITE_OK" } {}
  20. rbu close
  21. }
  22. forcedelete test.db-oal rbu.db
  23. reset_db
  24. #--------------------------------------------------------------------
  25. # Test that for an RBU to be applied, no corruption results if the
  26. # affinities on the source and target table do not match.
  27. #
  28. do_execsql_test 1.0 {
  29. CREATE TABLE x1(a INTEGER PRIMARY KEY, b TEXT, c REAL);
  30. CREATE INDEX i1 ON x1(b, c);
  31. } {}
  32. do_test 1.1 {
  33. sqlite3 db2 rbu.db
  34. db2 eval {
  35. CREATE TABLE data_x1(a, b, c, rbu_control);
  36. INSERT INTO data_x1 VALUES(1, '123', '123', 0);
  37. INSERT INTO data_x1 VALUES(2, 123, 123, 0);
  38. }
  39. db2 close
  40. run_rbu test.db rbu.db
  41. } {SQLITE_DONE}
  42. do_execsql_test 1.2 {
  43. PRAGMA integrity_check;
  44. } {ok}
  45. #--------------------------------------------------------------------
  46. # Test that NULL values may not be inserted into INTEGER PRIMARY KEY
  47. # columns.
  48. #
  49. forcedelete rbu.db
  50. reset_db
  51. do_execsql_test 2.0 {
  52. CREATE TABLE x1(a INTEGER PRIMARY KEY, b TEXT, c REAL);
  53. CREATE INDEX i1 ON x1(b, c);
  54. } {}
  55. foreach {tn rbudb} {
  56. 1 {
  57. CREATE TABLE data_x1(a, b, c, rbu_control);
  58. INSERT INTO data_x1 VALUES(NULL, 'a', 'b', 0);
  59. }
  60. 2 {
  61. CREATE TABLE data_x1(c, b, a, rbu_control);
  62. INSERT INTO data_x1 VALUES('b', 'a', NULL, 0);
  63. }
  64. } {
  65. do_test 2.$tn.1 {
  66. forcedelete rbu.db
  67. sqlite3 db2 rbu.db
  68. db2 eval $rbudb
  69. db2 close
  70. list [catch { run_rbu test.db rbu.db } msg] $msg
  71. } {1 {SQLITE_MISMATCH - datatype mismatch}}
  72. do_execsql_test 2.1.2 {
  73. PRAGMA integrity_check;
  74. } {ok}
  75. }
  76. #--------------------------------------------------------------------
  77. # Test that missing columns are detected.
  78. #
  79. forcedelete rbu.db
  80. reset_db
  81. do_execsql_test 2.0 {
  82. CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c);
  83. CREATE INDEX i1 ON x1(b, c);
  84. } {}
  85. do_test 2.1 {
  86. sqlite3 db2 rbu.db
  87. db2 eval {
  88. CREATE TABLE data_x1(a, b, rbu_control);
  89. INSERT INTO data_x1 VALUES(1, 'a', 0);
  90. }
  91. db2 close
  92. list [catch { run_rbu test.db rbu.db } msg] $msg
  93. } {1 {SQLITE_ERROR - column missing from data_x1: c}}
  94. do_execsql_test 2.2 {
  95. PRAGMA integrity_check;
  96. } {ok}
  97. # Also extra columns.
  98. #
  99. do_execsql_test 2.3 {
  100. CREATE TABLE x2(a INTEGER PRIMARY KEY, b, c);
  101. CREATE INDEX i2 ON x2(b, c);
  102. } {}
  103. do_test 2.4 {
  104. forcedelete rbu.db
  105. sqlite3 db2 rbu.db
  106. db2 eval {
  107. CREATE TABLE data_x2(a, b, c, d, rbu_control);
  108. INSERT INTO data_x2 VALUES(1, 'a', 2, 3, 0);
  109. }
  110. db2 close
  111. list [catch { run_rbu test.db rbu.db } msg] $msg
  112. } {1 SQLITE_ERROR}
  113. do_execsql_test 2.5 {
  114. PRAGMA integrity_check;
  115. } {ok}
  116. #-------------------------------------------------------------------------
  117. # Test that sqlite3rbu_create_vfs() returns an error if the requested
  118. # parent VFS is unknown.
  119. #
  120. # And that nothing disasterous happens if a VFS name passed to
  121. # sqlite3rbu_destroy_vfs() is unknown or not an RBU vfs.
  122. #
  123. do_test 3.1 {
  124. list [catch {sqlite3rbu_create_vfs xyz nosuchparent} msg] $msg
  125. } {1 SQLITE_NOTFOUND}
  126. do_test 3.2 {
  127. sqlite3rbu_destroy_vfs nosuchvfs
  128. sqlite3rbu_destroy_vfs unix
  129. sqlite3rbu_destroy_vfs win32
  130. } {}
  131. #-------------------------------------------------------------------------
  132. # Test that it is an error to specify an explicit VFS that does not
  133. # include rbu VFS functionality.
  134. #
  135. do_test 4.1 {
  136. testvfs tvfs
  137. sqlite3rbu rbu file:test.db?vfs=tvfs rbu.db
  138. list [catch { rbu step } msg] $msg
  139. } {0 SQLITE_ERROR}
  140. do_test 4.2 {
  141. list [catch { rbu close } msg] $msg
  142. } {1 {SQLITE_ERROR - rbu vfs not found}}
  143. tvfs delete
  144. #-------------------------------------------------------------------------
  145. # Test a large rbu update to ensure that wal_autocheckpoint does not get
  146. # in the way.
  147. #
  148. forcedelete rbu.db
  149. reset_db
  150. do_execsql_test 5.1 {
  151. CREATE TABLE x1(a, b, c, PRIMARY KEY(a)) WITHOUT ROWID;
  152. CREATE INDEX i1 ON x1(a);
  153. ATTACH 'rbu.db' AS rbu;
  154. CREATE TABLE rbu.data_x1(a, b, c, rbu_control);
  155. WITH s(a, b, c) AS (
  156. SELECT randomblob(300), randomblob(300), 1
  157. UNION ALL
  158. SELECT randomblob(300), randomblob(300), c+1 FROM s WHERE c<2000
  159. )
  160. INSERT INTO data_x1 SELECT a, b, c, 0 FROM s;
  161. }
  162. do_test 5.2 {
  163. sqlite3rbu rbu test.db rbu.db
  164. while {[rbu step]=="SQLITE_OK" && [file exists test.db-wal]==0} {}
  165. rbu close
  166. } {SQLITE_OK}
  167. do_test 5.3 {
  168. expr {[file size test.db-wal] > (1024 * 1200)}
  169. } 1
  170. do_test 6.1 { sqlite3rbu_internal_test } {}
  171. finish_test