Skip to content

Commit

Permalink
Merge pull request #449 from DependencyTrack/liquibase-additions
Browse files Browse the repository at this point in the history
Additions to Liquibase integration
  • Loading branch information
VithikaS authored Nov 22, 2023
2 parents c031ea0 + bb89d6a commit c154dfb
Show file tree
Hide file tree
Showing 26 changed files with 1,096 additions and 1,136 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.logging.core.NoOpLogService;
import liquibase.resource.ClassLoaderResourceAccessor;
import org.dependencytrack.common.ConfigKey;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Collections;
import javax.sql.DataSource;
import java.util.HashMap;

public class MigrationInitializer implements ServletContextListener {
private static final Logger LOGGER = Logger.getLogger(MigrationInitializer.class);
Expand All @@ -42,20 +44,29 @@ public void contextInitialized(final ServletContextEvent event) {

LOGGER.info("Running migrations");
try (final HikariDataSource dataSource = createDataSource()) {
Scope.child(Collections.emptyMap(), () -> {
final Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(dataSource.getConnection()));
final var liquibase = new Liquibase("migration/changelog-main.xml", new ClassLoaderResourceAccessor(), database);

final var updateCommand = new CommandScope(UpdateCommandStep.COMMAND_NAME);
updateCommand.addArgumentValue(DbUrlConnectionCommandStep.DATABASE_ARG, liquibase.getDatabase());
updateCommand.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, liquibase.getChangeLogFile());
updateCommand.execute();
});
runMigration(dataSource, false);
} catch (Exception e) {
throw new RuntimeException("Failed to execute migrations", e);
}
}

public static void runMigration(final DataSource dataSource, final boolean silent) throws Exception {
final var scopeAttributes = new HashMap<String, Object>();
if (silent) {
scopeAttributes.put(Scope.Attr.logService.name(), new NoOpLogService());
}

Scope.child(scopeAttributes, () -> {
final Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(dataSource.getConnection()));
final var liquibase = new Liquibase("migration/changelog-main.xml", new ClassLoaderResourceAccessor(), database);

final var updateCommand = new CommandScope(UpdateCommandStep.COMMAND_NAME);
updateCommand.addArgumentValue(DbUrlConnectionCommandStep.DATABASE_ARG, liquibase.getDatabase());
updateCommand.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, liquibase.getChangeLogFile());
updateCommand.execute();
});
}

