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

Added naming to migration bundles #1430

Merged
Show file tree
Hide file tree
Changes from 2 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
98 changes: 98 additions & 0 deletions docs/source/manual/migrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,104 @@ To mark a pending changeset as applied (e.g., after having backfilled your ``mig
This will mark the next pending changeset as applied. You can also use the ``--all`` flag to mark
all pending changesets as applied.

Support For Adding Multiple Migration Bundles
=============================================

Assuming migrations need to be done for two different databases, you would need to have two different data source factories:

.. code-block:: java

public class ExampleConfiguration extends Configuration {
@Valid
@NotNull
private DataSourceFactory database1 = new DataSourceFactory();

@Valid
@NotNull
private DataSourceFactory database2 = new DataSourceFactory();

@JsonProperty("database1")
public DataSourceFactory getDb1DataSourceFactory() {
return database1;
}

@JsonProperty("database2")
public DataSourceFactory getDb2DataSourceFactory() {
return database2;
}
}

Now multiple migration bundles can be added with unique names like so:

.. code-block:: java

@Override
public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {
bootstrap.addBundle(new MigrationsBundle<ExampleConfiguration>() {
@Override
public DataSourceFactory getDataSourceFactory(ExampleConfiguration configuration) {
return configuration.getDb1DataSourceFactory();
}

@Override
public String name() {
return "db1";
}
});

bootstrap.addBundle(new MigrationsBundle<ExampleConfiguration>() {
@Override
public DataSourceFactory getDataSourceFactory(ExampleConfiguration configuration) {
return configuration.getDb2DataSourceFactory();
}

@Override
public String name() {
return "db2";
}
});
}

To migrate your schema:

.. code-block:: text

java -jar hello-world.jar db1 migrate helloworld.yml

and

.. code-block:: text

java -jar hello-world.jar db2 migrate helloworld.yml

.. note::

Whenever a name is added to a migration bundle, it becomes the command that needs to be run at the command line.
eg: To check the state of your database, use the ``status`` command:

.. code-block:: text

java -jar hello-world.jar db1 status helloworld.yml

or

.. code-block:: text

java -jar hello-world.jar db2 status helloworld.yml

By default the migration bundle uses the "db" command. By overriding you can customize it to provide any name you want
and have multiple migration bundles. Wherever the "db" command was being used, this custom name can be used.

There will also be a need to provide different change log migration files as well. This can be done as

.. code-block:: text

java -jar hello-world.jar db1 migrate helloworld.yml --migrations <path_to_db1_migrations.xml>

.. code-block:: text

java -jar hello-world.jar db2 migrate helloworld.yml --migrations <path_to_db2_migrations.xml>

More Information
================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class DbCommand<T extends Configuration> extends AbstractLiquibaseCommand
private static final String COMMAND_NAME_ATTR = "subcommand";
private final SortedMap<String, AbstractLiquibaseCommand<T>> subcommands;

public DbCommand(DatabaseConfiguration<T> strategy, Class<T> configurationClass) {
super("db", "Run database migration tasks", strategy, configurationClass);
public DbCommand(String name, DatabaseConfiguration<T> strategy, Class<T> configurationClass) {
super(name, "Run database migration tasks", strategy, configurationClass);
this.subcommands = new TreeMap<>();
addSubcommand(new DbCalculateChecksumCommand<>(strategy, configurationClass));
addSubcommand(new DbClearChecksumsCommand<>(strategy, configurationClass));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
import io.dropwizard.setup.Environment;

public abstract class MigrationsBundle<T extends Configuration> implements Bundle, DatabaseConfiguration<T> {
public static final String DEFAULT_NAME = "db";
Copy link
Member

Choose a reason for hiding this comment

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

can you make this private?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


@Override
@SuppressWarnings("unchecked")
public final void initialize(Bootstrap<?> bootstrap) {
final Class<T> klass = (Class<T>) bootstrap.getApplication().getConfigurationClass();
bootstrap.addCommand(new DbCommand<>(this, klass));
bootstrap.addCommand(new DbCommand<>(name(), this, klass));
}

protected String name() {
Copy link
Member

Choose a reason for hiding this comment

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

can you make this public?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jplock : Thanks for the comments. Do you see usage of name() outside subclasses of MigrationBundle?

Copy link
Member

Choose a reason for hiding this comment

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

No, but to stay consistent with initialize() and run() and how its defined in https://github.com/dropwizard/dropwizard/blob/master/dropwizard-core/src/main/java/io/dropwizard/Application.java

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made the change. Thanks

return DEFAULT_NAME;
}

@Override
Expand Down