Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MySQL 'ON UPDATE CURRENT_TIMESTAMP' default expression #986

Closed
wants to merge 0 commits into from

Conversation

xJoeWoo
Copy link
Contributor

@xJoeWoo xJoeWoo commented Jul 4, 2020

Adds support for MySQL column DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP syntax.

This default statement is useful when other app updated the row, but without/forget assigning the "update time" column.

Test statements output

MySQL 5.5:

ERROR Test worker Exposed:descriptionDdl:95 - mysql 5.5 doesn't support expression 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP' as default value. Column will be created with NULL marker.
SQL: CREATE TABLE IF NOT EXISTS foo (id INT AUTO_INCREMENT PRIMARY KEY, `name` TEXT NOT NULL, defaultDateTimeAutoUpdate DATETIME NULL)
SQL: SELECT CURRENT_TIMESTAMP
SQL: INSERT INTO foo (defaultDateTimeAutoUpdate, `name`) VALUES (CURRENT_TIMESTAMP, 'bar')
SQL: SELECT foo.id, foo.`name`, foo.defaultDateTimeAutoUpdate FROM foo WHERE foo.id = 1
SQL: DROP TABLE IF EXISTS foo

MySQL 8.0:

SQL: CREATE TABLE IF NOT EXISTS foo (id INT AUTO_INCREMENT PRIMARY KEY, `name` TEXT NOT NULL, defaultDateTimeAutoUpdate DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) NOT NULL)
SQL: SELECT CURRENT_TIMESTAMP
SQL: INSERT INTO foo (defaultDateTimeAutoUpdate, `name`) VALUES (CURRENT_TIMESTAMP(6), 'bar')
SQL: SELECT foo.id, foo.`name`, foo.defaultDateTimeAutoUpdate FROM foo WHERE foo.id = 1
SQL: INSERT INTO foo (`name`) VALUES ('baz')
SQL: SELECT foo.id, foo.`name`, foo.defaultDateTimeAutoUpdate FROM foo WHERE foo.id = 2
SQL: UPDATE foo SET `name`='bah' WHERE foo.id = 2
SQL: SELECT foo.id, foo.`name`, foo.defaultDateTimeAutoUpdate FROM foo WHERE foo.id = 2
SQL: DROP TABLE IF EXISTS foo

@xJoeWoo
Copy link
Contributor Author

xJoeWoo commented Jul 4, 2020

Please note that this syntax is not support with h2 even in MySQL mode. A real MySQL/MariaDB is needed for testing.

@Tapac
Copy link
Contributor

Tapac commented Aug 23, 2020

@xJoeWoo , as I can see from MySQL documentation ON UPDATE clause could be used even without DEFAULT part (what could be hard to implement with current Column.defaultExpression() approach. But one could want to default to some constant datetime value and then update with ON UPDATE.

I would prefer some kind of extention on Expression<T : Temporal> to use it like:

datetime("columnName").default(CurrentDateTime().onUpdateCurrentTimestamp())
or
datetime("columnName").default(dateTimeLiteral(LocalDateTime.MIN).onUpdateCurrentTimestamp())

BTW, MySQL 8.0.13+ has normal support for expressions in DEFAULT!

@@ -117,14 +122,26 @@ open class MysqlDialect : VendorDialect(dialectName, MysqlDataTypeProvider, Mysq
TransactionManager.current().db.isVersionCovers(BigDecimal("8.0"))
}

abstract class OnUpdateCurrentTimestamp<T>(private val currentTimestamp: Expression<T>) : Expression<T>() {

fun ddlDefault():String = QueryBuilder(false).apply {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to make it in toQueryBuilder function?

/**
* Expression of MySQL syntax `DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`
*/
class CurrentTimestampOnUpdateCurrentTimestamp<T : Temporal> : MysqlDialect.OnUpdateCurrentTimestamp<T>(CurrentTimestamp<T>())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide similar for exposed-jodatime module.

@Burtan
Copy link

Burtan commented May 14, 2023

Please note that this syntax is not support with h2 even in MySQL mode. A real MySQL/MariaDB is needed for testing.

h2database/h2database#959

According to this pull, it is supported in all modes with h2 now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants