From fd37f4c31e079761a78204ae6f05269d58a023af Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 17 Dec 2024 09:25:23 +1100 Subject: [PATCH] Allows setting migrations format in the miskSchemaMigrator gradle plugin declaration. `migrationsFormat` defaults to `TRADITIONAL`. Example: ```kts miskSchemaMigrator { // database = ... // host = ... migrationsFormat = "TRADITIONAL" } ``` GitOrigin-RevId: e128d450e398aa9577f8985f506dddd6234857be --- misk-schema-migrator-gradle-plugin/README.md | 1 + .../build.gradle.kts | 1 + .../schemamigrator/SchemaMigratorExtension.kt | 3 +++ .../schemamigrator/SchemaMigratorModule.kt | 5 ++++- .../schemamigrator/SchemaMigratorPlugin.kt | 9 +++++++- .../SchemaMigratorPluginTest.kt | 13 +++++++---- .../build.gradle.kts | 22 +++++++++++++++++++ .../gradle.properties | 1 + .../settings.gradle.kts | 1 + .../main/resources/db-migrations/people.sql | 6 +++++ .../db-migrations/schema_version.sql | 5 +++++ .../src/main/resources/db.properties | 4 ++++ .../build.gradle.kts | 1 + 13 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/build.gradle.kts create mode 100644 misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/gradle.properties create mode 100644 misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/settings.gradle.kts create mode 100644 misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db-migrations/people.sql create mode 100644 misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db-migrations/schema_version.sql create mode 100644 misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db.properties diff --git a/misk-schema-migrator-gradle-plugin/README.md b/misk-schema-migrator-gradle-plugin/README.md index 86d7da29942..113ca9aa941 100644 --- a/misk-schema-migrator-gradle-plugin/README.md +++ b/misk-schema-migrator-gradle-plugin/README.md @@ -27,6 +27,7 @@ miskSchemaMigrator { username = dbConfig["user"] password = dbConfig["password"] migrationsDir = layout.projectDirectory.dir("src/main/resources/db-migrations") + migrationsFormat = "TRADITIONAL" } // If you want to integrate with JOOQ diff --git a/misk-schema-migrator-gradle-plugin/build.gradle.kts b/misk-schema-migrator-gradle-plugin/build.gradle.kts index 95f681dbb0d..b57c70ec147 100644 --- a/misk-schema-migrator-gradle-plugin/build.gradle.kts +++ b/misk-schema-migrator-gradle-plugin/build.gradle.kts @@ -32,6 +32,7 @@ dependencies { testImplementation(libs.assertj) testImplementation(libs.hikariCp) testImplementation(libs.junitApi) + testImplementation(libs.junitParams) } mavenPublishing { diff --git a/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorExtension.kt b/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorExtension.kt index 99f68b43831..411aa1457ed 100644 --- a/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorExtension.kt +++ b/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorExtension.kt @@ -31,4 +31,7 @@ abstract class SchemaMigratorExtension @Inject constructor(objects: ObjectFactor @get:InputDirectory abstract val migrationsDir: DirectoryProperty + + @get:Input + val migrationsFormat: Property = objects.property(String::class.java).convention("TRADITIONAL") } diff --git a/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorModule.kt b/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorModule.kt index 50e508cfd14..fbabbf2870f 100644 --- a/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorModule.kt +++ b/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorModule.kt @@ -6,6 +6,7 @@ import misk.inject.KAbstractModule import misk.jdbc.DataSourceConfig import misk.jdbc.DataSourceType import misk.jdbc.JdbcModule +import misk.jdbc.MigrationsFormat import misk.jdbc.RealDatabasePool import misk.resources.ResourceLoaderModule import wisp.deployment.TESTING @@ -19,7 +20,8 @@ class SchemaMigratorModule( private val dbType: String, private val username: String, private val password: String, - private val schemaDir: File + private val schemaDir: File, + private val migrationsFormat: String ): KAbstractModule() { override fun configure() { @@ -31,6 +33,7 @@ class SchemaMigratorModule( database = database, username = username, password = password, + migrations_format = MigrationsFormat.valueOf(migrationsFormat), ) bind().toInstance(Clock.systemUTC()) diff --git a/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorPlugin.kt b/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorPlugin.kt index 724cb8929c4..4a7d72f4f72 100644 --- a/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorPlugin.kt +++ b/misk-schema-migrator-gradle-plugin/src/main/kotlin/misk/gradle/schemamigrator/SchemaMigratorPlugin.kt @@ -2,6 +2,7 @@ package misk.gradle.schemamigrator import com.google.common.util.concurrent.ServiceManager import com.google.inject.Guice +import misk.jdbc.MigrationsFormat import org.gradle.api.DefaultTask import org.gradle.api.Plugin import org.gradle.api.Project @@ -27,6 +28,7 @@ class SchemaMigratorPlugin : Plugin { it.username.set(extension.username) it.password.set(extension.password) it.migrationsDir.set(extension.migrationsDir) + it.migrationsFormat.set(extension.migrationsFormat) } } @@ -63,6 +65,10 @@ abstract class SchemaMigratorTask : DefaultTask() { @get:InputDirectory abstract val migrationsDir: DirectoryProperty + @get:Input + @get:Optional + abstract val migrationsFormat: Property + @TaskAction fun migrateSchemas() { val injector = Guice.createInjector( @@ -73,7 +79,8 @@ abstract class SchemaMigratorTask : DefaultTask() { databaseType.get(), username.get(), password.get(), - migrationsDir.asFile.get() + migrationsDir.asFile.get(), + migrationsFormat.get() ) ) diff --git a/misk-schema-migrator-gradle-plugin/src/test/kotlin/misk/gradle/schemamigrator/SchemaMigratorPluginTest.kt b/misk-schema-migrator-gradle-plugin/src/test/kotlin/misk/gradle/schemamigrator/SchemaMigratorPluginTest.kt index 1308998a1fd..66e39b8afa3 100644 --- a/misk-schema-migrator-gradle-plugin/src/test/kotlin/misk/gradle/schemamigrator/SchemaMigratorPluginTest.kt +++ b/misk-schema-migrator-gradle-plugin/src/test/kotlin/misk/gradle/schemamigrator/SchemaMigratorPluginTest.kt @@ -6,13 +6,18 @@ import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.GradleRunner import org.gradle.testkit.runner.TaskOutcome import java.io.File -import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource import java.util.Properties class SchemaMigratorPluginTest { - @Test - fun `schema migrator plugin migrates schemas`() { - val testProjectDir = File(this.javaClass.getResource("/schema-migrator-plugin-test")!!.file) + @ParameterizedTest + @ValueSource(strings = [ + "/schema-migrator-plugin-test", + "/schema-migrator-plugin-test-with-declarative-migrations-format" + ]) + fun `schema migrator plugin migrates schemas`(projectDir: String) { + val testProjectDir = File(this.javaClass.getResource(projectDir)!!.file) val properties = Properties() properties.load(File(testProjectDir, "src/main/resources/db.properties").inputStream()) diff --git a/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/build.gradle.kts b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/build.gradle.kts new file mode 100644 index 00000000000..e1b0f541279 --- /dev/null +++ b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/build.gradle.kts @@ -0,0 +1,22 @@ +import java.util.Properties + +plugins { + id("com.squareup.misk.schema-migrator") +} + +val properties = Properties().apply { + val file = file("src/main/resources/db.properties") + if (file.exists()) { + load(file.inputStream()) + } +} + +miskSchemaMigrator { + host = "localhost" + port = 3306 + database = properties.getProperty("schema") + username = properties.getProperty("username") + password = properties.getProperty("password") + migrationsDir.set(layout.projectDirectory.dir("src/main/resources/db-migrations")) + migrationsFormat = "DECLARATIVE" +} diff --git a/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/gradle.properties b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/gradle.properties new file mode 100644 index 00000000000..ac1a35f78f1 --- /dev/null +++ b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/gradle.properties @@ -0,0 +1 @@ +org.gradle.configuration-cache=true \ No newline at end of file diff --git a/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/settings.gradle.kts b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/settings.gradle.kts new file mode 100644 index 00000000000..20308c41748 --- /dev/null +++ b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "test" diff --git a/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db-migrations/people.sql b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db-migrations/people.sql new file mode 100644 index 00000000000..188b51289ce --- /dev/null +++ b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db-migrations/people.sql @@ -0,0 +1,6 @@ +CREATE TABLE people ( + id BIGINT NOT NULL auto_increment, + name VARCHAR(191) NOT NULL, + nickname varchar(191) NOT NULL, + PRIMARY KEY (id) +); \ No newline at end of file diff --git a/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db-migrations/schema_version.sql b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db-migrations/schema_version.sql new file mode 100644 index 00000000000..7499d6fda09 --- /dev/null +++ b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db-migrations/schema_version.sql @@ -0,0 +1,5 @@ +CREATE TABLE `schema_version` ( + `version` varchar(50) COLLATE utf8mb4_bin NOT NULL, + `installed_by` varchar(30) COLLATE utf8mb4_bin DEFAULT NULL, + UNIQUE KEY `version` (`version`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin \ No newline at end of file diff --git a/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db.properties b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db.properties new file mode 100644 index 00000000000..87ed31873a1 --- /dev/null +++ b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test-with-declarative-migrations-format/src/main/resources/db.properties @@ -0,0 +1,4 @@ +jdbcUrl=jdbc:mysql://localhost:3306/ +schema=schema_migrator_plugin_test_with_migrations_format +username=root +password= \ No newline at end of file diff --git a/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test/build.gradle.kts b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test/build.gradle.kts index 643ba48526c..63817a1c190 100644 --- a/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test/build.gradle.kts +++ b/misk-schema-migrator-gradle-plugin/src/test/resources/schema-migrator-plugin-test/build.gradle.kts @@ -18,4 +18,5 @@ miskSchemaMigrator { username = properties.getProperty("username") password = properties.getProperty("password") migrationsDir.set(layout.projectDirectory.dir("src/main/resources/db-migrations")) + migrationsFormat = "TRADITIONAL" }