a database layer insipred by caqti and ecto
1open Repodb
2
3let test_create_table () =
4 let op =
5 Migration.create_table "posts"
6 [
7 Migration.column "id" "BIGINT" ~nullable:false ~primary_key:true;
8 Migration.column "title" "TEXT" ~nullable:false;
9 ]
10 in
11 let sql = Migration.operation_to_sql op in
12 Alcotest.(check bool)
13 "CREATE TABLE" true
14 (String.sub sql 0 12 = "CREATE TABLE")
15
16let test_drop_table () =
17 let op = Migration.drop_table "posts" in
18 let sql = Migration.operation_to_sql op in
19 Alcotest.(check string) "DROP TABLE" "DROP TABLE posts" sql
20
21let test_add_column () =
22 let change =
23 Migration.add_column (Migration.column "email" "TEXT" ~nullable:false)
24 in
25 let op = Migration.alter_table "users" [ change ] in
26 let sql = Migration.operation_to_sql op in
27 Alcotest.(check bool) "ADD COLUMN" true (String.length sql > 0)
28
29let test_drop_column () =
30 let change = Migration.drop_column "old_column" in
31 let op = Migration.alter_table "users" [ change ] in
32 let sql = Migration.operation_to_sql op in
33 Alcotest.(check bool) "DROP COLUMN" true (String.length sql > 0)
34
35let test_rename_column () =
36 let change = Migration.rename_column ~from:"old_name" ~to_:"new_name" in
37 let op = Migration.alter_table "users" [ change ] in
38 let sql = Migration.operation_to_sql op in
39 Alcotest.(check bool) "RENAME COLUMN" true (String.length sql > 0)
40
41let test_create_index () =
42 let op = Migration.create_index "users" [ "email" ] ~unique:true in
43 let sql = Migration.operation_to_sql op in
44 Alcotest.(check bool) "CREATE UNIQUE INDEX" true (String.length sql > 0)
45
46let test_drop_index () =
47 let op = Migration.drop_index "users_email_index" in
48 let sql = Migration.operation_to_sql op in
49 Alcotest.(check string) "DROP INDEX" "DROP INDEX users_email_index" sql
50
51let test_timestamps () =
52 let cols = Migration.timestamps () in
53 Alcotest.(check int) "two timestamp columns" 2 (List.length cols)
54
55let test_migration_definition () =
56 let m =
57 Migration.migration ~version:1L ~name:"create_users"
58 ~up:[ Migration.create_table "users" [ Migration.column "id" "BIGINT" ] ]
59 ~down:[ Migration.drop_table "users" ]
60 in
61 Alcotest.(check int64) "version" 1L m.version;
62 Alcotest.(check string) "name" "create_users" m.name
63
64let test_generate_up_sql () =
65 let m =
66 Migration.migration ~version:1L ~name:"test"
67 ~up:[ Migration.create_table "test" [ Migration.column "id" "BIGINT" ] ]
68 ~down:[]
69 in
70 let sqls = Migration.generate_up_sql m in
71 Alcotest.(check int) "one SQL statement" 1 (List.length sqls)
72
73let test_pending_migrations () =
74 let m1 = Migration.migration ~version:1L ~name:"first" ~up:[] ~down:[] in
75 let m2 = Migration.migration ~version:2L ~name:"second" ~up:[] ~down:[] in
76 let m3 = Migration.migration ~version:3L ~name:"third" ~up:[] ~down:[] in
77 let pending =
78 Migration.pending_migrations ~applied_versions:[ 1L ] [ m1; m2; m3 ]
79 in
80 Alcotest.(check int) "two pending" 2 (List.length pending)
81
82let test_sort_migrations () =
83 let m1 = Migration.migration ~version:3L ~name:"third" ~up:[] ~down:[] in
84 let m2 = Migration.migration ~version:1L ~name:"first" ~up:[] ~down:[] in
85 let m3 = Migration.migration ~version:2L ~name:"second" ~up:[] ~down:[] in
86 let sorted = Migration.sort_migrations [ m1; m2; m3 ] in
87 Alcotest.(check int64) "first is 1" 1L (List.hd sorted).version
88
89let tests =
90 [
91 ("create_table", `Quick, test_create_table);
92 ("drop_table", `Quick, test_drop_table);
93 ("add_column", `Quick, test_add_column);
94 ("drop_column", `Quick, test_drop_column);
95 ("rename_column", `Quick, test_rename_column);
96 ("create_index", `Quick, test_create_index);
97 ("drop_index", `Quick, test_drop_index);
98 ("timestamps", `Quick, test_timestamps);
99 ("migration definition", `Quick, test_migration_definition);
100 ("generate_up_sql", `Quick, test_generate_up_sql);
101 ("pending_migrations", `Quick, test_pending_migrations);
102 ("sort_migrations", `Quick, test_sort_migrations);
103 ]