1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# frozen_string_literal: true
require 'fast_spec_helper'
require 'rspec-parameterized'
require 'gitlab/dangerfiles/spec_helper'
require_relative '../../../tooling/danger/database'
RSpec.describe Tooling::Danger::Database, feature_category: :tooling do
include_context "with dangerfile"
let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) }
let(:migration_files) do
[
# regular migrations
'db/migrate/20220901010203_add_widgets_table.rb',
'db/migrate/20220909010203_add_properties_column.rb',
'db/migrate/20220910010203_drop_tools_table.rb',
'db/migrate/20220912010203_add_index_to_widgets_table.rb',
# post migrations
'db/post_migrate/20220901010203_add_widgets_table.rb',
'db/post_migrate/20220909010203_add_properties_column.rb',
'db/post_migrate/20220910010203_drop_tools_table.rb',
'db/post_migrate/20220912010203_add_index_to_widgets_table.rb',
# ee migrations
'ee/db/migrate/20220901010203_add_widgets_table.rb',
'ee/db/migrate/20220909010203_add_properties_column.rb',
'ee/db/migrate/20220910010203_drop_tools_table.rb',
'ee/db/migrate/20220912010203_add_index_to_widgets_table.rb',
# geo migrations
'ee/db/geo/migrate/20220901010203_add_widgets_table.rb',
'ee/db/geo/migrate/20220909010203_add_properties_column.rb',
'ee/db/geo/migrate/20220910010203_drop_tools_table.rb',
'ee/db/geo/migrate/20220912010203_add_index_to_widgets_table.rb'
]
end
let(:cutoff) { Date.parse('2022-10-01') - 21 }
subject(:database) { fake_danger.new(helper: fake_helper) }
describe '#find_migration_files_before' do
it 'returns migrations that are before the cutoff' do
expect(database.find_migration_files_before(migration_files, cutoff).length).to eq(8)
end
end
describe '#changes' do
using RSpec::Parameterized::TableSyntax
where do
{
'with database changes to a migration file' => {
modified_files: %w[
db/migrate/20230720114001_test_migration.rb
db/schema_migrations/20230720114001
db/structure.sql
app/models/test.rb
],
changed_lines: [],
changes_by_category: {
database: %w[
db/migrate/20230720114001_test_migration.rb
db/schema_migrations/20230720114001
db/structure.sql
]
},
impacted_files: %w[
db/migrate/20230720114001_test_migration.rb
db/schema_migrations/20230720114001
db/structure.sql
]
},
'with non-database changes' => {
modified_files: %w[
app/models/test.rb
],
changed_lines: %w[
+# Comment explaining scope :blah
],
changes_by_category: {
database: []
},
impacted_files: []
},
'with database changes in a doc' => {
modified_files: %w[doc/development/database/test.md],
changed_lines: [
'+scope :blah, ->() { where(hidden: false) }'
],
changes_by_category: {
database: []
},
impacted_files: []
},
'with database changes in a model' => {
modified_files: %w[app/models/test.rb],
changed_lines: [
'+# Comment explaining scope :blah',
'+scope :blah, ->() { where(hidden: false) }'
],
changes_by_category: {
database: []
},
impacted_files: %w[app/models/test.rb]
},
'with database changes in a concern' => {
modified_files: %w[app/models/concerns/test.rb],
changed_lines: [
'- .where(hidden: false)',
'+ .where(hidden: true)'
],
changes_by_category: {
database: []
},
impacted_files: %w[app/models/concerns/test.rb]
}
}
end
with_them do
before do
allow(fake_helper).to receive(:modified_files).and_return(modified_files)
allow(fake_helper).to receive(:all_changed_files).and_return(modified_files)
allow(fake_helper).to receive(:changed_lines).and_return(changed_lines)
allow(fake_helper).to receive(:changes_by_category).and_return(changes_by_category)
end
it 'returns database changes' do
expect(database.changes).to match impacted_files
end
end
end
end
|