-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: SQL Server / mssql dialect support
- Loading branch information
1 parent
8035859
commit 7f7454e
Showing
29 changed files
with
2,379 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,6 +177,40 @@ jobs: | |
cp .jvmopts-ci .jvmopts | ||
sbt -Dconfig.resource=application-h2.conf test | ||
test-sqlserver: | ||
name: Run test with SQL Server | ||
runs-on: ubuntu-22.04 | ||
if: github.repository == 'akka/akka-persistence-r2dbc' | ||
steps: | ||
- name: Checkout | ||
uses: actions/[email protected] | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Checkout GitHub merge | ||
if: github.event.pull_request | ||
run: |- | ||
git fetch origin pull/${{ github.event.pull_request.number }}/merge:scratch | ||
git checkout scratch | ||
- name: Cache Coursier cache | ||
uses: coursier/[email protected] | ||
|
||
- name: Set up JDK 11 | ||
uses: coursier/[email protected] | ||
with: | ||
jvm: temurin:1.11.0 | ||
|
||
- name: Start DB | ||
run: |- | ||
docker compose -f docker/docker-compose-sqlserver.yml up --wait | ||
docker exec -i sqlserver-db /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YourStrong@Passw0rd>' -d master < ddl-scripts/create_tables_sqlserver.sql | ||
- name: sbt test | ||
run: |- | ||
cp .jvmopts-ci .jvmopts | ||
sbt -Dconfig.resource=application-sqlserver.conf test | ||
test-docs: | ||
name: Docs | ||
runs-on: ubuntu-22.04 | ||
|
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
90 changes: 90 additions & 0 deletions
90
core/src/main/scala/akka/persistence/r2dbc/internal/sqlserver/SqlServerDialect.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,90 @@ | ||
/* | ||
* Copyright (C) 2022 - 2023 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.persistence.r2dbc.internal.sqlserver | ||
|
||
import akka.actor.typed.ActorSystem | ||
import akka.annotation.InternalApi | ||
import akka.persistence.r2dbc.R2dbcSettings | ||
import akka.persistence.r2dbc.internal._ | ||
import akka.util.JavaDurationConverters.JavaDurationOps | ||
import com.typesafe.config.Config | ||
import io.r2dbc.spi.ConnectionFactories | ||
import io.r2dbc.spi.ConnectionFactory | ||
import io.r2dbc.spi.ConnectionFactoryOptions | ||
|
||
import java.time.{ Duration => JDuration } | ||
import scala.concurrent.duration.FiniteDuration | ||
|
||
/** | ||
* INTERNAL API | ||
*/ | ||
@InternalApi | ||
private[r2dbc] object SqlServerDialect extends Dialect { | ||
|
||
private[r2dbc] final class SqlServerConnectionFactorySettings(config: Config) { | ||
val urlOption: Option[String] = | ||
Option(config.getString("url")) | ||
.filter(_.trim.nonEmpty) | ||
|
||
val driver: String = config.getString("driver") | ||
val host: String = config.getString("host") | ||
val port: Int = config.getInt("port") | ||
val user: String = config.getString("user") | ||
val password: String = config.getString("password") | ||
val database: String = config.getString("database") | ||
val connectTimeout: FiniteDuration = config.getDuration("connect-timeout").asScala | ||
|
||
} | ||
|
||
override def name: String = "sqlserver" | ||
|
||
override def adaptSettings(settings: R2dbcSettings): R2dbcSettings = { | ||
val res = settings | ||
// app timestamp is db timestamp because sqlserver does not provide a transaction timestamp | ||
.withUseAppTimestamp(true) | ||
// saw flaky tests where the Instant.now was smaller then the db timestamp AFTER the insert | ||
.withDbTimestampMonotonicIncreasing(false) | ||
res | ||
} | ||
|
||
override def createConnectionFactory(config: Config): ConnectionFactory = { | ||
|
||
val settings = new SqlServerConnectionFactorySettings(config) | ||
val builder = | ||
settings.urlOption match { | ||
case Some(url) => | ||
ConnectionFactoryOptions | ||
.builder() | ||
.from(ConnectionFactoryOptions.parse(url)) | ||
case _ => | ||
ConnectionFactoryOptions | ||
.builder() | ||
.option(ConnectionFactoryOptions.DRIVER, settings.driver) | ||
.option(ConnectionFactoryOptions.HOST, settings.host) | ||
.option(ConnectionFactoryOptions.PORT, Integer.valueOf(settings.port)) | ||
.option(ConnectionFactoryOptions.USER, settings.user) | ||
.option(ConnectionFactoryOptions.PASSWORD, settings.password) | ||
.option(ConnectionFactoryOptions.DATABASE, settings.database) | ||
.option(ConnectionFactoryOptions.CONNECT_TIMEOUT, JDuration.ofMillis(settings.connectTimeout.toMillis)) | ||
} | ||
ConnectionFactories.get(builder.build()) | ||
} | ||
|
||
override def createJournalDao(settings: R2dbcSettings, connectionFactory: ConnectionFactory)(implicit | ||
system: ActorSystem[_]): JournalDao = | ||
new SqlServerJournalDao(settings, connectionFactory)(system.executionContext, system) | ||
|
||
override def createQueryDao(settings: R2dbcSettings, connectionFactory: ConnectionFactory)(implicit | ||
system: ActorSystem[_]): QueryDao = | ||
new SqlServerQueryDao(settings, connectionFactory)(system.executionContext, system) | ||
|
||
override def createSnapshotDao(settings: R2dbcSettings, connectionFactory: ConnectionFactory)(implicit | ||
system: ActorSystem[_]): SnapshotDao = | ||
new SqlServerSnapshotDao(settings, connectionFactory)(system.executionContext, system) | ||
|
||
override def createDurableStateDao(settings: R2dbcSettings, connectionFactory: ConnectionFactory)(implicit | ||
system: ActorSystem[_]): DurableStateDao = | ||
new SqlServerDurableStateDao(settings, connectionFactory)(system.executionContext, system) | ||
} |
60 changes: 60 additions & 0 deletions
60
core/src/main/scala/akka/persistence/r2dbc/internal/sqlserver/SqlServerDialectHelper.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,60 @@ | ||
/* | ||
* Copyright (C) 2022 - 2023 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.persistence.r2dbc.internal.sqlserver | ||
|
||
import akka.annotation.InternalApi | ||
import akka.persistence.r2dbc.internal.InstantFactory | ||
import com.typesafe.config.Config | ||
import io.r2dbc.spi.Row | ||
|
||
import java.time.Instant | ||
import java.time.LocalDateTime | ||
import java.util.TimeZone | ||
|
||
/** | ||
* INTERNAL API | ||
*/ | ||
@InternalApi | ||
private[r2dbc] object SqlServerDialectHelper { | ||
def apply(config: Config) = new SqlServerDialectHelper(config) | ||
} | ||
|
||
/** | ||
* INTERNAL API | ||
*/ | ||
@InternalApi | ||
private[r2dbc] class SqlServerDialectHelper(config: Config) { | ||
|
||
private val tagSeparator = config.getString("tag-separator") | ||
|
||
require(tagSeparator.length == 1, s"Tag separator '$tagSeparator' must be a single character.") | ||
|
||
def tagsToDb(tags: Set[String]): String = { | ||
if (tags.exists(_.contains(tagSeparator))) { | ||
throw new IllegalArgumentException( | ||
s"A tag in [$tags] contains the character '$tagSeparator' which is reserved. Please change `akka.persistence.r2dbc.sqlserver.tag-separator` to a character that is not contained by any of your tags.") | ||
} | ||
tags.mkString(tagSeparator) | ||
} | ||
|
||
def tagsFromDb(row: Row): Set[String] = row.get("tags", classOf[String]) match { | ||
case null => Set.empty[String] | ||
case entries => entries.split(tagSeparator).toSet | ||
} | ||
|
||
private val zone = TimeZone.getTimeZone("UTC").toZoneId | ||
|
||
def nowInstant(): Instant = InstantFactory.now() | ||
|
||
def nowLocalDateTime(): LocalDateTime = LocalDateTime.ofInstant(nowInstant(), zone) | ||
|
||
def toDbTimestamp(timestamp: Instant): LocalDateTime = | ||
LocalDateTime.ofInstant(timestamp, zone) | ||
|
||
def fromDbTimestamp(time: LocalDateTime): Instant = time | ||
.atZone(zone) | ||
.toInstant | ||
|
||
} |
Oops, something went wrong.