Pomelo.EntityFrameworkCore.MySql icon indicating copy to clipboard operation
Pomelo.EntityFrameworkCore.MySql copied to clipboard

Improve support for stored procedures in idempotent migration generation

Open lauxjpn opened this issue 4 years ago • 0 comments

For idempotent migration generation, we need to conditionally check for the existence of a given migration ID in the history table. Because MySQL limits the execution of conditional statements to stored procedures only, we currently create a stored procedure and run all actual SQL statements from inside it.

Unfortunately, because MySQL has further limitations on which statements can be executed from within a stored procedure, custom SQL migration operations that create a stored procedure (as well as a couple of other statements) cannot be executed successfully from idempotent scripts, while they work fine from non-idempotent scripts. And while PREPAREd statements in general can be executed from within stored procedures, the CREATE PROCEDURE statement (and others) have not been supported as PREPAREd statements.

However, MariaDB 10.6.2 now supports the execution of any statement from PREPARED statements:

All statements can be prepared, except PREPARE, EXECUTE, and DEALLOCATE / DROP PREPARE (MDEV-16708)

This opens up possible solutions for MariaDB, where we can either use a stored procedure to dynamically/conditionally generate SQL, that we then always execute later after the stored procedure call has returned, or where we just PREPARE specific statements (e.g. custom SQL statements) and dynamically execute them from inside the stored procedure (assuming the latter is supported by MariaDB).

lauxjpn avatar Aug 15 '21 06:08 lauxjpn