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

Repurpose db-migrate to run all pending migrations #2936

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
wslulciuc marked this conversation as resolved.
Show resolved Hide resolved
* 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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to make more sense in the context of db-migrate

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
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ ignore:
- "api/src/main/java/marquez/db/migrations/V44_1__UpdateRunsWithJobUUID.java"
- "api/src/main/java/marquez/db/migrations/V44_2__BackfillAirflowParentRuns.java"
- "api/src/main/java/marquez/db/migrations/V44_3_BackfillJobsWithParents.java"
- "api/src/main/java/marquez/cli/DbMigrationCommand.java"
- "api/src/main/java/marquez/cli/DbMigrateCommand.java"
Loading