-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65ea38e
commit 25ea05c
Showing
7 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...a/voidpointer/spigot/voidwhitelist/storage/db/migration/AutoWhitelistSchemaMigration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package voidpointer.spigot.voidwhitelist.storage.db.migration; | ||
|
||
import voidpointer.spigot.voidwhitelist.storage.db.OrmliteDatabase; | ||
|
||
import java.sql.SQLException; | ||
|
||
final class AutoWhitelistSchemaMigration implements SchemaMigration { | ||
public static final String NAME = "auto_whitelist/whitelistable_rename"; | ||
private static final String RENAME_COLUMNS = "ALTER TABLE whitelist RENAME COLUMN uniqueId TO unique_id" + | ||
", RENAME COLUMN expiresAt TO expires_at"; | ||
|
||
@Override public String name() { | ||
return NAME; | ||
} | ||
|
||
@Override public void perform(final OrmliteDatabase database) throws SQLException { | ||
database.getWhitelistDao().executeRaw(RENAME_COLUMNS); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/voidpointer/spigot/voidwhitelist/storage/db/migration/MigrationModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package voidpointer.spigot.voidwhitelist.storage.db.migration; | ||
|
||
import com.j256.ormlite.field.DatabaseField; | ||
import com.j256.ormlite.table.DatabaseTable; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@DatabaseTable(tableName="migrations") | ||
public final class MigrationModel { | ||
@DatabaseField(id=true) | ||
private String name; | ||
@DatabaseField(columnName="is_finished") | ||
private boolean isFinished; | ||
} |
38 changes: 38 additions & 0 deletions
38
...n/java/voidpointer/spigot/voidwhitelist/storage/db/migration/OrmliteMigrationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package voidpointer.spigot.voidwhitelist.storage.db.migration; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import voidpointer.spigot.framework.localemodule.LocaleLog; | ||
import voidpointer.spigot.framework.localemodule.annotation.AutowiredLocale; | ||
import voidpointer.spigot.voidwhitelist.storage.db.OrmliteDatabase; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
final class OrmliteMigrationService { | ||
@AutowiredLocale private static LocaleLog log; | ||
private final OrmliteDatabase database; | ||
|
||
public CompletableFuture<Optional<Set<String>>> findAllFinished() { | ||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
return Optional.of(findAllFinished0()); | ||
} catch (final SQLException sqlException) { | ||
log.warn("Unable to get finished migrations list: {0}", sqlException.getMessage()); | ||
return Optional.empty(); | ||
} | ||
}); | ||
} | ||
|
||
void setFinished(final String migrationName) throws SQLException { | ||
database.getMigrationDao().createOrUpdate(new MigrationModel(migrationName, true)); | ||
} | ||
|
||
private Set<String> findAllFinished0() throws SQLException { | ||
return database.getMigrationDao().queryForMatching(new MigrationModel(null, true)).stream() | ||
.map(MigrationModel::getName).collect(Collectors.toSet()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/voidpointer/spigot/voidwhitelist/storage/db/migration/SchemaMigration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package voidpointer.spigot.voidwhitelist.storage.db.migration; | ||
|
||
import voidpointer.spigot.voidwhitelist.storage.db.OrmliteDatabase; | ||
|
||
import java.sql.SQLException; | ||
|
||
interface SchemaMigration { | ||
String name(); | ||
|
||
void perform(final OrmliteDatabase database) throws SQLException; | ||
} |
12 changes: 12 additions & 0 deletions
12
...java/voidpointer/spigot/voidwhitelist/storage/db/migration/SchemaMigrationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package voidpointer.spigot.voidwhitelist.storage.db.migration; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
final class SchemaMigrationRepository { | ||
public static Queue<SchemaMigration> migrations() { | ||
final Queue<SchemaMigration> migrations = new LinkedList<>(); | ||
migrations.add(new AutoWhitelistSchemaMigration()); | ||
return migrations; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/voidpointer/spigot/voidwhitelist/storage/db/migration/SchemaMigrator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package voidpointer.spigot.voidwhitelist.storage.db.migration; | ||
|
||
import com.j256.ormlite.misc.TransactionManager; | ||
import voidpointer.spigot.framework.localemodule.LocaleLog; | ||
import voidpointer.spigot.framework.localemodule.annotation.AutowiredLocale; | ||
import voidpointer.spigot.voidwhitelist.storage.db.OrmliteDatabase; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Queue; | ||
|
||
public final class SchemaMigrator { | ||
@AutowiredLocale private static LocaleLog log; | ||
private final OrmliteDatabase database; | ||
private final OrmliteMigrationService migrationService; | ||
|
||
public SchemaMigrator(final OrmliteDatabase database) { | ||
this.database = database; | ||
this.migrationService = new OrmliteMigrationService(database); | ||
} | ||
|
||
public void runUnfinishedMigrations() { | ||
migrationService.findAllFinished().join().ifPresent(finishedMigrations -> { | ||
final Queue<SchemaMigration> migrations = SchemaMigrationRepository.migrations(); | ||
while (!migrations.isEmpty()) { | ||
final SchemaMigration migration = migrations.poll(); | ||
if (finishedMigrations.contains(migration.name())) | ||
continue; | ||
if (!performMigration(migration)) | ||
break; | ||
} | ||
}); | ||
} | ||
|
||
private boolean performMigration(final SchemaMigration migration) { | ||
try { | ||
log.info("Performing {0} migration", migration.name()); | ||
final long start = System.currentTimeMillis(); | ||
TransactionManager.callInTransaction(database.getConnectionSource(), () -> { | ||
migration.perform(database); | ||
migrationService.setFinished(migration.name()); | ||
return null; | ||
}); | ||
log.info("{0} finished in {1} ms.", migration.name(), System.currentTimeMillis() - start); | ||
return true; | ||
} catch (final SQLException sqlException) { | ||
log.severe("Unable to perform migration {0}: {1}", migration.name(), sqlException.getMessage()); | ||
return false; | ||
} | ||
} | ||
} |