[DEV] implement partial migration to support new uuid for data and local cover of model

This commit is contained in:
Edouard DUPIN 2024-03-04 00:14:08 +01:00
parent c3e2b7240b
commit 48e14212db
5 changed files with 166 additions and 14 deletions

View File

@ -30,6 +30,7 @@ import org.kar.karusic.filter.KarusicAuthenticationFilter;
import org.kar.karusic.migration.Initialization; import org.kar.karusic.migration.Initialization;
import org.kar.karusic.migration.Migration20231126; import org.kar.karusic.migration.Migration20231126;
import org.kar.karusic.migration.Migration20240225; import org.kar.karusic.migration.Migration20240225;
import org.kar.karusic.migration.Migration20240226;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -39,15 +40,15 @@ public class WebLauncher {
final static Logger LOGGER = LoggerFactory.getLogger(WebLauncher.class); final static Logger LOGGER = LoggerFactory.getLogger(WebLauncher.class);
protected UpdateJwtPublicKey keyUpdater = null; protected UpdateJwtPublicKey keyUpdater = null;
protected HttpServer server = null; protected HttpServer server = null;
public WebLauncher() { public WebLauncher() {
ConfigBaseVariable.bdDatabase = "karusic"; ConfigBaseVariable.bdDatabase = "karusic";
} }
private static URI getBaseURI() { private static URI getBaseURI() {
return UriBuilder.fromUri(ConfigBaseVariable.getlocalAddress()).build(); return UriBuilder.fromUri(ConfigBaseVariable.getlocalAddress()).build();
} }
public void migrateDB() throws Exception { public void migrateDB() throws Exception {
WebLauncher.LOGGER.info("Create migration engine"); WebLauncher.LOGGER.info("Create migration engine");
final MigrationEngine migrationEngine = new MigrationEngine(); final MigrationEngine migrationEngine = new MigrationEngine();
@ -56,16 +57,17 @@ public class WebLauncher {
WebLauncher.LOGGER.info("Add migration since last version"); WebLauncher.LOGGER.info("Add migration since last version");
migrationEngine.add(new Migration20231126()); migrationEngine.add(new Migration20231126());
migrationEngine.add(new Migration20240225()); migrationEngine.add(new Migration20240225());
migrationEngine.add(new Migration20240226());
WebLauncher.LOGGER.info("Migrate the DB [START]"); WebLauncher.LOGGER.info("Migrate the DB [START]");
migrationEngine.migrateWaitAdmin(GlobalConfiguration.dbConfig); migrationEngine.migrateWaitAdmin(GlobalConfiguration.dbConfig);
WebLauncher.LOGGER.info("Migrate the DB [STOP]"); WebLauncher.LOGGER.info("Migrate the DB [STOP]");
} }
public static void main(final String[] args) throws Exception { public static void main(final String[] args) throws Exception {
WebLauncher.LOGGER.info("[START] application wake UP"); WebLauncher.LOGGER.info("[START] application wake UP");
final WebLauncher launcher = new WebLauncher(); final WebLauncher launcher = new WebLauncher();
launcher.migrateDB(); launcher.migrateDB();
launcher.process(); launcher.process();
WebLauncher.LOGGER.info("end-configure the server & wait finish process:"); WebLauncher.LOGGER.info("end-configure the server & wait finish process:");
Thread.currentThread().join(); Thread.currentThread().join();
@ -73,13 +75,13 @@ public class WebLauncher {
launcher.stopOther(); launcher.stopOther();
WebLauncher.LOGGER.info("STOP the REST server:"); WebLauncher.LOGGER.info("STOP the REST server:");
} }
public void process() throws InterruptedException { public void process() throws InterruptedException {
// =================================================================== // ===================================================================
// Configure resources // Configure resources
// =================================================================== // ===================================================================
final ResourceConfig rc = new ResourceConfig(); final ResourceConfig rc = new ResourceConfig();
// add multipart models .. // add multipart models ..
rc.register(MultiPartFeature.class); rc.register(MultiPartFeature.class);
// global authentication system // global authentication system
@ -101,15 +103,15 @@ public class WebLauncher {
rc.register(PlaylistResource.class); rc.register(PlaylistResource.class);
rc.register(TrackResource.class); rc.register(TrackResource.class);
rc.register(DataResource.class); rc.register(DataResource.class);
rc.register(HealthCheck.class); rc.register(HealthCheck.class);
rc.register(Front.class); rc.register(Front.class);
// add jackson to be discover when we are ins standalone server // add jackson to be discover when we are ins standalone server
rc.register(JacksonFeature.class); rc.register(JacksonFeature.class);
// enable this to show low level request // enable this to show low level request
//rc.property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_SERVER, Level.WARNING.getName()); //rc.property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_SERVER, Level.WARNING.getName());
//System.out.println("Connect on the BDD:"); //System.out.println("Connect on the BDD:");
//System.out.println(" getDBHost: '" + ConfigVariable.getDBHost() + "'"); //System.out.println(" getDBHost: '" + ConfigVariable.getDBHost() + "'");
//System.out.println(" getDBPort: '" + ConfigVariable.getDBPort() + "'"); //System.out.println(" getDBPort: '" + ConfigVariable.getDBPort() + "'");
@ -127,13 +129,13 @@ public class WebLauncher {
serverLink.shutdownNow(); serverLink.shutdownNow();
} }
}, "shutdownHook")); }, "shutdownHook"));
// =================================================================== // ===================================================================
// start periodic update of the token ... // start periodic update of the token ...
// =================================================================== // ===================================================================
this.keyUpdater = new UpdateJwtPublicKey(); this.keyUpdater = new UpdateJwtPublicKey();
this.keyUpdater.start(); this.keyUpdater.start();
// =================================================================== // ===================================================================
// run JERSEY // run JERSEY
// =================================================================== // ===================================================================
@ -145,14 +147,14 @@ public class WebLauncher {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void stop() { public void stop() {
if (this.server != null) { if (this.server != null) {
this.server.shutdownNow(); this.server.shutdownNow();
this.server = null; this.server = null;
} }
} }
public void stopOther() { public void stopOther() {
this.keyUpdater.kill(); this.keyUpdater.kill();
try { try {

View File

@ -0,0 +1,112 @@
package org.kar.karusic.migration;
import org.kar.archidata.migration.MigrationSqlStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Migration20240226 extends MigrationSqlStep {
private static final Logger LOGGER = LoggerFactory.getLogger(Migration20240226.class);
public static final int KARSO_INITIALISATION_ID = 1;
@Override
public String getName() {
return "migration-2024-02-26: convert base with UUID";
}
public Migration20240226() {
}
@Override
public void generateStep() throws Exception {
/*
addAction("""
ALTER TABLE `data` ADD `uuid` binary(16) AFTER `id`;
""");
addAction(() -> {
final List<UUIDConversion> datas = DataAccess.gets(UUIDConversion.class, new AccessDeletedItems(), new OverrideTableName("data"));
for (final UUIDConversion elem: datas) {
elem.uuid = UUID.randomUUID();
//elem.uuid = new UUID(elem.id * (long)Math.pow(2, 32), 0);
}
for (final UUIDConversion elem: datas) {
DataAccess.update(elem, elem.id, List.of("uuid"), new OverrideTableName("data"));
}
});
addAction("""
ALTER TABLE `data` CHANGE `uuid` `uuid` binary(16) DEFAULT (UUID_TO_BIN(UUID(), TRUE));
""");
final List<String> tableToTransform = List.of("album", "artist", "gender", "playlist", "track", "user");
for (final String tableName : tableToTransform ) {
addAction("ALTER TABLE `" + tableName + "` ADD `covers` text NULL;");
addAction(() -> {
final List<UUIDConversion> datas = DataAccess.gets(UUIDConversion.class, new AccessDeletedItems(), new OverrideTableName("data"));
final List<CoverConversion> medias = DataAccess.gets(CoverConversion.class, new AccessDeletedItems(), new OverrideTableName(tableName));
final List<LinkTable> links = DataAccess.gets(LinkTable.class, new OverrideTableName(tableName + "_link_cover"));
LOGGER.info("Get somes data: {} {} {}", datas.size(), medias.size(), links.size());
for (final CoverConversion media: medias) {
final List<UUID> values = new ArrayList<>();
for (final LinkTable link: links) {
if (link.object1Id.equals(media.id)) {
for (final UUIDConversion data: datas) {
if (data.id.equals(link.object2Id)) {
values.add(data.uuid);
break;
}
}
break;
}
}
if (values.size() != 0) {
media.covers = values;
LOGGER.info(" update: {} => {}", media.id, media.covers);
DataAccess.update(media, media.id, List.of("covers"), new OverrideTableName(tableName));
}
}
});
addAction("DROP TABLE `" + tableName + "_link_cover`;");
}
addAction("""
ALTER TABLE `track` ADD `dataUUID` binary(16) AFTER dataId;
""");
addAction(() -> {
final List<UUIDConversion> datas = DataAccess.gets(UUIDConversion.class, new AccessDeletedItems(), new OverrideTableName("data"));
final List<MediaConversion> medias = DataAccess.gets(MediaConversion.class, new AccessDeletedItems(), new OverrideTableName("track"));
for (final MediaConversion media: medias) {
for (final UUIDConversion data: datas) {
if (data.id.equals(media.dataId)) {
media.dataUUID = data.uuid;
DataAccess.update(media, media.id, List.of("dataUUID"), new OverrideTableName("track"));
break;
}
}
}
});
addAction("""
ALTER TABLE `track` CHANGE `dataUUID` `dataUUID` binary(16) NOT NULL;
""");
addAction("""
ALTER TABLE `track` DROP `dataId`;
""");
*/
addAction("""
DROP TABLE `playlist`;
""");
addAction("""
DROP TABLE `playlist_link_track`;
""");
/*
// Move the files...
addAction(() -> {
final List<UUIDConversion> datas = DataAccess.gets(UUIDConversion.class, new AccessDeletedItems(), new OverrideTableName("data"));
for (final UUIDConversion data: datas) {
final String origin = DataResource.getFileData(data.id);
final String destination = DataResource.getFileData(data.uuid);
}
});
*/
}
}

View File

@ -0,0 +1,15 @@
package org.kar.karusic.migration.model;
import java.util.List;
import java.util.UUID;
import org.kar.archidata.annotation.DataJson;
import jakarta.persistence.Id;
public class CoverConversion {
@Id
public Long id = null;
@DataJson
public List<UUID> covers = null;
}

View File

@ -0,0 +1,12 @@
package org.kar.karusic.migration.model;
import java.util.UUID;
import jakarta.persistence.Id;
public class MediaConversion {
@Id
public Long id = null;
public Long dataId = null;
public UUID dataUUID = null;
}

View File

@ -0,0 +1,11 @@
package org.kar.karusic.migration.model;
import java.util.UUID;
import jakarta.persistence.Id;
public class UUIDConversion {
@Id
public Long id = null;
public UUID uuid = null;
}