-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathdialects.go
More file actions
255 lines (229 loc) · 7.29 KB
/
dialects.go
File metadata and controls
255 lines (229 loc) · 7.29 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package database
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/pressly/goose/v3/database/dialect"
"github.com/pressly/goose/v3/internal/dialects"
)
// Dialect is the type of database dialect.
type Dialect string
var ErrUnknownDialect = errors.New("unknown dialect")
const (
DialectCustom Dialect = ""
DialectClickHouse Dialect = "clickhouse"
DialectAuroraDSQL Dialect = "dsql"
DialectMSSQL Dialect = "mssql"
DialectMySQL Dialect = "mysql"
DialectPostgres Dialect = "postgres"
DialectRedshift Dialect = "redshift"
DialectSQLite3 Dialect = "sqlite3"
DialectSpanner Dialect = "spanner"
DialectStarrocks Dialect = "starrocks"
DialectTiDB Dialect = "tidb"
DialectTurso Dialect = "turso"
DialectYdB Dialect = "ydb"
// DEPRECATED: Vertica support is deprecated and will be removed in a future release.
DialectVertica Dialect = "vertica"
)
// ParseDialect returns the corresponding [Dialect], if any, for a given string with external
// origin. A supported [Dialect] value, like [DialectPostgres], may have multiple supported
// aliases, e.g. "postgres" or "pgx".
func ParseDialect(s string) (d Dialect, err error) {
// Every non-error return value from this function must have an entry in the [NewStore] lookup map.
switch s {
case "postgres", "pgx":
d = DialectPostgres
case "mysql":
d = DialectMySQL
case "sqlite3", "sqlite":
d = DialectSQLite3
case "spanner":
d = DialectSpanner
case "mssql", "azuresql", "sqlserver":
d = DialectMSSQL
case "redshift":
d = DialectRedshift
case "tidb":
d = DialectTiDB
case "clickhouse":
d = DialectClickHouse
case "vertica":
d = DialectVertica
case "ydb":
d = DialectYdB
case "turso":
d = DialectTurso
case "starrocks":
d = DialectStarrocks
case "dsql":
d = DialectAuroraDSQL
default:
err = ErrUnknownDialect
}
return
}
// NewStore returns a new [Store] implementation for the given dialect. The
// dialect must be a valid [Dialect] value; see [ParseDialect].
func NewStore(d Dialect, tableName string) (Store, error) {
if d == DialectCustom {
return nil, errors.New("custom dialect is not supported")
}
lookup := map[Dialect]dialect.Querier{
DialectClickHouse: dialects.NewClickhouse(),
DialectAuroraDSQL: dialects.NewAuroraDSQL(),
DialectMSSQL: dialects.NewSqlserver(),
DialectMySQL: dialects.NewMysql(),
DialectPostgres: dialects.NewPostgres(),
DialectRedshift: dialects.NewRedshift(),
DialectSQLite3: dialects.NewSqlite3(),
DialectSpanner: dialects.NewSpanner(),
DialectStarrocks: dialects.NewStarrocks(),
DialectTiDB: dialects.NewTidb(),
DialectTurso: dialects.NewTurso(),
DialectVertica: dialects.NewVertica(),
DialectYdB: dialects.NewYDB(),
}
querier, ok := lookup[d]
if !ok {
return nil, ErrUnknownDialect
}
return NewStoreFromQuerier(tableName, querier)
}
// NewStoreFromQuerier returns a new [Store] implementation for the given querier.
//
// Most of the time you should use [NewStore] instead of this function, as it will automatically
// create a dialect-specific querier for you. This function is useful if you want to use a custom
// querier that is not part of the standard dialects.
func NewStoreFromQuerier(tableName string, querier dialect.Querier) (Store, error) {
if tableName == "" {
return nil, errors.New("table name must not be empty")
}
if querier == nil {
return nil, errors.New("querier must not be nil")
}
return &store{
tableName: tableName,
querier: newQueryController(querier),
}, nil
}
type store struct {
tableName string
querier *queryController
}
var _ Store = (*store)(nil)
func (s *store) Tablename() string {
return s.tableName
}
func (s *store) CreateVersionTable(ctx context.Context, db DBTxConn) error {
q := s.querier.CreateTable(s.tableName)
if _, err := db.ExecContext(ctx, q); err != nil {
return fmt.Errorf("failed to create version table %q: %w", s.tableName, err)
}
return nil
}
func (s *store) Insert(ctx context.Context, db DBTxConn, req InsertRequest) error {
q := s.querier.InsertVersion(s.tableName)
if _, err := db.ExecContext(ctx, q, req.Version, true); err != nil {
return fmt.Errorf("failed to insert version %d: %w", req.Version, err)
}
return nil
}
func (s *store) Delete(ctx context.Context, db DBTxConn, version int64) error {
q := s.querier.DeleteVersion(s.tableName)
if _, err := db.ExecContext(ctx, q, version); err != nil {
return fmt.Errorf("failed to delete version %d: %w", version, err)
}
return nil
}
func (s *store) GetMigration(
ctx context.Context,
db DBTxConn,
version int64,
) (*GetMigrationResult, error) {
q := s.querier.GetMigrationByVersion(s.tableName)
var result GetMigrationResult
if err := db.QueryRowContext(ctx, q, version).Scan(
&result.Timestamp,
&result.IsApplied,
); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, fmt.Errorf("%w: %d", ErrVersionNotFound, version)
}
return nil, fmt.Errorf("failed to get migration %d: %w", version, err)
}
return &result, nil
}
func (s *store) GetLatestVersion(ctx context.Context, db DBTxConn) (int64, error) {
q := s.querier.GetLatestVersion(s.tableName)
var version sql.NullInt64
if err := db.QueryRowContext(ctx, q).Scan(&version); err != nil {
return -1, fmt.Errorf("failed to get latest version: %w", err)
}
if !version.Valid {
return -1, fmt.Errorf("latest %w", ErrVersionNotFound)
}
return version.Int64, nil
}
func (s *store) ListMigrations(
ctx context.Context,
db DBTxConn,
) ([]*ListMigrationsResult, error) {
q := s.querier.ListMigrations(s.tableName)
rows, err := db.QueryContext(ctx, q)
if err != nil {
return nil, fmt.Errorf("failed to list migrations: %w", err)
}
defer rows.Close()
var migrations []*ListMigrationsResult
for rows.Next() {
var result ListMigrationsResult
if err := rows.Scan(&result.Version, &result.IsApplied); err != nil {
return nil, fmt.Errorf("failed to scan list migrations result: %w", err)
}
migrations = append(migrations, &result)
}
if err := rows.Err(); err != nil {
return nil, err
}
return migrations, nil
}
//
//
//
// Additional methods that are not part of the core Store interface, but are extended by the
// [controller.StoreController] type.
//
//
//
func (s *store) TableExists(ctx context.Context, db DBTxConn) (bool, error) {
q := s.querier.TableExists(s.tableName)
if q == "" {
return false, errors.ErrUnsupported
}
var exists bool
// Note, we do not pass the table name as an argument to the query, as the query should be
// pre-defined by the dialect.
if err := db.QueryRowContext(ctx, q).Scan(&exists); err != nil {
return false, fmt.Errorf("failed to check if table exists: %w", err)
}
return exists, nil
}
var _ dialect.Querier = (*queryController)(nil)
type queryController struct{ dialect.Querier }
// newQueryController returns a new QueryController that wraps the given Querier.
func newQueryController(querier dialect.Querier) *queryController {
return &queryController{Querier: querier}
}
// Optional methods
// TableExists returns the SQL query string to check if the version table exists. If the Querier
// does not implement this method, it will return an empty string.
//
// Returns a boolean value.
func (c *queryController) TableExists(tableName string) string {
if t, ok := c.Querier.(interface{ TableExists(string) string }); ok {
return t.TableExists(tableName)
}
return ""
}