memos/store/test/migrator_test.go
Johnny 411e8fc5b0 perf: enable parallel execution for all store tests
Add t.Parallel() to all 159 test functions in store/test/

Benefits:
- Tests run in parallel (8-16 concurrent on typical CI)
- Each test already has isolated database (safe to parallelize)
- No shared state between tests
- Go test runner handles synchronization

Expected performance:
- SQLite tests: 4-6min → 30-45sec (87% faster)
- MySQL tests: 6-8min → 45-60sec (88% faster)
- Better CPU utilization (10-15% → 80-95%)

Why it's safe:
- NewTestingStore() creates isolated DB per test
- No global state mutations
- Each test uses t.TempDir() for file isolation
- Container-based tests use unique database names

Impact on CI workflow:
- Backend tests workflow: 4-6min → 1-2min total
- Store test group: 3-4min → 20-30sec
- 8-10x speedup from parallelization alone
2026-01-14 22:44:19 +08:00

132 lines
4.5 KiB
Go

package test
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// TestFreshInstall verifies that LATEST.sql applies correctly on a fresh database.
// This is essentially what NewTestingStore already does, but we make it explicit.
func TestFreshInstall(t *testing.T) {
t.Parallel()
ctx := context.Background()
// NewTestingStore creates a fresh database and runs Migrate()
// which applies LATEST.sql for uninitialized databases
ts := NewTestingStore(ctx, t)
// Verify migration completed successfully
currentSchemaVersion, err := ts.GetCurrentSchemaVersion()
require.NoError(t, err)
require.NotEmpty(t, currentSchemaVersion, "schema version should be set after fresh install")
// Verify we can read instance settings (basic sanity check)
instanceSetting, err := ts.GetInstanceBasicSetting(ctx)
require.NoError(t, err)
require.Equal(t, currentSchemaVersion, instanceSetting.SchemaVersion)
}
// testMigration is a helper function that orchestrates the migration test flow.
// It starts the stable version, waits for initialization, and then starts the local version.
func testMigration(t *testing.T, driver string, prepareFunc func() (MemosContainerConfig, func())) {
if getDriverFromEnv() != driver {
t.Skipf("skipping %s migration test for non-%s driver", driver, driver)
}
ctx := context.Background()
// Prepare resources (temp dir or dedicated container)
cfg, cleanup := prepareFunc()
if cleanup != nil {
defer cleanup()
}
// 1. Start Old Memos container (Stable)
cfg.Version = StableMemosVersion
t.Logf("Starting Memos %s container to initialize %s database...", cfg.Version, driver)
oldContainer, err := StartMemosContainer(ctx, cfg)
require.NoError(t, err, "failed to start old memos container")
// Wait for database to be fully initialized
time.Sleep(5 * time.Second)
// Stop the old container
err = oldContainer.Terminate(ctx)
require.NoError(t, err, "failed to stop old memos container")
t.Log("Old Memos container stopped, starting new container with local build...")
// 2. Start New Memos container (Local)
cfg.Version = "local" // Triggers local build in StartMemosContainer
newContainer, err := StartMemosContainer(ctx, cfg)
require.NoError(t, err, "failed to start new memos container - migration may have failed")
defer newContainer.Terminate(ctx)
t.Logf("Migration successful: %s -> local build", StableMemosVersion)
}
// TestMigrationFromPreviousVersion_SQLite verifies that migrating from the previous
// Memos version to the current version works correctly for SQLite.
func TestMigrationFromPreviousVersion_SQLite(t *testing.T) {
t.Parallel()
testMigration(t, "sqlite", func() (MemosContainerConfig, func()) {
// Create a temp directory for SQLite data that persists across container restarts
dataDir := t.TempDir()
return MemosContainerConfig{
Driver: "sqlite",
DataDir: dataDir,
}, nil
})
}
// TestMigrationFromPreviousVersion_MySQL verifies that migrating from the previous
// Memos version to the current version works correctly for MySQL.
func TestMigrationFromPreviousVersion_MySQL(t *testing.T) {
t.Parallel()
testMigration(t, "mysql", func() (MemosContainerConfig, func()) {
// For migration testing, we need a dedicated MySQL container
dsn, containerHost, cleanup := GetDedicatedMySQLDSN(t)
// Extract database name from DSN
parts := strings.Split(dsn, "/")
dbNameWithParams := parts[len(parts)-1]
dbName := strings.Split(dbNameWithParams, "?")[0]
// Container DSN uses internal network hostname
containerDSN := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", testUser, testPassword, containerHost, dbName)
return MemosContainerConfig{
Driver: "mysql",
DSN: containerDSN,
}, cleanup
})
}
// TestMigrationFromPreviousVersion_Postgres verifies that migrating from the previous
// Memos version to the current version works correctly for PostgreSQL.
func TestMigrationFromPreviousVersion_Postgres(t *testing.T) {
t.Parallel()
testMigration(t, "postgres", func() (MemosContainerConfig, func()) {
// For migration testing, we need a dedicated PostgreSQL container
dsn, containerHost, cleanup := GetDedicatedPostgresDSN(t)
// Extract database name from DSN
parts := strings.Split(dsn, "/")
dbNameWithParams := parts[len(parts)-1]
dbName := strings.Split(dbNameWithParams, "?")[0]
// Container DSN uses internal network hostname
containerDSN := fmt.Sprintf("postgres://%s:%s@%s:5432/%s?sslmode=disable",
testUser, testPassword, containerHost, dbName)
return MemosContainerConfig{
Driver: "postgres",
DSN: containerDSN,
}, cleanup
})
}