Skip to content

Commit ee18bde

Browse files
authored
chore: Add migration module and move generateMigrationScript function to it (JetBrains#2128)
1 parent d0bb6f0 commit ee18bde

File tree

8 files changed

+77
-44
lines changed

8 files changed

+77
-44
lines changed

exposed-core/api/exposed-core.api

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,8 +2026,6 @@ public final class org/jetbrains/exposed/sql/SchemaUtils {
20262026
public static synthetic fun dropSchema$default (Lorg/jetbrains/exposed/sql/SchemaUtils;[Lorg/jetbrains/exposed/sql/Schema;ZZILjava/lang/Object;)V
20272027
public final fun dropSequence ([Lorg/jetbrains/exposed/sql/Sequence;Z)V
20282028
public static synthetic fun dropSequence$default (Lorg/jetbrains/exposed/sql/SchemaUtils;[Lorg/jetbrains/exposed/sql/Sequence;ZILjava/lang/Object;)V
2029-
public final fun generateMigrationScript ([Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;Z)Ljava/io/File;
2030-
public static synthetic fun generateMigrationScript$default (Lorg/jetbrains/exposed/sql/SchemaUtils;[Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Ljava/io/File;
20312029
public final fun listDatabases ()Ljava/util/List;
20322030
public final fun listTables ()Ljava/util/List;
20332031
public final fun setSchema (Lorg/jetbrains/exposed/sql/Schema;Z)V

exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import org.jetbrains.exposed.exceptions.ExposedSQLException
44
import org.jetbrains.exposed.sql.SqlExpressionBuilder.asLiteral
55
import org.jetbrains.exposed.sql.transactions.TransactionManager
66
import org.jetbrains.exposed.sql.vendors.*
7-
import java.io.File
87
import java.math.BigDecimal
98

109
/** Utility functions that assist with creating, altering, and dropping database schema objects. */
@@ -849,44 +848,6 @@ object SchemaUtils {
849848
return toDrop.toList()
850849
}
851850

852-
/**
853-
* @param tables The tables whose changes will be used to generate the migration script.
854-
* @param scriptName The name to be used for the generated migration script.
855-
* @param scriptDirectory The directory (path from repository root) in which to create the migration script.
856-
* @param withLogs By default, a description for each intermediate step, as well as its execution time, is logged at
857-
* the INFO level. This can be disabled by setting [withLogs] to `false`.
858-
*
859-
* @return The generated migration script.
860-
*
861-
* @throws IllegalArgumentException if no argument is passed for the [tables] parameter.
862-
*
863-
* This function simply generates the migration script without applying the migration. Its purpose is to show what
864-
* the migration script will look like before applying the migration.
865-
* If a migration script with the same name already exists, its content will be overwritten.
866-
*/
867-
@ExperimentalDatabaseMigrationApi
868-
fun generateMigrationScript(vararg tables: Table, scriptDirectory: String, scriptName: String, withLogs: Boolean = true): File {
869-
require(tables.isNotEmpty()) { "Tables argument must not be empty" }
870-
871-
val allStatements = statementsRequiredForDatabaseMigration(*tables, withLogs = withLogs)
872-
873-
val migrationScript = File("$scriptDirectory/$scriptName.sql")
874-
migrationScript.createNewFile()
875-
876-
// Clear existing content
877-
migrationScript.writeText("")
878-
879-
// Append statements
880-
allStatements.forEach { statement ->
881-
// Add semicolon only if it's not already there
882-
val conditionalSemicolon = if (statement.last() == ';') "" else ";"
883-
884-
migrationScript.appendText("$statement$conditionalSemicolon\n")
885-
}
886-
887-
return migrationScript
888-
}
889-
890851
/**
891852
* Returns the SQL statements that need to be executed to make the existing database schema compatible with
892853
* the table objects defined using Exposed. Unlike [statementsRequiredToActualizeScheme], DROP/DELETE statements are
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public final class MigrationUtils {
2+
public static final field INSTANCE LMigrationUtils;
3+
public final fun generateMigrationScript ([Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;Z)Ljava/io/File;
4+
public static synthetic fun generateMigrationScript$default (LMigrationUtils;[Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Ljava/io/File;
5+
}
6+

exposed-migration/build.gradle.kts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
plugins {
2+
id("java")
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
api(project(":exposed-core"))
11+
12+
testImplementation(platform("org.junit:junit-bom:5.10.0"))
13+
testImplementation("org.junit.jupiter:junit-jupiter")
14+
}
15+
16+
tasks.test {
17+
useJUnitPlatform()
18+
}
19+
20+
kotlin {
21+
jvmToolchain(8)
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import org.jetbrains.exposed.sql.ExperimentalDatabaseMigrationApi
2+
import org.jetbrains.exposed.sql.SchemaUtils.statementsRequiredForDatabaseMigration
3+
import org.jetbrains.exposed.sql.Table
4+
import java.io.File
5+
6+
object MigrationUtils {
7+
/**
8+
* This function simply generates the migration script without applying the migration. Its purpose is to show what
9+
* the migration script will look like before applying the migration. If a migration script with the same name
10+
* already exists, its content will be overwritten.
11+
*
12+
* @param tables The tables whose changes will be used to generate the migration script.
13+
* @param scriptName The name to be used for the generated migration script.
14+
* @param scriptDirectory The directory (path from repository root) in which to create the migration script.
15+
* @param withLogs By default, a description for each intermediate step, as well as its execution time, is logged at
16+
* the INFO level. This can be disabled by setting [withLogs] to `false`.
17+
*
18+
* @return The generated migration script.
19+
*
20+
* @throws IllegalArgumentException if no argument is passed for the [tables] parameter.
21+
*/
22+
@ExperimentalDatabaseMigrationApi
23+
fun generateMigrationScript(vararg tables: Table, scriptDirectory: String, scriptName: String, withLogs: Boolean = true): File {
24+
require(tables.isNotEmpty()) { "Tables argument must not be empty" }
25+
26+
val allStatements = statementsRequiredForDatabaseMigration(*tables, withLogs = withLogs)
27+
28+
val migrationScript = File("$scriptDirectory/$scriptName.sql")
29+
migrationScript.createNewFile()
30+
31+
// Clear existing content
32+
migrationScript.writeText("")
33+
34+
// Append statements
35+
allStatements.forEach { statement ->
36+
// Add semicolon only if it's not already there
37+
val conditionalSemicolon = if (statement.last() == ';') "" else ";"
38+
39+
migrationScript.appendText("$statement$conditionalSemicolon\n")
40+
}
41+
42+
return migrationScript
43+
}
44+
}

exposed-tests/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies {
2323
implementation(project(":exposed-jdbc"))
2424
implementation(project(":exposed-dao"))
2525
implementation(project(":exposed-kotlin-datetime"))
26+
implementation(project(":exposed-migration"))
2627

2728
implementation(libs.slf4j)
2829
implementation(libs.log4j.slf4j.impl)

exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/DatabaseMigrationTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class DatabaseMigrationTests : DatabaseTestsBase() {
4040
try {
4141
SchemaUtils.create(noPKTable)
4242

43-
val script = SchemaUtils.generateMigrationScript(singlePKTable, scriptDirectory = scriptDirectory, scriptName = scriptName)
43+
val script = MigrationUtils.generateMigrationScript(singlePKTable, scriptDirectory = scriptDirectory, scriptName = scriptName)
4444
assertTrue(script.exists())
4545
assertEquals("src/test/resources/$scriptName.sql", script.path)
4646

@@ -86,7 +86,7 @@ class DatabaseMigrationTests : DatabaseTestsBase() {
8686
}
8787

8888
// Generate script with the same name of initial script
89-
val newScript = SchemaUtils.generateMigrationScript(singlePKTable, scriptDirectory = directory, scriptName = name)
89+
val newScript = MigrationUtils.generateMigrationScript(singlePKTable, scriptDirectory = directory, scriptName = name)
9090

9191
val expectedStatements: List<String> = SchemaUtils.statementsRequiredForDatabaseMigration(singlePKTable)
9292
assertEquals(1, expectedStatements.size)
@@ -106,7 +106,7 @@ class DatabaseMigrationTests : DatabaseTestsBase() {
106106
fun testNoTablesPassedWhenGeneratingMigrationScript() {
107107
withDb {
108108
expectException<IllegalArgumentException> {
109-
SchemaUtils.generateMigrationScript(scriptDirectory = "src/test/resources", scriptName = "V2__Test")
109+
MigrationUtils.generateMigrationScript(scriptDirectory = "src/test/resources", scriptName = "V2__Test")
110110
}
111111
}
112112
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ include("exposed-bom")
1212
include("exposed-kotlin-datetime")
1313
include("exposed-crypt")
1414
include("exposed-json")
15+
include("exposed-migration")
1516

1617
pluginManagement {
1718
repositories {

0 commit comments

Comments
 (0)