Created
November 13, 2009 09:12
-
-
Save glejeune/233691 to your computer and use it in GitHub Desktop.
Sequel without... sequel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sq' | |
class CreateUsers < SQ::Schema 1.0 | |
def up | |
create_table :users do | |
String :login | |
String :password | |
end | |
end | |
def down | |
drop_table :users | |
end | |
end | |
class CreatePosts < SQ::Schema 1.1 | |
def up | |
create_table :posts do | |
String :title | |
String :body | |
end | |
end | |
def down | |
drop_table :posts | |
end | |
end | |
class User < SQ::Model | |
end | |
SQ.db_connect( "test.yml", "test.log" ) | |
User.insert(:login => 'Muriel', :password => "leiruM") | |
User.insert(:login => 'Greg', :password => 'gerG') | |
User.each do |u| | |
puts u[:login] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require 'sequel' | |
Sequel.extension :migration | |
Sequel.extension :inflector | |
rescue LoadError => e | |
raise LoadError, "Sequel ne doit pas être installé : #{e.message}" | |
end | |
class Object | |
def meta_def(m,&b) | |
(class<<self;self end).send(:define_method,m,&b) | |
end | |
end | |
class Hash | |
def keys_to_sym | |
self.each do |k, v| | |
self.delete(k) | |
self[k.to_s.to_sym] = v | |
end | |
end | |
end | |
module SQ | |
class Model | |
def self.method_missing( name, *args, &block ) | |
if block_given? | |
SQ::db[self.to_s.tableize.to_sym].__send__(name.to_sym, *args, &block) | |
else | |
SQ::db[self.to_s.tableize.to_sym].__send__(name.to_sym, *args) | |
end | |
end | |
end | |
class << self | |
def db | |
@db ||= Sequel.connect(@dbconfig) | |
end | |
def Schema( n ) | |
@final = [n, @final.to_f].max | |
m = (@migrations ||= []) | |
Class.new(Sequel::Migration) do | |
meta_def(:version) { n } | |
meta_def(:inherited) { |k| m << k } | |
end | |
end | |
def db_connect( dbfile, logfile ) | |
@dbconfig = YAML::load(File.open(dbfile)).keys_to_sym | |
@dbconfig[:adapter] = "sqlite" if @dbconfig[:adapter] == "sqlite3" | |
version = dbconfig.delete(:schema_version) { |_| @final } | |
if @migrations | |
SQ::db.create_table? :schema_table do | |
Float :version | |
end | |
si = SQ::db[:schema_table].first || (SQ::db[:schema_table].insert(:version => 0); {:version => 0}) | |
@migrations.each do |k| | |
k.apply(SQ::db, :up) if si[:version] < k.version and k.version <= version | |
k.apply(SQ::db, :down) if si[:version] >= k.version and k.version > version | |
end | |
SQ::db[:schema_table].where(:version => si[:version]).update(:version => version) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
adapter: sqlite3 | |
database: test.db | |
schema_version: 1.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment