Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions integration-tests/sqlite/src/Main.gren
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,44 @@ tests fsPerm =
test "Works" <| \_ ->
Expect.pass
]
, await "Create database for backup testing" createTestDb <| \db ->
let
backupDbPath =
Path.fromPosixString ".backup.db"

slowBackupDbPath =
Path.fromPosixString ".backup-slow.db"
in
concat
[ await "Insert a person into the created database" (insertPerson joey db) <| \_ ->
concat
[ await "Backup the database"
(Sqlite.backup fsPerm backupDbPath db |> Sqlite.runBackup) <| \_ ->
await "Open the newly backed up database"
(Sqlite.open fsPerm Sqlite.defaultOptions (Sqlite.File backupDbPath)) <| \backupDb ->
await "Get the person from the backup database"
(Sqlite.getOne backupDb (personByNameQ joey.name)) <| \backupPerson ->
await "Also get the person from the original database"
(Sqlite.getOne db (personByNameQ joey.name)) <| \originalPerson ->
concat
[ test "Name is the same in original and backup database" <| \_ ->
Expect.equal backupPerson.name originalPerson.name
, test "Role is the same in original and backup database" <| \_ ->
Expect.equal backupPerson.role originalPerson.role
, await "Remove from disk" (FileSystem.remove fsPerm { recursive = False } backupDbPath) <| \_ ->
test "Worked" <| \_ ->
Expect.pass
]
, await "Backup the database to a different file with a smaller page count"
(Sqlite.backup fsPerm slowBackupDbPath db
|> Sqlite.withBackupPageRate 1
|> Sqlite.runBackup
) <| \_ ->
await "Remove from disk" (FileSystem.remove fsPerm { recursive = False } slowBackupDbPath) <| \_ ->
test "Worked" <| \_ ->
Expect.pass
]
]
, await "Open physical database" (initializePhysicalDb fsPerm) <| \db ->
await "Can be closed" (Sqlite.close db) <| \_ ->
await "Remove from disk" (FileSystem.remove fsPerm { recursive = False } diskDbPath) <| \_ ->
Expand Down Expand Up @@ -258,6 +296,13 @@ justin =
}


joey : Person
joey =
{ name = "Joey"
, role = "Contributor"
}


people : Array Person
people = [ robin, justin ]

Expand Down
21 changes: 21 additions & 0 deletions src/Gren/Kernel/Sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ var _Sqlite_close = function (db) {
});
};

var _Sqlite_backup = F3(function (destination, pages, db) {
return __Scheduler_binding(function (callback) {
try {
sqlite
.backup(db, _FilePath_toString(destination), {
source: "main",
target: "main",
rate: pages,
})
.then(function (res) {
callback(__Scheduler_succeed({}));
})
.catch(function (e) {
callback(_Sqlite_constructError(e));
});
} catch (e) {
callback(_Sqlite_constructError(e));
}
});
});

var _Sqlite_foldl = F4(function (query, db, func, acc) {
return __Scheduler_binding(function (callback) {
try {
Expand Down
33 changes: 33 additions & 0 deletions src/Sqlite.gren
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,39 @@ close =
Gren.Kernel.Sqlite.close



-- Backup


type Backup
= Backup
{ destination : Path
, pages : Int
, db : Database
}



backup : FileSystem.Permission -> Path -> Database -> Backup
backup _ path db =
Backup
{ destination = path
, pages = 100
, db = db
}


withBackupPageRate : Int -> Backup -> Backup
withBackupPageRate pages (Backup record) =
Backup { record | pages = pages }


runBackup : Backup -> Task Error {}
runBackup (Backup { destination, pages, db }) =
Gren.Kernel.Sqlite.backup destination pages db



-- Query


Expand Down
Loading