Introduce the StoneSQL migration engine with embedded SQL migration support, scientific error tracking, and extensive documentation in the README. Include implementation of core functionality, error handling, and unit tests to validate success and failure scenarios.
44 lines
896 B
Go
44 lines
896 B
Go
package stonesql
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"gitstormr.dev/stone-utils/stonesql/test"
|
|
)
|
|
|
|
type migratorSuccessMock struct{}
|
|
|
|
func (m *migratorSuccessMock) ExecuteMigration(
|
|
ctx context.Context, name string, sqlContent string,
|
|
) error {
|
|
fmt.Println(name, sqlContent)
|
|
return nil
|
|
}
|
|
|
|
type migratorFailureMock struct{}
|
|
|
|
func (m *migratorFailureMock) ExecuteMigration(
|
|
ctx context.Context, name string, sqlContent string,
|
|
) error {
|
|
return fmt.Errorf("failed to execute migration %s", name)
|
|
}
|
|
|
|
func Test_MigrationSuccess(t *testing.T) {
|
|
migrator := &migratorSuccessMock{}
|
|
err := RunMigrations(context.Background(), migrator, test.TestFS, ".")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func Test_MigrationFailure(t *testing.T) {
|
|
migrator := &migratorFailureMock{}
|
|
err := RunMigrations(context.Background(), migrator, test.TestFS, ".")
|
|
|
|
if err == nil {
|
|
t.Error("expected error")
|
|
}
|
|
}
|