# SchemaUtils

object SchemaUtils

Utility functions that assist with creating, altering, and dropping database schema objects.

## Functions

addMissingColumnsStatements
Link copied to clipboard
fun addMissingColumnsStatements(vararg tables: Table, withLogs: Boolean = true): List<String>

Returns the SQL statements that create any columns defined in tables, which are missing from the existing tables in the database.

checkCycle
Link copied to clipboard
fun checkCycle(vararg tables: Table): Boolean

Checks whether any of the tables have a sequence of foreign key constraints that cycle back to them.

checkExcessiveForeignKeyConstraints
Link copied to clipboard
fun checkExcessiveForeignKeyConstraints(vararg tables: Table, withLogs: Boolean): List<ForeignKeyConstraint>

Checks all tables for any that have more than one defined foreign key constraint and logs the findings. If found, this function also logs the SQL statements that can be used to drop these foreign key constraints.

checkExcessiveIndices
Link copied to clipboard
fun checkExcessiveIndices(vararg tables: Table, withLogs: Boolean): List<Index>

Checks all tables for any that have more than one defined index and logs the findings. If found, this function also logs the SQL statements that can be used to drop these indices.

checkMappingConsistence
Link copied to clipboard
fun checkMappingConsistence(vararg tables: Table, withLogs: Boolean = true): List<String>

Log Exposed table mappings <-> real database mapping problems and returns DDL Statements to fix them

create
Link copied to clipboard
fun <T : Table> create(vararg tables: T, inBatch: Boolean = false)

Creates all tables that do not already exist, using a batch execution if inBatch is set to `true`.

createDatabase
Link copied to clipboard
fun createDatabase(vararg databases: String, inBatch: Boolean = false)

Creates databases

createFKey
Link copied to clipboard
fun createFKey(foreignKey: ForeignKeyConstraint): List<String>

Returns the SQL statements that create the provided ForeignKeyConstraint.

createIndex
Link copied to clipboard
fun createIndex(index: Index): List<String>

Returns the SQL statements that create the provided index.

createMissingTablesAndColumns
Link copied to clipboard
fun createMissingTablesAndColumns(vararg tables: Table, inBatch: Boolean = false, withLogs: Boolean = true)

This function should be used in cases when an easy-to-use auto-actualization of database schema is required. It creates any missing tables and, if possible, adds any missing columns for existing tables (for example, when columns are nullable or have default values).

createSchema
Link copied to clipboard
fun createSchema(vararg schemas: Schema, inBatch: Boolean = false)

Creates schemas

createSequence
Link copied to clipboard
fun createSequence(vararg seq: Sequence, inBatch: Boolean = false)

Creates the provided sequences, using a batch execution if inBatch is set to `true`.

createStatements
Link copied to clipboard
fun createStatements(vararg tables: Table): List<String>

Returns the SQL statements that create all tables that do not already exist.

drop
Link copied to clipboard
fun drop(vararg tables: Table, inBatch: Boolean = false)

Drops all tables, using a batch execution if inBatch is set to `true`.

dropDatabase
Link copied to clipboard
fun dropDatabase(vararg databases: String, inBatch: Boolean = false)

Drops databases

dropSchema
Link copied to clipboard
fun dropSchema(vararg schemas: Schema, cascade: Boolean = false, inBatch: Boolean = false)

Drops schemas

dropSequence
Link copied to clipboard
fun dropSequence(vararg seq: Sequence, inBatch: Boolean = false)

Drops the provided sequences, using a batch execution if inBatch is set to `true`.

listDatabases
Link copied to clipboard
fun listDatabases(): List<String>

Returns a list of all databases.

listTables
Link copied to clipboard
fun listTables(): List<String>

Retrieves a list of all table names in the current database.

setSchema
Link copied to clipboard
fun setSchema(schema: Schema, inBatch: Boolean = false)

Sets the current default schema to schema. Supported by H2, MariaDB, Mysql, Oracle, PostgreSQL and SQL Server. SQLite doesn't support schemas.

sortTablesByReferences
Link copied to clipboard
fun sortTablesByReferences(tables: Iterable<Table>): List<Table>

Returns a list of tables sorted according to the targets of their foreign key constraints, if any exist.

statementsRequiredToActualizeScheme
Link copied to clipboard
fun statementsRequiredToActualizeScheme(vararg tables: Table, withLogs: Boolean = true): List<String>

Returns the SQL statements that need to be executed to make the existing database schema compatible with the table objects defined using Exposed.

withDataBaseLock
Link copied to clipboard
fun <T> Transaction.withDataBaseLock(body: () -> T)

Creates table with name "busy" (if not present) and single column to be used as "synchronization" point. Table wont be dropped after execution.