Skip to content

Commit

Permalink
Merge pull request #14 from maiste/fix/issue-13
Browse files Browse the repository at this point in the history
Give ability to execute scripts
  • Loading branch information
maiste authored Nov 15, 2022
2 parents 7d944de + 9e4c2fd commit 5f15988
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
run: opam exec -- dune build @install

- name: Run tests
run: opam exec -- dune test
run: GITHUB_TESTS=true opam exec -- dune test

- name: Format code
run: opam exec -- dune build --auto-promote @fmt
Expand Down
5 changes: 5 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(cram
(applies_to example)
(enabled_if
(= true %{env:GITHUB_TESTS=false}))
(deps %{bin:omigrate}))
2 changes: 0 additions & 2 deletions example.t/dune

This file was deleted.

2 changes: 1 addition & 1 deletion example.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Execute the sqlite migrations

Verify the tables in the sqlite3 database
$ sqlite3 test.db ".tables"
pets schema_migrations
pets schema_migrations users

Check that we have everything in the sqlite3 schema table
$ omigrate ls -d "sqlite3:///$PWD/test.db" -s ./sqlite3 -vv
Expand Down
1 change: 1 addition & 0 deletions example.t/sqlite3/33_create_table.down.sql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DROP TABLE IF EXISTS pets;
DROP TABLE IF EXISTS users;
6 changes: 6 additions & 0 deletions example.t/sqlite3/33_create_table.up.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
CREATE TABLE pets (
name string NOT NULL DEFAULT "dog"
);

CREATE TABLE users (
name string NOT NULL DEFAULT "anonymous"
);

ALTER TABLE users ADD developer bool NOT NULL DEFAULT false;
21 changes: 15 additions & 6 deletions lib/omigrate_drivers/sqlite_3.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ module Db = struct
bind t stmt values;
exec_stmt t stmt

let exec_script t sql =
let rc = Sqlite3.exec t.db ~cb:(fun _ _ -> ()) sql in
match rc with
| Sqlite3.Rc.OK | Sqlite3.Rc.DONE -> ()
| err ->
let msg =
Printf.sprintf "Sqlite3 driver: [exec_stmt] [%s] %s"
(Sqlite3.Rc.to_string err) (Sqlite3.errmsg t.db)
in
failwith msg

let query t stmt values =
bind t stmt values;
let results = ref [] in
Expand Down Expand Up @@ -119,8 +130,8 @@ module T = struct
Logs_lwt.info (fun m -> m "Applying up migration %Ld" version)
in
let _ =
let stmt = Sqlite3.prepare t.Db.db migration.Omigrate.Migration.up in
Db.query t stmt []
let stmt = migration.Omigrate.Migration.up in
Db.exec_script t stmt
in
let* () =
Logs_lwt.debug (fun m ->
Expand Down Expand Up @@ -157,10 +168,8 @@ module T = struct
Logs_lwt.info (fun m -> m "Applying down migration %Ld" version)
in
let _ =
let stmt =
Sqlite3.prepare t.Db.db migration.Omigrate.Migration.down
in
Db.query t stmt []
let stmt = migration.Omigrate.Migration.down in
Db.exec_script t stmt
in
let* () =
Logs_lwt.debug (fun m ->
Expand Down

0 comments on commit 5f15988

Please sign in to comment.