Skip to content

Commit

Permalink
Extends SchemaUtils for custom config keys (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvollmar authored Jul 1, 2021
1 parent 61d5648 commit 2f7ec79
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ import slick.jdbc.SQLServerProfile
@InternalApi
private[jdbc] object SchemaUtilsImpl {

def legacy(config: Config): Boolean =
config.getString("jdbc-journal.dao") != "akka.persistence.jdbc.journal.dao.DefaultJournalDao"
def legacy(configKey: String, config: Config): Boolean =
config.getConfig(configKey).getString("dao") != "akka.persistence.jdbc.journal.dao.DefaultJournalDao"

/**
* INTERNAL API
*/
@InternalApi
private[jdbc] def dropIfExists(logger: Logger)(implicit actorSystem: ClassicActorSystemProvider): Future[Done] = {
val slickDb: SlickDatabase = loadSlickDatabase("jdbc-journal")
private[jdbc] def dropIfExists(configKey: String, logger: Logger)(
implicit actorSystem: ClassicActorSystemProvider): Future[Done] = {
val slickDb: SlickDatabase = loadSlickDatabase(configKey)
val (fileToLoad, separator) =
dropScriptFor(slickProfileToSchemaType(slickDb.profile), legacy(actorSystem.classicSystem.settings.config))
dropScriptFor(
slickProfileToSchemaType(slickDb.profile),
legacy(configKey, actorSystem.classicSystem.settings.config))

val blockingEC = actorSystem.classicSystem.dispatchers.lookup(Dispatchers.DefaultBlockingDispatcherId)
Future(applyScriptWithSlick(fromClasspathAsString(fileToLoad), separator, logger, slickDb.database))(blockingEC)
Expand All @@ -50,12 +53,14 @@ private[jdbc] object SchemaUtilsImpl {
* INTERNAL API
*/
@InternalApi
private[jdbc] def createIfNotExists(logger: Logger)(
private[jdbc] def createIfNotExists(configKey: String, logger: Logger)(
implicit actorSystem: ClassicActorSystemProvider): Future[Done] = {

val slickDb: SlickDatabase = loadSlickDatabase("jdbc-journal")
val slickDb: SlickDatabase = loadSlickDatabase(configKey)
val (fileToLoad, separator) =
createScriptFor(slickProfileToSchemaType(slickDb.profile), legacy(actorSystem.classicSystem.settings.config))
createScriptFor(
slickProfileToSchemaType(slickDb.profile),
legacy(configKey, actorSystem.classicSystem.settings.config))

val blockingEC = actorSystem.classicSystem.dispatchers.lookup(Dispatchers.DefaultBlockingDispatcherId)
Future(applyScriptWithSlick(fromClasspathAsString(fileToLoad), separator, logger, slickDb.database))(blockingEC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,26 @@ object SchemaUtils {
*/
@ApiMayChange
def dropIfExists(actorSystem: ClassicActorSystemProvider): CompletionStage[Done] =
SchemaUtilsImpl.dropIfExists(logger)(actorSystem).toJava
dropIfExists(configKey = "jdbc-journal", actorSystem)

/**
* Drops the schema for both the journal and the snapshot table using the default schema definition.
*
* For information about the different schemas and supported databases consult
* https://doc.akka.io/docs/akka-persistence-jdbc/current/index.html#database-schema
*
* This utility method is intended to be used for testing only.
* For production, it's recommended to run any DDL statements before the system is started.
*
* This method will automatically detects the configured database using the settings from `configKey` config.
* If configured with `use-shared-db`, it will use the `akka-persistence-jdbc.shared-databases` definition instead.
* See https://doc.akka.io/docs/akka-persistence-jdbc/current/index.html#sharing-the-database-connection-pool-between-the-journals for details.
*
* @param configKey the database journal configuration key to use.
*/
@ApiMayChange
def dropIfExists(configKey: String, actorSystem: ClassicActorSystemProvider): CompletionStage[Done] =
SchemaUtilsImpl.dropIfExists(configKey, logger)(actorSystem).toJava

/**
* Creates the schema for both the journal and the snapshot table using the default schema definition.
Expand All @@ -51,7 +70,26 @@ object SchemaUtils {
*/
@ApiMayChange
def createIfNotExists(actorSystem: ClassicActorSystemProvider): CompletionStage[Done] =
SchemaUtilsImpl.createIfNotExists(logger)(actorSystem).toJava
createIfNotExists("jdbc-journal", actorSystem)

/**
* Creates the schema for both the journal and the snapshot table using the default schema definition.
*
* For information about the different schemas and supported databases consult
* https://doc.akka.io/docs/akka-persistence-jdbc/current/index.html#database-schema
*
* This utility method is intended to be used for testing only.
* For production, it's recommended to create run DDL statements before the system is started.
*
* This method will automatically detects the configured database using the settings from `configKey` config.
* If configured with `use-shared-db`, it will use the `akka-persistence-jdbc.shared-databases` definition instead.
* See https://doc.akka.io/docs/akka-persistence-jdbc/current/index.html#sharing-the-database-connection-pool-between-the-journals for details.
*
* @param configKey the database journal configuration key to use.
*/
@ApiMayChange
def createIfNotExists(configKey: String, actorSystem: ClassicActorSystemProvider): CompletionStage[Done] =
SchemaUtilsImpl.createIfNotExists(configKey, logger)(actorSystem).toJava

/**
* This method can be used to load alternative DDL scripts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,26 @@ object SchemaUtils {
*/
@ApiMayChange
def dropIfExists()(implicit actorSystem: ClassicActorSystemProvider): Future[Done] =
SchemaUtilsImpl.dropIfExists(logger)
dropIfExists(configKey = "jdbc-journal")

/**
* Drops the schema for both the journal and the snapshot table using the default schema definition.
*
* For information about the different schemas and supported databases consult
* https://doc.akka.io/docs/akka-persistence-jdbc/current/index.html#database-schema
*
* This utility method is intended to be used for testing only.
* For production, it's recommended to run any DDL statements before the system is started.
*
* This method will automatically detects the configured database using the settings from `configKey` config.
* If configured with `use-shared-db`, it will use the `akka-persistence-jdbc.shared-databases` definition instead.
* See https://doc.akka.io/docs/akka-persistence-jdbc/current/index.html#sharing-the-database-connection-pool-between-the-journals for details.
*
* @param configKey the database journal configuration key to use.
*/
@ApiMayChange
def dropIfExists(configKey: String)(implicit actorSystem: ClassicActorSystemProvider): Future[Done] =
SchemaUtilsImpl.dropIfExists(configKey, logger)

/**
* Creates the schema for both the journal and the snapshot table using the default schema definition.
Expand All @@ -49,7 +68,26 @@ object SchemaUtils {
*/
@ApiMayChange
def createIfNotExists()(implicit actorSystem: ClassicActorSystemProvider): Future[Done] =
SchemaUtilsImpl.createIfNotExists(logger)
createIfNotExists(configKey = "jdbc-journal")

/**
* Creates the schema for both the journal and the snapshot table using the default schema definition.
*
* For information about the different schemas and supported databases consult
* https://doc.akka.io/docs/akka-persistence-jdbc/current/index.html#database-schema
*
* This utility method is intended to be used for testing only.
* For production, it's recommended to run any DDL statements before the system is started.
*
* This method will automatically detects the configured database using the settings from `configKey` config.
* If configured with `use-shared-db`, it will use the `akka-persistence-jdbc.shared-databases` definition instead.
* See https://doc.akka.io/docs/akka-persistence-jdbc/current/index.html#sharing-the-database-connection-pool-between-the-journals for details.
*
* @param configKey the database journal configuration key to use.
*/
@ApiMayChange
def createIfNotExists(configKey: String)(implicit actorSystem: ClassicActorSystemProvider): Future[Done] =
SchemaUtilsImpl.createIfNotExists(configKey, logger)

/**
* This method can be used to load alternative DDL scripts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private[jdbc] trait DropCreate {
def db: Database
def config: Config

def newDao: Boolean = !SchemaUtilsImpl.legacy(config)
def newDao: Boolean = !SchemaUtilsImpl.legacy("jdbc-journal", config)

/**
* INTERNAL API
Expand Down

0 comments on commit 2f7ec79

Please sign in to comment.