aboutsummaryrefslogtreecommitdiffstats
path: root/spec/db/schema_spec.rb
diff options
context:
space:
mode:
authorcedricnjanga2017-11-22 10:20:03 +0100
committercedricnjanga2017-11-22 10:20:03 +0100
commiteec1d9e43509a1677d346584ee24cd3ee6f8c124 (patch)
tree5ea0ffc28a65f7178e5f11ae19cff353139d3159 /spec/db/schema_spec.rb
parent577204834de4dfcd1851fc9b5f85558eaa141a89 (diff)
parentf01e81604053e9cbdaf4b84534214e37fbfcae5e (diff)
downloadchouette-core-eec1d9e43509a1677d346584ee24cd3ee6f8c124.tar.bz2
Merge branch 'master' into 4941-refactoring_object_id
Diffstat (limited to 'spec/db/schema_spec.rb')
-rw-r--r--spec/db/schema_spec.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb
new file mode 100644
index 000000000..a7fe0a162
--- /dev/null
+++ b/spec/db/schema_spec.rb
@@ -0,0 +1,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