Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add foreign keys when creating a new transition table #248

Merged
merged 2 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add foreign keys when creating a new transition table
  • Loading branch information
greysteil committed May 18, 2017
commit d01e6366d3eb18e5449643a07f50210f1f7862bf
4 changes: 4 additions & 0 deletions lib/generators/statesman/generator_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def parent_name
parent.demodulize.underscore
end

def parent_table_name
parent.demodulize.underscore.pluralize
end

def parent_id
parent_name + "_id"
end
Expand Down
2 changes: 2 additions & 0 deletions lib/generators/statesman/templates/create_migration.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Create<%= migration_class_name %> < ActiveRecord::Migration
t.timestamps null: false
end

add_foreign_key :<%= table_name %>, :<%= parent_table_name %>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you should change lib/generators/statesman/templates/update_migration.rb.erb too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deliberately kept it out of there, under the assumption that the end user would have already added a foreign key to the existing table they're updating if they wanted one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

under the assumption that the end user would have already added a foreign key to the existing table they're updating if they wanted one.

I see, but it depends on the situation.


add_index(:<%= table_name %>,
[:<%= parent_id %>, :sort_key],
unique: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
let(:migration_name) { 'db/migrate/create_bacon_transitions.rb' }
end

describe 'creates a migration' do
subject { file("db/migrate/#{time}_create_bacon_transitions.rb") }

before { allow(Time).to receive(:now).and_return(mock_time) }
before { run_generator %w(Yummy::Bacon Yummy::BaconTransition) }

let(:mock_time) { double('Time', utc: double('UTCTime', strftime: time)) }
let(:time) { '5678309' }

it "includes a foreign key" do
expect(subject).to contain("add_foreign_key :bacon_transitions, :bacons")
end
end

describe 'properly adds class names' do
before { run_generator %w(Yummy::Bacon Yummy::BaconTransition) }
subject { file('app/models/yummy/bacon_transition.rb') }
Expand Down