Skip to content

Commit

Permalink
r3190@asus: jeremy | 2005-11-19 20:20:48 -0800
Browse files Browse the repository at this point in the history
 Apply [3098] to stable.  Introducing the session_migration generator.  Creates an add_session_table migration.  Closes #2958.


git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/stable@3099 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Nov 20, 2005
1 parent bed4fec commit 3f2541b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 53 deletions.
2 changes: 2 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Introducing the session_migration generator. Creates an add_session_table migration. #2958 [Rick Olson]

* Update to Prototype 1.4.0_rc4. Closes #2943 (old Array.prototype.reverse behavior can be obtained by passing false as an argument). [Sam Stephenson]

* Eliminate nil from newly generated logfiles. #2927 [Blair Zajac <blair@orcaware.com>]
Expand Down
48 changes: 45 additions & 3 deletions railties/lib/rails_generator/commands.rb
Expand Up @@ -55,6 +55,30 @@ def class_collisions(*class_names)
def readme(*args)
end

protected
def existing_migrations(file_name)
Dir.glob("db/migrate/[0-9]*_#{file_name}.rb")
end

def migration_exists?(file_name)
not existing_migrations(file_name).empty?
end

def current_migration_number
Dir.glob('db/migrate/[0-9]*.rb').inject(0) do |max, file_path|
n = File.basename(file_path).split('_', 2).first.to_i
if n > max then n else max end
end
end

def next_migration_number
current_migration_number + 1
end

def next_migration_string(padding = 3)
"%.#{padding}d" % next_migration_number
end

private
# Ask the user interactively whether to force collision.
def force_file_collision?(destination)
Expand Down Expand Up @@ -84,10 +108,10 @@ def render_template_part(template_options)
begin_mark = template_part_mark(template_options[:begin_mark], template_options[:mark_id])
end_mark = template_part_mark(template_options[:end_mark], template_options[:mark_id])
begin_mark + rendered_part + end_mark
end
end

def template_part_mark(name, id)
"<!--[#{name}:#{id}]-->\n"
def template_part_mark(name, id)
"<!--[#{name}:#{id}]-->\n"
end
end

Expand Down Expand Up @@ -279,6 +303,12 @@ def readme(*relative_sources)
end
end

# When creating a migration, it knows to find the first available file in db/migrate and use the migration.rb template.
def migration_template(relative_source, relative_destination, template_options = {})
raise "Another migration is already named #{file_name}: #{existing_migrations(file_name).first}" if migration_exists?(file_name)
template(relative_source, "#{relative_destination}/#{next_migration_string}_#{file_name}.rb", template_options)
end

private
# Raise a usage error with an informative WordNet suggestion.
# Thanks to Florian Gross (flgr).
Expand Down Expand Up @@ -385,6 +415,14 @@ def directory(relative_path)
def complex_template(*args)
# nothing should be done here
end

# When deleting a migration, it knows to delete every file named "[0-9]*_#{file_name}".
def migration_template(relative_source, relative_destination, template_options = {})
raise "There is no migration named #{file_name}" unless migration_exists?(file_name)
existing_migrations(file_name).each do |file_path|
file(relative_source, file_path, template_options)
end
end
end


Expand Down Expand Up @@ -417,6 +455,10 @@ def directory(relative_path)
def readme(*args)
logger.readme args.join(', ')
end

def migration_template(relative_source, relative_destination, options = {})
logger.migration_template file_name
end
end

# Update generator's action manifest.
Expand Down
Expand Up @@ -5,54 +5,4 @@ def manifest
m.migration_template 'migration.rb', 'db/migrate'
end
end

protected
def existing_migrations(file_name)
Dir.glob("db/migrate/[0-9]*_#{file_name}.rb")
end

def migration_exists?(file_name)
not existing_migrations(file_name).empty?
end

def current_migration_number
Dir.glob('db/migrate/[0-9]*.rb').inject(0) do |max, file_path|
n = File.basename(file_path).split('_', 2).first.to_i
if n > max then n else max end
end
end

def next_migration_number
current_migration_number + 1
end

def next_migration_string(padding = 3)
"%.#{padding}d" % next_migration_number
end
end

module Rails::Generator::Commands
# When creating, it knows to find the first available file in db/migrate and use the migration.rb template.
class Create
def migration_template(relative_source, relative_destination, template_options = {})
raise "Another migration is already named #{file_name}: #{existing_migrations(file_name).first}" if migration_exists?(file_name)
template(relative_source, "#{relative_destination}/#{next_migration_string}_#{file_name}.rb", template_options)
end
end

# When deleting, it knows to delete every file named "[0-9]*_#{file_name}".
class Destroy
def migration_template(relative_source, relative_destination, template_options = {})
raise "There is no migration named #{file_name}" unless migration_exists?(file_name)
existing_migrations(file_name).each do |file_path|
file(relative_source, file_path, template_options)
end
end
end

class List
def migration_template(relative_source, relative_destination, options = {})
logger.migration_template file_name
end
end
end
@@ -0,0 +1,15 @@
Description:
The session table migration generator creates a migration for adding a session table
used by CGI::Session::ActiveRecordStore.

The generator takes a migration name as its argument. The migration name may be
given in CamelCase or under_score.

The generator creates a migration class in db/migrate prefixed by its number
in the queue.

Example:
./script/generate session_migration AddSessionTable

With 4 existing migrations, this will create an AddSessionTable migration in the
file db/migrate/5_add_session_table.rb
@@ -0,0 +1,13 @@
class SessionMigrationGenerator < Rails::Generator::NamedBase
def initialize(runtime_args, runtime_options = {})
runtime_args << 'add_session_table' if runtime_args.empty?
super
end

def manifest
record do |m|
m.directory 'db/migrate'
m.migration_template 'migration.rb', 'db/migrate'
end
end
end
@@ -0,0 +1,15 @@
class <%= class_name %> < ActiveRecord::Migration
def self.up
create_table :sessions do |t|
t.column :session_id, :string
t.column :data, :text
t.column :updated_at, :datetime
end

add_index :sessions, :session_id
end

def self.down
drop_table :sessions
end
end

0 comments on commit 3f2541b

Please sign in to comment.