|
|
|
@ -934,24 +934,20 @@ func Test_Backup(t *testing.T) {
|
|
|
|
|
t.Fatalf("failed to insert records: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dstDB, err := ioutil.TempFile("", "rqlilte-bak-")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to create temp file: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
dstDB.Close()
|
|
|
|
|
defer os.Remove(dstDB.Name())
|
|
|
|
|
dstDB := mustTempFile()
|
|
|
|
|
defer os.Remove(dstDB)
|
|
|
|
|
|
|
|
|
|
err = db.Backup(dstDB.Name())
|
|
|
|
|
err = db.Backup(dstDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to backup database: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newDB, err := Open(dstDB.Name())
|
|
|
|
|
newDB, err := Open(dstDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to open backup database: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
defer newDB.Close()
|
|
|
|
|
defer os.Remove(dstDB.Name())
|
|
|
|
|
defer os.Remove(dstDB)
|
|
|
|
|
ro, err := newDB.QueryStringStmt(`SELECT * FROM foo`)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to query table: %s", err.Error())
|
|
|
|
@ -961,6 +957,60 @@ func Test_Backup(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_Copy(t *testing.T) {
|
|
|
|
|
srcDB, path := mustCreateDatabase()
|
|
|
|
|
defer srcDB.Close()
|
|
|
|
|
defer os.Remove(path)
|
|
|
|
|
|
|
|
|
|
_, err := srcDB.ExecuteStringStmt("CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to create table: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := &command.Request{
|
|
|
|
|
Transaction: true,
|
|
|
|
|
Statements: []*command.Statement{
|
|
|
|
|
{
|
|
|
|
|
Sql: `INSERT INTO foo(id, name) VALUES(1, "fiona")`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Sql: `INSERT INTO foo(id, name) VALUES(2, "fiona")`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Sql: `INSERT INTO foo(id, name) VALUES(3, "fiona")`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Sql: `INSERT INTO foo(id, name) VALUES(4, "fiona")`,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
_, err = srcDB.Execute(req, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to insert records: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dstFile := mustTempFile()
|
|
|
|
|
defer os.Remove(dstFile)
|
|
|
|
|
dstDB, err := Open(dstFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to open destination database: %s", err)
|
|
|
|
|
}
|
|
|
|
|
defer dstDB.Close()
|
|
|
|
|
|
|
|
|
|
err = srcDB.Copy(dstDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to copy database: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ro, err := dstDB.QueryStringStmt(`SELECT * FROM foo`)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to query table: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
if exp, got := `[{"columns":["id","name"],"types":["integer","text"],"values":[[1,"fiona"],[2,"fiona"],[3,"fiona"],[4,"fiona"]]}]`, asJSON(ro); exp != got {
|
|
|
|
|
t.Fatalf("unexpected results for query\nexp: %s\ngot: %s", exp, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_Serialize(t *testing.T) {
|
|
|
|
|
db, path := mustCreateDatabase()
|
|
|
|
|
defer db.Close()
|
|
|
|
@ -1132,38 +1182,28 @@ func Test_DumpMemory(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
func mustCreateDatabase() (*DB, string) {
|
|
|
|
|
var err error
|
|
|
|
|
f, err := ioutil.TempFile("", "rqlite-test-")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("failed to create temp file")
|
|
|
|
|
}
|
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
|
|
db, err := Open(f.Name())
|
|
|
|
|
f := mustTempFile()
|
|
|
|
|
db, err := Open(f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("failed to open database")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return db, f.Name()
|
|
|
|
|
return db, f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustWriteAndOpenDatabase(b []byte) (*DB, string) {
|
|
|
|
|
var err error
|
|
|
|
|
f, err := ioutil.TempFile("", "rqlilte-test-write-")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("failed to create temp file")
|
|
|
|
|
}
|
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(f.Name(), b, 0660)
|
|
|
|
|
f := mustTempFile()
|
|
|
|
|
err = ioutil.WriteFile(f, b, 0660)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("failed to write file")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db, err := Open(f.Name())
|
|
|
|
|
db, err := Open(f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("failed to open database")
|
|
|
|
|
}
|
|
|
|
|
return db, f.Name()
|
|
|
|
|
return db, f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mustExecute executes a statement, and panics on failure. Used for statements
|
|
|
|
@ -1191,3 +1231,14 @@ func asJSON(v interface{}) string {
|
|
|
|
|
}
|
|
|
|
|
return string(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mustTempFile returns a path to a temporary file in directory dir. It is up to the
|
|
|
|
|
// caller to remove the file once it is no longer needed.
|
|
|
|
|
func mustTempFile() string {
|
|
|
|
|
tmpfile, err := ioutil.TempFile("", "rqlite-db-test")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
}
|
|
|
|
|
tmpfile.Close()
|
|
|
|
|
return tmpfile.Name()
|
|
|
|
|
}
|
|
|
|
|