-
Notifications
You must be signed in to change notification settings - Fork 678
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add migration module and move
generateMigrationScript
functi…
…on to it
- Loading branch information
Showing
8 changed files
with
77 additions
and
44 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,6 @@ | ||
public final class MigrationUtils { | ||
public static final field INSTANCE LMigrationUtils; | ||
public final fun generateMigrationScript ([Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;Z)Ljava/io/File; | ||
public static synthetic fun generateMigrationScript$default (LMigrationUtils;[Lorg/jetbrains/exposed/sql/Table;Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Ljava/io/File; | ||
} | ||
|
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,22 @@ | ||
plugins { | ||
id("java") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
api(project(":exposed-core")) | ||
|
||
testImplementation(platform("org.junit:junit-bom:5.10.0")) | ||
testImplementation("org.junit.jupiter:junit-jupiter") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(8) | ||
} |
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,44 @@ | ||
import org.jetbrains.exposed.sql.ExperimentalDatabaseMigrationApi | ||
import org.jetbrains.exposed.sql.SchemaUtils.statementsRequiredForDatabaseMigration | ||
import org.jetbrains.exposed.sql.Table | ||
import java.io.File | ||
|
||
object MigrationUtils { | ||
/** | ||
* This function simply generates the migration script without applying the migration. Its purpose is to show what | ||
* the migration script will look like before applying the migration. If a migration script with the same name | ||
* already exists, its content will be overwritten. | ||
* | ||
* @param tables The tables whose changes will be used to generate the migration script. | ||
* @param scriptName The name to be used for the generated migration script. | ||
* @param scriptDirectory The directory (path from repository root) in which to create the migration script. | ||
* @param withLogs By default, a description for each intermediate step, as well as its execution time, is logged at | ||
* the INFO level. This can be disabled by setting [withLogs] to `false`. | ||
* | ||
* @return The generated migration script. | ||
* | ||
* @throws IllegalArgumentException if no argument is passed for the [tables] parameter. | ||
*/ | ||
@ExperimentalDatabaseMigrationApi | ||
fun generateMigrationScript(vararg tables: Table, scriptDirectory: String, scriptName: String, withLogs: Boolean = true): File { | ||
require(tables.isNotEmpty()) { "Tables argument must not be empty" } | ||
|
||
val allStatements = statementsRequiredForDatabaseMigration(*tables, withLogs = withLogs) | ||
|
||
val migrationScript = File("$scriptDirectory/$scriptName.sql") | ||
migrationScript.createNewFile() | ||
|
||
// Clear existing content | ||
migrationScript.writeText("") | ||
|
||
// Append statements | ||
allStatements.forEach { statement -> | ||
// Add semicolon only if it's not already there | ||
val conditionalSemicolon = if (statement.last() == ';') "" else ";" | ||
|
||
migrationScript.appendText("$statement$conditionalSemicolon\n") | ||
} | ||
|
||
return migrationScript | ||
} | ||
} |
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