Commit c48b7bc7 authored by Will Cory's avatar Will Cory

explicit migrations dir path

parent 6ef9887c
...@@ -21,11 +21,12 @@ RUN make indexer ...@@ -21,11 +21,12 @@ RUN make indexer
FROM alpine:3.18 FROM alpine:3.18
COPY --from=builder /app/indexer/indexer /app/indexer/indexer COPY --from=builder /app/indexer/indexer /usr/local/bin
COPY --from=builder /app/indexer/indexer.toml /app/indexer/indexer.toml
COPY --from=builder /app/indexer/migrations /app/indexer/migrations COPY --from=builder /app/indexer/migrations /app/indexer/migrations
WORKDIR /app WORKDIR /app
ENV PATH="/app:${PATH}" ENV INDEXER_MIGRATIONS_DIR="/app/indexer/migrations"
CMD ["indexer", "index", "--config", "/app/indexer/indexer.toml"] CMD ["indexer", "index", "--config", "/app/indexer/indexer.toml"]
...@@ -19,6 +19,12 @@ var ( ...@@ -19,6 +19,12 @@ var (
Usage: "path to config file", Usage: "path to config file",
EnvVars: []string{"INDEXER_CONFIG"}, EnvVars: []string{"INDEXER_CONFIG"},
} }
MigrationsFlag = &cli.StringFlag{
Name: "migrations-dir",
Value: "./migrations",
Usage: "path to migrations folder",
EnvVars: []string{"INDEXER_MIGRATIONS_DIR"},
}
) )
func runIndexer(ctx *cli.Context) error { func runIndexer(ctx *cli.Context) error {
...@@ -67,6 +73,7 @@ func runApi(ctx *cli.Context) error { ...@@ -67,6 +73,7 @@ func runApi(ctx *cli.Context) error {
func runMigrations(ctx *cli.Context) error { func runMigrations(ctx *cli.Context) error {
log := log.NewLogger(log.ReadCLIConfig(ctx)).New("role", "api") log := log.NewLogger(log.ReadCLIConfig(ctx)).New("role", "api")
cfg, err := config.LoadConfig(log, ctx.String(ConfigFlag.Name)) cfg, err := config.LoadConfig(log, ctx.String(ConfigFlag.Name))
migrationsDir := ctx.String(MigrationsFlag.Name)
if err != nil { if err != nil {
log.Error("failed to load config", "err", err) log.Error("failed to load config", "err", err)
return err return err
...@@ -79,12 +86,14 @@ func runMigrations(ctx *cli.Context) error { ...@@ -79,12 +86,14 @@ func runMigrations(ctx *cli.Context) error {
} }
defer db.Close() defer db.Close()
return db.ExecuteSQLMigration() return db.ExecuteSQLMigration(migrationsDir)
} }
func newCli(GitCommit string, GitDate string) *cli.App { func newCli(GitCommit string, GitDate string) *cli.App {
flags := []cli.Flag{ConfigFlag} flags := []cli.Flag{ConfigFlag}
flags = append(flags, log.CLIFlags("INDEXER")...) flags = append(flags, log.CLIFlags("INDEXER")...)
migrationFlags := []cli.Flag{MigrationsFlag, ConfigFlag}
migrationFlags = append(migrationFlags, log.CLIFlags("INDEXER")...)
return &cli.App{ return &cli.App{
Version: params.VersionWithCommit(GitCommit, GitDate), Version: params.VersionWithCommit(GitCommit, GitDate),
Description: "An indexer of all optimism events with a serving api layer", Description: "An indexer of all optimism events with a serving api layer",
...@@ -104,7 +113,7 @@ func newCli(GitCommit string, GitDate string) *cli.App { ...@@ -104,7 +113,7 @@ func newCli(GitCommit string, GitDate string) *cli.App {
}, },
{ {
Name: "migrate", Name: "migrate",
Flags: flags, Flags: migrationFlags,
Description: "Runs the database migrations", Description: "Runs the database migrations",
Action: runMigrations, Action: runMigrations,
}, },
......
...@@ -103,8 +103,8 @@ func dbFromGormTx(tx *gorm.DB) *DB { ...@@ -103,8 +103,8 @@ func dbFromGormTx(tx *gorm.DB) *DB {
} }
} }
func (db *DB) ExecuteSQLMigration() error { func (db *DB) ExecuteSQLMigration(migrationsFolder string) error {
err := filepath.Walk("./migrations", func(path string, info os.FileInfo, err error) error { err := filepath.Walk(migrationsFolder, func(path string, info os.FileInfo, err error) error {
// Check for any walking error // Check for any walking error
if err != nil { if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to process migration file: %s", path)) return errors.Wrap(err, fmt.Sprintf("Failed to process migration file: %s", path))
......
...@@ -17,6 +17,27 @@ services: ...@@ -17,6 +17,27 @@ services:
- postgres_data:/data/postgres - postgres_data:/data/postgres
- ./migrations:/docker-entrypoint-initdb.d/ - ./migrations:/docker-entrypoint-initdb.d/
migrations:
build:
context: ..
dockerfile: indexer/Dockerfile
command: ["indexer", "migrate"]
environment:
- INDEXER_RPC_URL_L1=$INDEXER_RPC_URL_L1
- INDEXER_RPC_URL_L2=$INDEXER_RPC_URL_L2
- INDEXER_CONFIG=/indexer/indexer.toml
- INDEXER_CHAIN_PRESET=$INDEXER_CHAIN_PRESET
- INDEXER_DB_PORT=5432
- INDEXER_DB_HOST=postgres
- INDEXER_DB_USER=db_username
- INDEXER_DB_PASS=db_password
- INDEXER_DB_NAME=db_name
volumes:
- ./indexer.toml:/indexer/indexer.toml
depends_on:
postgres:
condition: service_healthy
indexer: indexer:
build: build:
context: .. context: ..
...@@ -37,6 +58,10 @@ services: ...@@ -37,6 +58,10 @@ services:
depends_on: depends_on:
postgres: postgres:
condition: service_healthy condition: service_healthy
depends_on:
migrations:
condition: service_started
api: api:
build: build:
......
...@@ -141,7 +141,7 @@ func setupTestDatabase(t *testing.T) string { ...@@ -141,7 +141,7 @@ func setupTestDatabase(t *testing.T) string {
db, err := database.NewDB(dbConfig) db, err := database.NewDB(dbConfig)
require.NoError(t, err) require.NoError(t, err)
defer db.Close() defer db.Close()
err = db.ExecuteSQLMigration() err = db.ExecuteSQLMigration("../migrations")
require.NoError(t, err) require.NoError(t, err)
t.Logf("database %s setup and migrations executed", dbName) t.Logf("database %s setup and migrations executed", dbName)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment