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

Apply a baseline script to a newly created database. #255

Closed
erlis opened this issue May 26, 2017 · 3 comments
Closed

Apply a baseline script to a newly created database. #255

erlis opened this issue May 26, 2017 · 3 comments

Comments

@erlis
Copy link

erlis commented May 26, 2017

Hi I see you have functionality to create the Sql Server database if it doesn't exists. This is described in Issue 74

My request is not only valid for Sql Server although that's the database I'm using at the moment.

Why I need this? I'm introducing DbUp to a project that already existed. My goal is to be able to run the migrations in a machine without even the database and have everything created (same effect as if this project were using DbUp from the beginning).

The problem I'm facing is that because the database will already start in a given state I need to execute a baseline script upon database creation (this should be only execute once) and I don't find a way to do this with the current functionality in DbUp.

You can think you can add the first migration as this baseline script. That could be a possible solution, but serious attention has to be pay to the way that script is written, because the baseline script can be executed in databases that contain all the structures the script is trying to add, remember that is not just structure but also data.

I think that even when this is achievable with the current functionality, having something like this will be really useful.

var upgrader =
           DeployChanges.To
               .SqlDatabase(connectionString)
               .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
               .WithBaselineScript("[Name of the Baseline script]") // <---- suggested addition
               .LogToConsole()
               .Build();
...
EnsureDatabase.For.SqlDatabase(connectionString, applyBaseline: true );  // <---- new parameter, not even necessary, because the builder definition can be enough. 

Any thoughts??
Thanks,
Erlis

@afaure-bkk
Copy link
Contributor

Do you mean that when running DbUp the baseline script would be run when working from a non existant DB (i.e.: when EnsureDatabase kicks in) ?

In this case, shouldn't the .withBaselineScript directive be added to the EnsureDatabase rather than the upgrader? This way, the upgrader does not need to know whether the EnsureDatabase kicked in or not

@erlis
Copy link
Author

erlis commented May 26, 2017

Exactly!

Well, what I just explained was just an idea on how to do it. I think you guys will have more insight on how to do it. But I think you are absolutely right, EnsureDatabase is the one that should apply the baseline, is more cohesive this way.

@droyad
Copy link
Member

droyad commented Feb 26, 2018

You can do this by adding a line to the start of the first script(s) to check whether the structures already exist, and if so, return from the script. DBup will then create the schemaversions table and add a record for the script.

@droyad droyad closed this as completed Feb 26, 2018
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

No branches or pull requests

3 participants