-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'mssql' of https://github.com/pubkraal/schema into pubkr…
…aal-mssql * 'mssql' of https://github.com/pubkraal/schema: feat: add mssql support
- Loading branch information
Showing
8 changed files
with
234 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
) | ||
`, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package schema | ||
|
||
import ( | ||
"testing" | ||
|
||
// MSSQL Driver | ||
_ "github.com/denisenkom/go-mssqldb" | ||
) | ||
|
||
// Interface verification that MSSQL is a valid Dialect | ||
var ( | ||
_ Dialect = MSSQL | ||
) | ||
|
||
func TestMSSQLQuotedTableName(t *testing.T) { | ||
type qtnTest struct { | ||
schema, table string | ||
expected string | ||
} | ||
|
||
tests := []qtnTest{ | ||
{"public", "ta[ble", `[public].[ta[ble]`}, | ||
{"pu[b]lic", "ta[ble", `[pu[b]]lic].[ta[ble]`}, | ||
{"tempdb", "users", `[tempdb].[users]`}, | ||
{"schema.with.dot", "table.with.dot", `[schema.with.dot].[table.with.dot]`}, | ||
{`public"`, `"; DROP TABLE users`, `[public"].["DROPTABLEusers]`}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
t.Run(tc.expected, func(t *testing.T) { | ||
actual := MSSQL.QuotedTableName(tc.schema, tc.table) | ||
if actual != tc.expected { | ||
t.Errorf("Expected %s, got %s", tc.expected, actual) | ||
} | ||
}) | ||
} | ||
|
||
} | ||
|
||
func TestMSSQLQuotedIdent(t *testing.T) { | ||
table := map[string]string{ | ||
"": "", | ||
"MY_TABLE": "[MY_TABLE]", | ||
"users_roles": `[users_roles]`, | ||
"table.with.dot": `[table.with.dot]`, | ||
`table"with"quotes`: `[table"with"quotes]`, | ||
"table[with]brackets": "[table[with]]brackets]", | ||
} | ||
|
||
for ident, expected := range table { | ||
t.Run(expected, func(t *testing.T) { | ||
actual := MSSQL.QuotedIdent(ident) | ||
if expected != actual { | ||
t.Errorf("Expected %s, got %s", expected, actual) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.