Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

fixed session_store.rb and added rake task. #8

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.rdoc
Expand Up @@ -11,17 +11,19 @@ Using sequel with rails3 requires a couple minor changes.
First, add the following to your Gemfile: First, add the following to your Gemfile:


gem 'sequel-rails' gem 'sequel-rails'

... be sure to run "bundle install" if needed! ... be sure to run "bundle install" if needed!


Secondly, you'll need to require "sequel-rails/railtie" in your config/application.rb file, and not require activerecord. The top of your config/application.rb will probably look something like: Secondly, you'll need to require "sequel-rails/railtie" in your config/application.rb file, and not require activerecord. The top of your config/application.rb will probably look something like:

# require 'rails/all' # require 'rails/all'

# Instead of 'rails/all', require these: # Instead of 'rails/all', require these:
require "action_controller/railtie" require "action_controller/railtie"
require "sequel-rails/railtie" require "sequel-rails/railtie"
require "action_mailer/railtie" require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"




After those changes, you should be good to go! After those changes, you should be good to go!
Expand Down
44 changes: 31 additions & 13 deletions lib/sequel-rails/railties/database.rake
Expand Up @@ -10,12 +10,12 @@ namespace :db do
end end
Rake::Task["db:schema:dump"].reenable Rake::Task["db:schema:dump"].reenable
end end

desc "Load a schema.rb file into the database" desc "Load a schema.rb file into the database"
task :load, :needs => :environment do task :load, :needs => :environment do
require 'sequel-rails/storage' require 'sequel-rails/storage'
Rails::Sequel::Storage.new(Rails.env).create Rails::Sequel::Storage.new(Rails.env).create

file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb" file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
if File.exists?(file) if File.exists?(file)
load(file) load(file)
Expand All @@ -36,30 +36,30 @@ namespace :db do
desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?" desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?"
task :create, :env, :needs => :environment do |t, args| task :create, :env, :needs => :environment do |t, args|
args.with_defaults(:env => Rails.env) args.with_defaults(:env => Rails.env)

require 'sequel-rails/storage' require 'sequel-rails/storage'
Rails::Sequel::Storage.new(args.env).create Rails::Sequel::Storage.new(args.env).create

if Rails.env.development? && Rails.configuration.database_configuration['test'] if Rails.env.development? && Rails.configuration.database_configuration['test']
Rails::Sequel::Storage.new('test').create Rails::Sequel::Storage.new('test').create
end end
end end

namespace :drop do namespace :drop do
desc 'Drops all the local databases defined in config/database.yml' desc 'Drops all the local databases defined in config/database.yml'
task :all, :needs => :environment do task :all, :needs => :environment do
require 'sequel-rails/storage' require 'sequel-rails/storage'
Rails::Sequel::Storage.drop_all Rails::Sequel::Storage.drop_all
end end
end end

desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?" desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?"
task :drop, :env, :needs => :environment do |t, args| task :drop, :env, :needs => :environment do |t, args|
args.with_defaults(:env => Rails.env) args.with_defaults(:env => Rails.env)

require 'sequel-rails/storage' require 'sequel-rails/storage'
Rails::Sequel::Storage.new(args.env).drop Rails::Sequel::Storage.new(args.env).drop

if Rails.env.development? && Rails.configuration.database_configuration['test'] if Rails.env.development? && Rails.configuration.database_configuration['test']
Rails::Sequel::Storage.new('test').drop Rails::Sequel::Storage.new('test').drop
end end
Expand Down Expand Up @@ -101,7 +101,7 @@ namespace :db do
Rake::Task["db:schema:dump"].invoke if Rails.env != 'test' Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
end end
end end

