aboutsummaryrefslogtreecommitdiffstats
path: root/spec/db/schema_spec.rb
blob: a36d3f9265db0ba5e5cc4ed76a9d7c7b216fdff1 (plain)
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
RSpec.describe ActiveRecord::Schema do
  it "uses type `bigint` for primary and foreign keys" do
    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(filename, 'r') do |f|
      @non_bigint_primary_keys = f
        .grep(/create_table /)
        .grep_v(/id: (:bigserial|false)/)

      f.rewind

      @non_bigint_foreign_keys = f
        .grep(/t\.integer +"\w+_id"/)
        .grep_v(/limit: 8/)
    end

    @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