Skip to content
Browse files

when a developer already has a users table, change the users table to…

… only add missing columns
  • Loading branch information...
1 parent bfc6101 commit 8c0f4d71c39c6cbfa4c54938562b3d1aa68e7e5c Dan Croak and Joe Ferris committed with croaky
View
2 db/migrate/20110111224543_create_users.rb → ...11224543_create_diesel_clearance_users.rb
@@ -1,4 +1,4 @@
-class CreateUsers < ActiveRecord::Migration
+class CreateDieselClearanceUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.string :email
View
28 features/integration.feature
@@ -1,7 +1,7 @@
@disable-bundler
Feature: integrate with application
- Scenario: generate a Rails app, run the generators, and run the tests
+ Background:
When I successfully run "rails new testapp"
And I cd to "testapp"
And I remove the file "public/index.html"
@@ -19,8 +19,32 @@ Feature: integrate with application
And I run "bundle install --local"
And I successfully run "rails generate cucumber:install"
And I disable Capybara Javascript emulation
- And I successfully run "rails generate clearance:install"
And I successfully run "rails generate clearance:features"
+
+ Scenario: generate a Rails app, run the generators, and run the tests
+ When I successfully run "rails generate clearance:install"
+ And I successfully run "rake db:migrate --trace"
+ And I successfully run "rake --trace"
+ Then the output should contain "passed"
+ And the output should not contain "failed"
+ And the output should not contain "Could not find generator"
+
+ Scenario: Developer already has a users table in their database
+ When I write to "db/migrate/001_create_users.rb" with:
+ """
+ class CreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table(:users) do |t|
+ t.string :email
+ t.string :name
+ end
+ end
+ def self.down
+ end
+ end
+ """
+ And I successfully run "rake db:migrate --trace"
+ And I successfully run "rails generate clearance:install"
And I successfully run "rake db:migrate --trace"
And I successfully run "rake --trace"
Then the output should contain "passed"
View
14 lib/generators/clearance/install/install_generator.rb
@@ -25,6 +25,20 @@ def install
template "test/factories.rb", "test/factories/clearance.rb"
end
end
+
+ private
+
+ def migrations
+ if users_table_exists?
+ super.reject { |name| name.include?("create") } + ["db/migrate/upgrade_clearance_to_diesel.rb"]
+ else
+ super
+ end
+ end
+
+ def users_table_exists?
+ ActiveRecord::Base.connection.table_exists?(:users)
+ end
end
end
end
View
39 lib/generators/clearance/install/templates/db/migrate/upgrade_clearance_to_diesel.rb
@@ -0,0 +1,39 @@
+class UpgradeClearanceToDiesel < ActiveRecord::Migration
+ def self.up
+<%
+ existing_columns = ActiveRecord::Base.connection.columns(:users).collect { |each| each.name }
+ columns = [
+ [:email, 't.string :email'],
+ [:encrypted_password, 't.string :encrypted_password, :limit => 128'],
+ [:salt, 't.string :salt, :limit => 128'],
+ [:confirmation_token, 't.string :confirmation_token, :limit => 128'],
+ [:remember_token, 't.string :remember_token, :limit => 128']
+ ].delete_if {|c| existing_columns.include?(c.first.to_s)}
+-%>
+ change_table(:users) do |t|
+<% columns.each do |c| -%>
+ <%= c.last %>
+<% end -%>
+ end
+
+<%
+ existing_indexes = ActiveRecord::Base.connection.indexes(:users)
+ index_names = existing_indexes.collect { |each| each.name }
+ new_indexes = [
+ [:index_users_on_email, 'add_index :users, :email'],
+ [:index_users_on_remember_token, 'add_index :users, :remember_token']
+ ].delete_if { |each| index_names.include?(each.first.to_s) }
+-%>
+<% new_indexes.each do |each| -%>
+ <%= each.last %>
+<% end -%>
+ end
+
+ def self.down
+ change_table(:users) do |t|
+<% unless columns.empty? -%>
+ t.remove <%= columns.collect { |each| ":#{each.first}" }.join(',') %>
+<% end -%>
+ end
+ end
+end

0 comments on commit 8c0f4d7

Please sign in to comment.
Something went wrong with that request. Please try again.