Skip to content

Commit

Permalink
feat: JVM DB migrations (#3568)
Browse files Browse the repository at this point in the history
fixes #3484
  • Loading branch information
stuartwdouglas authored Nov 29, 2024
1 parent 33501ce commit 6770334
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- migrate:up
CREATE TABLE requests
(
id SERIAL PRIMARY KEY NOT NULL,
data TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE SEQUENCE requests_seq;
-- migrate:down
DROP TABLE requests;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package xyz.block.ftl.java.test.database;

public class InsertResponse {

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
import jakarta.persistence.Table;

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

@Entity
@Table(name = "requests")
public class Request extends PanacheEntity {
public String data;

@Column(name = "created_at")
@CreationTimestamp
public Timestamp createdAt;

@Column(name = "updated_at")
@UpdateTimestamp
public Timestamp updatedAt;

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.datasource.testdb.db-kind=postgresql
quarkus.hibernate-orm.datasource=testdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- migrate:up
CREATE TABLE requests (
id bigint NOT NULL,
created_at datetime(6),
data VARCHAR(255),
updated_at datetime(6),
PRIMARY KEY (id)
) engine=InnoDB;

CREATE TABLE requests_SEQ (
next_val bigint
) engine=InnoDB;

INSERT INTO requests_SEQ VALUES ( 1 );

-- migrate:down
DROP TABLE requests;
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package xyz.block.ftl.java.test.database;

import java.util.List;
import java.util.Map;

import jakarta.transaction.Transactional;

import xyz.block.ftl.Verb;
import java.util.Map;
import java.util.List;

public class Database {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import jakarta.persistence.Entity;
import jakarta.persistence.Table;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import io.quarkus.hibernate.orm.panache.PanacheEntity;

@Entity
Expand All @@ -14,9 +17,11 @@ public class Request extends PanacheEntity {
public String data;

@Column(name = "created_at")
@CreationTimestamp
public Timestamp createdAt;

@Column(name = "updated_at")
@UpdateTimestamp
public Timestamp updatedAt;

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.datasource.testdb.db-kind=mysql
quarkus.hibernate-orm.datasource=testdb
2 changes: 1 addition & 1 deletion internal/buildengine/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func handleBuildResult(ctx context.Context, projectConfig projectconfig.Config,

logger.Infof("Module built (%.2fs)", time.Since(result.StartTime).Seconds())

migrationFiles, err := handleDatabaseMigrations(config.Dir, config.SQLMigrationDirectory, result.Schema)
migrationFiles, err := handleDatabaseMigrations(config.DeployDir, config.SQLMigrationDirectory, result.Schema)
if err != nil {
return nil, nil, fmt.Errorf("failed to extract migrations %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/buildengine/sql_migration_extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ func createMigrationTarball(migrationDir string, target string) error {
return nil
}

func handleDatabaseMigrations(moduleDir string, dbDir string, module *schema.Module) ([]string, error) {
target := filepath.Join(moduleDir, ".ftl", "migrations")
func handleDatabaseMigrations(deployDir string, dbDir string, module *schema.Module) ([]string, error) {
target := filepath.Join(deployDir, ".ftl", "migrations")
err := os.MkdirAll(target, 0770) // #nosec
if err != nil {
return nil, fmt.Errorf("failed to create migration directory: %w", err)
Expand All @@ -131,7 +131,7 @@ func handleDatabaseMigrations(moduleDir string, dbDir string, module *schema.Mod
}
relativeFiles := []string{}
for _, file := range migrations {
filePath := filepath.Join("migrations", file)
filePath := filepath.Join(".ftl", "migrations", file)
relativeFiles = append(relativeFiles, filePath)
}
return relativeFiles, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class StaticConfigSource implements ConfigSource {

public static final String QUARKUS_BANNER_ENABLED = "quarkus.banner.enabled";
final static String OTEL_METRICS_ENABLED = "quarkus.otel.metrics.enabled";
final static String DEV_SERVICES_ENABLED = "quarkus.devservices.enabled";

@Override
public Set<String> getPropertyNames() {
Expand All @@ -23,6 +24,9 @@ public String getValue(String propertyName) {
case OTEL_METRICS_ENABLED -> {
return "true";
}
case DEV_SERVICES_ENABLED -> {
return "false";
}
}
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions jvm-runtime/plugin/common/java_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestJavaConfigDefaults(t *testing.T) {
"build/generated",
"target/generated-sources",
},
SQLMigrationDir: "src/main/db",
},
},
{
Expand All @@ -62,6 +63,7 @@ func TestJavaConfigDefaults(t *testing.T) {
"build/generated",
"target/generated-sources",
},
SQLMigrationDir: "src/main/db",
},
},
} {
Expand Down
3 changes: 2 additions & 1 deletion jvm-runtime/plugin/common/jvmcommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func build(ctx context.Context, bctx buildContext, autoRebuild bool) (*langpb.Bu
command := exec.Command(ctx, log.Debug, config.Dir, "bash", "-c", config.Build)
err = command.Run()
if err != nil {
return &langpb.BuildEvent{Event: &langpb.BuildEvent_BuildFailure{&langpb.BuildFailure{
return &langpb.BuildEvent{Event: &langpb.BuildEvent_BuildFailure{BuildFailure: &langpb.BuildFailure{
IsAutomaticRebuild: autoRebuild,
ContextId: bctx.ID,
Errors: &langpb.ErrorList{Errors: []*langpb.Error{{Msg: err.Error(), Level: langpb.Error_ERROR, Type: langpb.Error_COMPILER}}},
Expand Down Expand Up @@ -574,6 +574,7 @@ func (s *Service) ModuleConfigDefaults(ctx context.Context, req *connect.Request
GeneratedSchemaDir: ptr("src/main/ftl-module-schema"),
LanguageConfig: &structpb.Struct{Fields: map[string]*structpb.Value{}},
Watch: []string{"pom.xml", "src/**", "build/generated", "target/generated-sources"},
SqlMigrationDir: "src/main/db",
}
dir := req.Msg.Dir
pom := filepath.Join(dir, "pom.xml")
Expand Down

0 comments on commit 6770334

Please sign in to comment.