this repo has no description
1package db
2
3import "core:fmt"
4import "core:mem"
5import "core:os"
6import "core:path/filepath"
7import "core:slice"
8import "core:strings"
9
10import sqlite "../../vendor/sqlite"
11import sa "../../vendor/sqlite/addons"
12
13main :: proc() {
14
15 args := os.args
16 fmt.printfln("Args: |%#v|", args)
17
18 db_url: cstring = "db.sqlite"
19
20 if len(args) == 2 {
21 last := args[1]
22
23 if (len(last) > 0 &&
24 (strings.has_suffix(last, ".db") || strings.has_suffix(last, ".sqlite"))) {
25
26 db_url = strings.clone_to_cstring(last)
27 }
28 }
29
30
31 track: mem.Tracking_Allocator
32 mem.tracking_allocator_init(&track, context.allocator)
33 context.allocator = mem.tracking_allocator(&track)
34 defer {
35 if len(track.allocation_map) > 0 {
36 fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map))
37 for _, entry in track.allocation_map {
38 fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
39 }
40 }
41 if len(track.bad_free_array) > 0 {
42 fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array))
43 for entry in track.bad_free_array {
44 fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
45 }
46 }
47 mem.tracking_allocator_destroy(&track)
48 }
49
50
51 migration_dir, err := filepath.join({#directory, "migrations"}, context.allocator)
52 defer delete(migration_dir)
53
54 assert(err == nil, "Unable to resolve migrations folder path")
55
56 dir_entries, err2 := os.read_all_directory_by_path(migration_dir, context.allocator)
57 defer os.file_info_slice_delete(dir_entries, context.allocator)
58
59 assert(err2 == nil, "Unable to find migration list from folder")
60
61 only_sql := slice.filter(dir_entries, proc(x: os.File_Info) -> bool {
62 return strings.has_suffix(x.fullpath, ".sql")
63 })
64 defer delete(only_sql)
65
66 slice.sort_by(only_sql, proc(a, b: os.File_Info) -> bool {
67 return strings.compare(a.fullpath, b.fullpath) < 0
68 })
69
70 fmt.printfln("Entries: %#v", only_sql)
71
72 // Apply
73
74
75 db: ^sqlite.Connection
76
77 if rc := sqlite.open(db_url, &db); rc != .Ok {
78 fmt.panicf("failed to open database. result code {}", rc)
79 }
80 fmt.printfln("connected to database")
81
82 defer {
83 sqlite.close(db)
84 fmt.printfln("\nconnection closed")
85 }
86
87 for migration in only_sql {
88 apply(db, migration.fullpath)
89 }
90}
91
92apply :: proc(db: ^sqlite.Connection, path: string, allocator := context.allocator) {
93
94 data, err := os.read_entire_file_from_path(path, allocator)
95 assert(err == nil, "Unable to apply migration")
96 defer delete(data, allocator)
97
98 text := string(data)
99
100 expressions := strings.split(text, ";")
101 defer delete(expressions)
102
103 trimmed: string
104 for exp in expressions {
105 trimmed = strings.trim_space(exp)
106 if (len(trimmed) == 0) {continue}
107
108 fmt.printfln("Expressions: |%#v| \nApplying", trimmed)
109
110 rc := sa.execute(db, trimmed)
111 assert(rc == .Ok)
112
113 fmt.printfln("Applied successfully")
114 }
115 delete(trimmed)
116
117 // rc := sa.execute(db, text)
118 // assert(rc == .Ok)
119}