-
Notifications
You must be signed in to change notification settings - Fork 5
/
mssql.go
101 lines (87 loc) · 2.54 KB
/
mssql.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
package schema
import (
"context"
"fmt"
"strings"
"time"
"unicode"
)
// MSSQL is the dialect for MS SQL-compatible databases
var MSSQL = mssqlDialect{}
type mssqlDialect struct{}
func (s mssqlDialect) QuotedTableName(schemaName, tableName string) string {
if schemaName == "" {
return s.QuotedIdent(tableName)
}
return fmt.Sprintf("%s.%s", s.QuotedIdent(schemaName), s.QuotedIdent(tableName))
}
func (s mssqlDialect) QuotedIdent(ident string) string {
if ident == "" {
return ""
}
var sb strings.Builder
sb.WriteRune('[')
for _, r := range ident {
switch {
case unicode.IsSpace(r):
continue
case r == ';':
continue
case r == ']':
sb.WriteRune(r)
sb.WriteRune(r)
default:
sb.WriteRune(r)
}
}
sb.WriteRune(']')
return sb.String()
}
func (s mssqlDialect) CreateMigrationsTable(ctx context.Context, tx Queryer, tableName string) error {
unquotedTableName := tableName[1 : len(tableName)-1]
query := fmt.Sprintf(`
IF NOT EXISTS (SELECT * FROM Sysobjects WHERE NAME='%s' AND XTYPE='U')
CREATE TABLE %s (
id VARCHAR(255) NOT NULL,
checksum VARCHAR(32) NOT NULL DEFAULT '',
execution_time_in_millis INTEGER NOT NULL DEFAULT 0,
applied_at DATETIMEOFFSET NOT NULL
)
`, unquotedTableName, tableName)
_, err := tx.ExecContext(ctx, query)
return err
}
func (s mssqlDialect) GetAppliedMigrations(ctx context.Context, tx Queryer, tableName string) (migrations []*AppliedMigration, err error) {
migrations = make([]*AppliedMigration, 0)
query := fmt.Sprintf(`
SELECT id, checksum, execution_time_in_millis, applied_at
FROM %s ORDER BY id ASC
`, tableName)
rows, err := tx.QueryContext(ctx, query)
if err != nil {
return migrations, err
}
defer rows.Close()
for rows.Next() {
migration := AppliedMigration{}
err = rows.Scan(&migration.ID, &migration.Checksum, &migration.ExecutionTimeInMillis, &migration.AppliedAt)
if err != nil {
err = fmt.Errorf("failed to GetAppliedMigrations. Did somebody change the structure of the %s table?: %w", tableName, err)
return migrations, err
}
migration.AppliedAt = migration.AppliedAt.In(time.Local)
migrations = append(migrations, &migration)
}
return migrations, err
}
func (s mssqlDialect) InsertAppliedMigration(ctx context.Context, tx Queryer, tableName string, am *AppliedMigration) error {
query := fmt.Sprintf(`
INSERT INTO %s
( id, checksum, execution_time_in_millis, applied_at )
VALUES
( @p1, @p2, @p3, @p4 )`,
tableName,
)
_, err := tx.ExecContext(ctx, query, am.ID, am.MD5(), am.ExecutionTimeInMillis, am.AppliedAt)
return err
}