-
-
Notifications
You must be signed in to change notification settings - Fork 30
[harmony] Bug 2052103: Drop FK references around altering attachment id type #157
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
Merged
+279
−32
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb742fc
Bug 2052103: Drop FK references around altering attachment id type
justdave 1f712f7
Add missing =over in POD
justdave 2693b74
Bug 2052103: Preserve NOTNULL on flag type_id columns; clarify bz_fk_…
mrenvoize 635788a
Bug 2052103: Add unit test for bz_fk_safe_alter_columns
mrenvoize 477a71b
Merge branch 'main' into bug-2052103-harmony
justdave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| # | ||
| # This Source Code Form is "Incompatible With Secondary Licenses", as | ||
| # defined by the Mozilla Public License, v. 2.0. | ||
| use 5.10.1; | ||
| use strict; | ||
| use warnings; | ||
| use lib qw( . lib local/lib/perl5 ); | ||
| use Test::More; | ||
| use Test2::Tools::Mock qw(mock); | ||
|
|
||
| BEGIN { | ||
| $ENV{LOCALCONFIG_ENV} = 'BMO'; | ||
| $ENV{BMO_db_driver} = 'sqlite'; | ||
| $ENV{BMO_db_name} = ':memory:'; | ||
| } | ||
| use Bugzilla; | ||
| use Bugzilla::DB; | ||
|
|
||
| # bz_fk_safe_alter_columns only calls back into bz_column_info, | ||
| # bz_drop_related_fks and bz_alter_column, so we can exercise it against a | ||
| # bare Bugzilla::DB object with those three methods mocked. This keeps the | ||
| # test database-independent and lets us assert exactly what gets passed to | ||
| # bz_alter_column. | ||
|
|
||
| # The (mocked) current state of the database, keyed by "table.column". | ||
| my %current_column; | ||
|
|
||
| # Ordered log of drop/alter events, so we can assert both what happened and | ||
| # that foreign keys are dropped *before* the column is altered. | ||
| my @events; | ||
|
|
||
| my $mock = mock 'Bugzilla::DB' => ( | ||
| override => [ | ||
| bz_column_info => sub { | ||
| my ($self, $table, $column) = @_; | ||
| return $current_column{"$table.$column"}; | ||
| }, | ||
| bz_drop_related_fks => sub { | ||
| my ($self, $table, $column) = @_; | ||
| push @events, {action => 'drop', table => $table, column => $column}; | ||
| return []; | ||
| }, | ||
| bz_alter_column => sub { | ||
| my ($self, $table, $column, $def) = @_; | ||
| push @events, {action => 'alter', table => $table, column => $column, def => $def}; | ||
| }, | ||
| ], | ||
| ); | ||
|
|
||
| my $dbh = bless {}, 'Bugzilla::DB'; | ||
|
|
||
| # Run a fix set with warnings silenced (the method warns for every drop). | ||
| sub run_fixes { | ||
| @events = (); | ||
| local $SIG{__WARN__} = sub { }; | ||
| $dbh->bz_fk_safe_alter_columns(\@_); | ||
| } | ||
|
|
||
| # --- The definition is passed to bz_alter_column verbatim ------------------ | ||
| # This is the regression guard for bug 2052103: passing only {TYPE => ...} | ||
| # would silently drop NOTNULL. The method must forward the caller's complete | ||
| # definition unchanged. | ||
| %current_column = ('flags.type_id' => {TYPE => 'INT2', NOTNULL => 1}); | ||
| run_fixes({ | ||
| table => 'flags', | ||
| column => 'type_id', | ||
| definition => {TYPE => 'INT3', NOTNULL => 1}, | ||
| only_if_type => 'INT2', | ||
| }); | ||
| is(scalar(@events), 2, 'a needed fix produces a drop and an alter'); | ||
| is($events[0]{action}, 'drop', 'foreign keys are dropped first'); | ||
| is($events[1]{action}, 'alter', 'column is altered second'); | ||
| is_deeply( | ||
| $events[1]{def}, | ||
| {TYPE => 'INT3', NOTNULL => 1}, | ||
| 'the complete definition (including NOTNULL) is forwarded unchanged' | ||
| ); | ||
|
|
||
| # --- only_if_type gating: scalar mismatch skips the fix -------------------- | ||
| %current_column = ('flags.type_id' => {TYPE => 'INT3', NOTNULL => 1}); | ||
| run_fixes({ | ||
| table => 'flags', | ||
| column => 'type_id', | ||
| definition => {TYPE => 'INT3', NOTNULL => 1}, | ||
| only_if_type => 'INT2', | ||
| }); | ||
| is(scalar(@events), 0, 'only_if_type mismatch (scalar) skips the fix entirely'); | ||
|
|
||
| # --- only_if_type gating: arrayref match applies the fix ------------------- | ||
| %current_column = ('flags.type_id' => {TYPE => 'INT4', NOTNULL => 1}); | ||
| run_fixes({ | ||
| table => 'flags', | ||
| column => 'type_id', | ||
| definition => {TYPE => 'INT3', NOTNULL => 1}, | ||
| only_if_type => ['INT2', 'INT4'], | ||
| }); | ||
| is(scalar(@events), 2, 'only_if_type arrayref match applies the fix'); | ||
|
|
||
| # --- Idempotency: no change needed means no drop and no alter -------------- | ||
| %current_column = ('flags.type_id' => {TYPE => 'INT3', NOTNULL => 1}); | ||
| run_fixes({ | ||
| table => 'flags', | ||
| column => 'type_id', | ||
| definition => {TYPE => 'INT3', NOTNULL => 1}, | ||
| }); | ||
| is(scalar(@events), 0, 'a column already matching the target is left untouched'); | ||
|
|
||
| # A differing attribute (NOTNULL) still triggers the fix even when TYPE matches. | ||
| %current_column = ('flags.type_id' => {TYPE => 'INT3'}); | ||
| run_fixes({ | ||
| table => 'flags', | ||
| column => 'type_id', | ||
| definition => {TYPE => 'INT3', NOTNULL => 1}, | ||
| }); | ||
| is(scalar(@events), 2, 'a differing scalar attribute triggers the fix'); | ||
|
|
||
| # --- Missing column is skipped, not fatal ---------------------------------- | ||
| %current_column = (); | ||
| run_fixes({ | ||
| table => 'no_such_table', | ||
| column => 'no_such_column', | ||
| definition => {TYPE => 'INT3'}, | ||
| }); | ||
| is(scalar(@events), 0, 'a non-existent column is skipped'); | ||
|
|
||
| # --- Incomplete fix specs are skipped -------------------------------------- | ||
| %current_column = ('flags.type_id' => {TYPE => 'INT2'}); | ||
| run_fixes( | ||
| {column => 'type_id', definition => {TYPE => 'INT3'}}, # no table | ||
| {table => 'flags', definition => {TYPE => 'INT3'}}, # no column | ||
| {table => 'flags', column => 'type_id'}, # no definition | ||
| ); | ||
| is(scalar(@events), 0, 'fixes missing table/column/definition are skipped'); | ||
|
|
||
| # --- Multiple fixes are processed in order --------------------------------- | ||
| %current_column = ( | ||
| 'flags.type_id' => {TYPE => 'INT2', NOTNULL => 1}, | ||
| 'flagtypes.id' => {TYPE => 'MEDIUMINT', NOTNULL => 1, PRIMARYKEY => 1}, | ||
| ); | ||
| run_fixes( | ||
| {table => 'flags', column => 'type_id', definition => {TYPE => 'INT3', NOTNULL => 1}, only_if_type => 'INT2'}, | ||
| {table => 'flagtypes', column => 'id', definition => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}}, | ||
| ); | ||
| my @altered = map { "$_->{table}.$_->{column}" } grep { $_->{action} eq 'alter' } @events; | ||
| is_deeply( | ||
| \@altered, | ||
| ['flags.type_id', 'flagtypes.id'], | ||
| 'multiple fixes are altered in the order given' | ||
| ); | ||
|
|
||
| done_testing; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like unit tests. We don't have enough of them.