Skip to content

Commit

Permalink
Added create_session_table, drop_session_table, and purge_session_tab…
Browse files Browse the repository at this point in the history
…le as rake tasks for databases that supports migrations (MySQL, PostgreSQL, SQLite) to get a table for use with CGI::Session::ActiveRecordStore. Added configuration of session options through config.session_options in Rails::Configuration

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2219 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Sep 12, 2005
1 parent e14acca commit d25fe84
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Added create_session_table, drop_session_table, and purge_session_table as rake tasks for databases that supports migrations (MySQL, PostgreSQL, SQLite) to get a table for use with CGI::Session::ActiveRecordStore

* Added dump of schema version to the db_structure_dump task for databases that support migrations #1835 [Rick Olson]

* Fixed script/profiler for Ruby 1.8.2 #1863 [Rick Olson]
Expand Down
4 changes: 4 additions & 0 deletions railties/environments/environment.rb
Expand Up @@ -18,6 +18,10 @@
# (by default production uses INFO, the others DEBUG)
# config.log_level = Logger::DEBUG

# Use the database for sessions instead of the file system
# (create the session table with 'rake create_session_table')
# config.session_options[:database_manager] = CGI::Session::ActiveRecordStore

# See Rails::Configuration for more options
end

Expand Down
12 changes: 12 additions & 0 deletions railties/lib/initializer.rb
Expand Up @@ -39,6 +39,7 @@ def process
initialize_framework_logging
initialize_framework_views
initialize_routing
initialize_session_settings
end

def set_load_path
Expand Down Expand Up @@ -94,6 +95,11 @@ def initialize_routing
ActionController::Routing::Routes.reload
Object.const_set "Controllers", Dependencies::LoadingModule.root(*configuration.controller_paths)
end

def initialize_session_settings
return if !configuration.frameworks.include?(:action_controller)
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.merge!(configuration.session_options)
end
end

# The Configuration class holds all the parameters for the Initializer and ships with defaults that suites most
Expand All @@ -105,6 +111,7 @@ def initialize_routing
# Rails::Initializer.run(:process, config)
class Configuration
attr_accessor :frameworks, :load_paths, :log_level, :log_path, :database_configuration_file, :view_path, :controller_paths
attr_accessor :session_options

def initialize
self.frameworks = default_frameworks
Expand All @@ -113,6 +120,7 @@ def initialize
self.log_level = default_log_level
self.view_path = default_view_path
self.controller_paths = default_controller_paths
self.session_options = default_session_options
self.database_configuration_file = default_database_configuration_file
end

Expand Down Expand Up @@ -182,5 +190,9 @@ def default_view_path
def default_controller_paths
[ File.join(RAILS_ROOT, 'app', 'controllers'), File.join(RAILS_ROOT, 'components') ]
end

def default_session_options
{}
end
end
end
30 changes: 27 additions & 3 deletions railties/lib/tasks/databases.rake
Expand Up @@ -37,7 +37,7 @@ task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do
ActiveRecord::Base.connection.execute(ddl)
end
else
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
end
end

Expand All @@ -59,7 +59,7 @@ task :db_structure_dump => :environment do
`scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r`
`scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r`
else
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
end

if ActiveRecord::Base.connection.supports_migrations?
Expand Down Expand Up @@ -92,6 +92,30 @@ task :purge_test_database => :environment do
ActiveRecord::Base.connection.execute(ddl)
end
else
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
end
end

desc "Creates a sessions table for use with CGI::Session::ActiveRecordStore"
task :create_session_table => :environment do
raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations?

ActiveRecord::Base.connection.create_table :sessions do |t|
t.column :sessid, :string
t.column :data, :text
t.column :updated_at, :datetime
end

ActiveRecord::Base.connection.add_index :sessions, :sessid
end

desc "Drop the sessions table"
task :drop_session_table => :environment do
raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations?

ActiveRecord::Base.connection.drop_table :sessions
end

desc "Drop and recreate the session table (much faster than 'DELETE * FROM sessions')"
task :purge_session_table => [ :drop_session_table, :create_session_table ] do
end

0 comments on commit d25fe84

Please sign in to comment.