fix: verify server snapshots and recover whole migration sessions (#3)
This commit is contained in:
1 parent
3d4101ecc0
commit
8569871a17
29 files changed
+1923
-133
No files matched your search
@@ -0,0 +1,70 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
qfassets "git.mchus.pro/mchus/quoteforge"
|
||||
"io/fs"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSQLStatementParser(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, sql string
|
||||
want []string
|
||||
fail bool
|
||||
}{
|
||||
{"empty", "-- comment;\n# more;", nil, false},
|
||||
{"literal separators", "SELECT 'a;b', \"c;d\", `e;f`; SELECT 2;", []string{"SELECT 'a;b', \"c;d\", `e;f`", "SELECT 2"}, false},
|
||||
{"doubled quotes", "SELECT 'a'';b', `a``;b`;", []string{"SELECT 'a'';b', `a``;b`"}, false},
|
||||
{"backslash quote", "SELECT 'a\\';b'; SELECT 2", []string{"SELECT 'a\\';b'", "SELECT 2"}, false},
|
||||
{"inline comments", "SELECT/* ; */ 1; -- end;\nSELECT 2# end;\n;", []string{"SELECT 1", "SELECT 2"}, false},
|
||||
{"not a comment", "SELECT 4--2;", []string{"SELECT 4--2"}, false},
|
||||
{"unclosed quote", "SELECT 'a;", nil, true},
|
||||
{"unclosed block", "SELECT 1; /*", nil, true},
|
||||
{"executable comment", "/*! SELECT 1 */", nil, true},
|
||||
{"MariaDB comment", "/*M! SELECT 1 */", nil, true},
|
||||
{"delimiter", "DELIMITER $$\nSELECT 1", nil, true},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := splitSQLStatements(tt.sql)
|
||||
if (err != nil) != tt.fail {
|
||||
t.Fatalf("error=%v", err)
|
||||
}
|
||||
if !tt.fail && !reflect.DeepEqual(got, tt.want) {
|
||||
t.Fatalf("got=%q want=%q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
long := "SELECT '" + strings.Repeat("x", 2<<20) + ";y';"
|
||||
if got, err := splitSQLStatements(long); err != nil || len(got) != 1 {
|
||||
t.Fatalf("large statement: count=%d err=%v", len(got), err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmbeddedServerMigrationSQL(t *testing.T) {
|
||||
migrations, err := qfassets.MigrationsFS()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files, err := listSQLMigrationFiles(migrations)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range files {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
content, err := fs.ReadFile(migrations, name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
statements, err := splitSQLStatements(string(content))
|
||||
if err != nil || len(statements) == 0 {
|
||||
t.Fatalf("count=%d err=%v", len(statements), err)
|
||||
}
|
||||
checks, err := parseMigrationVerifications(string(content))
|
||||
if err != nil || len(checks) == 0 {
|
||||
t.Fatalf("checks=%d err=%v", len(checks), err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user