JP
11/17/2020, 12:06 AMtransaction{
Table.addColumn(...)
}
?redenergy
11/17/2020, 12:26 AMJP
11/17/2020, 12:57 AMredenergy
11/17/2020, 7:16 AMimport org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.transactions.DEFAULT_REPETITION_ATTEMPTS
import org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import java.sql.Connection
object SampleAddressTable: IntIdTable("sample") {
val name = varchar("name", 64)
val address = varchar("address", 128)
}
fun main(args: Array<String>) {
Database.connect("jdbc:sqlite:sample.db",
manager = { ThreadLocalTransactionManager(it, Connection.TRANSACTION_SERIALIZABLE, DEFAULT_REPETITION_ATTEMPTS) })
transaction {
addLogger(StdOutSqlLogger)
SchemaUtils.createMissingTablesAndColumns(SampleAddressTable)
}
}
Upon execution it outputs (notice db requests logging):
[2020-11-17, 10:12:21] [INFO] Preparing create tables statements took 18ms
SQL: CREATE TABLE IF NOT EXISTS sample (id INTEGER PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(64) NOT NULL, address VARCHAR(128) NOT NULL)
[2020-11-17, 10:12:21] [INFO] Executing create tables statements took 17ms
[2020-11-17, 10:12:21] [INFO] Extracting table columns took 2ms
[2020-11-17, 10:12:21] [INFO] Preparing alter table statements took 3ms
[2020-11-17, 10:12:21] [INFO] Executing alter table statements took 0ms
[2020-11-17, 10:12:21] [INFO] Checking mapping consistence took 6ms
and creates specified table in database (see attached image)
Now I'll add another column and table definition will look like this:
object SampleAddressTable: IntIdTable("sample") {
val name = varchar("name", 64)
val address = varchar("address", 128)
val description = varchar("description", 128).nullable()
}
When program executed again and SchemaUtils.createMissingTablesAndColumns is called Exposed will create our new column:
[2020-11-17, 10:15:33] [INFO] Preparing create tables statements took 10ms
[2020-11-17, 10:15:33] [INFO] Executing create tables statements took 0ms
[2020-11-17, 10:15:33] [INFO] Extracting table columns took 2ms
[2020-11-17, 10:15:33] [INFO] Preparing alter table statements took 9ms
SQL: ALTER TABLE sample ADD description VARCHAR(128) NULL
[2020-11-17, 10:15:33] [INFO] Executing alter table statements took 13ms
[2020-11-17, 10:15:33] [INFO] Checking mapping consistence took 6ms
JP
11/18/2020, 3:42 AM