Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: database migrations #3457

Merged
merged 2 commits into from
Nov 24, 2024
Merged

feat: database migrations #3457

merged 2 commits into from
Nov 24, 2024

Conversation

stuartwdouglas
Copy link
Collaborator

No description provided.

This was referenced Nov 22, 2024
@stuartwdouglas stuartwdouglas force-pushed the stuartwdouglas/migrations branch 11 times, most recently from d58d4a6 to 7aa111b Compare November 22, 2024 06:40
@stuartwdouglas stuartwdouglas marked this pull request as ready for review November 22, 2024 07:32
@stuartwdouglas stuartwdouglas requested review from wesbillman and removed request for a team November 22, 2024 07:32
@stuartwdouglas stuartwdouglas requested a review from a team as a code owner November 22, 2024 07:51
@stuartwdouglas stuartwdouglas requested review from worstell and removed request for a team November 22, 2024 07:51
@stuartwdouglas stuartwdouglas force-pushed the stuartwdouglas/migrations branch 3 times, most recently from e88667f to d4f0282 Compare November 22, 2024 08:31
@@ -41,6 +42,12 @@ message MysqlResource {
MysqlResourceOutput output = 1;
}

message MigrationResource {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please prefix all these types/functions with "SQL", "migrations" is not specific enough.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify why I think this is important - it's very likely there will be other kinds of migrations in the future.

backend/provisioner/migration_provisioner.go Outdated Show resolved Hide resolved
backend/provisioner/registry.go Outdated Show resolved Hide resolved
Schema *schema.Schema
Dependencies []string
BuildEnv []string
AdditionalFiles []string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm 95% sure this should just be part of the ModuleConfig .proto rather than here (cc @matt2e) - this is exactly what it's designed for. Each language plugin then just returns the directory for any migrations, and the build engine does the bundling rather than each runtime having to have custom logic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if we wanted the build engine to modify the schema after the language plugin was finished with it. I can definitely change it but I think it does kinda blur the lines about what is responsible for generating the schema.

I figured language plugins could just re-use the ExtractMigrations function and achieve re-use that way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a requirement that language plugins be in Go, and in general we try to push as much as possible out of the plugins and into the engine for this reason.

The pattern is that the ModuleConfig tells the BuildEngine where to look for different parts of the build, and the BuildEngine does the work of collecting it into the schema+build. This fits that fairly well. It could be a glob, or a regex, or a map of db names to paths or something.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed this.

internal/migration/extract_migration.go Outdated Show resolved Hide resolved
internal/migration/extract_migration_test.go Outdated Show resolved Hide resolved
targetDir := t.TempDir()

// Create dummy migration files
migrationSubDir := filepath.Join(migrationsDir, "testdb")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't want to use testdata for the fixtures?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that was just for integration tests? Also it's possible Chad may have written this test...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahaha 😂

Nope testdata is standard Go for any test that has fixtures.

internal/schema/parser.go Outdated Show resolved Hide resolved
@@ -729,6 +731,7 @@ module todo {
secret secretValue String
// A database
database postgres testdb
+migration sha256:8cc04c75ab7967eb2ec82e11e886831e00b7cb00507e9a8ecf400bdc599eccfd
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noice.

switch md := md.(type) {
case *MetadataMigration:
if found {
merr = append(merr, fmt.Errorf("database %q has multiple migration metadata", n.Name))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah nice.

@stuartwdouglas stuartwdouglas force-pushed the stuartwdouglas/migrations branch 2 times, most recently from e90d83b to 59d6913 Compare November 23, 2024 03:06
@stuartwdouglas stuartwdouglas force-pushed the stuartwdouglas/migrations branch 2 times, most recently from e03fdd8 to c0f8696 Compare November 23, 2024 03:37
@stuartwdouglas
Copy link
Collaborator Author

I think I have addressed everything, except for the questions around if this should be handled by the build engine or language plugins.

return err
}
_, err = db.Get(ctx).Exec("INSERT INTO requests (id,data) VALUES ($1, $2);", req.Id, req.Data)
_, err := db.Get(ctx).Exec("INSERT INTO requests (id,data) VALUES ($1, $2);", req.Id, req.Data)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So much nicer!

@@ -34,6 +34,9 @@ message ModuleConfig {
// LanguageConfig contains any metadata specific to a specific language.
// These are stored in the ftl.toml file under the same key as the language (eg: "go", "java")
google.protobuf.Struct language_config = 10;

// The directory containing the SQL migration files
string sql_migration_dir = 11;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the syntax of the directory? <sql_migration_dir>/<database_name/*.sql ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No docs yet, but none of the DB stuff has docs yet.

@stuartwdouglas stuartwdouglas merged commit 2745c83 into main Nov 24, 2024
92 checks passed
@stuartwdouglas stuartwdouglas deleted the stuartwdouglas/migrations branch November 25, 2024 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants