diff options
| author | Teddy Wing | 2017-11-14 12:12:40 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2017-11-14 12:22:29 +0100 | 
| commit | a35a040fb6fbcccf7b9bc23386ea494bb6371c39 (patch) | |
| tree | 8b1abf7361fe478c7a25ae6266194f086dd2a45c /spec | |
| parent | 96afaed78fa043449c0264ea09f0106147755c8e (diff) | |
| download | chouette-core-a35a040fb6fbcccf7b9bc23386ea494bb6371c39.tar.bz2 | |
Add spec to ensure `schema.rb` keys are `bigint`s
We use `bigint`s by default for all primary and foreign keys in the
application.
Using `bigint`s provides two advantages:
* Matching the Java application, which expects `bigint`s
* Providing support for large integer identifiers
Here, we add a spec that checks the `schema.rb` file with line-by-line
string matching to validate that no primary or foreign keys use
non-`bigint` types.
The test is a bit rough at the moment, but it works against the
temporary migration included in this commit. It should be reworked,
though, to include line numbers of the problem fields.
Refs #4951
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/db/schema_spec.rb | 26 | 
1 files changed, 26 insertions, 0 deletions
| diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb new file mode 100644 index 000000000..8c5492cd7 --- /dev/null +++ b/spec/db/schema_spec.rb @@ -0,0 +1,26 @@ +RSpec.describe ActiveRecord::Schema do +  it "uses type `bigint` for primary and foreign keys" do +# grep -e 'create_table.*id: :bigserial' db/schema.rb | grep -v 'id: :bigserial' +# grep -e 'create_table.+id: :bigserial' db/schema.rb | grep -v 'id: :bigserial' +# grep -e  't\.integer *"\w*_id"' db/schema.rb | grep -v -e 'limit: 8' +# grep -e  't\.integer +"\w+_id"' db/schema.rb | grep -v -e 'limit: 8' + +    non_bigint_primary_keys = [] +    non_bigint_foreign_keys = [] + +    File.open('db/schema.rb', 'r') do |f| +      non_bigint_primary_keys = f +        .grep(/create_table /) +        .grep_v(/id: :bigserial/) + +      f.rewind + +      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 +  end +end | 
