From 682c2ec2e2bf853ea492aa566a926ba3a6dd4851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20Soykan?= Date: Mon, 7 Aug 2023 09:57:33 +0200 Subject: [PATCH] feature: add migration order to the migrations (#184) * feature: add migration order to the migrations * fix: make state and sqloperations protected on ReleationalDatabaseSystem * fix: exposedConfiguration made protected * add compatible substitute for elastic --- .../testing/e2e/couchbase/CouchbaseTestSystemTests.kt | 5 ++++- .../testing/e2e/elasticsearch/DefaultIndexMigrator.kt | 3 +++ .../trendyol/stove/testing/e2e/elasticsearch/Extensions.kt | 5 +++-- .../trendyol/stove/testing/e2e/elasticsearch/Options.kt | 2 ++ .../e2e/elasticsearch/ElasticsearchTestSystemTests.kt | 3 +++ .../stove/testing/e2e/rdbms/RelationalDatabaseSystem.kt | 7 ++++--- .../testing/e2e/database/migrations/DatabaseMigration.kt | 7 +++++++ .../testing/e2e/database/migrations/MigrationCollection.kt | 2 +- 8 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/stove-testing-e2e-couchbase/src/test/kotlin/com/trendyol/stove/testing/e2e/couchbase/CouchbaseTestSystemTests.kt b/lib/stove-testing-e2e-couchbase/src/test/kotlin/com/trendyol/stove/testing/e2e/couchbase/CouchbaseTestSystemTests.kt index 30ef1f4c..2355030f 100644 --- a/lib/stove-testing-e2e-couchbase/src/test/kotlin/com/trendyol/stove/testing/e2e/couchbase/CouchbaseTestSystemTests.kt +++ b/lib/stove-testing-e2e-couchbase/src/test/kotlin/com/trendyol/stove/testing/e2e/couchbase/CouchbaseTestSystemTests.kt @@ -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 @@ -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" @@ -47,6 +48,8 @@ class NoOpApplication : ApplicationUnderTest { class DefaultMigration : DatabaseMigration { private val logger: Logger = LoggerFactory.getLogger(javaClass) + + override val order: Int = MigrationPriority.HIGHEST.value override suspend fun execute(connection: ReactiveCluster) { connection .bucket(testBucket) diff --git a/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/DefaultIndexMigrator.kt b/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/DefaultIndexMigrator.kt index 3eee2289..335e35be 100644 --- a/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/DefaultIndexMigrator.kt +++ b/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/DefaultIndexMigrator.kt @@ -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 @@ -18,6 +19,8 @@ data class DefaultIndex( class DefaultIndexMigrator(private val index: String) : DatabaseMigration { 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) diff --git a/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/Extensions.kt b/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/Extensions.kt index 1bbe6635..c6a5e2ea 100644 --- a/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/Extensions.kt +++ b/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/Extensions.kt @@ -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()) diff --git a/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/Options.kt b/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/Options.kt index 6f4aa14b..53aa2cbc 100644 --- a/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/Options.kt +++ b/lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/Options.kt @@ -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 @@ -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 = None, val exposedPorts: List = listOf(9200), val password: String = "password", val disableSecurity: Boolean = true, diff --git a/lib/stove-testing-e2e-elasticsearch/src/test/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/ElasticsearchTestSystemTests.kt b/lib/stove-testing-e2e-elasticsearch/src/test/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/ElasticsearchTestSystemTests.kt index 63fe0088..dc1375f7 100644 --- a/lib/stove-testing-e2e-elasticsearch/src/test/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/ElasticsearchTestSystemTests.kt +++ b/lib/stove-testing-e2e-elasticsearch/src/test/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/ElasticsearchTestSystemTests.kt @@ -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 @@ -25,6 +26,7 @@ const val testIndex = "stove-test-index" const val anotherIndex = "stove-another-index" class TestIndexMigrator : DatabaseMigration { + 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() @@ -36,6 +38,7 @@ class TestIndexMigrator : DatabaseMigration { } class AnotherIndexMigrator : DatabaseMigration { + 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() diff --git a/lib/stove-testing-e2e-rdbms/src/main/kotlin/com/trendyol/stove/testing/e2e/rdbms/RelationalDatabaseSystem.kt b/lib/stove-testing-e2e-rdbms/src/main/kotlin/com/trendyol/stove/testing/e2e/rdbms/RelationalDatabaseSystem.kt index 40b6a91c..85a61a56 100644 --- a/lib/stove-testing-e2e-rdbms/src/main/kotlin/com/trendyol/stove/testing/e2e/rdbms/RelationalDatabaseSystem.kt +++ b/lib/stove-testing-e2e-rdbms/src/main/kotlin/com/trendyol/stove/testing/e2e/rdbms/RelationalDatabaseSystem.kt @@ -20,10 +20,11 @@ abstract class RelationalDatabaseSystem> 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, RelationalDatabaseExposedConfiguration> = + + protected lateinit var exposedConfiguration: RelationalDatabaseExposedConfiguration + protected lateinit var sqlOperations: SqlOperations + protected val state: StateOfSystem, RelationalDatabaseExposedConfiguration> = StateOfSystem(testSystem.options, javaClass.kotlin, RelationalDatabaseExposedConfiguration::class) protected abstract fun connectionFactory(exposedConfiguration: RelationalDatabaseExposedConfiguration): ConnectionFactory diff --git a/lib/stove-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/database/migrations/DatabaseMigration.kt b/lib/stove-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/database/migrations/DatabaseMigration.kt index d5948fce..77d7e478 100644 --- a/lib/stove-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/database/migrations/DatabaseMigration.kt +++ b/lib/stove-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/database/migrations/DatabaseMigration.kt @@ -17,4 +17,11 @@ interface DatabaseMigration { * [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) } diff --git a/lib/stove-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/database/migrations/MigrationCollection.kt b/lib/stove-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/database/migrations/MigrationCollection.kt index 8515e5cf..812c17c3 100644 --- a/lib/stove-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/database/migrations/MigrationCollection.kt +++ b/lib/stove-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/database/migrations/MigrationCollection.kt @@ -80,5 +80,5 @@ class MigrationCollection { * @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) } }