|
| 1 | +/* |
| 2 | + * Copyright 2018-2022 contributors to the Marquez project |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package marquez.cli; |
| 7 | + |
| 8 | +import io.dropwizard.Application; |
| 9 | +import io.dropwizard.cli.EnvironmentCommand; |
| 10 | +import io.dropwizard.db.DataSourceFactory; |
| 11 | +import io.dropwizard.db.ManagedDataSource; |
| 12 | +import io.dropwizard.jdbi3.JdbiFactory; |
| 13 | +import io.dropwizard.setup.Environment; |
| 14 | +import javax.sql.DataSource; |
| 15 | +import lombok.extern.slf4j.Slf4j; |
| 16 | +import marquez.db.migrations.V57_1__BackfillFacets; |
| 17 | +import net.sourceforge.argparse4j.inf.Namespace; |
| 18 | +import net.sourceforge.argparse4j.inf.Subparser; |
| 19 | +import org.jdbi.v3.core.Jdbi; |
| 20 | +import org.jdbi.v3.jackson2.Jackson2Plugin; |
| 21 | +import org.jdbi.v3.postgres.PostgresPlugin; |
| 22 | +import org.jdbi.v3.sqlobject.SqlObjectPlugin; |
| 23 | + |
| 24 | +/** |
| 25 | + * A command to manually run V55 database migration. This migration requires a heavy DB operation |
| 26 | + * which can be done asynchronously (with limited API downtime) due to separate migration command. |
| 27 | + * |
| 28 | + * <p>Please refer to @link marquez/db/migration/V55__readme.md for more details. |
| 29 | + */ |
| 30 | +@Slf4j |
| 31 | +public class V57MigrationCommand<MarquezConfig> extends EnvironmentCommand<marquez.MarquezConfig> { |
| 32 | + |
| 33 | + private static final String COMMAND_NAME = "v55_migrate"; |
| 34 | + private static final String COMMAND_DESCRIPTION = |
| 35 | + """ |
| 36 | + A command to manually run V55 database migration. |
| 37 | + Please refer to https://github.com/MarquezProject/marquez/blob/main/api/src/main/resources/marquez/db/migration/V55__readme.md for more details. |
| 38 | + """; |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a new environment command. |
| 42 | + * |
| 43 | + * @param application the application providing this command |
| 44 | + */ |
| 45 | + public V57MigrationCommand(Application<marquez.MarquezConfig> application) { |
| 46 | + super(application, COMMAND_NAME, COMMAND_DESCRIPTION); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void configure(Subparser subparser) { |
| 51 | + subparser |
| 52 | + .addArgument("--chunkSize") |
| 53 | + .dest("chunkSize") |
| 54 | + .type(Integer.class) |
| 55 | + .required(false) |
| 56 | + .setDefault(V57_1__BackfillFacets.DEFAULT_CHUNK_SIZE) |
| 57 | + .help("amount of lineage_events rows processed in a single SQL query and transaction."); |
| 58 | + addFileArgument(subparser); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected void run( |
| 63 | + Environment environment, Namespace namespace, marquez.MarquezConfig configuration) |
| 64 | + throws Exception { |
| 65 | + log.info("Running v55 migration command"); |
| 66 | + |
| 67 | + final DataSourceFactory sourceFactory = configuration.getDataSourceFactory(); |
| 68 | + final DataSource source = sourceFactory.build(environment.metrics(), "MarquezApp-source"); |
| 69 | + |
| 70 | + final JdbiFactory factory = new JdbiFactory(); |
| 71 | + final Jdbi jdbi = |
| 72 | + factory |
| 73 | + .build( |
| 74 | + environment, |
| 75 | + configuration.getDataSourceFactory(), |
| 76 | + (ManagedDataSource) source, |
| 77 | + "postgresql-command") |
| 78 | + .installPlugin(new SqlObjectPlugin()) |
| 79 | + .installPlugin(new PostgresPlugin()) |
| 80 | + .installPlugin(new Jackson2Plugin()); |
| 81 | + |
| 82 | + V57_1__BackfillFacets migration = new V57_1__BackfillFacets(); |
| 83 | + migration.setTriggeredByCommand(true); |
| 84 | + migration.setJdbi(jdbi); |
| 85 | + migration.setChunkSize(namespace.getInt("chunkSize")); |
| 86 | + migration.migrate(null); |
| 87 | + |
| 88 | + log.info("Migration finished successfully"); |
| 89 | + } |
| 90 | +} |
0 commit comments