-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18a624c
commit cae791d
Showing
4 changed files
with
128 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
# CONTAINER FOR BUILDING BINARY | ||
FROM golang:1.22.4 AS build | ||
|
||
WORKDIR $GOPATH/src/github.com/0xPolygon/cdk | ||
|
||
# INSTALL DEPENDENCIES | ||
RUN go install github.com/gobuffalo/packr/v2/[email protected] | ||
COPY go.mod go.sum /src/ | ||
RUN cd /src && go mod download | ||
COPY go.mod go.sum ./ | ||
RUN go mod download | ||
|
||
# BUILD BINARY | ||
COPY . /src | ||
RUN cd /src/aggregator/db && packr2 | ||
RUN cd /src && make build | ||
COPY . . | ||
RUN make build | ||
|
||
# CONTAINER FOR RUNNING BINARY | ||
FROM alpine:3.18.4 | ||
COPY --from=build /src/dist/cdk /app/cdk | ||
|
||
COPY --from=build /go/src/github.com/0xPolygon/cdk/dist/cdk /app/cdk | ||
|
||
RUN apk update && apk add postgresql15-client | ||
|
||
EXPOSE 8123 | ||
|
||
CMD ["/bin/sh", "-c", "/app/cdk run"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package db | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
|
||
"github.com/0xPolygon/cdk/log" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/jackc/pgx/v4/stdlib" | ||
migrate "github.com/rubenv/sql-migrate" | ||
) | ||
|
||
const ( | ||
// AggregatorMigrationName is the name of the migration used to associate with the migrations dir | ||
AggregatorMigrationName = "zkevm-aggregator-db" | ||
) | ||
|
||
var ( | ||
//go:embed migrations/aggregator/*.sql | ||
embedAggregatorMigrations embed.FS | ||
|
||
// embedMigrations is a map of migrations with the name | ||
embedMigrations = map[string]embed.FS{} | ||
) | ||
|
||
func init() { | ||
embedMigrations[AggregatorMigrationName] = embedAggregatorMigrations | ||
} | ||
|
||
// RunMigrationsUp runs migrate-up for the given config. | ||
func RunMigrationsUp(cfg Config, name string) error { | ||
log.Info("running migrations up") | ||
return runMigrations(cfg, name, migrate.Up) | ||
} | ||
|
||
// CheckMigrations runs migrate-up for the given config. | ||
func CheckMigrations(cfg Config, name string) error { | ||
return checkMigrations(cfg, name) | ||
} | ||
|
||
// RunMigrationsDown runs migrate-down for the given config. | ||
func RunMigrationsDown(cfg Config, name string) error { | ||
log.Info("running migrations down") | ||
return runMigrations(cfg, name, migrate.Down) | ||
} | ||
|
||
// runMigrations will execute pending migrations if needed to keep | ||
// the database updated with the latest changes in either direction, | ||
// up or down. | ||
func runMigrations(cfg Config, name string, direction migrate.MigrationDirection) error { | ||
c, err := pgx.ParseConfig(fmt.Sprintf("postgres://%s:%s@%s:%s/%s", cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Name)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
db := stdlib.OpenDB(*c) | ||
|
||
embedMigration, ok := embedMigrations[name] | ||
if !ok { | ||
return fmt.Errorf("migration not found with name: %v", name) | ||
} | ||
|
||
var migrations = &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigration} | ||
nMigrations, err := migrate.Exec(db, "postgres", migrations, direction) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Info("successfully ran ", nMigrations, " migrations") | ||
return nil | ||
} | ||
|
||
func checkMigrations(cfg Config, name string) error { | ||
c, err := pgx.ParseConfig(fmt.Sprintf("postgres://%s:%s@%s:%s/%s", cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Name)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
db := stdlib.OpenDB(*c) | ||
|
||
embedMigration, ok := embedMigrations[name] | ||
if !ok { | ||
return fmt.Errorf("migration not found with name: %v", name) | ||
} | ||
|
||
migrationSource := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigration} | ||
migrations, err := migrationSource.FindMigrations() | ||
if err != nil { | ||
log.Errorf("error getting migrations from source: %v", err) | ||
return err | ||
} | ||
|
||
var expected int | ||
for _, migration := range migrations { | ||
if len(migration.Up) != 0 { | ||
expected++ | ||
} | ||
} | ||
|
||
var actual int | ||
query := `SELECT COUNT(1) FROM public.gorp_migrations` | ||
err = db.QueryRow(query).Scan(&actual) | ||
if err != nil { | ||
log.Error("error getting migrations count: ", err) | ||
return err | ||
} | ||
if expected == actual { | ||
log.Infof("Found %d migrations as expected", actual) | ||
} else { | ||
return fmt.Errorf("error the component needs to run %d migrations before starting. DB only contains %d migrations", expected, actual) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters