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

Update migrate.ps1 to support test database used by integration tests #4912

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

addisonbeck
Copy link
Contributor

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-10863

📔 Objective

The integration tests for databases use different secrets.json properties than the ones used by locally running servers. This is to support not corrupting folk's local test data.

Currently these test databases are not supported by migrate.ps1 and other tooling. This PR updates migrate.ps1 with a --test switch, and adds logic for connecting to and migrating these databases.

The --all switch was modified to include running migrations against test databases as well.

🧮 Side Effects

As part of this work I had to modify an old MySql migration because of an error in its manually written logic. The script in question checks for the existence of a column from system tables before trying to drop it. This doesn't check for a specific databases, and so can cause false positives if you try to have multiple databases on the same server.

📸 Screenshots

In this demo I build fresh databases from scratch and run some migrations using migrate.ps1. It:

  1. starts with a pwsh migrate.ps1 --all command that skips test databases because no connection strings are found.
  2. Runs pwsh migrate.ps1 --all again, this time with connection strings set
  3. Shows examples of pwsh migrate.ps1 --test --{DATABASE} for targeting specific test databases.
  4. Shows the newly created databases in Azure Data Studio.
migrate.mov

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

@addisonbeck addisonbeck requested a review from a team as a code owner October 17, 2024 23:25
Copy link

codecov bot commented Oct 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 41.75%. Comparing base (1fb366d) to head (235020f).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4912   +/-   ##
=======================================
  Coverage   41.75%   41.75%           
=======================================
  Files        1363     1363           
  Lines       63915    63915           
  Branches     5853     5853           
=======================================
  Hits        26686    26686           
  Misses      36026    36026           
  Partials     1203     1203           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

Logo
Checkmarx One – Scan Summary & Detailse67ffdc6-cd5a-490e-aceb-2492c99d4863

No New Or Fixed Issues Found

Copy link
Member

@eliykat eliykat left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time to follow this one up.

Copy link
Member

Choose a reason for hiding this comment

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

This makes sense to me but requesting a review from @rkac-bw to confirm this retroactive change is OK.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the change for MySQL makes sense. The existence check for the Id column in the Grant table is scoped to the current database schema. This prevents false positives when multiple databases are present on the same server, avoiding unintended changes to other databases.

Comment on lines +63 to +65
@($mysql, "MySQL", "MySqlMigrations", "mySql", 2),
@($postgres, "PostgreSQL", "PostgresMigrations", "postgreSql", 0),
@($sqlite, "SQLite", "SqliteMigrations", "sqlite", 1)
Copy link
Member

Choose a reason for hiding this comment

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

The test attributes (DatabaseData, DatabaseTheory) allow these providers to be defined in any order, but this script assumes that postgres is 0, sqlite is 1, mySql is 2 and mssql is 3. That won't be the case for everyone, depending on how they've set up their user secrets.

You could add logic here iterating over the user secrets to find the index for each database type, but it might make more sense to update the structure of the user secrets themselves to adopt a more conventional key-value format, like test:database:mysql:connectionString. That would also require updating the test attributes, so it's a little in the weeds, but it would make sure this Just Works for everyone.

@eliykat eliykat requested a review from rkac-bw October 23, 2024 03:11
Copy link
Contributor

@rkac-bw rkac-bw left a comment

Choose a reason for hiding this comment

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

LGTM

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