-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1313 from openmeterio/feat/migrations
Add versioned migrations
- Loading branch information
Showing
42 changed files
with
1,004 additions
and
136 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
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
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,98 @@ | ||
env "local" { | ||
src = "${local.schema_src}" | ||
|
||
migration { | ||
dir = "${local.migrations_dir}" | ||
format = "${local.migrations_format}" | ||
} | ||
|
||
format { | ||
migrate { | ||
diff = "{{ sql . \" \" }}" | ||
} | ||
} | ||
|
||
url = "${local.local_url}" | ||
|
||
// Define the URL of the Dev Database for this environment | ||
// See: https://atlasgo.io/concepts/dev-database | ||
dev = "docker://postgres/15/dev?search_path=public" | ||
|
||
lint { | ||
// Lint the effects of the 100 latest migration files | ||
latest = 100 | ||
} | ||
} | ||
|
||
env "ci" { | ||
src = "${local.schema_src}" | ||
|
||
migration { | ||
dir = "${local.migrations_dir}" | ||
format = "${local.migrations_format}" | ||
} | ||
|
||
format { | ||
migrate { | ||
diff = "{{ sql . \" \" }}" | ||
} | ||
} | ||
|
||
dev = "${local.ci_url}" | ||
} | ||
|
||
// CAN be used for all remote deployments | ||
env "remote" { | ||
src = "${local.schema_src}" | ||
|
||
migration { | ||
// Define the directory where the migrations are stored. | ||
dir = "file://tools/migrate/migrations" | ||
// We use golang-migrate | ||
format = "${local.migrations_format}" | ||
// Remote deployments already had auto deploy present | ||
baseline = "${local.init_migration_ts}" | ||
} | ||
|
||
format { | ||
migrate { | ||
diff = "{{ sql . \" \" }}" | ||
} | ||
} | ||
|
||
// Define the URL of the Dev Database for this environment | ||
// See: https://atlasgo.io/concepts/dev-database | ||
dev = "docker://postgres/15/dev?search_path=public" | ||
} | ||
|
||
locals { | ||
// Define the directory where the schema definition resides. | ||
schema_src = "ent://openmeter/ent/schema" | ||
// Define the initial migration timestamp | ||
init_migration_ts = "20240826120919" | ||
// Define the directory where the migrations are stored. | ||
migrations_dir = "file://tools/migrate/migrations" | ||
// We use golang-migrate | ||
migrations_format = "golang-migrate" | ||
// Define common connection URLs | ||
local_url = "postgres://postgres:postgres@localhost:5432/postgres?search_path=public&sslmode=disable" | ||
ci_url = "postgres://postgres:postgres@postgres:5432/postgres?search_path=public&sslmode=disable" | ||
} | ||
|
||
lint { | ||
non_linear { | ||
error = true | ||
} | ||
|
||
destructive { | ||
error = false | ||
} | ||
|
||
data_depend { | ||
error = true | ||
} | ||
|
||
incompatible { | ||
error = true | ||
} | ||
} |
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,85 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/openmeterio/openmeter/ci/internal/dagger" | ||
"github.com/sourcegraph/conc/pool" | ||
) | ||
|
||
func (m *Ci) Migrate() *Migrate { | ||
return &Migrate{ | ||
Source: m.Source, | ||
} | ||
} | ||
|
||
type Migrate struct { | ||
Source *dagger.Directory | ||
} | ||
|
||
func (m *Migrate) Check(ctx context.Context) error { | ||
app := goModuleCross(""). | ||
WithSource(m.Source). | ||
Container(). | ||
WithEnvVariable("GOFLAGS", "-tags=musl") | ||
|
||
bin := dag.Container(dagger.ContainerOpts{ | ||
Platform: "linux/amd64", | ||
}).From(atlasImage).File("atlas") | ||
|
||
atlas := app. | ||
WithFile("/bin/atlas", bin). | ||
WithDirectory("openmeter/ent", m.Source.Directory("openmeter/ent")). | ||
WithDirectory("tools/migrate/migrations", m.Source.Directory("tools/migrate/migrations")). | ||
WithFile("atlas.hcl", m.Source.File("atlas.hcl")) | ||
|
||
p := pool.New().WithErrors().WithContext(ctx) | ||
|
||
// Always validate schema is generated | ||
p.Go(func(ctx context.Context) error { | ||
result := app. | ||
WithExec([]string{"go", "generate", "-x", "./openmeter/ent/..."}). | ||
Directory("openmeter/ent") | ||
|
||
source := m.Source.Directory("openmeter/ent") | ||
|
||
err := diff(ctx, source, result) | ||
if err != nil { | ||
return fmt.Errorf("schema is not in sync with generated code") | ||
} | ||
return nil | ||
}) | ||
|
||
// Always validate migrations are in sync with schema | ||
p.Go(func(ctx context.Context) error { | ||
result := atlas. | ||
WithServiceBinding("postgres", postgresNamed("no-diff")). | ||
WithExec([]string{"atlas", "migrate", "--env", "ci", "diff", "test"}). | ||
Directory("tools/migrate/migrations") | ||
|
||
source := m.Source.Directory("tools/migrate/migrations") | ||
err := diff(ctx, source, result) | ||
if err != nil { | ||
return fmt.Errorf("migrations are not in sync with schema") | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
// Always lint last 10 migrations | ||
p.Go(syncFunc( | ||
atlas. | ||
WithServiceBinding("postgres", postgresNamed("last-10")). | ||
WithExec([]string{"atlas", "migrate", "--env", "ci", "lint", "--latest", "10"}), | ||
)) | ||
|
||
// Validate checksum is intact | ||
p.Go(syncFunc( | ||
atlas. | ||
WithServiceBinding("postgres", postgresNamed("validate")). | ||
WithExec([]string{"atlas", "migrate", "--env", "ci", "validate"}), | ||
)) | ||
|
||
return p.Wait() | ||
} |
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
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
Oops, something went wrong.