Created
February 16, 2011 20:42
-
-
Save jeffrafter/830153 to your computer and use it in GitHub Desktop.
Working with ActiveRecord in Sinata
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
class CreateUsers < ActiveRecord::Migration | |
def self.up | |
create_table(:users) do |t| | |
t.string :phone_number | |
t.integer :party_on | |
t.integer :last_call_user_id | |
# ... | |
t.timestamps | |
end | |
add_index :users, [:phone_number] | |
end | |
def self.down | |
drop_table :users | |
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
APPLICATION_ROOT = File.expand_path(File.dirname(__FILE__)) | |
$:.unshift(File.join(APPLICATION_ROOT, 'lib')) | |
Dir[File.join(APPLICATION_ROOT, 'models', '**', '*')].each {|f| require f } | |
configure do | |
ActiveRecord::Base.establish_connection(YAML::load_file("config/database.yml")["application"]) | |
# ... | |
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
application: | |
adapter: sqlite3 | |
database: db/development.sqlite3 | |
pool: 5 | |
timeout: 5000 |
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 'active_support' | |
require 'active_record' | |
require 'rake' | |
require 'yaml' | |
task :default => :setup | |
task :load do | |
# Change application as needed... | |
$application = YAML::load_file("config/database.yml")['application'] | |
end | |
desc "Setup database" | |
task :setup => :load do | |
create($application) | |
migrate($application) | |
end | |
desc "Do migrations" | |
task :migrate => :load do | |
migrate($application) | |
end | |
def create(config) | |
if config['adapter'] =~ /sqlite/ | |
if File.exist?(config['database']) | |
$stderr.puts "#{config['database']} already exists" | |
else | |
begin | |
# Create the SQLite database | |
ActiveRecord::Base.establish_connection(config) | |
ActiveRecord::Base.connection | |
rescue | |
$stderr.puts $!, *($!.backtrace) | |
$stderr.puts "Couldn't create database for #{config.inspect}" | |
end | |
end | |
return # Skip the else clause of begin/rescue | |
elsif config['adapter'] =~ /mysql/ | |
# Try to connect | |
connection = ActiveRecord::Base.establish_connection(config) rescue nil | |
$stderr.puts "#{config['database']} already exists" if connection | |
return ActiveRecord::Base.connection if connection | |
# Nope, lets create | |
@charset = ENV['CHARSET'] || 'utf8' | |
@collation = ENV['COLLATION'] || 'utf8_general_ci' | |
begin | |
ActiveRecord::Base.establish_connection(config.merge('database' => nil)) | |
ActiveRecord::Base.connection.create_database(config['database'], :charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)) | |
ActiveRecord::Base.establish_connection(config) | |
rescue | |
$stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, | |
collation: #{config['collation'] || @collation}" | |
end | |
end | |
end | |
def migrate(config) | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Base.establish_connection(config) | |
ActiveRecord::Migrator.up "db/migrate/" | |
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
require 'rubygems' | |
require 'active_support' | |
require 'active_record' | |
class User < ActiveRecord::Base | |
scope :partying, where('party_on = 1') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment