# SchemaUtils

object SchemaUtils : SchemaUtilityApi

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

## Functions

addMissingColumnsStatements
Link copied to clipboard
suspend 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
suspend 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
suspend 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
suspend 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
suspend 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
suspend 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
suspend 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
suspend fun createSchema(vararg schemas: Schema, inBatch: Boolean = false)

Creates schemas

createSequence
Link copied to clipboard
suspend 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
suspend 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
suspend 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
suspend fun dropDatabase(vararg databases: String, inBatch: Boolean = false)

Drops databases

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

Drops schemas

dropSequence
Link copied to clipboard
suspend 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
suspend fun listDatabases(): List<String>

Returns a list of all databases.

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

Retrieves a list of all table names in the current database schema. The names will be returned with schema prefixes if the database supports it.

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

Returns a list with the names of all the tables in all database schemas. The names will be returned with schema prefixes, if the database supports it, and non-user defined tables, like system information table names, will be included.

setSchema
Link copied to clipboard
suspend 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
suspend 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
suspend fun <T> R2dbcTransaction.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.