-
Notifications
You must be signed in to change notification settings - Fork 702
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
feat: EXPOSED-733 Detect column type change for migrations in H2 #2419
base: main
Are you sure you want to change the base?
Conversation
f223c7d
to
4ccfeb7
Compare
/** Returns a map of all the columns' names mapped to their type. */ | ||
fun fetchAllColumnTypes(tableName: String): ConcurrentHashMap<String, String> = ConcurrentHashMap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some databases, like H2, MySQL, and MariaDB, have support for the query SHOW COLUMNS FROM table_name;
which provides information about columns and their types. For those databases, it's better to use this query and get all the information in one go, rather than querying the information column by column.
669bf18
to
971360e
Compare
971360e
to
fefb2e8
Compare
@@ -373,9 +376,9 @@ object SchemaUtils { | |||
|
|||
val incorrectCaseSensitiveName = existingCol.name.inProperCase() != col.nameUnquoted().inProperCase() | |||
|
|||
val incorrectSizeOrScale = isIncorrectSizeOrScale(existingCol, columnType) | |||
val incorrectSizeOrScale = if (incorrectType) false else isIncorrectSizeOrScale(existingCol, columnType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The size and scale are only compared if the types are confirmed to be equivalent because the size and scale differences are taken into account when comparing the types. This might change later as we refine this feature.
@@ -6,6 +6,8 @@ package org.jetbrains.exposed.sql | |||
data class ColumnDiff( | |||
/** Whether there is a mismatch between nullability of the existing column and the defined column. */ | |||
val nullability: Boolean, | |||
/** Whether there is a mismatch between type of the existing column and the defined column. */ | |||
val type: Boolean, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we store old and new types directly?
} | ||
} | ||
else -> checkConstraints | ||
}.let { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please consider avoiding multiple let statement in a sequence: it is hard to follow the final result
} | ||
|
||
/** Returns the list of CHECK constraints in this table. */ | ||
fun checkConstraints(): List<CheckConstraint> = filteredCheckConstraints() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you tell me why we need filteredCheckConstraints
? Looks like the implementation can be here
Description
Detailed description:
Column type changes in H2 are now detected when generating database migration statements.
ColumnMetadata
now has a new property,sqlType
. To get this type, the column types pre-fetched (if the database supports that) in theextractColumns
function inJdbcDatabaseMetadataImpl
. If pre-fetching is not supported, the newgetColumnType
function queries the database for information about the column (future PRs).Some SQL types are mapped to a different type in the database than one might expect, so normalizing the type name is done in those cases. Let me know if you think the
normalizedColumnType
function in H2 should be moved toDatabaseDialect
and turned into an open function that users can override.When checking for column differences, the type of the existing column is compared with the type of the Exposed table's column using the new function
areEquivalentColumnTypes
.Next up: PostgreSQL 🚀
Type of Change
Please mark the relevant options with an "X":
Updates/remove existing public API methods:
Affected databases:
Checklist
Related Issues