I use sequel in sinatra apps and its pretty easy to use a database.yml
file with it.  You can pass most of the same arguments that Rails uses
to Sequel.connect.

For example:

module MyApp
  class << self
    def env
      ENV['RACK_ENV'] || 'development'
    end
    def root
      File.expand_path(File.dirname(__FILE__))
    end
    def config
      config = YAML.load_file(File.join(root,'config','database.yml'))
[env]
      # I like to use logger to track queries
      config['logger'] = Logger.new(File.join(root,'log',"sequel_#
{env}.log"))
      config
    end
    def connect
      Sequel.connect(config)
    end
  end
end

Somewhere in my library, I then call the connect method.

DB = MyApp.connect unless Object.const_defined?('DB')

There are a few things to look for.  For example, if you are using
sqlite, you may be used to having the relative path to the db dir, you
may need to change that depending on where you are calling it from and
where your db dir is.

----

As far as migrations go, I built a quick rake task to handle those.
Although it sounds like using the sequel command line tool might be
even easier

namespace :db do

  # this gives you
  # migrate from current to end
  # rake db:migrate
  # migrate up from version 2 to 4
  # rake db:migrate FROM=2 TO=4
  # migrate down from 4 to 2
  # rake db:migrate FROM=4 TO=2
  desc "Perform migration accepts TO and FROM variables"
  task :migrate => :init do
    Sequel::Migrator.apply(
      Sequel::Model.db,
      File.join(MyApp.root,'db','migrations'),
      ENV["TO"] ? ENV["TO"].to_i : nil,
      ENV["FROM"] ? ENV["FROM"].to_i : nil
    )
  end

  desc "Drop all tables"
  task :drop => :init do
    if MyApp.env == 'production'
      unless ENV['FORCE'] == 'true'
        puts "You must pass FORCE=true to run this in production mode"
        exit 1
      end
    end
    DB.tables.each {|table| DB.drop_table(table)}
  end

  desc "Drop all tables and re-run migrations"
  task :reset => [:init, :drop, :migrate] do
    if MyApp.env == 'production'
      unless ENV['FORCE'] == 'true'
        puts "You must pass FORCE=true to run this in production mode"
        exit 1
      end
    end
  end

  desc "Prints out current migration version"
  task :version => :init do
    begin
      version = DB[:schema_info].first[:version]
      puts "Current Migration Version: #{version}"
    rescue Sequel::DatabaseError => e
      puts e.message
      exit(1)
    end
  end

  # Assume that mylib requires sequel, establishes the connection
  # loads your models, etc...
  task :init do
    unless Object.const_defined?('MyApp')
      require File.join('somepath','mylib')
    end
  end

end

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to