question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Order of statements using Execute.WithConnection() and Execute.Sql()

See original GitHub issue

Describe the question I’m trying to use .patch files to migrate my MSSQL stored procedures instead of describing the full stored procedure for UP and DOWN migrations. (Side note: because I’m simultaneously dealing with many code branches and using the traditional approach I found that after merging branches, some stored proc migrations might undo older changes. And patch files can be applied in reverse too, which simplifies the DOWN migration).

Anyway, to do this I have to extract the current script from MSSQL (via sp_helptext)

So, in short, I have this migration:

public override void Up()
{
    string newScript = "";
    Execute.WithConnection((connection, tx) => newScript = GetStoredProcTextAndApplyPatch());

    if (string.IsNullOrWhiteSpace(newScript))
        Console.WriteLine("No script to execute");
    else
    {
        Console.WriteLine("Executing change script");
        Execute.Sql(newScript);
    }
}

When migrating (using console runner), it seems the bottom block executes first, hence newScript is empty and the Execute.WithConnection executes after that. Some kind of delayed lambda function?

I guess I can execute the new script inside the .WithConnection block, but then I don’t have access to the fluent features.

Is this behaviour intended?

Documentation Pages You’ve Read So Far I browsed through the documentation and open issues on Github

Expected benefit Statements to be executed in order in which they appear in the code.

Information (please complete the following information):

  • OS: Windows 10
  • Platform: .NET 4.7.2
  • FluentMigrator version [ e.g 3.2.9 ]
  • FluentMigrator runner [FluentMigrator.Console 3.2.9]
  • Database Management System [ SQLServer ]
  • Database Management System Version [ SQLServer 2016 ]

Additional context

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jzabroskicommented, Sep 3, 2020

I suppose if we implemented #854 , we could have each top-level command return a Task, and have a way to ContinueWith the migration. Perhaps then a With.Task or something could be used to get any results from Execute.WithConnection. This would be more possible with return type covariance mentioned for C# 9.0 https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/

However, it would also mean dropping support for legacy .NET Framework, etc. So it’s realistically not going to be a change I’ll want to make for at least two years.

0reactions
jzabroskicommented, Oct 1, 2020

BTW, this may seem dumb, but - rather than use patches, why not just dump a copy of the entire object tree into a folder structure where the parent folder is a release name. Then your migration simply computes a sha1 hash sum on the most recent release name and the previous release number, and does a file comparison for differences. Then you avoid the complexity of figuring out how a patch got applied, and potentially any bugs with a patch not working since you’re doing a live merge operation in a production app (which is why I think your approach is interesting).

You could then create githooks that block modifications to a release folder once its created. You can also create a post checkout hook that makes a folder readonly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Painful to use edge cases with Execute.WithConnection #987
In FM, the solution, today, is pretty much to use Execute.WithConnection, due to limitations in T-SQL's Transactional DDL.
Read more >
Calling FluentMigrator methods inside Execute. ...
WithConnection creates an PerformDBOperationExpression expression. When processing the expression, a processor calls the Operation property (an ...
Read more >
SQL Query Order of Execution
This clause is used with aggregations such as sum() or count() to show one value per grouped field or combination of fields. When...
Read more >
Working with Engines and Connections
The Connection object always emits SQL statements within the context of a transaction block. The first time the Connection.execute() method is called to...
Read more >
SQL Query Execution Order - Scaler Topics
This article will cover SQL query execution order like From, Where clause, and Group By clause. We will use a simple example to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found