libdoltlite-sys 0.38.21

Native bindings to the DoltLite SQLite-compatible library
Documentation
#
# 13-January-2022
#
#-------------------------------------------------------------------------
#

source [file join [file dirname [info script]] test_common.tcl]
set testprefix bcvfs_poll1


bcv_create_container mycont
bcv_upload_new_db mycont xyz.db {
  CREATE TABLE t1(x);
  CREATE INDEX i1 ON t1(x);
  INSERT INTO t1 VALUES(2), (4), (6);
}

bcv_mkdir testdir1
bcv_mkdir testdir2
set cmd1 [bcvfs_create testdir1 bcvfs1]
bcvfs_attach $cmd1 mycont
set cmd2 [bcvfs_create testdir2 bcvfs2]
bcvfs_attach $cmd2 mycont


sqlite3 db /mycont/xyz.db -vfs bcvfs1
sqlite3 db2 /mycont/xyz.db -vfs bcvfs2

do_execsql_test  1.0a { SELECT * FROM t1 } {2 4 6}
do_execsql2_test 1.0b { SELECT * FROM t1 } {2 4 6}

do_execsql_test 1.1a {
  INSERT INTO t1 VALUES('new_value');
  PRAGMA bcv_upload;
}
do_execsql2_test 1.1b {
  INSERT INTO t1 VALUES('old value');
}
do_test 1.2 {
  list [catch {db2 eval "PRAGMA bcv_poll"} msg] $msg
} {1 {cannot update manifest - write collision on database xyz.db}}

do_test 1.3 {
  set fd [open testdir2/mycont/xyz.db-wal r+]
  seek $fd 400
  puts -nonewline $fd "hello world"
  close $fd
  list [catch {db2 eval "PRAGMA bcv_poll"} msg] $msg
} {0 {}}

do_execsql_test 1.4a {
  INSERT INTO t1 VALUES('new_value2');
  PRAGMA bcv_upload;
}

do_test 1.5 {
  set fd [open testdir2/mycont/xyz.db-wal r+]
  seek $fd 0
  puts -nonewline $fd "hello world"
  close $fd
  list [catch {db2 eval "PRAGMA bcv_poll"} msg] $msg
} {0 {}}

do_execsql_test 1.6a {
  INSERT INTO t1 VALUES('new_value3');
  PRAGMA bcv_upload;
}

do_test 1.7 {
  set fd [open testdir2/mycont/xyz.db-wal r+]
  chan truncate $fd 20
  close $fd
  list [catch {db2 eval "PRAGMA bcv_poll"} msg] $msg
} {0 {}}

db2 close
sqlite3 db2 /mycont/xyz.db -vfs bcvfs2
do_execsql_test 1.8a {
  INSERT INTO t1 VALUES('new_value4');
  PRAGMA bcv_upload;
}
do_execsql2_test 1.8b {
  INSERT INTO t1 VALUES('another value');
}
do_test 1.9 {
  list [catch {db2 eval "PRAGMA bcv_poll"} msg] $msg
} {1 {cannot update manifest - write collision on database xyz.db}}
do_test 1.10 {
  set sz [file size testdir2/mycont/xyz.db-wal]
  set fd [open testdir2/mycont/xyz.db-wal r+]
  chan truncate $fd [expr $sz-10]
  close $fd
  list [catch {db2 eval "PRAGMA bcv_poll"} msg] $msg
} {0 {}}

do_test 1.11 {
  list [catch { $cmd1 poll nosuchcontainer } msg] $msg
} {1 {no such container: nosuchcontainer}}

db close
db2 close
$cmd1 destroy
$cmd2 destroy

#-------------------------------------------------------------------------
#
bcv_create_container mycont
bcv_upload_new_db mycont xyz.db {
  CREATE TABLE t1(x);
  CREATE INDEX i1 ON t1(x);
  INSERT INTO t1 VALUES(2), (4), (6);
}

bcv_mkdir testdir
set cmd [bcvfs_create testdir bcvfs]
bcvfs_attach $cmd mycont

sqlite3 db /mycont/xyz.db -vfs bcvfs
do_execsql_test 2.1 {
  SELECT * FROM t1
} {2 4 6}

bcv_upload_new_db mycont xyz2.db {
  CREATE TABLE t2(y);
  INSERT INTO t2 VALUES(1), (2), (3);
}

do_execsql_test 2.2 {
  PRAGMA bcv_poll;
  SELECT * FROM t1;
} {2 4 6}

bcv_delete_database -container mycont xyz2.db
bcv_delete_database -container mycont xyz.db

do_execsql_test 2.3 {
  PRAGMA bcv_poll;
} {}

# An IO error - as the open database has been deleted from the manifest.
#
do_catchsql_test 2.4 {
  SELECT * FROM t1
} {1 {disk I/O error}}

bcv_upload_new_db mycont test.db1 {
  CREATE TABLE t1(a);
  INSERT INTO t1 VALUES('db1');
}
bcv_upload_new_db mycont test.db2 {
  CREATE TABLE t1(a);
  INSERT INTO t1 VALUES('db2');
}

do_test 2.5 {
  $cmd poll mycont
  sqlite3 db  /mycont/test.db1 -vfs bcvfs
  execsql {
    BEGIN;
      INSERT INTO t1 VALUES('new value');
  }

  bcv_delete_database -container mycont test.db1
} {}

