Repository: kestra-io/kestra
Issue: Provide a database upgrade manually
Labels: enhancement

- the migration happen at the startup of the application
- the healthcheck endpoint is not up (normal) during the migration  
- when having large migration (like adding the tenantId cols), the migration take a lot of time 
- leading to infinite restart of the pod before the migration is done 

Solution proposed : 
- We should add a configuration flag to disabled auto jdbc migration 
- Add a command to manually execute the migration  

Or any others idea @loicmathieu ? 

Issue comments:

Pull request review comments:

Patch that resolved the issue:
diff --git a/cli/src/main/java/io/kestra/cli/commands/sys/SysCommand.java b/cli/src/main/java/io/kestra/cli/commands/sys/SysCommand.java
index ba90feb4aa..6cf801cc31 100644
--- a/cli/src/main/java/io/kestra/cli/commands/sys/SysCommand.java
+++ b/cli/src/main/java/io/kestra/cli/commands/sys/SysCommand.java
@@ -1,5 +1,6 @@
 package io.kestra.cli.commands.sys;
 
+import io.kestra.cli.commands.sys.database.DatabaseCommand;
 import io.micronaut.configuration.picocli.PicocliRunner;
 import lombok.extern.slf4j.Slf4j;
 import io.kestra.cli.AbstractCommand;
@@ -11,7 +12,8 @@
     description = "handle systems maintenance",
     mixinStandardHelpOptions = true,
     subcommands = {
-        ReindexCommand.class
+        ReindexCommand.class,
+        DatabaseCommand.class
     }
 )
 @Slf4j
diff --git a/cli/src/main/java/io/kestra/cli/commands/sys/database/DatabaseCommand.java b/cli/src/main/java/io/kestra/cli/commands/sys/database/DatabaseCommand.java
new file mode 100644
index 0000000000..0649c5e928
--- /dev/null
+++ b/cli/src/main/java/io/kestra/cli/commands/sys/database/DatabaseCommand.java
@@ -0,0 +1,15 @@
+package io.kestra.cli.commands.sys.database;
+
+import io.kestra.cli.AbstractCommand;
+import picocli.CommandLine;
+
+@CommandLine.Command(
+    name = "database",
+    description = "manage Kestra database",
+    mixinStandardHelpOptions = true,
+    subcommands = {
+        DatabaseMigrateCommand.class,
+    }
+)
+public class DatabaseCommand extends AbstractCommand {
+}
diff --git a/cli/src/main/java/io/kestra/cli/commands/sys/database/DatabaseMigrateCommand.java b/cli/src/main/java/io/kestra/cli/commands/sys/database/DatabaseMigrateCommand.java
new file mode 100644
index 0000000000..f342189c55
--- /dev/null
+++ b/cli/src/main/java/io/kestra/cli/commands/sys/database/DatabaseMigrateCommand.java
@@ -0,0 +1,40 @@
+package io.kestra.cli.commands.sys.database;
+
+import ch.qos.logback.classic.Level;
+import io.kestra.cli.AbstractCommand;
+import lombok.extern.slf4j.Slf4j;
+import picocli.CommandLine;
+
+import java.util.Map;
+
+@CommandLine.Command(
+    name = "migrate",
+    description = "Force database schema migration.\nKestra uses Flyway to manage database schema evolution, this command will run Flyway then exit.",
+    mixinStandardHelpOptions = true
+)
+@Slf4j
+public class DatabaseMigrateCommand extends AbstractCommand {
+
+    static {
+        // Force enable Flyway logs
+        ch.qos.logback.classic.Logger flywayLogger = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger("org.flywaydb");
+        flywayLogger.setLevel(Level.INFO);
+    }
+
+    @Override
+    public Integer call() throws Exception {
+        // Flyway will run automatically
+        super.call();
+        stdOut("Successfully run the database schema migration.");
+        return 0;
+    }
+
+    public static Map<String, Object> propertiesOverrides() {
+        // Forcing the props to enabled Flyway: it allows to disable Flyway globally and still using this command.
+        return Map.of(
+            "flyway.datasources.postgres.enabled", "true",
+            "flyway.datasources.mysql.enabled", "true",
+            "flyway.datasources.h2.enabled", "true"
+        );
+    }
+}


Test patch: