Guest User

Untitled

a guest
Jul 24th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #! /usr/bin/env ruby
  2. # ^ Change this shebang to jruby.
  3. # It's only ruby so that gist can correctly format this gist.
  4.  
  5. require 'rubygems'
  6. require 'sequel'
  7.  
  8. Sequel.extension :migration
  9.  
  10. Sequel::Model.db = Sequel.connect('jdbc:postgresql://localhost/postgres?user=postgres')
  11. # Sequel::Model.db = Sequel.connect('jdbc:postgresql://localhost/test?user=test')
  12.  
  13. module Sequel
  14. class Migration
  15. def self.up
  16. apply(Sequel::Model.db, :up)
  17. end
  18. def self.down
  19. apply(Sequel::Model.db, :down)
  20. end
  21. end
  22. end
  23.  
  24. module Sequel
  25. class Model
  26. # Returns the list of Model descendants.
  27. def self.descendants
  28. @descendants ||= []
  29. end
  30.  
  31. # Adds the new model class to the list of Model descendants.
  32. def self.inherited(base)
  33. descendants << base
  34. super
  35. end
  36. end
  37. end
  38.  
  39. class TestSetup < Sequel::Migration
  40. def up
  41. create_table! :test do
  42. primary_key :id
  43. String :name
  44. String :value
  45. String :object_type, :null => false
  46. end unless table_exists? :test
  47. Test.plugin :class_table_inheritance, :key => :object_type
  48. end
  49.  
  50. def down
  51. drop_table :test if table_exists? :test
  52. end
  53. end
  54.  
  55. class Test < Sequel::Model
  56. set_dataset :test
  57. end
  58.  
  59. if Sequel::Model.db["select count(*) > 0 from pg_catalog.pg_database where datname = 'test';"].first.values.first
  60. Sequel::Model.db.run "drop database test"
  61. end
  62. if Sequel::Model.db["select count(*) > 0 from pg_shadow where usename = 'test';"].first.values.first
  63. Sequel::Model.db.run "drop user test"
  64. end
  65.  
  66. Sequel::Model.db.run "create database test"
  67. Sequel::Model.db.run "create user test"
  68. Sequel::Model.db = Sequel.connect('jdbc:postgresql://localhost/test?user=test')
  69. Sequel::Model.descendants.each { |model| model.db = Sequel::Model.db }
  70.  
  71. TestSetup.down
  72. TestSetup.up
  73.  
  74. a = Test.find_or_create(:name => "Hello world!")
  75. puts "#{a.inspect}"
  76.  
  77. a.value = "Hello universe!"
  78. a.save
  79.  
  80. puts "#{a.inspect}"
Add Comment
Please, Sign In to add comment