forked from pressly/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlserver.go
51 lines (43 loc) · 1.08 KB
/
sqlserver.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
package dialectquery
import "fmt"
type Sqlserver struct {
Table string
}
var _ Querier = (*Sqlserver)(nil)
func (s *Sqlserver) CreateTable() string {
q := `CREATE TABLE %s (
id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
version_id BIGINT NOT NULL,
is_applied BIT NOT NULL,
tstamp DATETIME NULL DEFAULT CURRENT_TIMESTAMP
)`
return fmt.Sprintf(q, s.Table)
}
func (s *Sqlserver) InsertVersion() string {
q := `INSERT INTO %s (version_id, is_applied) VALUES (@p1, @p2)`
return fmt.Sprintf(q, s.Table)
}
func (s *Sqlserver) DeleteVersion() string {
q := `DELETE FROM %s WHERE version_id=@p1`
return fmt.Sprintf(q, s.Table)
}
func (s *Sqlserver) GetMigrationByVersion() string {
q := `
WITH Migrations AS
(
SELECT tstamp, is_applied,
ROW_NUMBER() OVER (ORDER BY tstamp) AS 'RowNumber'
FROM %s
WHERE version_id=@p1
)
SELECT tstamp, is_applied
FROM Migrations
WHERE RowNumber BETWEEN 1 AND 2
ORDER BY tstamp DESC
`
return fmt.Sprintf(q, s.Table)
}
func (s *Sqlserver) ListMigrations() string {
q := `SELECT version_id, is_applied FROM %s ORDER BY id DESC`
return fmt.Sprintf(q, s.Table)
}