chibenwa commented on code in PR #2679: URL: https://github.com/apache/james-project/pull/2679#discussion_r1998698387
########## server/apps/migration/core-data-jpa-to-pg/src/main/java/org/apache/james/JpaToPgCoreDataMigration.java: ########## @@ -0,0 +1,455 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.james; + +import java.util.Set; +import java.util.UUID; + +import jakarta.inject.Inject; +import jakarta.persistence.EntityManagerFactory; + +import org.apache.james.backends.cassandra.migration.Migration; +import org.apache.james.backends.postgres.PostgresModule; +import org.apache.james.core.Username; +import org.apache.james.domainlist.api.DomainListException; +import org.apache.james.domainlist.jpa.JPADomainList; +import org.apache.james.domainlist.lib.DomainListConfiguration; +import org.apache.james.domainlist.postgres.PostgresDomainList; +import org.apache.james.droplists.jpa.JPADropList; +import org.apache.james.droplists.postgres.PostgresDropList; +import org.apache.james.filesystem.api.FileSystem; +import org.apache.james.mailrepository.api.MailRepositoryUrlStore; +import org.apache.james.mailrepository.jpa.JPAMailRepositoryUrlStore; +import org.apache.james.mailrepository.postgres.PostgresMailRepositoryUrlStore; +import org.apache.james.modules.data.JPAEntityManagerModule; +import org.apache.james.modules.data.PostgresCommonModule; +import org.apache.james.modules.data.PostgresDomainListModule; +import org.apache.james.modules.data.PostgresDropListsModule; +import org.apache.james.modules.data.PostgresQuotaGuiceModule; +import org.apache.james.modules.data.PostgresRecipientRewriteTableModule; +import org.apache.james.modules.data.PostgresUsersRepositoryModule; +import org.apache.james.modules.data.SievePostgresRepositoryModules; +import org.apache.james.modules.server.DNSServiceModule; +import org.apache.james.modules.server.DropWizardMetricsModule; +import org.apache.james.rrt.api.RecipientRewriteTableException; +import org.apache.james.rrt.jpa.JPARecipientRewriteTable; +import org.apache.james.rrt.postgres.PostgresRecipientRewriteTable; +import org.apache.james.server.core.configuration.Configuration; +import org.apache.james.server.core.configuration.ConfigurationProvider; +import org.apache.james.server.core.configuration.FileConfigurationProvider; +import org.apache.james.server.core.filesystem.FileSystemImpl; +import org.apache.james.sieve.jpa.JPASieveRepository; +import org.apache.james.sieve.jpa.model.JPASieveScript; +import org.apache.james.sieve.postgres.PostgresSieveRepository; +import org.apache.james.sieverepository.api.ScriptContent; +import org.apache.james.sieverepository.api.ScriptName; +import org.apache.james.user.api.AlreadyExistInUsersRepositoryException; +import org.apache.james.user.api.UsersRepositoryException; +import org.apache.james.user.jpa.JPAUsersDAO; +import org.apache.james.user.jpa.model.JPAUser; +import org.apache.james.user.lib.model.Algorithm; +import org.apache.james.user.lib.model.DefaultUser; +import org.apache.james.user.postgres.PostgresUsersDAO; +import org.apache.james.user.postgres.PostgresUsersRepositoryConfiguration; +import org.apache.james.util.LoggingLevel; +import org.apache.james.utils.InitializationOperation; +import org.apache.james.utils.InitializationOperations; +import org.apache.james.utils.InitilizationOperationBuilder; +import org.apache.james.utils.PropertiesProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.github.fge.lambdas.Throwing; +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Module; +import com.google.inject.Provides; +import com.google.inject.Scopes; +import com.google.inject.Singleton; +import com.google.inject.multibindings.Multibinder; +import com.google.inject.multibindings.ProvidesIntoSet; +import com.google.inject.util.Modules; + +public class JpaToPgCoreDataMigration { + private static final Logger LOGGER = LoggerFactory.getLogger(JpaToPgCoreDataMigration.class); + + private static final Module JPA_MODULES = Modules.combine( + new JPAEntityManagerModule(), + new UnboundJPAMigrationModule() + ); + private static final Module POSTGRESQL_MODULES = Modules.combine( + new PostgresCommonModule(), + new PostgresDomainListModule(), + new PostgresRecipientRewriteTableModule(), + new PostgresUsersRepositoryModule(), + new PostgresMailRepositoryUrlStoreModule(), + new PostgresDropListsModule(), + new PostgresQuotaGuiceModule(), + new SievePostgresRepositoryModules() + ); + + static final Module MIGRATION_MODULES = Modules.combine( + new DNSServiceModule(), + new DropWizardMetricsModule(), + + new CoreEntityValidatorsModule(), + PostgresUsersRepositoryModule.USER_CONFIGURATION_MODULE, + JPA_MODULES, + POSTGRESQL_MODULES + ); + + private final Injector injector; + + public static void main(String[] args) { + MigrationConfiguration configuration = MigrationConfiguration.builder() + .useWorkingDirectoryEnvProperty() + .build(); + LOGGER.info("Loading configuration {}", configuration.toString()); + + var module = Modules.combine( + new MigrationModule(configuration), + MIGRATION_MODULES + ); + + + JpaToPgCoreDataMigration migration = new JpaToPgCoreDataMigration(module); + migration.start(); + } + + static class DomainMigration { + private static final Logger LOGGER = LoggerFactory.getLogger(DomainMigration.class); + + private final JPADomainList jpaDomainList; + private final PostgresDomainList pgDomainList; + + @Inject + DomainMigration( + JPADomainList jpaDomainList, + PostgresDomainList pgDomainList + ) { + this.jpaDomainList = jpaDomainList; + this.pgDomainList = pgDomainList; + } + + void doMigration() { + LOGGER.info("Start domains migration"); + try { + jpaDomainList.getDomains().forEach(domain -> { + try { + pgDomainList.addDomain(domain); + } catch (DomainListException e) { + if (!e.getMessage().contains("already exists.")) { + LOGGER.warn("Failed to migrate domain {}", domain, e); + } + } + } + ); + + } catch (DomainListException e) { + throw new RuntimeException("Unable to migrate domains, aborting processing", e); + } + LOGGER.info("Domains migration completed"); + } + } + + static class UsersMigration { + private static final Logger LOGGER = LoggerFactory.getLogger(UsersMigration.class); + private final Algorithm algorithm; + private final JPAUsersDAO jpaUsersDAO; + private final PostgresUsersDAO postgresUsersDAO; + + + @Inject + UsersMigration( + JPAUsersDAO jpaUsersDAO, + PostgresUsersDAO postgresUsersDAO, + PostgresUsersRepositoryConfiguration postgresUsersRepositoryConfiguration + ) { + this.algorithm = postgresUsersRepositoryConfiguration.getPreferredAlgorithm(); + this.jpaUsersDAO = jpaUsersDAO; + this.postgresUsersDAO = postgresUsersDAO; + } + + void doMigration() { + LOGGER.info("Start users migration"); + try { + jpaUsersDAO.list().forEachRemaining(username -> { + try { + jpaUsersDAO.getUserByName(username).ifPresent(user -> { + var jpaUser = (JPAUser) user; + var pgUser = new DefaultUser( + username, + jpaUser.getPasswordHash(), + jpaUser.getAlgorithm(), + algorithm + ); + try { + postgresUsersDAO.addUser(username, UUID.randomUUID().toString()); Review Comment: Ok thanks for the explanation. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org