Skip to content

Commit

Permalink
Allows setting migrations format in the miskSchemaMigrator gradle
Browse files Browse the repository at this point in the history
plugin declaration.

`migrationsFormat` defaults to `TRADITIONAL`.

Example:
```kts
miskSchemaMigrator {
  // database = ...
  // host = ...
  migrationsFormat = "TRADITIONAL"
}
```
GitOrigin-RevId: e128d450e398aa9577f8985f506dddd6234857be
  • Loading branch information
hugomd authored and svc-squareup-copybara committed Dec 16, 2024
1 parent 19cbecd commit fd37f4c
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 6 deletions.
1 change: 1 addition & 0 deletions misk-schema-migrator-gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions misk-schema-migrator-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
testImplementation(libs.assertj)
testImplementation(libs.hikariCp)
testImplementation(libs.junitApi)
testImplementation(libs.junitParams)
}

mavenPublishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ abstract class SchemaMigratorExtension @Inject constructor(objects: ObjectFactor

@get:InputDirectory
abstract val migrationsDir: DirectoryProperty

@get:Input
val migrationsFormat: Property<String> = objects.property(String::class.java).convention("TRADITIONAL")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -31,6 +33,7 @@ class SchemaMigratorModule(
database = database,
username = username,
password = password,
migrations_format = MigrationsFormat.valueOf(migrationsFormat),
)

bind<Clock>().toInstance(Clock.systemUTC())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +28,7 @@ class SchemaMigratorPlugin : Plugin<Project> {
it.username.set(extension.username)
it.password.set(extension.password)
it.migrationsDir.set(extension.migrationsDir)
it.migrationsFormat.set(extension.migrationsFormat)
}
}

Expand Down Expand Up @@ -63,6 +65,10 @@ abstract class SchemaMigratorTask : DefaultTask() {
@get:InputDirectory
abstract val migrationsDir: DirectoryProperty

@get:Input
@get:Optional
abstract val migrationsFormat: Property<String>

@TaskAction
fun migrateSchemas() {
val injector = Guice.createInjector(
Expand All @@ -73,7 +79,8 @@ abstract class SchemaMigratorTask : DefaultTask() {
databaseType.get(),
username.get(),
password.get(),
migrationsDir.asFile.get()
migrationsDir.asFile.get(),
migrationsFormat.get()
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.configuration-cache=true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "test"
Original file line number Diff line number Diff line change
@@ -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)
);
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jdbcUrl=jdbc:mysql://localhost:3306/
schema=schema_migrator_plugin_test_with_migrations_format
username=root
password=
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit fd37f4c

Please sign in to comment.