Skip to content

Commit

Permalink
feat: allow db-migrate without version
Browse files Browse the repository at this point in the history
Signed-off-by: David Goss <david@davidgoss.co>
  • Loading branch information
davidjgoss committed Oct 21, 2024
1 parent d9ec368 commit deefca5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 122 deletions.
9 changes: 2 additions & 7 deletions api/src/main/java/marquez/MarquezApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import marquez.api.filter.JobRedirectFilter;
import marquez.api.filter.exclusions.Exclusions;
import marquez.api.filter.exclusions.ExclusionsConfig;
import marquez.cli.DbMigrationCommand;
import marquez.cli.DbMigrateCommand;
import marquez.cli.DbRetentionCommand;
import marquez.cli.MetadataCommand;
import marquez.cli.SeedCommand;
Expand Down Expand Up @@ -90,6 +90,7 @@ public void initialize(@NonNull Bootstrap<MarquezConfig> bootstrap) {
new EnvironmentVariableSubstitutor(ERROR_ON_UNDEFINED)));

// Add CLI commands
bootstrap.addCommand(new DbMigrateCommand());
bootstrap.addCommand(new DbRetentionCommand());
bootstrap.addCommand(new MetadataCommand());
bootstrap.addCommand(new SeedCommand());
Expand Down Expand Up @@ -202,12 +203,6 @@ public void registerResources(
}
}

@Override
protected void addDefaultCommands(Bootstrap<MarquezConfig> bootstrap) {
bootstrap.addCommand(new DbMigrationCommand<>(this));
super.addDefaultCommands(bootstrap);
}

private void registerServlets(@NonNull Environment env) {
log.debug("Registering servlets...");

Expand Down
42 changes: 42 additions & 0 deletions api/src/main/java/marquez/cli/DbMigrateCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2018-2022 contributors to the Marquez project
* SPDX-License-Identifier: Apache-2.0
*/

package marquez.cli;

import io.dropwizard.cli.ConfiguredCommand;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.db.ManagedDataSource;
import io.dropwizard.setup.Bootstrap;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import marquez.MarquezConfig;
import marquez.db.DbMigration;
import net.sourceforge.argparse4j.inf.Namespace;

/**
* A command to manually run database migrations. This command to be used to run migrations
* decoupled from application deployment.
*/
@Slf4j
public class DbMigrateCommand extends ConfiguredCommand<MarquezConfig> {

public DbMigrateCommand() {
super("db-migrate", "A command to manually run database migrations.");
}

@Override
protected void run(
@NonNull Bootstrap<MarquezConfig> bootstrap,
@NonNull Namespace namespace,
@NonNull MarquezConfig configuration)
throws Exception {

final DataSourceFactory sourceFactory = configuration.getDataSourceFactory();
final ManagedDataSource source =
sourceFactory.build(bootstrap.getMetricRegistry(), "MarquezApp-source");

DbMigration.migrateDbOrError(configuration.getFlywayFactory(), source, true);
}
}
113 changes: 0 additions & 113 deletions api/src/main/java/marquez/cli/DbMigrationCommand.java

This file was deleted.

4 changes: 2 additions & 2 deletions api/src/main/java/marquez/db/DbMigration.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public static void migrateDbOrError(@NonNull final DataSource source) {
public static void migrateDbOrError(
@NonNull final FlywayFactory flywayFactory,
@NonNull final DataSource source,
final boolean migrateDbOnStartup) {
final boolean migrateNow) {
final Flyway flyway = flywayFactory.build(source);
// Only attempt a database migration if there are pending changes to be applied,
// or on the initialization of a new database. Otherwise, error on pending changes
// when the flag 'migrateOnStartup' is set to 'false'.
if (!hasPendingDbMigrations(flyway)) {
log.info("No pending migrations found, skipping...");
return;
} else if (!migrateDbOnStartup && hasDbMigrationsApplied(flyway)) {
} else if (!migrateNow && hasDbMigrationsApplied(flyway)) {
errorOnPendingDbMigrations(flyway);
}
// Attempt to perform a database migration. An exception is thrown on failed migration attempts
Expand Down

0 comments on commit deefca5

Please sign in to comment.