Class: Rails::Sequel::Database::Postgres
- Inherits:
-
Object
- Object
- Rails::Sequel::Database::Postgres
- Defined in:
- lib/rails3_sequel/adapters/postgres.rb
Instance Method Summary collapse
- #connect(options = {}) ⇒ Object
-
#create_database(options = {}) ⇒ Object
from ActiveRecord.
- #drop_database ⇒ Object
-
#initialize(env) ⇒ Postgres
constructor
A new instance of Postgres.
Constructor Details
#initialize(env) ⇒ Postgres
Returns a new instance of Postgres.
6 7 8 9 |
# File 'lib/rails3_sequel/adapters/postgres.rb', line 6 def initialize (env) @env = env @config = (Database.configurations[@env]) end |
Instance Method Details
#connect(options = {}) ⇒ Object
11 12 13 14 |
# File 'lib/rails3_sequel/adapters/postgres.rb', line 11 def connect ( = {}) = () ::Sequel.connect(@config.merge()) end |
#create_database(options = {}) ⇒ Object
from ActiveRecord
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rails3_sequel/adapters/postgres.rb', line 17 def create_database ( = {}) db = management_connect name = @config['database'] if name.nil? or name.strip == '' then raise "No valid database specified for #{@env} environment" end = .reverse_merge(:encoding => "utf8") option_string = .sum do |key, value| case key when 'owner' " OWNER = \"#{value}\"" when 'template' " TEMPLATE = \"#{value}\"" when 'encoding' " ENCODING = '#{value}'" when 'tablespace' " TABLESPACE = \"#{value}\"" when :connection_limit " CONNECTION LIMIT = #{value}" else "" end end # TODO: quote table name db.execute "CREATE DATABASE #{name} #{option_string}" # create schema too, if the previous command succeeds, this should be fine # first connect without schema specified db = connect('schema_search_path' => nil) for schema in @config['schema_search_path'].split(',') do next if schema.strip == 'public' db.execute "CREATE SCHEMA #{schema}" end end |
#drop_database ⇒ Object
56 57 58 59 60 61 |
# File 'lib/rails3_sequel/adapters/postgres.rb', line 56 def drop_database db = management_connect name = @config['database'] db.execute "DROP DATABASE #{name}" end |