Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env ruby
- # ^ Change this shebang to jruby.
- # It's only ruby so that gist can correctly format this gist.
- require 'rubygems'
- require 'sequel'
- Sequel.extension :migration
- Sequel::Model.db = Sequel.connect('jdbc:postgresql://localhost/postgres?user=postgres')
- # Sequel::Model.db = Sequel.connect('jdbc:postgresql://localhost/test?user=test')
- module Sequel
- class Migration
- def self.up
- apply(Sequel::Model.db, :up)
- end
- def self.down
- apply(Sequel::Model.db, :down)
- end
- end
- end
- module Sequel
- class Model
- # Returns the list of Model descendants.
- def self.descendants
- @descendants ||= []
- end
- # Adds the new model class to the list of Model descendants.
- def self.inherited(base)
- descendants << base
- super
- end
- end
- end
- class TestSetup < Sequel::Migration
- def up
- create_table! :test do
- primary_key :id
- String :name
- String :value
- String :object_type, :null => false
- end unless table_exists? :test
- Test.plugin :class_table_inheritance, :key => :object_type
- end
- def down
- drop_table :test if table_exists? :test
- end
- end
- class Test < Sequel::Model
- set_dataset :test
- end
- if Sequel::Model.db["select count(*) > 0 from pg_catalog.pg_database where datname = 'test';"].first.values.first
- Sequel::Model.db.run "drop database test"
- end
- if Sequel::Model.db["select count(*) > 0 from pg_shadow where usename = 'test';"].first.values.first
- Sequel::Model.db.run "drop user test"
- end
- Sequel::Model.db.run "create database test"
- Sequel::Model.db.run "create user test"
- Sequel::Model.db = Sequel.connect('jdbc:postgresql://localhost/test?user=test')
- Sequel::Model.descendants.each { |model| model.db = Sequel::Model.db }
- TestSetup.down
- TestSetup.up
- a = Test.find_or_create(:name => "Hello world!")
- puts "#{a.inspect}"
- a.value = "Hello universe!"
- a.save
- puts "#{a.inspect}"
Add Comment
Please, Sign In to add comment