mirror of
https://github.com/jlengrand/Exposed.git
synced 2026-03-10 08:11:20 +00:00
Adding create and drop database methods (#782)
* Adding create and drop database method * Correct Create database statement in postgesql dialect * Correct Create database statement in oracle and sqlserver dialects
This commit is contained in:
@@ -179,6 +179,32 @@ object SchemaUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates databases
|
||||
*
|
||||
* @param databases the names of the databases
|
||||
* @param inBatch flag to perform database creation in a single batch
|
||||
*/
|
||||
fun createDatabase(vararg databases: String, inBatch: Boolean = false) {
|
||||
with(TransactionManager.current()) {
|
||||
val createStatements = databases.flatMap { listOf(currentDialect.createDatabase(it)) }
|
||||
execStatements(inBatch, createStatements)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops databases
|
||||
*
|
||||
* @param databases the names of the databases
|
||||
* @param inBatch flag to perform database creation in a single batch
|
||||
*/
|
||||
fun dropDatabase(vararg databases: String, inBatch: Boolean = false) {
|
||||
with(TransactionManager.current()) {
|
||||
val createStatements = databases.flatMap { listOf(currentDialect.dropDatabase(it)) }
|
||||
execStatements(inBatch, createStatements)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function should be used in cases when you want an easy-to-use auto-actualization of database scheme.
|
||||
* It will create all absent tables, add missing columns for existing tables if it's possible (columns are nullable or have default values).
|
||||
|
||||
@@ -575,6 +575,10 @@ interface DatabaseDialect {
|
||||
this.append("$str1 $str2")
|
||||
}
|
||||
}
|
||||
|
||||
fun createDatabase(name: String) = "CREATE DATABASE IF NOT EXISTS ${name.inProperCase()}"
|
||||
|
||||
fun dropDatabase(name: String) = "DROP DATABASE IF EXISTS ${name.inProperCase()}"
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -103,6 +103,10 @@ open class H2Dialect : VendorDialect(dialectName, H2DataTypeProvider, H2Function
|
||||
return super.createIndex(index)
|
||||
}
|
||||
|
||||
override fun createDatabase(name: String) = "CREATE SCHEMA IF NOT EXISTS ${name.inProperCase()}"
|
||||
|
||||
override fun dropDatabase(name: String) = "DROP SCHEMA IF EXISTS ${name.inProperCase()}"
|
||||
|
||||
companion object {
|
||||
/** H2 dialect name */
|
||||
const val dialectName: String = "h2"
|
||||
|
||||
@@ -182,6 +182,10 @@ open class OracleDialect : VendorDialect(dialectName, OracleDataTypeProvider, Or
|
||||
|
||||
override fun modifyColumn(column: Column<*>): String = super.modifyColumn(column).replace("MODIFY COLUMN", "MODIFY")
|
||||
|
||||
override fun createDatabase(name: String): String = "CREATE DATABASE ${name.inProperCase()}"
|
||||
|
||||
override fun dropDatabase(name: String): String = "DROP DATABASE ${name.inProperCase()}"
|
||||
|
||||
companion object {
|
||||
/** Oracle dialect name */
|
||||
const val dialectName: String = "oracle"
|
||||
|
||||
@@ -172,6 +172,10 @@ open class PostgreSQLDialect : VendorDialect(dialectName, PostgreSQLDataTypeProv
|
||||
}
|
||||
}
|
||||
|
||||
override fun createDatabase(name: String): String = "CREATE DATABASE ${name.inProperCase()}"
|
||||
|
||||
override fun dropDatabase(name: String): String = "DROP DATABASE ${name.inProperCase()}"
|
||||
|
||||
companion object {
|
||||
/** PostgreSQL dialect name */
|
||||
const val dialectName: String = "postgresql"
|
||||
|
||||
@@ -106,6 +106,10 @@ open class SQLServerDialect : VendorDialect(dialectName, SQLServerDataTypeProvid
|
||||
override fun modifyColumn(column: Column<*>): String =
|
||||
super.modifyColumn(column).replace("MODIFY COLUMN", "ALTER COLUMN")
|
||||
|
||||
override fun createDatabase(name: String): String = "CREATE DATABASE ${name.inProperCase()}"
|
||||
|
||||
override fun dropDatabase(name: String) = "DROP DATABASE ${name.inProperCase()}"
|
||||
|
||||
companion object {
|
||||
/** SQLServer dialect name */
|
||||
const val dialectName: String = "sqlserver"
|
||||
|
||||
@@ -144,6 +144,10 @@ open class SQLiteDialect : VendorDialect(dialectName, SQLiteDataTypeProvider, SQ
|
||||
}
|
||||
}
|
||||
|
||||
override fun createDatabase(name: String) = "ATTACH DATABASE '${name.toLowerCase()}.db' AS ${name.inProperCase()}"
|
||||
|
||||
override fun dropDatabase(name: String) = "DETACH DATABASE ${name.inProperCase()}"
|
||||
|
||||
companion object {
|
||||
/** SQLite dialect name */
|
||||
const val dialectName: String = "sqlite"
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.jetbrains.exposed.sql.tests.shared.ddl
|
||||
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
|
||||
import org.jetbrains.exposed.sql.tests.TestDB
|
||||
import org.junit.Test
|
||||
|
||||
class DatabaseTests : DatabaseTestsBase() {
|
||||
|
||||
@Test
|
||||
fun `create database test`() {
|
||||
// PostgreSQL will be tested in the next test function
|
||||
withDb(excludeSettings = listOf(TestDB.POSTGRESQL)) {
|
||||
val dbName = "jetbrains"
|
||||
SchemaUtils.createDatabase(dbName)
|
||||
SchemaUtils.dropDatabase(dbName)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `create database test in postgreSQL`() {
|
||||
// PostgreSQL needs auto commit to be "ON" to allow create database statement
|
||||
withDb(TestDB.POSTGRESQL) {
|
||||
connection.autoCommit = true
|
||||
val dbName = "jetbrains"
|
||||
SchemaUtils.createDatabase(dbName)
|
||||
SchemaUtils.dropDatabase(dbName)
|
||||
connection.autoCommit = false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user