Skip to content

Commit bfd154b

Browse files
authored
enhancement(controlplane): versioned migrations support (#220)
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent 95f814f commit bfd154b

File tree

94 files changed

+1372
-1064
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1372
-1064
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
app/controlplane/api/gen/frontend/** linguist-generated=true
22
app/controlplane/internal/data/ent/** linguist-generated=true
3+
app/controlplane/internal/data/ent/migrate/** linguist-generated=false
4+
app/controlplane/internal/data/ent/migrate/** linguist-detectable=true
5+
app/controlplane/internal/data/ent/schema/* linguist-generated=false
36
app/controlplane/internal/data/ent/schema/* linguist-detectable=true

.goreleaser.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ dockers:
6565
image_templates:
6666
- "ghcr.io/chainloop-dev/chainloop/control-plane:{{ .Tag }}"
6767
- "ghcr.io/chainloop-dev/chainloop/control-plane:latest"
68+
- dockerfile: app/controlplane/Dockerfile.migrations
69+
ids:
70+
- control-plane
71+
extra_files:
72+
- app/controlplane/internal/data/ent/migrate/migrations
73+
image_templates:
74+
- "ghcr.io/chainloop-dev/chainloop/control-plane-migrations:{{ .Tag }}"
6875
- dockerfile: app/artifact-cas/Dockerfile.goreleaser
6976
ids:
7077
- artifact-cas

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ init:
1212
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
1313
go install github.com/google/wire/cmd/wire@latest
1414
go install github.com/vektra/mockery/v2@v2.20.0
15-
go install entgo.io/ent/cmd/ent@v0.11.4
15+
go install ariga.io/atlas/cmd/atlas@v0.12.0
1616
go install github.com/bufbuild/buf/cmd/buf@v1.10.0
1717

1818
.PHONY: api
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Container image built by go-releaser that's used to run migrations against the database during deployment
2+
# See https://atlasgo.io/guides/deploying/image
3+
FROM arigaio/atlas@sha256:37b8b163719e2f9baf5c97099e8d0772bc1bd84f392e402afcc3e565d11e074f
4+
5+
COPY app/controlplane/internal/data/ent/migrate/migrations /migrations

app/controlplane/Makefile

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,23 @@ build:
2020

2121
.PHONY: run
2222
# run
23-
run:
23+
run: migration_apply
2424
go run ./cmd/... --conf ./configs
2525

26+
local_migrations_dir = file://internal/data/ent/migrate/migrations
27+
local_db = postgres://postgres:@localhost:5432/controlplane?sslmode=disable
28+
29+
.PHONY: migration_apply
30+
# run migrations against local db
31+
migration_apply:
32+
atlas migrate status --dir ${local_migrations_dir} --url ${local_db}
33+
atlas migrate apply --dir ${local_migrations_dir} --url ${local_db}
34+
35+
.PHONY: migration_new
36+
# generate new migration if needed from the current ent schema
37+
migration_new:
38+
atlas migrate diff --dir ${local_migrations_dir} --to "ent://internal/data/ent/schema" --dev-url "docker://postgres/15/test?search_path=public"
39+
2640
.PHONY: test
2741
# All tests, both unit and integration
2842
test:

app/controlplane/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,18 @@ make api
7272

7373
### Update Application data model
7474

75-
We use [ent](https://entgo.io) as database Object Relational Mapping (ORM).
75+
We use [ent](https://entgo.io) as database Object Relational Mapping (ORM) and [atlas versioned migrations](https://entgo.io/docs/versioned-migrations) to manage the database schema changes.
7676

77-
The way a change in the data model is as follows
77+
The way you can make a change in the data model is
7878

79+
**Update the schema**
7980
- Add a new/update an existing entity via a schema update. Schemas can be found at `internal/data/ent/schema`
8081
- Generate the code changes associated with that schema change. `make generate`
81-
- Restarting the control plane will cause the schema change to be automatically migrated.
82+
- Generate a new versioned migration `make migration_new`. This will create a new migration file at `internal/data/ent/migrate/migrations
83+
84+
**Apply the schema change in development DB**
85+
- `make migration_apply` will apply the migration to the development database
86+
- Restart the control plane
8287

8388
### Update configuration schema
8489

app/controlplane/internal/biz/testhelpers/database.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import (
2323
"testing"
2424
"time"
2525

26-
// Requuired for the database waitFor strategy
26+
// Required for the database waitFor strategy
2727
_ "github.com/lib/pq"
2828

2929
"github.com/chainloop-dev/chainloop/app/controlplane/internal/biz"
3030
"github.com/chainloop-dev/chainloop/app/controlplane/internal/conf"
31+
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data"
3132
"github.com/chainloop-dev/chainloop/app/controlplane/plugins/sdk/v1"
3233
"github.com/chainloop-dev/chainloop/internal/credentials"
3334
creds "github.com/chainloop-dev/chainloop/internal/credentials/mocks"
@@ -45,8 +46,9 @@ import (
4546
// NOTE: It connects to a real database
4647
type TestingUseCases struct {
4748
// Misc
48-
DB *TestDatabase
49-
L log.Logger
49+
DB *TestDatabase
50+
Data *data.Data
51+
L log.Logger
5052

5153
// Use cases
5254
Membership *biz.MembershipUseCase
@@ -101,6 +103,9 @@ func NewTestingUseCases(t *testing.T, opts ...NewTestingUCOpt) *TestingUseCases
101103
}, newArgs.integrations)
102104
assert.NoError(t, err)
103105

106+
// Run DB migrations for testing
107+
require.NoError(t, testData.Data.SchemaLoad())
108+
104109
return testData
105110
}
106111

app/controlplane/internal/biz/testhelpers/wire_gen.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/internal/data/data.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"entgo.io/ent/dialect"
2727
entsql "entgo.io/ent/dialect/sql"
28-
"entgo.io/ent/dialect/sql/schema"
2928

3029
"github.com/chainloop-dev/chainloop/app/controlplane/internal/conf"
3130
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent"
@@ -35,7 +34,7 @@ import (
3534
"github.com/google/wire"
3635

3736
// Load PGX driver
38-
_ "github.com/jackc/pgx/v4/stdlib"
37+
_ "github.com/jackc/pgx/v5/stdlib"
3938
)
4039

4140
// ProviderSet is data providers.
@@ -59,6 +58,13 @@ type Data struct {
5958
db *ent.Client
6059
}
6160

61+
// Load DB schema
62+
// NOTE: this is different than running migrations
63+
// this method is used to load the schema into the DB for TESTING!
64+
func (data *Data) SchemaLoad() error {
65+
return data.db.Schema.Create(context.Background())
66+
}
67+
6268
// NewData .
6369
func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) {
6470
if logger == nil {
@@ -96,11 +102,8 @@ func initSQLDatabase(c *conf.Data_Database, log *log.Helper) (*ent.Client, error
96102
drv := entsql.OpenDB(dialect.Postgres, db)
97103
client := ent.NewClient(ent.Driver(drv))
98104

99-
// Run DB migration
100-
if err := client.Schema.Create(context.Background(), schema.WithDropColumn(true)); err != nil {
101-
return nil, fmt.Errorf("error performing the schema change: %w", err)
102-
}
103-
105+
// NOTE: We do not run migrations automatically anymore
106+
// Instead we leverage atlas cli to run migrations
104107
return client, nil
105108
}
106109

app/controlplane/internal/data/ent/ent.go

Lines changed: 34 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)