erikbra / grate

grate - the SQL scripts migration runner
MIT License
164 stars 36 forks source link

--drop without continuing migration use case #526

Open Jantero93 opened 1 month ago

Jantero93 commented 1 month ago

EDIT: Tried rewrite whole post with better english

I have a use case where I need to drop a database without continuing the migration. Specifically, I have a database rebuild script that first drops the database. I want to handle creating the database with Grate (setting collation, etc.) on SQL Server.

So my flow on database rebuild

  1. Only drop and create database (I want --drop flag without further migrations applied)
  2. Run EF Core Migrations
  3. Run rest Grate migrations

So my ultimate goal have

RebuildDatabase.sh

containining (WITH SINGLE GRATE FOLDER) something like

grate --folders=./grate \
--connectionstring="example-database" \
--drop

dotnet ef database update

grate --folders=./grate \
--connectionstring="example-database" 

Now my workaround is to create set Drop & Create database script different folder and run them from master db (I can't use --drop flag, because that would trigger migrations before Entity Framework Core migrations. And when I dont use --drop, grate will not trigger scripts in dropDatabase folder)

So my current setup is something like this (example)

# Step 1: Drop and create the database using Grate
grate --folders=./grate/initDb \
--connectionstring="SQL-server-master-database" 

# Step 2: Entity Framework migrations
dotnet ef database update

# Step 3: Rest migrations
grate --folders=./grate/migrations \
--connectionstring="example-database" \

Then my another ultimate goal is have UpdateDatabase.sh what would contain

dotnet ef database update

grate --folders=./grate \
--connectionstring="example-database" 
Jantero93 commented 1 month ago

I know it's kind of strange to have two db managment tool. But Im only using Entity Framework Core with OpenIddict related tables because EF can handle migration automatically. Otherwise I want use only grate and handle db calls manually with Dapper etc.