Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give ability to execute scripts #14

Merged
merged 3 commits into from
Nov 15, 2022
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
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}))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this: why enable the tests only on the CI?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a container running with Postgres to execute the PostgreSQL test and an SQLite executable available to run the SQLite test. However, opam-repo-ci doesn't support these kinds of tests... This is the only option I've found to ensure we don't break opam-repo-ci when releasing new versions.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I can't come up with a better alternative either, so let's go with this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can correct it later if we find something better 👍

(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