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

[DEVOPS-1537 & DEVOPS-1519] Update dbo.Migrations table to support own migration schema #3212

Conversation

michalchecinski
Copy link
Contributor

@michalchecinski michalchecinski commented Aug 18, 2023

Type of change

- [ ] Bug fix
- [x] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other

Objective

In the Migrator project create our own custom journaling, so that we can add migration scripts as repeatable in the migrations table.

Code changes

  • util/Migrator/RerunableSqlTableJournal.cs: Sql MIgrations Journal implementation. Our own implementation for Journal to support re-runnable SQL migrations as described in DEVOPS-1519 Jira issue.
  • util/Migrator/RerunableSqlTableJournalExtensions.cs Add extension for RerunableSqlTableJournal to use in fluent way in util/Migrator/DbMigrator.cs
  • util/Migrator/DbMigrator.cs Change DbUp JournalToSqlTable to RerunableSqlTableJournal using RerunableSqlTableJournalExtensions.

Before you submit

  • Please check for formatting errors (dotnet format --verify-no-changes) (required)
  • If making database changes - make sure you also update Entity Framework queries and/or migrations
  • Please add unit tests where it makes sense to do so (encouraged but not required)
  • If this change requires a documentation update - notify the documentation team
  • If this change has particular deployment requirements - notify the DevOps team

@michalchecinski michalchecinski requested a review from a team August 18, 2023 11:43
@bitwarden-bot
Copy link

bitwarden-bot commented Aug 18, 2023

Logo
Checkmarx One – Scan Summary & Detailsd423a448-8c1c-494c-b6ab-ad564e2bc87a

New Issues

Severity Issue Source File / Package Checkmarx Insight
HIGH Passwords And Secrets - Generic Password /database.yml: 69 Query to find passwords and secrets in infrastructure code.
HIGH Passwords And Secrets - Generic Password /infrastructure-tests.yml: 78 Query to find passwords and secrets in infrastructure code.
HIGH Passwords And Secrets - Generic Password /database.yml: 65 Query to find passwords and secrets in infrastructure code.
HIGH Passwords And Secrets - Generic Password /infrastructure-tests.yml: 91 Query to find passwords and secrets in infrastructure code.
HIGH Passwords And Secrets - Generic Password /infrastructure-tests.yml: 97 Query to find passwords and secrets in infrastructure code.

Copy link
Contributor

@withinfocus withinfocus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still a work in progress? I added a few comments as I am interested in this change but ... I don't see any differences from what's already available.


namespace Bit.Migrator;

public static class RerunableSqlTableJournalExtensions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ I'd call this just SqlTableJournalExtensions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


namespace Bit.Migrator;

public class RerunableSqlTableJournal : TableJournal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 I think it's fine to have this class in the extensions class, as it's only used there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Done.


namespace Bit.Migrator;

public class RerunableSqlTableJournal : TableJournal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me give a little background on this. We want to support transition migrations as rerunnable and mark this fact in the migrations database. We split this implementation in two steps:

  1. Reflect the current functionality in our own code (and passing it to the DbUp), to then :
  2. Extend "RerunableSqlTableJournal" class to support marking a script as rerunnable - adding a column in the Migrations table, fill and check value when adding and getting the values from this table.

This PR is about the first point. This class is basically the same implementation as you posted, but we want to test the existing migration process with our own code, to then extended it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so it was split up. I realize that's not how the tasks are written, but I don't feel it's needed to review this functionality clone unless some value is being created; I think the two items above should be one change. I would think you could still use SqlTableJournal and override potentially just one method to add what you're describing.

If you want to be able to run something many times over, DbUp provides NullJournal as well -- could that be used instead?

Copy link
Contributor Author

@michalchecinski michalchecinski Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had this conversation with the DevOps team if we want/need to journal those migrations that must be run multiple times. We concluded that we do want to track which migrations have been run, but at the same time mark them as rerunnable to run multiple times.

@michalchecinski michalchecinski enabled auto-merge (squash) August 21, 2023 16:18
Copy link
Contributor

@withinfocus withinfocus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per my inline comment.

@michalchecinski
Copy link
Contributor Author

Hey @withinfocus 👋 I just finished testing the whole utility. It's working as intended, so the PR is ready for review 😊

@@ -24,6 +24,8 @@ public DbMigrator(string connectionString, ILogger<DbMigrator> logger)
}

public bool MigrateMsSqlDatabaseWithRetries(bool enableLogging = true,
bool rerunable = false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 Hate to do this since it's in so many places, but two Ns for "rerunnable". I suggest we go with a less obscure English word and use "repeatable".

@@ -0,0 +1,3 @@
ALTER TABLE dbo.Migration
ADD Rerunable bit NOT NULL DEFAULT 0
GO
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ Newline.

@@ -2,6 +2,7 @@

<ItemGroup>
<EmbeddedResource Include="DbScripts\**\*.sql" />
<EmbeddedResource Include="DbScripts_data_migration\**\*.sql" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Is the _data_migration in docs somewhere, or a recommendation? DbDataMigrationScripts or DbDataMigrations is cleaner to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've got another card to rename this folder: https://bitwarden.atlassian.net/browse/DEVOPS-1520


namespace Bit.Migrator;

public static class SqlTableJournalExtensions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Did you ever look into my comment about using NullJournal? Now that the implementation is here, what is the purpose of tracking scripts that you always want to run? If you have a folder for some that can always run, that's a use case for a different journal otherwise you'd keep using the one already in place. What am I missing in the requirements?

@withinfocus
Copy link
Contributor

I started top down and left a few comments but the last one is the key and worth stepping back for a moment.

@michalchecinski michalchecinski changed the title [DEVOPS-1537] Update dbo.Migrations table to support own migration schema [DEVOPS-1537 & DEVOPS-1519] Update dbo.Migrations table to support own migration schema Sep 6, 2023
@michalchecinski
Copy link
Contributor Author

After talking about it, we decided to not track the Transition migrations that have been applied to the database until they are applied from the migrations directory.

I'm closing the PR but not deleting the branch for further reference.

auto-merge was automatically disabled September 11, 2023 10:18

Pull request was closed

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

Successfully merging this pull request may close these issues.

3 participants