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

Generate SQL scripts that do no contain DELIMITER

Open jerrysdev opened this issue 3 years ago • 1 comments

Steps to reproduce

dotnet ef migrations script --idempotent

The issue

The generated SQL contains DELIMITER, making it invalid for use with MySqlConnector. Is there a way to to get clean SQL without all of these DELIMITER? For small scripts one could simply remove them manually, but for a continuously updated script with several thousand lines it would be nice to have the tooling do that.

MySqlConnector.MySqlException: 'DELIMITER' should not be used with MySqlConnector. See https://fl.vu/mysql-delimiter

jerrysdev avatar Jun 17 '22 16:06 jerrysdev

@jerrysdev To apply a script that contains stored procedures correctly, it has to be split into batches (or individual commands), since stored procedures use the same delimiter ; for their sub statements in their procedure body, as MySQL does for normal "root" statements.

If you just run the generated script using the MySQL CLI or MySQLWorkbench, then the DELIMITER statements will automatically take care of that, and you won't need to split the script into batches yourself.

If you want to parse and run the script yourself, then it is up to you to split the script accordingly.

To help you get started, here is a PowerShell command, that splits the script into an array of strings. What you do with that array afterwards is up to you:

(dotnet ef migrations script --idempotent) -split '(?:[\r\n]*[^\S\r\n]*DELIMITER[^\S\r\n]+(?://|;)[^\S\r\n]*\s*|\s*//[\r\n]+\s*)' | ? { $_ -ne '' }

lauxjpn avatar Jun 17 '22 22:06 lauxjpn