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

refactor: EXPOSED-710 Move plain SQL for retrieving foreign keys from MysqlDialect #2380

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bog-walk
Copy link
Member

@bog-walk bog-walk commented Jan 29, 2025

Description

Summary of the change: Plain SQL in MysqlDialect.fillConstraintCacheForTables() is now in exposed-jdbc under tableConstraints().

Detailed description:

  • Why:

In preparation for R2DBC, JDBC-specific execution of plain SQL needs to be extracted from exposed-core. MysqlDialect.fillConstraintCacheForTables() currently executes a custom SQL query to retrieve foreign key constraints associated with the provided table. This is an override for a protected method that is only called in 1 place: DatabaseDialect.columnConstraints(). The latter is responsible for all SchemaUtils methods that check foreign key consistency.

  • How:
    • Query is now located in JDBC tableConstraints(), because this is the metadata query called by the default implementation of fillConstraintCacheForTables(). This is also the only place where tableConstraints() is invoked.
    • Logic was copied to the new location, with 1 extra line. The custom query only returns a mapping of table-column data for every foreign key, where as the JDBC built-in query returns a mapping of table data for every foreign key and every table involved in the foreign key. This can be seen in highlighted tests, which would fail if the result of the query was not adjusted to match. This was done in the event that users call tableConstraints() directly. Calling columnConstraints() of SchemaUtils methods has not changed behavior.

Type of Change

Please mark the relevant options with an "X":

  • Other: refactor

Affected databases:

  • MariaDB
  • Mysql5
  • Mysql8

Checklist

  • Unit tests are in place
  • The build is green (including the Detekt check)
  • All public methods affected by my PR has up to date API docs

Related Issues

EXPOSED-710
R2DBC parent - EXPOSED-517

… MysqlDial>

Plain SQL in MysqlDialect.fillConstraintCacheForTables() is now extracted out o>
core module and into jdbc module, in preparation for R2DBC implementation.

Logic has been placed in tableConstraints() and end result adjusted to fit
previous behavior.
Comment on lines +436 to +437
// This ensures MySQL/MariaDB have same behavior as before: a map entry for every table even if no FKs
allTables.keys.forEach { constraintsToLoad.putIfAbsent(it, mutableMapOf()) }
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the only new line of logic, as mentioned in the PR description. If this was not included, technically any tests that call tableConstraints directly (or if users do this themselves?) would fail.

The custom SQL returns values only for tables that have a foreign key (FK). Whereas DatabaseMetaData.getImportedKeys() below associates a query for each table involved in the FK (i.e. both parent & child), due to SchemaUtils.sortTablesByReferences() at the start. Meaning there will always be a key for the parent table, but with a value of an empty list.

I'm not sure the reason behind this behavior, so I added this line to make sure that there was no breaking change.

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.

1 participant