aboutsummaryrefslogtreecommitdiffstats
path: root/spec/db/schema_spec.rb
blob: a7fe0a162af5044ecb90d5acedae0916cfaa078e (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
41
42
43
44
45
46
47
48
49
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|
    @original = ""
    @expected = ""

    File.open(filename, 'r') do |f|
      f.each_line do |line|
        expected_line = line

        # Primary key
        if line =~ /create_table\s/ &&
          !(line =~ /id: \s+ (?: :bigserial | false)/x)
          expected_line = line.sub(/(create_table\s"\w+",\s)/, '\1id: :bigserial, ')
        end

        # Foreign key
        if line =~ /t\.integer\s+"\w+_id"/ &&
          !(line =~ /limit: 8/)
          expected_line = line.sub(/(t.integer\s+"\w+")/, '\1, limit: 8')
        end

        @original += line
        @expected += expected_line
      end
    end

    @original == @expected
  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_as_string(@original, @expected)
  end
end