-
Notifications
You must be signed in to change notification settings - Fork 649
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
Delete.Table.IfExists #494
Comments
@katlimruiz I beg to differ, the entire idea of doing migrations is expecting your database to be in a given state. If you execute a drop table, you do it because you know the table exists |
But maybe adding an if exists to the fluent api could give the user a choice, but i am agains auto opt-in |
The user should at least have a choice of doing if-exists style checks before certain operations. I agree that you should expect the database to be in a certain state, but reality is that on any given project many people may be writing migrations, interfering with each other and having to go back to alter existing migrations after others have already run in the migration then updating migration numbers and all sorts of things in order to get the job done quicker. Or simply because they are a junior member of the team and they make mistakes. Or because various people in various branches of development are looking at different views of the database with different sets of migrations run in. At the end of all that the migrations are messier but they work over a wide range of systems in various initial states including each developers' personal database, the test, UAT, DEMO, Release-Candidate and, of course, live databases. |
I vote for a IfExists on a drop table, I agree in the case the database sometimes is not perfect as expected. |
I would differ on it as well, I'm working on a very big heathcare project and we just switched to using FluentMigrator as our database change support. Problem is that lots of hospitals have differences in databases and we need sometimes to remove a table if it exists. I agree with @eldersantos that DeleteIfExists should be supported across all the schema elements. |
@migol @eldersantos Like already said i am open to accept a pull considering this ;) |
For those working with SQL Server, in the mean-time you can create an extension method like below:
And call it: It falls short of a real solution where all database providers are considered, and does not take schemas into consideration. |
I copy stuff like this around all my migration steps:
I even use more specialized helper methods to create a clustered key different from primary keys for tables that have uniqueidentifier as PK:
That makes it easy to do stuff like:
That's what I like about FluentMigrator as opposed of writing SQL scripts by hand. It's so easy to write reusable helper methods for common patterns in C# and make things a bit more typesafe and less error prone. |
Querying the current state of the database doesn't work well with SQL script generation. We have to find a middle ground between PR #664 and this solution. |
@fubar-coder I think if the ask is to support this syntax, the complexity is low. Note: I had a real use case for this just now :) I have a customer with tables named SomeData_2018Q2, SomeData_2018Q3, etc. that are driven by a quarterly process. However, in my test environment, that quarterly process doesn't run, making it annoying to run migrations since I have to tag them with specific environments as opposed to running the same migrations in all environments. |
Workaround: private void DropTableIfExists(string schemaName, string tableName)
{
this.Execute.Sql($"DROP TABLE IF EXISTS [{schemaName}].[{tableName}];");
} |
Databases supporting DROP TABLE IF EXISTSMySQL https://dev.mysql.com/doc/refman/8.0/en/drop-table.html Databases with equivalent idiomsNote: might be hard to code-generate. Oracle https://stackoverflow.com/a/1801453/1040437 No Support |
All DROP TABLE commands should do a If Exists check first (I'm using Sql Server 2012).
The text was updated successfully, but these errors were encountered: