-
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
Showing
21 changed files
with
434 additions
and
172 deletions.
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
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
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
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
File renamed without changes.
File renamed without changes.
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
121 changes: 121 additions & 0 deletions
121
myndla-api/src/main/scala/no/ndla/myndlaapi/LpMigration.scala
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,121 @@ | ||
/* | ||
* Part of NDLA myndla-api | ||
* Copyright (C) 2023 NDLA | ||
* | ||
* See LICENSE | ||
*/ | ||
|
||
package no.ndla.myndlaapi | ||
|
||
import com.typesafe.scalalogging.StrictLogging | ||
import com.zaxxer.hikari.HikariDataSource | ||
import no.ndla.common.Clock | ||
import no.ndla.myndla.model.domain.MyNDLAUserDocument | ||
import no.ndla.myndla.repository.{ConfigRepository, FolderRepository, UserRepository} | ||
import scalikejdbc.{ | ||
ConnectionPool, | ||
DBSession, | ||
DataSourceConnectionPool, | ||
NamedDB, | ||
scalikejdbcSQLInterpolationImplicitDef | ||
} | ||
|
||
// TODO: Delete this when migration is done | ||
case class LpMigration(props: MyNdlaApiProperties, localDataSource: HikariDataSource, lpDataSource: HikariDataSource) | ||
extends FolderRepository | ||
with UserRepository | ||
with ConfigRepository | ||
with Clock | ||
with StrictLogging { | ||
override val folderRepository: FolderRepository = new FolderRepository | ||
override val userRepository: UserRepository = new UserRepository | ||
override val configRepository: ConfigRepository = new ConfigRepository | ||
override val clock: SystemClock = new SystemClock | ||
|
||
def migrateConfig(lpSession: DBSession, myndlaSession: DBSession): Unit = { | ||
val allLpConfigs = configRepository.getAllConfigs(lpSession) | ||
allLpConfigs.foreach(x => configRepository.updateConfigParam(x)(myndlaSession)) | ||
} | ||
|
||
def migrateUsers(lpSession: DBSession, myndlaSession: DBSession): Unit = { | ||
val oldUsers = userRepository.getAllUsers(lpSession) | ||
oldUsers.foreach(x => | ||
userRepository.insertUser( | ||
x.feideId, | ||
MyNDLAUserDocument( | ||
favoriteSubjects = x.favoriteSubjects, | ||
userRole = x.userRole, | ||
lastUpdated = x.lastUpdated, | ||
organization = x.organization, | ||
groups = x.groups, | ||
username = x.username, | ||
displayName = x.displayName, | ||
email = x.email, | ||
arenaEnabled = x.arenaEnabled, | ||
shareName = x.shareName | ||
) | ||
)(myndlaSession) | ||
) | ||
} | ||
|
||
def migrateFolders(lpSession: DBSession, myndlaSession: DBSession): Unit = { | ||
val folders = folderRepository.getAllFolderRows(lpSession) | ||
val resources = folderRepository.getAllResourceRows(lpSession) | ||
val folderResources = folderRepository.getAllFolderResourceRows(lpSession) | ||
|
||
folders.foreach(x => folderRepository.insertFolderRow(x)(myndlaSession)) | ||
resources.foreach(x => folderRepository.insertResourceRow(x)(myndlaSession)) | ||
folderResources.foreach(x => folderRepository.insertFolderResourceRow(x)(myndlaSession)) | ||
} | ||
|
||
def detectExistingMigration(myndlaSession: DBSession): Boolean = { | ||
configRepository.getAllConfigs(myndlaSession).nonEmpty | ||
|
||
} | ||
|
||
def addForeignKeys(myndlasession: DBSession): Unit = { | ||
// Foreign keys constraints fails with migration so we need to add them after the migration is done :^) | ||
sql""" | ||
ALTER TABLE folders | ||
ADD CONSTRAINT fk_parent_id FOREIGN KEY (parent_id) REFERENCES folders(id); | ||
""".execute()(myndlasession): Unit | ||
|
||
sql""" | ||
ALTER TABLE folder_resources | ||
ADD CONSTRAINT folder_resources_folder_id_fkey | ||
FOREIGN KEY (folder_id) | ||
REFERENCES folders(id) | ||
ON DELETE CASCADE; | ||
""".execute()(myndlasession): Unit | ||
|
||
sql""" | ||
ALTER TABLE folder_resources | ||
ADD CONSTRAINT folder_resources_resource_id_fkey | ||
FOREIGN KEY (resource_id) | ||
REFERENCES resources(id) | ||
ON DELETE CASCADE; | ||
""".execute()(myndlasession): Unit | ||
} | ||
|
||
def start(): Unit = { | ||
val lpPool = new DataSourceConnectionPool(lpDataSource) | ||
val myNdlaPool = new DataSourceConnectionPool(localDataSource) | ||
ConnectionPool.add(Symbol("learningpath"), lpPool) | ||
ConnectionPool.add(Symbol("myndla"), myNdlaPool) | ||
|
||
NamedDB(Symbol("learningpath")).readOnly { lpSession => | ||
NamedDB(Symbol("myndla")).localTx { myndlaSession => | ||
if (!detectExistingMigration(myndlaSession)) { | ||
logger.info("Doing learningpath-api -> myndla-api migration!") | ||
|
||
migrateConfig(lpSession, myndlaSession) | ||
migrateUsers(lpSession, myndlaSession) | ||
migrateFolders(lpSession, myndlaSession) | ||
addForeignKeys(myndlaSession) | ||
} else { | ||
logger.info("Migration already done! Please delete `LpMigration` from `myndla-api`.") | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.