-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
149 lines (121 loc) · 2.97 KB
/
storage.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package services
import (
"database/sql"
"fmt"
errors "github.com/joaosoft/errors"
"github.com/joaosoft/logger"
manager "github.com/joaosoft/manager"
)
type StoragePostgres struct {
logger logger.ILogger
conn manager.IDB
}
func NewStoragePostgres(logger logger.ILogger, connection manager.IDB) *StoragePostgres {
return &StoragePostgres{
logger: logger,
conn: connection,
}
}
func (storage *StoragePostgres) GetMigration(idMigration string) (*Migration, error) {
row := storage.conn.Get().QueryRow(`
SELECT
mode,
"user",
executed_at
FROM migration
WHERE id_migration = $1
ORDER BY id_migration ASC
`, idMigration)
migration := &Migration{IdMigration: idMigration}
if err := row.Scan(
&migration.Mode,
&migration.User,
&migration.ExecutedAt); err != nil {
if err != sql.ErrNoRows {
return nil, errors.New(errors.LevelError, 0, err)
}
return nil, nil
}
return migration, nil
}
func (storage *StoragePostgres) GetMigrations(values map[string][]string) (ListMigration, error) {
query := `
SELECT
id_migration,
mode,
"user",
executed_at
FROM migration
`
index := 1
params := make([]interface{}, 0)
if values != nil {
for key, value := range values {
if index == 1 {
query += ` WHERE `
} else {
query += ` AND `
}
query += fmt.Sprintf(`%s = $%d`, key, index)
index = index + 1
params = append(params, value[0])
}
}
query += ` ORDER BY id_migration ASC`
rows, err := storage.conn.Get().Query(query, params...)
if err != nil {
return nil, errors.New(errors.LevelError, 0, err)
}
defer rows.Close()
migrations := make(ListMigration, 0)
for rows.Next() {
migration := &Migration{}
if err := rows.Scan(
&migration.IdMigration,
&migration.Mode,
&migration.User,
&migration.ExecutedAt); err != nil {
if err != sql.ErrNoRows {
return nil, errors.New(errors.LevelError, 0, err)
}
return nil, nil
}
migrations = append(migrations, migration)
}
return migrations, nil
}
func (storage *StoragePostgres) CreateMigration(newMigration *Migration) error {
if _, err := storage.conn.Get().Exec(`
INSERT INTO migration(
id_migration,
mode)
VALUES($1, $2)
`,
newMigration.IdMigration, newMigration.Mode); err != nil {
return errors.New(errors.LevelError, 0, err)
}
return nil
}
func (storage *StoragePostgres) DeleteMigration(idMigration string) error {
if _, err := storage.conn.Get().Exec(`
DELETE
FROM migration
WHERE id_migration = $1
`, idMigration); err != nil {
return errors.New(errors.LevelError, 0, err)
}
return nil
}
func (storage *StoragePostgres) DeleteMigrations() error {
if _, err := storage.conn.Get().Exec(`
DELETE FROM migration`); err != nil {
return errors.New(errors.LevelError, 0, err)
}
return nil
}
func (storage *StoragePostgres) ExecuteMigration(migration string) error {
if _, err := storage.conn.Get().Exec(migration); err != nil {
return errors.New(errors.LevelError, 0, err)
}
return nil
}