-
Notifications
You must be signed in to change notification settings - Fork 11
/
init_scripts.go
57 lines (47 loc) 路 1.16 KB
/
init_scripts.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
// SPDX-FileCopyrightText: 2024 PK Lab AG <contact@pklab.io>
// SPDX-License-Identifier: MIT
package postgres
import (
"bytes"
"context"
"os"
"path/filepath"
"github.com/pkg/errors"
)
func (s Storage) createScripts(ctx context.Context, subFolder string, split bool) error {
scriptsDir := filepath.Join(s.scriptsDir, subFolder)
files, err := os.ReadDir(scriptsDir)
if err != nil {
return err
}
for i := range files {
if files[i].IsDir() {
continue
}
path := filepath.Join(scriptsDir, files[i].Name())
raw, err := os.ReadFile(path)
if err != nil {
return err
}
if split {
queries := bytes.Split(raw, []byte{';'})
if len(queries) == 0 {
continue
}
for _, query := range queries {
query = bytes.TrimLeft(query, "\n ")
if len(query) == 0 {
continue
}
if _, err := s.Connection().DB().NewRaw(string(query)).Exec(ctx); err != nil {
return errors.Wrapf(err, "creating %s '%s'", subFolder, files[i].Name())
}
}
} else {
if _, err := s.Connection().DB().NewRaw(string(raw)).Exec(ctx); err != nil {
return errors.Wrapf(err, "creating %s '%s'", subFolder, files[i].Name())
}
}
}
return nil
}