Skip to content

GitLab

  • Menu
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • C chouette-core
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 36
    • Merge requests 36
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Infrastructure Registry
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Administrator
  • chouette-core
  • Merge requests
  • !117

Merged
Created Nov 14, 2017 by Administrator@rootMaintainer

4951 spec to validate that primary and foreign keys in the database are type bigint

  • Overview 7
  • Commits 10
  • Changes 1

Created by: teddywing

A spec that checks the schema.rb file to make sure all primary and foreign keys defined in the application have type bigint.

We're required to use bigints, but for the moment, this is enforced by convention only. Thus it's very easy to forget to type keys as bigint when creating new migrations.

This spec is a remedy for that forgetfulness.

Testing

You can use or modify the following migration (or create your own) to test the spec:

# 20171114110225_test_bad_foreign_keys.rb
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

Demo

RSpec output should look like this:

Failures:

  1) ActiveRecord::Schema uses type `bigint` for primary and foreign keys
     Failure/Error: expect('db/schema.rb').to use_bigint_keys
     
       expected "db/schema.rb" to use bigint keys
       Diff: 
       
       
       @@ -82,11 +82,11 @@
        
          add_index "api_keys", ["organisation_id"], name: "index_api_keys_on_organisation_id", using: :btree
        
       -  create_table "bad_foreign_keys", id: :bigserial, force: :cascade do |t|
       +  create_table "bad_foreign_keys", force: :cascade do |t|
            t.string   "thing"
       -    t.integer  "forei_id", limit: 8
       +    t.integer  "forei_id"
            t.integer  "quant"
       -    t.integer  "anoth_for_id", limit: 8
       +    t.integer  "anoth_for_id"
            t.datetime "created_at",   null: false
            t.datetime "updated_at",   null: false
          end
Assignee
Assign to
Reviewer
Request review from
None
Milestone
None
Assign milestone
Time tracking
Source branch: 4951-spec-to-validate-that-primary-and-foreign-keys-in-the-database-are-type-bigint