mirror of
https://gitea.invidious.io/iv-org/shard-crystal-sqlite3.git
synced 2024-08-15 00:53:26 +00:00
Merge master changes into db branch.
Refactor flags, add option to dump from one database connection to another.
Merge commit '135f33d9ea
' into db
# Conflicts:
# spec/database_spec.cr
# src/sqlite3/lib_sqlite3.cr
# src/sqlite3/result_set.cr
# src/sqlite3/statement.cr
This commit is contained in:
commit
881ef79893
11 changed files with 138 additions and 229 deletions
47
spec/connection_spec.cr
Normal file
47
spec/connection_spec.cr
Normal file
|
@ -0,0 +1,47 @@
|
|||
require "./spec_helper"
|
||||
|
||||
private def dump(source, target)
|
||||
source.using_connection do |conn|
|
||||
conn = conn.as(SQLite3::Connection)
|
||||
target.using_connection do |backup_conn|
|
||||
backup_conn = backup_conn.as(SQLite3::Connection)
|
||||
conn.dump(backup_conn)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Connection do
|
||||
it "opens a database and then backs it up to another db" do
|
||||
with_db do |db|
|
||||
with_db("./test2.db") do |backup_db|
|
||||
db.exec "create table person (name string, age integer)"
|
||||
db.exec "insert into person values (\"foo\", 10)"
|
||||
|
||||
dump db, backup_db
|
||||
|
||||
backup_name = backup_db.scalar "select name from person"
|
||||
backup_age = backup_db.scalar "select age from person"
|
||||
source_name = db.scalar "select name from person"
|
||||
source_age = db.scalar "select age from person"
|
||||
|
||||
{backup_name, backup_age}.should eq({source_name, source_age})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "opens a database, inserts records, dumps to an in-memory db, insers some more, then dumps to the source" do
|
||||
with_db do |db|
|
||||
with_mem_db do |in_memory_db|
|
||||
db.exec "create table person (name string, age integer)"
|
||||
db.exec "insert into person values (\"foo\", 10)"
|
||||
dump db, in_memory_db
|
||||
|
||||
in_memory_db.scalar("select count(*) from person").should eq(1)
|
||||
in_memory_db.exec "insert into person values (\"bar\", 22)"
|
||||
dump in_memory_db, db
|
||||
|
||||
db.scalar("select count(*) from person").should eq(2)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,17 +1,5 @@
|
|||
require "./spec_helper"
|
||||
|
||||
DB_FILENAME = "./test.db"
|
||||
|
||||
def with_db(&block : DB::Database ->)
|
||||
DB.open "sqlite3:#{DB_FILENAME}", &block
|
||||
ensure
|
||||
File.delete(DB_FILENAME)
|
||||
end
|
||||
|
||||
def with_mem_db(&block : DB::Database ->)
|
||||
DB.open "sqlite3://%3Amemory%3A", &block
|
||||
end
|
||||
|
||||
def sql(s : String)
|
||||
"#{s.inspect}"
|
||||
end
|
||||
|
|
|
@ -2,3 +2,23 @@ require "spec"
|
|||
require "../src/sqlite3"
|
||||
|
||||
include SQLite3
|
||||
|
||||
DB_FILENAME = "./test.db"
|
||||
|
||||
def with_db(&block : DB::Database ->)
|
||||
File.delete(DB_FILENAME) rescue nil
|
||||
DB.open "sqlite3:#{DB_FILENAME}", &block
|
||||
ensure
|
||||
File.delete(DB_FILENAME)
|
||||
end
|
||||
|
||||
def with_db(name, &block : DB::Database ->)
|
||||
File.delete(name) rescue nil
|
||||
DB.open "sqlite3:#{name}", &block
|
||||
ensure
|
||||
File.delete(name)
|
||||
end
|
||||
|
||||
def with_mem_db(&block : DB::Database ->)
|
||||
DB.open "sqlite3://%3Amemory%3A", &block
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue