diff options
Diffstat (limited to 'spec/db')
| -rw-r--r-- | spec/db/schema_spec.rb | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb index 8111988ee..0e0fe780a 100644 --- a/spec/db/schema_spec.rb +++ b/spec/db/schema_spec.rb @@ -1,21 +1,40 @@ RSpec.describe ActiveRecord::Schema do it "uses type `bigint` for primary and foreign keys" do - non_bigint_primary_keys = [] - non_bigint_foreign_keys = [] + expect('db/schema.rb').to use_bigint_keys + end +end + + +RSpec::Matchers.define :use_bigint_keys do + match do |filename| + @non_bigint_primary_keys = [] + @non_bigint_foreign_keys = [] - File.open('db/schema.rb', 'r') do |f| - non_bigint_primary_keys = f + File.open(filename, 'r') do |f| + @non_bigint_primary_keys = f .grep(/create_table /) .grep_v(/id: :bigserial/) f.rewind - non_bigint_foreign_keys = f + @non_bigint_foreign_keys = f .grep(/t\.integer +"\w+_id"/) .grep_v(/limit: 8/) end - expect(non_bigint_primary_keys).to be_empty - expect(non_bigint_foreign_keys).to be_empty + @non_bigint_primary_keys.empty? && @non_bigint_foreign_keys.empty? + end + + failure_message do |filename| + <<~EOS + expected #{filename.inspect} to use bigint keys + Diff: #{diff} + EOS + end + + def diff + RSpec::Support::Differ.new( + color: RSpec::Matchers.configuration.color? + ).diff(@non_bigint_primary_keys + @non_bigint_foreign_keys, []) end end |
