All Downloads are FREE. Search and download functionalities are using the official Maven repository.

akka.persistence.journal.sqlasync.SQLAsyncWriteJournal.scala Maven / Gradle / Ivy

There is a newer version: 0.5.1
Show newest version
package akka.persistence.journal.sqlasync

import akka.persistence.common.{MySQLPlugin, PostgreSQLPlugin}
import scala.concurrent.Future
import scalikejdbc._
import scalikejdbc.async.{TxAsyncDBSession, _}

class MySQLAsyncWriteJournal extends ScalikeJDBCWriteJournal with MySQLPlugin {
  override protected[this] def updateSequenceNr(persistenceId: String, sequenceNr: Long)
                                               (implicit session: TxAsyncDBSession): Future[Unit] = {
    val sql = sql"INSERT INTO $metadataTable (persistence_id, sequence_nr) VALUES ($persistenceId, $sequenceNr) ON DUPLICATE KEY UPDATE sequence_nr = $sequenceNr"
    logging(sql).update().future().map(_ => ())
  }
}

class PostgreSQLAsyncWriteJournal extends ScalikeJDBCWriteJournal with PostgreSQLPlugin {
  override protected[this] def updateSequenceNr(persistenceId: String, sequenceNr: Long)
                                               (implicit session: TxAsyncDBSession): Future[Unit] = {
    val sql = sql"WITH upsert AS (UPDATE $metadataTable SET sequence_nr = $sequenceNr WHERE persistence_id = $persistenceId RETURNING *) INSERT INTO $metadataTable (persistence_id, sequence_nr) SELECT $persistenceId, $sequenceNr WHERE NOT EXISTS (SELECT * FROM upsert)"
    logging(sql).update().future().map(_ => ())
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy