SchemaUtils

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

Functions

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.

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.

Link copied to clipboard

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.

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.

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

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.

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

Creates databases

Link copied to clipboard

Returns the SQL statements that create the provided ForeignKeyConstraint.

Link copied to clipboard

Returns the SQL statements that create the provided index.

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).

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

Creates schemas

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.

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

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

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.

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

Drops databases

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

Drops schemas

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.

Link copied to clipboard

Returns a list of all databases.

Link copied to clipboard

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

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.

Link copied to clipboard

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

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.

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.