Skip to content

Commit

Permalink
feature: add migration order to the migrations (#184)
Browse files Browse the repository at this point in the history
* feature: add migration order to the migrations

* fix: make state and sqloperations protected on ReleationalDatabaseSystem

* fix: exposedConfiguration made protected

* add compatible substitute for elastic
  • Loading branch information
osoykan authored Aug 7, 2023
1 parent aa2ab16 commit 682c2ec
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.couchbase.client.java.ReactiveCluster
import com.couchbase.client.java.manager.collection.CollectionSpec
import com.trendyol.stove.testing.e2e.couchbase.CouchbaseSystem.Companion.shouldGet
import com.trendyol.stove.testing.e2e.database.migrations.DatabaseMigration
import com.trendyol.stove.testing.e2e.database.migrations.MigrationPriority
import com.trendyol.stove.testing.e2e.system.TestSystem
import com.trendyol.stove.testing.e2e.system.TestSystem.Companion.validate
import com.trendyol.stove.testing.e2e.system.abstractions.ApplicationUnderTest
Expand All @@ -16,7 +17,7 @@ import org.junit.jupiter.api.assertThrows
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.time.Duration
import java.util.UUID
import java.util.*

const val testBucket = "test-couchbase-bucket"

Expand Down Expand Up @@ -47,6 +48,8 @@ class NoOpApplication : ApplicationUnderTest<Unit> {

class DefaultMigration : DatabaseMigration<ReactiveCluster> {
private val logger: Logger = LoggerFactory.getLogger(javaClass)

override val order: Int = MigrationPriority.HIGHEST.value
override suspend fun execute(connection: ReactiveCluster) {
connection
.bucket(testBucket)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.trendyol.stove.testing.e2e.elasticsearch
import co.elastic.clients.elasticsearch.ElasticsearchClient
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest
import com.trendyol.stove.testing.e2e.database.migrations.DatabaseMigration
import com.trendyol.stove.testing.e2e.database.migrations.MigrationPriority
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand All @@ -18,6 +19,8 @@ data class DefaultIndex(

class DefaultIndexMigrator(private val index: String) : DatabaseMigration<ElasticsearchClient> {
private val logger: Logger = LoggerFactory.getLogger(javaClass)

override val order: Int = MigrationPriority.HIGHEST.value
override suspend fun execute(connection: ElasticsearchClient) {
val createIndexRequest: CreateIndexRequest = CreateIndexRequest.Builder()
.index(index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ fun TestSystem.withElasticsearch(
}

return withProvidedRegistry(
"elasticsearch/elasticsearch:${options.containerOptions.imageVersion}",
options.containerOptions.registry
imageName = "elasticsearch/elasticsearch:${options.containerOptions.imageVersion}",
registry = options.containerOptions.registry,
compatibleSubstitute = options.containerOptions.compatibleSubstitute.getOrNull()
) { ElasticsearchContainer(it) }
.apply {
addExposedPorts(*options.containerOptions.exposedPorts.toIntArray())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.trendyol.stove.testing.e2e.elasticsearch

import arrow.core.None
import arrow.core.Option
import arrow.core.none
import co.elastic.clients.elasticsearch.ElasticsearchClient
Expand Down Expand Up @@ -61,6 +62,7 @@ data class ElasticsearchContext(
data class ContainerOptions(
val registry: String = "docker.elastic.co/",
val imageVersion: String = "8.6.1",
val compatibleSubstitute: Option<String> = None,
val exposedPorts: List<Int> = listOf(9200),
val password: String = "password",
val disableSecurity: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.trendyol.stove.testing.e2e.database.migrations.DatabaseMigration
import com.trendyol.stove.testing.e2e.database.migrations.MigrationPriority
import com.trendyol.stove.testing.e2e.elasticsearch.ElasticsearchSystem.Companion.shouldGet
import com.trendyol.stove.testing.e2e.elasticsearch.ElasticsearchSystem.Companion.shouldQuery
import com.trendyol.stove.testing.e2e.system.TestSystem
Expand All @@ -25,6 +26,7 @@ const val testIndex = "stove-test-index"
const val anotherIndex = "stove-another-index"

class TestIndexMigrator : DatabaseMigration<ElasticsearchClient> {
override val order: Int = MigrationPriority.HIGHEST.value
private val logger: Logger = LoggerFactory.getLogger(javaClass)
override suspend fun execute(connection: ElasticsearchClient) {
val createIndexRequest: CreateIndexRequest = CreateIndexRequest.Builder()
Expand All @@ -36,6 +38,7 @@ class TestIndexMigrator : DatabaseMigration<ElasticsearchClient> {
}

class AnotherIndexMigrator : DatabaseMigration<ElasticsearchClient> {
override val order: Int = MigrationPriority.HIGHEST.value + 1
private val logger: Logger = LoggerFactory.getLogger(javaClass)
override suspend fun execute(connection: ElasticsearchClient) {
val createIndexRequest: CreateIndexRequest = CreateIndexRequest.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ abstract class RelationalDatabaseSystem<SELF : RelationalDatabaseSystem<SELF>> p
final override val testSystem: TestSystem,
protected val context: RelationalDatabaseContext<*>
) : PluggedSystem, RunAware, ExposesConfiguration {
private lateinit var sqlOperations: SqlOperations
private lateinit var exposedConfiguration: RelationalDatabaseExposedConfiguration
private val logger: Logger = LoggerFactory.getLogger(javaClass)
private val state: StateOfSystem<RelationalDatabaseSystem<SELF>, RelationalDatabaseExposedConfiguration> =

protected lateinit var exposedConfiguration: RelationalDatabaseExposedConfiguration
protected lateinit var sqlOperations: SqlOperations
protected val state: StateOfSystem<RelationalDatabaseSystem<SELF>, RelationalDatabaseExposedConfiguration> =
StateOfSystem(testSystem.options, javaClass.kotlin, RelationalDatabaseExposedConfiguration::class)

protected abstract fun connectionFactory(exposedConfiguration: RelationalDatabaseExposedConfiguration): ConnectionFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ interface DatabaseMigration<in TConnection> {
* [connection] is ready for executing operations
*/
suspend fun execute(connection: TConnection)

val order: Int
}

enum class MigrationPriority(val value: Int) {
LOWEST(Int.MAX_VALUE),
HIGHEST(Int.MIN_VALUE)
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ class MigrationCollection<TConnection> {
* @param connection The connection of the database
* @return Unit
*/
suspend fun run(connection: TConnection): Unit = types.map { it.value }.forEach { it.execute(connection) }
suspend fun run(connection: TConnection): Unit = types.map { it.value }.sortedBy { it.order }.forEach { it.execute(connection) }
}

0 comments on commit 682c2ec

Please sign in to comment.