I've read all sequel's docs, but I could not find a way to create a database, assuming that I can do that.
I'm running a rake task as such:
require 'rubygems'
require 'bundler/setup'
require 'pg'
require 'sequel'
require 'yaml'
require 'erb'
namespace :db do
task :connect do
end
task :create => :connect do
puts db_config
Sequel.connect(db_config['production']){ |db|
db.create_table :user do
primary_key :id
String :name
String :email
end
user = db[:user]
user.insert(:name => 'Roland', :email => '[email protected]')
}
end
task :drop => :connect do
end
end
def db_config
YAML.load(ERB.new(File.read('config/database.yml')).result)
end
But obviously that will not create the database if it does not exist, so I am a bit unsure what I can do about it. When I run the task I get:
PG::ConnectionBad: FATAL: database "pulsr" does not exist
And the database.yml
file:
production: &production
adapter: postgres
host: localhost
encoding: unicode
database: pulsr
username:
password:
development:
<<: *production
Does anyone know what I can do to create the database? Will that be a manual process as starting the postgresql server?
You can do this with Sequel without having to resort to command line tools, which may not be available to you on every system on which your script is used.
Typically you will specify the 'postgres' database in order to fulfill the 'database' parameter requirement.
production = db_config['production']
Sequel.connect(production.merge('database' => 'postgres')) do |db|
db.execute "DROP DATABASE IF EXISTS #{production['database']}"
db.execute "CREATE DATABASE #{production['database']}"
end
Sequel.connect(production) do |db|
# ...
end
This is how ActiveRecord does it, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With