do_test 2.6 {
  list [catch { $cmd poll mycont } msg] $msg
} {1 {cannot update manifest - write collision on database test.db1}}

do_test 2.7 {
  execsql {
    COMMIT;
    PRAGMA wal_checkpoint;
  }
  list [catch { $cmd poll mycont } msg] $msg
} {1 {cannot update manifest - write collision on database test.db1}}

do_test 2.8 {
  list [catch { $cmd poll -noerr mycont } msg] $msg
} {1 rc=1}


db close
$cmd destroy

#-------------------------------------------------------------------------
#
bcv_create_container cont1
bcv_upload_new_db cont1 t1.db {
  CREATE TABLE t1(x);
  INSERT INTO t1 VALUES(1), (2);
}

bcv_mkdir testdir
set cmd [bcvfs_create testdir bcvfs]
bcvfs_attach $cmd cont1

sqlite3 db /cont1/t1.db -vfs bcvfs
do_test 3.1 {
  db eval {SELECT x FROM t1} {
    if {$x==1} {
      set res [list [catch { db eval { PRAGMA bcv_poll } } msg] $msg]
    }
  }
  set res
} {1 {database is locked}}

do_test 3.2 {
  db eval {
    BEGIN;
  } 
  list [catch { db eval { PRAGMA bcv_poll } } msg] $msg
} {1 {database is locked}}

db close
$cmd destroy

#-------------------------------------------------------------------------
# Test that when a manifest is polled, local changes are not lost.
#
bcv_create_container cont1
bcv_upload_new_db cont1 t1.db {
  CREATE TABLE t1(x);
  INSERT INTO t1 VALUES(1), (2);
}

bcv_mkdir testdir
set cmd [bcvfs_create testdir bcvfs]
bcvfs_attach $cmd cont1

sqlite3 db /cont1/t1.db -vfs bcvfs
sqlite3_bcvfs_register_vtab db

do_execsql_test 3.1 {
  INSERT INTO t1 VALUES(3), (4);
  PRAGMA wal_checkpoint;
} {0 1 1}

do_execsql_test 3.2 {
  SELECT container, database, nblock, ncache, 0 FROM bcv_database
} {cont1 t1.db 1 1 0}

bcv_upload_new_db cont1 t2.db {
  CREATE TABLE t2(y);
} 

do_test 3.3 {
  $cmd poll cont1
  sqlite3 db2 /cont1/t1.db -vfs bcvfs
  db2 eval {
    SELECT * FROM t1
  }
} {1 2 3 4}

db2 close
bcv_mkdir testdir2
set cmd2 [bcvfs_create testdir2 bcvfs2]
bcvfs_attach $cmd2 cont1
sqlite3 db2 /cont1/t1.db -vfs bcvfs2

do_execsql2_test 3.4 {
  SELECT * FROM t1;
} {1 2}

sqlite3 db3 /cont1/t2.db -vfs bcvfs2
do_execsql3_test 3.5 {
  INSERT INTO t2 VALUES('hello world');
}
do_test 3.6 {
  $cmd upload cont1
  $cmd2 poll cont1
  db2 eval { SELECT * FROM t1 }
} {1 2 3 4}
do_execsql3_test 3.7 {
  SELECT * FROM t2
} {{hello world}}
db3 close

sqlite3_bcvfs_register_vtab db2
do_execsql2_test 3.8 {
  SELECT * FROM bcv_database
} {
  cont1 t1.db 1 1 0 0 {} 1 0 1
  cont1 t2.db 1 1 1 0 {} 0 0 0
}


do_test 3.9 {
  $cmd2 delete cont1 t2.db
} {}
do_execsql2_test 3.10 {
  SELECT * FROM bcv_database
} {
  cont1 t1.db 1 1 0 0 {} 1 0 1
  cont1 t2.db 0 0 0 0 deleted 0 0 0
}
do_test 3.11 {
  db eval {
    INSERT INTO t1 VALUES(5), (6);
    PRAGMA wal_checkpoint;
  }
  $cmd upload cont1
} {}

do_test 3.12 {
  $cmd2 poll cont1
} {}

do_execsql2_test 3.13 {
  SELECT * FROM bcv_database
} {
  cont1 t1.db 1 1 0 0 {} 1 0 1
  cont1 t2.db 0 0 0 0 deleted 0 0 0
}

db2 close
db close
$cmd destroy
$cmd2 destroy

#-------------------------------------------------------------------------

bcv_create_container cont1
bcv_upload_new_db cont1 t1.db {
  CREATE TABLE t1(x);
  INSERT INTO t1 VALUES(1), (2), (3), (4);
}

bcv_mkdir testdir
set cmd [bcvfs_create testdir bcvfs]
bcvfs_attach $cmd cont1

sqlite3 db /cont1/t1.db -vfs bcvfs
sqlite3_bcvfs_register_vtab db
do_execsql_test 4.1 {
  SELECT * FROM t1
} {1 2 3 4}
do_execsql_test 4.2 {
  DELETE FROM t1 WHERE x=2;
} {}

db close
$cmd poll cont1
sqlite3 db /cont1/t1.db -vfs bcvfs
do_execsql_test 4.3 {
  SELECT * FROM t1
} {1 3 4}


db close
$cmd destroy

finish_test