#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")/../.."

legacy_tidb_migrations="$(find internal/store -path '*/tidb_migrations/*.sql' -print 2>/dev/null || true)"
if [[ -n "${legacy_tidb_migrations}" ]]; then
  echo "error: active TiDB migrations must live under internal/tidb/migrations, not store domain migration directories:" >&2
  printf '%s\n' "${legacy_tidb_migrations}" >&2
  exit 1
fi

legacy_tidb_embeds="$(rg -n 'TiDBMigration(Files|Dir)|//go:embed[^\n]*tidb_migrations' internal/store --glob '*.go' 2>/dev/null || true)"
if [[ -n "${legacy_tidb_embeds}" ]]; then
  echo "error: store domains must not expose TiDB migration embed registries; use internal/tidb/migrations as the single source:" >&2
  printf '%s\n' "${legacy_tidb_embeds}" >&2
  exit 1
fi

go run ./guardrails/store/check-store-sql-ownership.go
