All checks were successful
Check / check (pull_request) Successful in 3m15s
Pre-1.0 software with no installed base — no need to handle converting from old TEXT-based schema_migrations format. Removed: convertLegacyMigrations, ensureBootstrapVersion, readLegacyVersions, rebuildMigrationsTable, and TestApplyMigrationsLegacyConversion. Simplified bootstrapMigrationsTable to only check existence and run 000_migration.sql if missing.
118 lines
2.7 KiB
Go
118 lines
2.7 KiB
Go
package database_test
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"testing"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"sneak.berlin/go/upaas/internal/database"
|
|
)
|
|
|
|
func TestParseMigrationVersion(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
filename string
|
|
wantVersion int
|
|
wantErr bool
|
|
}{
|
|
{filename: "000_migration.sql", wantVersion: 0},
|
|
{filename: "001_initial.sql", wantVersion: 1},
|
|
{filename: "002_remove_container_id.sql", wantVersion: 2},
|
|
{filename: "007_add_resource_limits.sql", wantVersion: 7},
|
|
{filename: "100_large_version.sql", wantVersion: 100},
|
|
{filename: ".sql", wantErr: true},
|
|
{filename: "_foo.sql", wantErr: true},
|
|
{filename: "abc_foo.sql", wantErr: true},
|
|
{filename: "1a2_bad.sql", wantErr: true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.filename, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
version, err := database.ParseMigrationVersion(tt.filename)
|
|
if tt.wantErr {
|
|
assert.Error(t, err)
|
|
|
|
return
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tt.wantVersion, version)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyMigrationsFreshDatabase(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:?_foreign_keys=on")
|
|
require.NoError(t, err)
|
|
|
|
defer func() { _ = db.Close() }()
|
|
|
|
ctx := context.Background()
|
|
|
|
err = database.ApplyMigrations(ctx, db, nil)
|
|
require.NoError(t, err)
|
|
|
|
// Verify schema_migrations table exists with INTEGER version column.
|
|
var version int
|
|
|
|
err = db.QueryRowContext(ctx,
|
|
"SELECT version FROM schema_migrations WHERE version = 0",
|
|
).Scan(&version)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 0, version)
|
|
|
|
// Verify that all migrations were recorded.
|
|
var count int
|
|
|
|
err = db.QueryRowContext(ctx,
|
|
"SELECT COUNT(*) FROM schema_migrations",
|
|
).Scan(&count)
|
|
require.NoError(t, err)
|
|
// 000 bootstrap + 001 through 007 = 8 entries.
|
|
assert.Equal(t, 8, count)
|
|
|
|
// Verify application tables were created by the migrations.
|
|
var tableCount int
|
|
|
|
err = db.QueryRowContext(ctx,
|
|
"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='users'",
|
|
).Scan(&tableCount)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 1, tableCount)
|
|
}
|
|
|
|
func TestApplyMigrationsIdempotent(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:?_foreign_keys=on")
|
|
require.NoError(t, err)
|
|
|
|
defer func() { _ = db.Close() }()
|
|
|
|
ctx := context.Background()
|
|
|
|
// Apply twice — second run should be a no-op.
|
|
err = database.ApplyMigrations(ctx, db, nil)
|
|
require.NoError(t, err)
|
|
|
|
err = database.ApplyMigrations(ctx, db, nil)
|
|
require.NoError(t, err)
|
|
|
|
var count int
|
|
|
|
err = db.QueryRowContext(ctx,
|
|
"SELECT COUNT(*) FROM schema_migrations",
|
|
).Scan(&count)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 8, count)
|
|
}
|