-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kales generate migration task (#14)
* Add kales generate migration task * Couple of bug fixes * Another nit
- Loading branch information
Showing
9 changed files
with
139 additions
and
25 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package kales.cli.task | ||
|
||
import java.io.File | ||
|
||
class DbCreateTask(workingDir: File) : KalesContextualTask(workingDir) { | ||
override fun run() { | ||
TODO("Not implemented yet") | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
kales-cli/src/main/kotlin/kales/cli/task/GenerateMigrationTask.kt
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,48 @@ | ||
package kales.cli.task | ||
|
||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import kales.cli.writeTextWithLogging | ||
import kales.migrations.Migration | ||
import java.io.ByteArrayOutputStream | ||
import java.io.File | ||
import java.io.OutputStreamWriter | ||
import java.nio.charset.StandardCharsets | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
|
||
class GenerateMigrationTask( | ||
workingDir: File, | ||
private val migrationClassName: String, | ||
private val dateProvider: () -> Date = { Date() } | ||
) : KalesContextualTask(workingDir) { | ||
override fun run() { | ||
val timestamp = SimpleDateFormat("yyyyMMddhhmmss").format(dateProvider()) | ||
val migrationTypeSpec = TypeSpec.classBuilder(migrationClassName) | ||
.superclass(Migration::class) | ||
.addFunction(FunSpec.builder("up") | ||
.addModifiers(KModifier.OVERRIDE) | ||
.build()) | ||
.addFunction(FunSpec.builder("down") | ||
.addModifiers(KModifier.OVERRIDE) | ||
.build()) | ||
.build() | ||
val fileSpec = FileSpec.builder("$appPackageName.db.migrate", migrationClassName) | ||
.addType(migrationTypeSpec) | ||
.build() | ||
val outputPath = dbMigrateDir.toPath().resolve("M${timestamp}_$migrationClassName.kt") | ||
ByteArrayOutputStream().use { baos -> | ||
OutputStreamWriter(baos, StandardCharsets.UTF_8).use { writer -> | ||
fileSpec.writeTo(writer) | ||
} | ||
// We need to instantiate the migration class name at the bottom of the migration file so that | ||
// the Kotlin script engine will return that type from eval() and be able to execute the | ||
// migration. Without that, eval() returns `null` and we cannot run it. There must be a better | ||
// way to do this... | ||
val finalText = "$baos\n$migrationClassName()\n" | ||
outputPath.toFile().writeTextWithLogging(finalText) | ||
} | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
kales-cli/src/test/kotlin/kales/cli/task/GenerateMigrationTaskTest.kt
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,41 @@ | ||
package kales.cli.task | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
import java.io.File | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
|
||
class GenerateMigrationTaskTest { | ||
@get:Rule val tempDir = TemporaryFolder() | ||
|
||
@Test fun `test create migration`() { | ||
val root = tempDir.root | ||
val appName = "com.example.testapp" | ||
val date = Date() | ||
val timestamp = SimpleDateFormat("yyyyMMddhhmmss").format(date) | ||
NewCommandTask(root, appName).run() | ||
GenerateMigrationTask(root, "CreateFooBar") { date }.run() | ||
val dbMigrateDir = File(root, "src/main/kotlin/com/example/testapp/db/migrate") | ||
val migrationFile = File(dbMigrateDir, "M${timestamp}_CreateFooBar.kt") | ||
assertThat(dbMigrateDir.listFiles().toList()).containsExactly(migrationFile) | ||
assertThat(migrationFile.readText()).isEqualTo(""" | ||
package com.example.testapp.db.migrate | ||
import kales.migrations.Migration | ||
class CreateFooBar : Migration() { | ||
override fun up() { | ||
} | ||
override fun down() { | ||
} | ||
} | ||
CreateFooBar() | ||
""".trimIndent()) | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...sample/db/migrate/CreatePostsMigration.kt → ...e/M20190224204400_CreatePostsMigration.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
development: | ||
adapter: postgresql | ||
host: localhost | ||
database: norp_development | ||
database: kales_sampleapp_development | ||
username: felipecsl | ||
password: |