aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-11-14 12:12:40 +0100
committerTeddy Wing2017-11-14 12:22:29 +0100
commita35a040fb6fbcccf7b9bc23386ea494bb6371c39 (patch)
tree8b1abf7361fe478c7a25ae6266194f086dd2a45c
parent96afaed78fa043449c0264ea09f0106147755c8e (diff)
downloadchouette-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
-rw-r--r--db/migrate/20171114110225_test_bad_foreign_keys.rb12
-rw-r--r--spec/db/schema_spec.rb26
2 files changed, 38 insertions, 0 deletions
diff --git a/db/migrate/20171114110225_test_bad_foreign_keys.rb b/db/migrate/20171114110225_test_bad_foreign_keys.rb
new file mode 100644
index 000000000..0f2f5311b
--- /dev/null
+++ b/db/migrate/20171114110225_test_bad_foreign_keys.rb
@@ -0,0 +1,12 @@
+class TestBadForeignKeys < ActiveRecord::Migration
+ def change
+ create_table :bad_foreign_keys do |t|
+ t.string :thing
+ t.references :forei
+ t.integer :quant
+ t.references :anoth_for
+
+ t.timestamps null: false
+ end
+ end
+end
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