-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
93 lines (76 loc) · 1.85 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package pms
import (
"fmt"
"io/fs"
"os"
"sort"
"strings"
"github.com/jmoiron/sqlx"
)
const (
SELECT_VERSION = "SELECT version FROM migrations"
)
func getFilesWithDirection(files []fs.DirEntry, inc Direction) ([]fs.DirEntry, error) {
var filesToRead []fs.DirEntry
sort.Slice(filesToRead, func(i, j int) bool {
return filesToRead[i].Name() > filesToRead[j].Name()
})
for _, file := range files {
if file.IsDir() {
continue
}
filenameChunks := strings.Split(file.Name(), ".")
if len(filenameChunks) < 3 {
return nil, fmt.Errorf("file %q should have an extension", file.Name())
}
if filenameChunks[1] == string(inc) {
filesToRead = append(filesToRead, file)
}
}
return filesToRead, nil
}
func getFileContent(path string, fileName string) ([]byte, error) {
file, err := fs.ReadFile(os.DirFS(path), fileName)
if err != nil {
return nil, err
}
return file, nil
}
func getVersionFromName(name string) int {
var version int
for _, s := range name {
if s == '_' {
break
}
if s >= '0' && s <= '9' {
version = version*10 + int(s-'0')
}
}
return version
}
func readDir(path string) ([]fs.DirEntry, error) {
if files, err := os.ReadDir(path); err != nil {
return nil, fmt.Errorf("directory %q not found. Error: %w", path, err)
} else {
return files, err
}
}
func createTable(db *sqlx.DB, tableName string) error {
_, err := db.Exec(fmt.Sprintf(QUERY_CREATE_TABLE, tableName))
if err != nil {
return fmt.Errorf("cannot create table %q: %w", tableName, err)
}
return nil
}
func getMigrationVersion(db *sqlx.DB) (int, error) {
var migrationVersion int
err := db.Get(&migrationVersion, SELECT_VERSION)
if err != nil {
return 0, err
}
return migrationVersion, nil
}
func tableExists(db *sqlx.DB, tableName string) bool {
_, tableCheck := db.Query("SELECT * FROM " + tableName + ";")
return tableCheck == nil
}