This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
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

113 lines
4.0 KiB
Go

package schema
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"hash/fnv"
"time"
)
const (
migrationLockPrefix = "bd_schema_init:"
migrationLockNameMaxLength = 64
// Keep this below the server-mode callers' retry budgets so a contended
// lock wait can time out and still leave room for a real retry.
migrationLockAcquireTimeoutSeconds = 5
migrationLockCleanupTimeout = 5 * time.Second
)
var (
// ErrMigrationLockUnavailable marks transient failures to acquire the
// per-database migration lock.
ErrMigrationLockUnavailable = errors.New("schema migration lock unavailable")
// ErrMigrationLockRelease marks uncertain lock cleanup after migrations.
ErrMigrationLockRelease = errors.New("schema migration lock release failed")
)
// IsMigrationLockError reports whether err came from migration lock acquisition
// or cleanup and should be treated as retryable by callers with a retry budget.
func IsMigrationLockError(err error) bool {
return errors.Is(err, ErrMigrationLockUnavailable) || errors.Is(err, ErrMigrationLockRelease)
}
// MigrationLockName returns a Dolt/MySQL named-lock key within the documented
// 64-byte limit while preserving readable names when they already fit.
func MigrationLockName(databaseName string) string {
raw := migrationLockPrefix + databaseName
if len(raw) <= migrationLockNameMaxLength {
return raw
}
h := fnv.New64a()
_, _ = h.Write([]byte(databaseName))
return fmt.Sprintf("%s%016x", migrationLockPrefix, h.Sum64())
}
// MigrateUpWithLock serializes schema migrations for a single Dolt sql-server
// database. conn must be a pinned *sql.Conn because MySQL/Dolt named locks are
// session-scoped; GET_LOCK, migrations, and RELEASE_LOCK must run on the same
// server session. Embedded Dolt intentionally uses bare MigrateUp because it
// relies on the embedded driver's file/concurrency controls instead of
// sql-server session locks.
func MigrateUpWithLock(ctx context.Context, conn *sql.Conn, databaseName string) (applied int, err error) {
lockName := MigrationLockName(databaseName)
if err := AcquireMigrationLock(ctx, conn, lockName); err != nil {
return 0, err
}
defer func() {
if releaseErr := ReleaseMigrationLock(conn, lockName); releaseErr != nil {
err = errors.Join(err, releaseErr)
}
}()
return MigrateUp(ctx, conn)
}
// AcquireMigrationLock acquires the named schema migration lock on the pinned
// connection's current Dolt/MySQL session.
func AcquireMigrationLock(ctx context.Context, conn *sql.Conn, lockName string) error {
var locked sql.NullInt64
if err := conn.QueryRowContext(ctx, "SELECT GET_LOCK(?, ?)", lockName, migrationLockAcquireTimeoutSeconds).Scan(&locked); err != nil {
return fmt.Errorf("schema: acquire migration lock: %w: %w", ErrMigrationLockUnavailable, err)
}
if !locked.Valid {
return fmt.Errorf("schema: acquire migration lock: %w: returned NULL", ErrMigrationLockUnavailable)
}
if locked.Int64 != 1 {
return fmt.Errorf("schema: acquire migration lock: %w: timeout", ErrMigrationLockUnavailable)
}
return nil
}
// ReleaseMigrationLock releases the named schema migration lock from the same
// pinned Dolt/MySQL session used to acquire it.
func ReleaseMigrationLock(conn *sql.Conn, lockName string) error {
cleanupCtx, cancel := context.WithTimeout(context.Background(), migrationLockCleanupTimeout)
defer cancel()
var released sql.NullInt64
if err := conn.QueryRowContext(cleanupCtx, "SELECT RELEASE_LOCK(?)", lockName).Scan(&released); err != nil {
discardConn(conn)
return fmt.Errorf("schema: release migration lock: %w: %w", ErrMigrationLockRelease, err)
}
if !released.Valid {
discardConn(conn)
return fmt.Errorf("schema: release migration lock: %w: returned NULL", ErrMigrationLockRelease)
}
if released.Int64 != 1 {
discardConn(conn)
return fmt.Errorf("schema: release migration lock: %w: returned %d", ErrMigrationLockRelease, released.Int64)
}
return nil
}
func discardConn(conn *sql.Conn) {
_ = conn.Raw(func(driverConn any) error {
return driver.ErrBadConn
})
}