desc 'Migrate the database to the latest version' desc 'Migrate the database to the latest version'
task :migrate => :'migrate:load' do task :migrate => :'migrate:load' do
Rails::Sequel::Migrations.migrate_up!(ENV["VERSION"] ? ENV["VERSION"].to_i : nil) Rails::Sequel::Migrations.migrate_up!(ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
Expand All @@ -121,19 +121,19 @@ namespace :db do
Sequel::Migrator.forward('db/migrate/', step) Sequel::Migrator.forward('db/migrate/', step)
Rake::Task["db:schema:dump"].invoke if Rails.env != 'test' Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
end end

desc 'Load the seed data from db/seeds.rb' desc 'Load the seed data from db/seeds.rb'
task :seed => :environment do task :seed => :environment do
seed_file = File.join(Rails.root, 'db', 'seeds.rb') seed_file = File.join(Rails.root, 'db', 'seeds.rb')
load(seed_file) if File.exist?(seed_file) load(seed_file) if File.exist?(seed_file)
end end

desc 'Create the database, load the schema, and initialize with the seed data' desc 'Create the database, load the schema, and initialize with the seed data'
task :setup => [ 'db:create', 'db:migrate', 'db:seed' ] task :setup => [ 'db:create', 'db:migrate', 'db:seed' ]

desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.' desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
task :reset => [ 'db:drop', 'db:setup' ] task :reset => [ 'db:drop', 'db:setup' ]

namespace :test do namespace :test do
task :prepare do task :prepare do
Rails.env = 'test' Rails.env = 'test'
Expand All @@ -143,6 +143,24 @@ namespace :db do
end end
end end
end end

namespace :sessions do
desc "Creates the sessions table for SequelStore"
task :create => :environment do
require 'sequel-rails/session_store'
Rails::Sequel::SessionStore::Session.auto_migrate!
database = Rails::Sequel.configuration.environments[Rails.env]['database']
puts "Created '#{database}.sessions'"
end

desc "Clear the sessions table for SequelStore"
task :clear => :environment do
require 'sequel-rails/session_store'
Rails::Sequel::SessionStore::Session.delete
database = Rails::Sequel.configuration.environments[Rails.env]['database']
puts "Deleted entries from '#{database}.sessions'"
end
end
end end


task 'test:prepare' => 'db:test:prepare' task 'test:prepare' => 'db:test:prepare'
55 changes: 42 additions & 13 deletions lib/sequel-rails/session_store.rb
Expand Up @@ -15,30 +15,55 @@ class Session < ::Sequel::Model
# property :updated_at, DateTime, :required => false, :index => true # property :updated_at, DateTime, :required => false, :index => true


class << self class << self

def auto_migrate! def auto_migrate!
self.db.create_table :sessions do self.db.create_table :sessions do
primary_key :id primary_key :id
column :session_id, String, column :session_id, String,
:null => false, :null => false,
:unique => true, :unique => true,
:index => true :index => true


column :data, :text, column :data, :text,
:null => false :null => false

column :updated_at, DateTime, column :updated_at, DateTime,
:null => true, :null => true,
:index => true :index => true
end end
end end


def marshal(data)
ActiveSupport::Base64.encode64(Marshal.dump(data)) if data
end

def unmarshal(data)
Marshal.load(ActiveSupport::Base64.decode64(data)) if data
end
end end


def self.name def self.name
'session' 'session'
end end


def data
@data ||= self.class.unmarshal(self[:data]) || {}
end
attr_writer :data

def marshal_data!
return false unless @data
self[:data] = self.class.marshal(data)
end

def before_save
marshal_data!
super
end

def loaded?
@data
end
end end


SESSION_RECORD_KEY = 'rack.session.record'.freeze SESSION_RECORD_KEY = 'rack.session.record'.freeze
Expand All @@ -58,8 +83,8 @@ def get_session(env, sid)
def set_session(env, sid, session_data) def set_session(env, sid, session_data)
session = get_session_resource(env, sid) session = get_session_resource(env, sid)
session.data = session_data session.data = session_data
session.updated_at = Time.now if session.dirty? session.updated_at = Time.now if session.modified?
session.save session.save ? sid : false
end end


def get_session_resource(env, sid) def get_session_resource(env, sid)
Expand All @@ -72,10 +97,14 @@ def get_session_resource(env, sid)


def find_session(sid) def find_session(sid)
klass = self.class.session_class klass = self.class.session_class

klass.where(:session_id => sid).first || klass.new(:session_id => sid) klass.where(:session_id => sid).first || klass.new(:session_id => sid)
end end


def destroy(env)
find_session(current_session_id(env)).destroy
end

end end


end end
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel-rails/setup.rb
Expand Up @@ -12,7 +12,7 @@ module Sequel
def self.setup(environment) def self.setup(environment)
puts "[sequel] Setting up the #{environment.inspect} environment:" puts "[sequel] Setting up the #{environment.inspect} environment:"


::Sequel.connect({:logger => configuration.logger}.merge(::Rails::Sequel.configuration.environment_for(environment.to_s))) ::Sequel.connect({:logger => configuration.logger, :sql_log_level => :debug}.merge(::Rails::Sequel.configuration.environment_for(environment.to_s)))
end end


end end
Expand Down