caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

174 lines
6.2 KiB
Go

package schema
import (
"context"
"errors"
"regexp"
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
)
func TestMigrationLockNameUsesRawNameWhenBounded(t *testing.T) {
got := MigrationLockName("testdb_short")
want := migrationLockPrefix + "testdb_short"
if got != want {
t.Fatalf("MigrationLockName() = %q, want %q", got, want)
}
}
func TestMigrationLockNameHashesLongNames(t *testing.T) {
dbName := strings.Repeat("a", 64)
got := MigrationLockName(dbName)
if len(got) > migrationLockNameMaxLength {
t.Fatalf("MigrationLockName() length = %d, want <= %d", len(got), migrationLockNameMaxLength)
}
if got == migrationLockPrefix+dbName {
t.Fatalf("MigrationLockName() used over-limit raw name %q", got)
}
if got != MigrationLockName(dbName) {
t.Fatal("MigrationLockName() is not deterministic")
}
}
func TestIsMigrationLockError(t *testing.T) {
err := errors.Join(ErrMigrationLockUnavailable, errors.New("timeout"))
if !IsMigrationLockError(err) {
t.Fatal("IsMigrationLockError() = false, want true")
}
}
func TestMigrateUpRunsWithoutAdvisoryLock(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("create sql mock: %v", err)
}
defer db.Close()
expectOnePendingMigration(t, mock)
applied, err := MigrateUp(context.Background(), db)
if err != nil {
t.Fatalf("MigrateUp() error = %v", err)
}
if applied != 1 {
t.Fatalf("MigrateUp() applied = %d, want 1", applied)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
}
func TestMigrateUpWithLockUsesDatabaseScopedLockOnly(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("create sql mock: %v", err)
}
defer db.Close()
ctx := context.Background()
conn, err := db.Conn(ctx)
if err != nil {
t.Fatalf("pin mock connection: %v", err)
}
defer conn.Close()
lockName := MigrationLockName("testdb")
mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")).
WithArgs(lockName, migrationLockAcquireTimeoutSeconds).
WillReturnRows(sqlmock.NewRows([]string{"locked"}).AddRow(1))
expectOnePendingMigration(t, mock)
mock.ExpectQuery(regexp.QuoteMeta("SELECT RELEASE_LOCK(?)")).
WithArgs(lockName).
WillReturnRows(sqlmock.NewRows([]string{"released"}).AddRow(1))
applied, err := MigrateUpWithLock(ctx, conn, "testdb")
if err != nil {
t.Fatalf("MigrateUpWithLock() error = %v", err)
}
if applied != 1 {
t.Fatalf("MigrateUpWithLock() applied = %d, want 1", applied)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
}
func expectOnePendingMigration(t *testing.T, mock sqlmock.Sqlmock) {
t.Helper()
latest := LatestVersion()
latestIgnored := LatestIgnoredVersion()
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", latest-1)
expectDoltStatusRows(mock)
expectDoltStatusRows(mock)
// MigrateUp probes the aux-rekey crash sentinel (bd-578h9.16); this
// mocked world has no local_metadata table, so no crashed pass.
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM INFORMATION_SCHEMA\.TABLES`).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
// MigrateUp captures the pre-pass main cursor for the aux re-key
// watershed (bd-578h9.4) before the main migrations run.
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", latest-1)
mock.ExpectExec("(?s)^CREATE TABLE IF NOT EXISTS schema_migrations").
WillReturnResult(sqlmock.NewResult(0, 0))
expectContentHashColumnExists(mock)
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", latest-1)
mock.ExpectExec("(?s).*").
WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectExec(regexp.QuoteMeta("INSERT IGNORE INTO schema_migrations (version, content_hash) VALUES (?, ?)")).
WithArgs(latest, sqlmock.AnyArg()).
WillReturnResult(sqlmock.NewResult(0, 1))
expectScalar(mock, "SELECT COUNT(*) FROM custom_types", "count", 1)
expectScalar(mock, "SELECT COUNT(*) FROM custom_statuses", "count", 1)
// rekeyDependencyIDs probes whether each edge table has an id column; this
// mocked world has no such table, so both probes return 0 and the re-key
// no-ops without scanning/updating rows.
expectColumnExists(mock, false)
expectColumnExists(mock, false)
// rekeyAuxRowIDs reads the ignored cursor to see whether its clone-local
// marker is pending; at latest it is not, so the re-key no-ops.
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM ignored_schema_migrations", "version", latestIgnored)
mock.ExpectExec(regexp.QuoteMeta("REPLACE INTO dolt_ignore VALUES ('ignored_schema_migrations', true)")).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec("(?s)^CREATE TABLE IF NOT EXISTS ignored_schema_migrations").
WillReturnResult(sqlmock.NewResult(0, 0))
expectContentHashColumnExists(mock)
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM ignored_schema_migrations", "version", latestIgnored)
expectDoltStatusRows(mock)
expectDoltStatusRows(mock)
mock.ExpectQuery("(?s)SELECT t\\.TABLE_NAME\\s+FROM INFORMATION_SCHEMA\\.TABLES t").
WillReturnRows(sqlmock.NewRows([]string{"TABLE_NAME"}).AddRow("schema_migrations"))
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_ADD('-f', ?)")).
WithArgs("schema_migrations").
WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', 'schema: apply migrations')")).
WillReturnResult(sqlmock.NewResult(0, 0))
}
func expectColumnExists(mock sqlmock.Sqlmock, present bool) {
n := 0
if present {
n = 1
}
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM INFORMATION_SCHEMA\.COLUMNS`).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(n))
}
// expectContentHashColumnExists mocks the idempotent ensureContentHashColumn
// probe, reporting that the content_hash column already exists (so no ALTER runs).
func expectContentHashColumnExists(mock sqlmock.Sqlmock) {
expectColumnExists(mock, true)
}
func expectScalar(mock sqlmock.Sqlmock, query, column string, value any) {
mock.ExpectQuery(regexp.QuoteMeta(query)).
WillReturnRows(sqlmock.NewRows([]string{column}).AddRow(value))
}
func expectDoltStatusRows(mock sqlmock.Sqlmock) {
mock.ExpectQuery("(?s)SELECT s\\.table_name, s\\.staged\\s+FROM dolt_status s").
WillReturnRows(sqlmock.NewRows([]string{"table_name", "staged"}))
}