private HikariDataSource createDataSource() {
final var hikariCfg = new HikariConfig();
hikariCfg.setJdbcUrl(config.getProperty(Config.AlpineKey.DATABASE_URL));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.dependencytrack.persistence.migration.change;
package org.dependencytrack.persistence.migration.change.v530;

import liquibase.change.custom.CustomTaskChange;
import liquibase.database.Database;
Expand All @@ -23,7 +23,7 @@ public class RenameForeignKeysChange implements CustomTaskChange {
private record ForeignKeyDefinition(String targetTable, String referencedTable) {
}

private record ForeignKeyNameMapping(String table, String oldName, String newName) {
record ForeignKeyNameMapping(String table, String oldName, String newName) {
}

private static ForeignKeyDefinition foreignKeyDef(final String targetTable, final String referencedTale) {
Expand Down Expand Up @@ -374,7 +374,7 @@ public ValidationErrors validate(final Database database) {
return null;
}

private static List<ForeignKeyNameMapping> getForeignNameMappings(final JdbcConnection connection) throws DatabaseException, SQLException {
static List<ForeignKeyNameMapping> getForeignNameMappings(final JdbcConnection connection) throws DatabaseException, SQLException {
return switch (connection.getDatabaseProductName()) {
case "PostgreSQL" -> getForeignKeyNameMappingsFromPostgres(connection);
default -> throw new IllegalStateException();
Expand All @@ -386,7 +386,7 @@ private static List<ForeignKeyNameMapping> getForeignKeyNameMappingsFromPostgres

try (final var stmt = connection.createStatement()) {
final ResultSet rs = stmt.executeQuery("""
SELECT
SELECT
tc.constraint_name AS foreign_key_name,
tc.table_name AS target_table_name,
kcu.column_name,
Expand All @@ -399,6 +399,7 @@ private static List<ForeignKeyNameMapping> getForeignKeyNameMappingsFromPostgres
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.constraint_name ~ '_FK[0-9]+$'
""");
while (rs.next()) {
final String tableName = rs.getString("target_table_name");
Expand All @@ -419,6 +420,7 @@ private static List<ForeignKeyNameMapping> renameForeignKeys(final JdbcConnectio
}


@SuppressWarnings("SqlSourceToSinkFlow")
private static List<ForeignKeyNameMapping> renameForeignKeysForPostgres(final JdbcConnection connection, final List<ForeignKeyNameMapping> foreignKeyNameMappings) throws DatabaseException, SQLException {
final var renamedForeignKeys = new ArrayList<ForeignKeyNameMapping>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.dependencytrack.persistence.migration.change;
package org.dependencytrack.persistence.migration.change.v530;

import liquibase.change.custom.CustomTaskChange;
import liquibase.database.Database;
Expand All @@ -25,7 +25,7 @@ public class RenameNumberedIndexesChange implements CustomTaskChange {
private record IndexDefinition(String table, Set<String> columns) {
}

private record IndexNameMapping(String oldName, String newName) {
record IndexNameMapping(String oldName, String newName) {
}

private static IndexDefinition indexDef(final String table, final String... columns) {
Expand Down Expand Up @@ -371,7 +371,7 @@ private static List<IndexNameMapping> getIndexNameMappings(final JdbcConnection
};
}

private static List<IndexNameMapping> getIndexNameMappingsFromPostgres(final JdbcConnection connection) throws DatabaseException, SQLException {
static List<IndexNameMapping> getIndexNameMappingsFromPostgres(final JdbcConnection connection) throws DatabaseException, SQLException {
final var indexNameMapping = new ArrayList<IndexNameMapping>();

try (final var stmt = connection.createStatement()) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/migration/changelog-main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<!-- Use separate changelogs per release. -->
<include file="migration/changelog-v5.3.0.xml"/>
<include file="migration/changelog-procedures.xml"/>
</databaseChangeLog>
32 changes: 32 additions & 0 deletions src/main/resources/migration/changelog-procedures.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog
objectQuotingStrategy="QUOTE_ALL_OBJECTS"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet id="function_cvssv2-to-severity" author="[email protected]" runOnChange="true">
<createProcedure path="procedures/function_cvssv2-to-severity.sql" relativeToChangelogFile="true"/>
</changeSet>
<changeSet id="function_cvssv3-to-severity" author="[email protected]" runOnChange="true">
<createProcedure path="procedures/function_cvssv3-to-severity.sql" relativeToChangelogFile="true"/>
</changeSet>
<changeSet id="function_calc-severity" author="[email protected]" runOnChange="true">
<createProcedure path="procedures/function_calc-severity.sql" relativeToChangelogFile="true"/>
</changeSet>
<changeSet id="function_calc-risk-score" author="[email protected]" runOnChange="true">
<createProcedure path="procedures/function_calc-risk-score.sql" relativeToChangelogFile="true"/>
</changeSet>
<changeSet id="procedure_update-component-metrics" author="[email protected]" runOnChange="true">
<createProcedure path="procedures/procedure_update-component-metrics.sql" relativeToChangelogFile="true"/>
</changeSet>
<changeSet id="procedure_update-project-metrics" author="[email protected]" runOnChange="true">
<createProcedure path="procedures/procedure_update-project-metrics.sql" relativeToChangelogFile="true"/>
</changeSet>
<changeSet id="procedure_update-portfolio-metrics" author="[email protected]" runOnChange="true">
<createProcedure path="procedures/procedure_update-portfolio-metrics.sql" relativeToChangelogFile="true"/>
</changeSet>
</databaseChangeLog>
Loading

0 comments on commit c154dfb

Please sign in to comment.