diff options
author | José Valim <jose.valim@gmail.com> | 2009-06-28 13:16:14 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-06-28 13:16:14 +0200 |
commit | 1ca51df0001a750fafb0da70e4560c1ec439236c (patch) | |
tree | 677de24572e4d698149694145f14dfd5438fea56 /railties/lib | |
parent | 4f3e44fa03ab8fc47f75fa710b28c72b9b2328b9 (diff) | |
download | rails-1ca51df0001a750fafb0da70e4560c1ec439236c.tar.gz rails-1ca51df0001a750fafb0da70e4560c1ec439236c.tar.bz2 rails-1ca51df0001a750fafb0da70e4560c1ec439236c.zip |
Added migration and session_migration generator.
Diffstat (limited to 'railties/lib')
9 files changed, 126 insertions, 1 deletions
diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index 2c4c3286d4..4e585a11e7 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -14,7 +14,7 @@ module ActiveRecord # # ==== Examples # - # migration_template "migrate.rb", "db/migrate/add_foo_to_bar" + # migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb" # def migration_template(source, destination=nil, log_status=true) destination = File.expand_path(destination || source, self.destination_root) diff --git a/railties/lib/generators/active_record/migration/migration_generator.rb b/railties/lib/generators/active_record/migration/migration_generator.rb new file mode 100644 index 0000000000..baf9650790 --- /dev/null +++ b/railties/lib/generators/active_record/migration/migration_generator.rb @@ -0,0 +1,25 @@ +require 'generators/active_record' + +module ActiveRecord + module Generators + class MigrationGenerator < Base + argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" + + def create_migration_file + set_local_assigns! + migration_template "migration.rb", "db/migrate/#{file_name}.rb" + end + + protected + attr_reader :migration_action + + def set_local_assigns! + if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/ + @migration_action = $1 + @table_name = $2.pluralize + end + end + + end + end +end diff --git a/railties/lib/generators/active_record/migration/templates/migration.rb b/railties/lib/generators/active_record/migration/templates/migration.rb new file mode 100644 index 0000000000..2851f7cb42 --- /dev/null +++ b/railties/lib/generators/active_record/migration/templates/migration.rb @@ -0,0 +1,11 @@ +class <%= @migration_class_name %> < ActiveRecord::Migration + def self.up<% attributes.each do |attribute| %> + <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end -%> + <%- end %> + end + + def self.down<% attributes.reverse.each do |attribute| %> + <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end -%> + <%- end %> + end +end diff --git a/railties/lib/generators/active_record/session_migration/session_migration_generator.rb b/railties/lib/generators/active_record/session_migration/session_migration_generator.rb new file mode 100644 index 0000000000..d60da5c0a5 --- /dev/null +++ b/railties/lib/generators/active_record/session_migration/session_migration_generator.rb @@ -0,0 +1,20 @@ +require 'generators/active_record' + +module ActiveRecord + module Generators + class SessionMigrationGenerator < Base + argument :name, :type => :string, :default => "add_sessions_table" + + def create_migration_file + migration_template "migration.rb", "db/migrate/#{file_name}.rb" + end + + protected + + def session_table_name + ActiveRecord::Base.pluralize_table_names ? 'session'.pluralize : 'session' + end + + end + end +end diff --git a/railties/lib/generators/active_record/session_migration/templates/migration.rb b/railties/lib/generators/active_record/session_migration/templates/migration.rb new file mode 100644 index 0000000000..19811d9455 --- /dev/null +++ b/railties/lib/generators/active_record/session_migration/templates/migration.rb @@ -0,0 +1,16 @@ +class <%= @migration_class_name %> < ActiveRecord::Migration + def self.up + create_table :<%= session_table_name %> do |t| + t.string :session_id, :null => false + t.text :data + t.timestamps + end + + add_index :<%= session_table_name %>, :session_id + add_index :<%= session_table_name %>, :updated_at + end + + def self.down + drop_table :<%= session_table_name %> + end +end diff --git a/railties/lib/generators/rails/migration/USAGE b/railties/lib/generators/rails/migration/USAGE new file mode 100644 index 0000000000..d91127aac3 --- /dev/null +++ b/railties/lib/generators/rails/migration/USAGE @@ -0,0 +1,29 @@ +Description: + Stubs out a new database migration. Pass the migration name, either + CamelCased or under_scored, and an optional list of attribute pairs as arguments. + + A migration class is generated in db/migrate prefixed by a timestamp of the current date and time. + + You can name your migration in either of these formats to generate add/remove + column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable + +Example: + `./script/generate migration AddSslFlag` + + If the current date is May 14, 2008 and the current time 09:09:12, this creates the AddSslFlag migration + db/migrate/20080514090912_add_ssl_flag.rb + + `./script/generate migration AddTitleBodyToPost title:string body:text published:boolean` + + This will create the AddTitleBodyToPost in db/migrate/20080514090912_add_title_body_to_post.rb with + this in the Up migration: + + add_column :posts, :title, :string + add_column :posts, :body, :text + add_column :posts, :published, :boolean + + And this in the Down migration: + + remove_column :posts, :published + remove_column :posts, :body + remove_column :posts, :title diff --git a/railties/lib/generators/rails/migration/migration_generator.rb b/railties/lib/generators/rails/migration/migration_generator.rb new file mode 100644 index 0000000000..6aefddd3fd --- /dev/null +++ b/railties/lib/generators/rails/migration/migration_generator.rb @@ -0,0 +1,8 @@ +module Rails + module Generators + class MigrationGenerator < NamedBase + argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" + hook_for :orm + end + end +end diff --git a/railties/lib/generators/rails/session_migration/USAGE b/railties/lib/generators/rails/session_migration/USAGE new file mode 100644 index 0000000000..e106f6ecc8 --- /dev/null +++ b/railties/lib/generators/rails/session_migration/USAGE @@ -0,0 +1,8 @@ +Description: + Creates a migration to add the sessions table used by the ORM session store. + Pass the migration name, either CamelCased or under_scored, as an argument. + + Before invoking this generator, be sure that your ORM supports session stores. + +Example: + `./script/generate session_migration CreateSessionTable` diff --git a/railties/lib/generators/rails/session_migration/session_migration_generator.rb b/railties/lib/generators/rails/session_migration/session_migration_generator.rb new file mode 100644 index 0000000000..e7d988359c --- /dev/null +++ b/railties/lib/generators/rails/session_migration/session_migration_generator.rb @@ -0,0 +1,8 @@ +module Rails + module Generators + class SessionMigrationGenerator < NamedBase + argument :name, :type => :string, :default => "add_session_table" + hook_for :orm + end + end +end |