Search in sources :

Example 1 with DataMigration

use of dev.rosewood.rosegarden.database.DataMigration in project RoseGarden by Rosewood-Development.

the class DataMigrationManager method reload.

@Override
public void reload() {
    AbstractDataManager dataManager = this.rosePlugin.getManager(AbstractDataManager.class);
    DatabaseConnector databaseConnector = dataManager.getDatabaseConnector();
    databaseConnector.connect((connection -> {
        int currentMigration = -1;
        boolean migrationsExist;
        String query;
        if (databaseConnector instanceof SQLiteConnector) {
            query = "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?";
        } else {
            query = "SHOW TABLES LIKE ?";
        }
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, this.getMigrationsTableName());
            migrationsExist = statement.executeQuery().next();
        }
        if (!migrationsExist) {
            // No migration table exists, create one
            String createTable = "CREATE TABLE " + this.getMigrationsTableName() + " (migration_version INT NOT NULL)";
            try (PreparedStatement statement = connection.prepareStatement(createTable)) {
                statement.execute();
            }
            // Insert primary row into migration table
            String insertRow = "INSERT INTO " + this.getMigrationsTableName() + " VALUES (?)";
            try (PreparedStatement statement = connection.prepareStatement(insertRow)) {
                statement.setInt(1, -1);
                statement.execute();
            }
        } else {
            // Grab the current migration version
            String selectVersion = "SELECT migration_version FROM " + this.getMigrationsTableName();
            try (PreparedStatement statement = connection.prepareStatement(selectVersion)) {
                ResultSet result = statement.executeQuery();
                result.next();
                currentMigration = result.getInt("migration_version");
            }
        }
        // Grab required migrations
        int finalCurrentMigration = currentMigration;
        List<DataMigration> requiredMigrations = this.migrations.stream().filter(x -> x.getRevision() > finalCurrentMigration).sorted(Comparator.comparingInt(DataMigration::getRevision)).collect(Collectors.toList());
        // Nothing to migrate, abort
        if (requiredMigrations.isEmpty())
            return;
        // Migrate the data
        for (DataMigration dataMigration : requiredMigrations) dataMigration.migrate(databaseConnector, connection, dataManager.getTablePrefix());
        // Set the new current migration to be the highest migrated to
        currentMigration = requiredMigrations.stream().mapToInt(DataMigration::getRevision).max().orElse(-1);
        String updateVersion = "UPDATE " + this.getMigrationsTableName() + " SET migration_version = ?";
        try (PreparedStatement statement = connection.prepareStatement(updateVersion)) {
            statement.setInt(1, currentMigration);
            statement.execute();
        }
    }));
}
Also used : DatabaseConnector(dev.rosewood.rosegarden.database.DatabaseConnector) List(java.util.List) ResultSet(java.sql.ResultSet) DataMigration(dev.rosewood.rosegarden.database.DataMigration) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) RoseGardenUtils(dev.rosewood.rosegarden.utils.RoseGardenUtils) PreparedStatement(java.sql.PreparedStatement) Comparator(java.util.Comparator) Collectors(java.util.stream.Collectors) RosePlugin(dev.rosewood.rosegarden.RosePlugin) ArrayList(java.util.ArrayList) DatabaseConnector(dev.rosewood.rosegarden.database.DatabaseConnector) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) List(java.util.List) ArrayList(java.util.ArrayList) DataMigration(dev.rosewood.rosegarden.database.DataMigration)

Aggregations

RosePlugin (dev.rosewood.rosegarden.RosePlugin)1 DataMigration (dev.rosewood.rosegarden.database.DataMigration)1 DatabaseConnector (dev.rosewood.rosegarden.database.DatabaseConnector)1 SQLiteConnector (dev.rosewood.rosegarden.database.SQLiteConnector)1 RoseGardenUtils (dev.rosewood.rosegarden.utils.RoseGardenUtils)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1