forked from brasten/sequel-rails
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jesse Farmer
committed
Jul 28, 2010
1 parent
0ad2955
commit c2bd89a
Showing
2 changed files
with
120 additions
and
85 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,4 @@ def migrate_down!(version=nil) | |
|
||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,147 @@ | ||
require 'sequel-rails/setup' | ||
require 'sequel-rails/storage' | ||
|
||
# TODO: DRY these up | ||
namespace :db do | ||
|
||
task :load_models => :environment do | ||
FileList["app/models/**/*.rb"].each { |model| load model } | ||
end | ||
|
||
desc 'Create the database, load the schema, and initialize with the seed data' | ||
task :setup => [ 'db:create', 'db:migrate', 'db:seed' ] | ||
|
||
namespace :test do | ||
task :prepare do | ||
Rails.env = "test" | ||
Rake::Task["db:setup"].invoke | ||
namespace :schema do | ||
desc "Create a db/schema.rb file that can be portably used against any DB supported by Sequel" | ||
task :dump do | ||
Sequel.extension :schema_dumper | ||
db = Sequel.connect(Rails.configuration.database_configuration[Rails.env]) | ||
File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file| | ||
file.write(db.dump_schema_migration) | ||
end | ||
Rake::Task["db:schema:dump"].reenable | ||
end | ||
|
||
desc "Load a schema.rb file into the database" | ||
task :load do | ||
file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb" | ||
if File.exists?(file) | ||
load(file) | ||
else | ||
abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/boot.rb to limit the frameworks that will be loaded} | ||
end | ||
end | ||
end | ||
|
||
namespace :create do | ||
desc 'Create all the local databases defined in config/database.yml' | ||
task :all => :environment do | ||
Rails::Sequel.storage.create_all | ||
task :all do | ||
Rails.configuration.database_configuration.each_value do |config| | ||
next unless config['database'] | ||
database = config.delete('database') | ||
DB = Sequel.connect(config) | ||
default_options = "DEFAULT CHARSET utf8 COLLATE utf8_unicode_ci" | ||
puts "Creating database \"#{config['database']}\" if it doesn't already exist" | ||
DB.run "CREATE DATABASE IF NOT EXISTS `#{config['database']}` /*!40100 #{default_options} */" | ||
end | ||
end | ||
end | ||
|
||
desc "Create the database(s) defined in config/database.yml for the current Rails.env - also creates the test database(s) if Rails.env.development?" | ||
task :create => :environment do | ||
Rails::Sequel.storage.create_environment(Rails::Sequel.configuration.environments[Rails.env]) | ||
if Rails.env.development? && Rails::Sequel.configuration.environments['test'] | ||
Rails::Sequel.storage.create_environment(Rails::Sequel.configuration.environments['test']) | ||
desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?" | ||
task :create do | ||
connect_options = Rails.configuration.database_configuration[Rails.env] | ||
connect_options.delete('database') | ||
DB = Sequel.connect(connect_options) | ||
default_options = "DEFAULT CHARSET utf8 COLLATE utf8_unicode_ci" | ||
puts "Creating database \"#{Rails.configuration.database_configuration[Rails.env]['database']}\" if it doesn't already exist" | ||
DB.run "CREATE DATABASE IF NOT EXISTS `#{Rails.configuration.database_configuration[Rails.env]['database']}` /*!40100 #{default_options} */" | ||
if Rails.env.development? && Rails.configuration.database_configuration['test'] | ||
puts "Creating database \"#{Rails.configuration.database_configuration['test']['database']}\" if it doesn't already exist" | ||
DB.run "CREATE DATABASE IF NOT EXISTS `#{Rails.configuration.database_configuration['test']['database']}` /*!40100 #{default_options} */" | ||
end | ||
end | ||
|
||
namespace :drop do | ||
desc 'Drop all the local databases defined in config/database.yml' | ||
task :all => :environment do | ||
Rails::Sequel.storage.drop_all | ||
desc 'Drops all the local databases defined in config/database.yml' | ||
task :all do | ||
Rails.configuration.database_configuration.each_value do |config| | ||
next unless config['database'] | ||
database = config.delete('database') | ||
DB = Sequel.connect(config) | ||
puts "Dropping database #{database} if it exists" | ||
DB.run "DROP DATABASE IF EXISTS `#{database}`" | ||
end | ||
end | ||
end | ||
|
||
desc "Drops the database(s) for the current Rails.env - also drops the test database(s) if Rails.env.development?" | ||
task :drop => :environment do | ||
Rails::Sequel.storage.drop_environment(Rails::Sequel.configuration.environments[Rails.env]) | ||
if Rails.env.development? && Rails::Sequel.configuration.environments['test'] | ||
Rails::Sequel.storage.drop_environment(Rails::Sequel.configuration.environments['test']) | ||
end | ||
end | ||
|
||
desc 'Load the seed data from db/seeds.rb' | ||
task :seed => :environment do | ||
seed_file = File.join(Rails.root, 'db', 'seeds.rb') | ||
load(seed_file) if File.exist?(seed_file) | ||
|
||
desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?" | ||
task :drop do | ||
connect_options = Rails.configuration.database_configuration[Rails.env] | ||
connect_options.delete('database') | ||
DB = Sequel.connect(connect_options) | ||
|
||
puts "Dropping database #{Rails.configuration.database_configuration[Rails.env]['database']} if it exists" | ||
DB.run "DROP DATABASE IF EXISTS `#{Rails.configuration.database_configuration[Rails.env]['database']}`" | ||
end | ||
|
||
namespace :migrate do | ||
task :load => :environment do | ||
# FileList['db/migrate/*.rb'].each do |migration| | ||
# load migration | ||
# end | ||
task :load do | ||
require File.expand_path('../../sequel_migration', __FILE__) | ||
end | ||
|
||
desc 'Migrate up using migrations' | ||
task :up, :version, :needs => :load do |t, args| | ||
require 'sequel-rails/migrations' | ||
::Rails::Sequel::Migrations.migrate_up!(args[:version]) | ||
desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.' | ||
task :redo => :load do | ||
if ENV["VERSION"] | ||
Rake::Task["db:migrate:down"].invoke | ||
Rake::Task["db:migrate:up"].invoke | ||
else | ||
Rake::Task["db:rollback"].invoke | ||
Rake::Task["db:migrate"].invoke | ||
end | ||
end | ||
|
||
desc 'Migrate down using migrations' | ||
task :down, :version, :needs => :load do |t, args| | ||
require 'sequel-rails/migrations' | ||
::Rails::Sequel::Migrations.migrate_down!(args[:version]) | ||
end | ||
end | ||
|
||
## | ||
# TODO: deal with this at some point | ||
# | ||
# namespace :schema do | ||
# desc 'Create a db/schema.rb file that can be portably used against any DB supported by Sequel' | ||
# task :dump => :environment do | ||
# File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file| | ||
# file.puts(::Sequel::Model.db.schema()) | ||
# end | ||
# end | ||
# | ||
# desc 'Load a schema.rb file into the database' | ||
# task :load => :environment do | ||
# | ||
# end | ||
# end | ||
desc 'Resets your database using your migrations for the current environment' | ||
task :reset => ["db:drop", "db:create", "db:migrate"] | ||
|
||
desc 'Migrate the database to the latest version' | ||
task :migrate => 'db:migrate:up' | ||
|
||
namespace :sessions do | ||
desc "Creates the sessions table for SequelStore" | ||
task :create => :environment do | ||
require 'sequel-rails/session_store' | ||
Rails::Sequel::SessionStore::Session.auto_migrate! | ||
puts "Created '#{::Rails::Sequel.configuration.environments[Rails.env]['database']}.sessions'" | ||
desc 'Runs the "up" for a given migration VERSION.' | ||
task :up => :load do | ||
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil | ||
raise "VERSION is required" unless version | ||
Sequel::Migrator.run(:up, "db/migrate/", version) | ||
Rake::Task["db:schema:dump"].invoke | ||
end | ||
|
||
desc "Clear the sessions table for SequelStore" | ||
task :clear => :environment do | ||
require 'sequel-rails/session_store' | ||
Rails::Sequel::SessionStore::Session.delete() | ||
puts "Deleted entries from '#{::Rails::Sequel.configuration.environments[Rails.env]['database']}.sessions'" | ||
desc 'Runs the "down" for a given migration VERSION.' | ||
task :down => :load do | ||
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil | ||
raise "VERSION is required" unless version | ||
Sequel::Migrator.run(:down, "db/migrate/", version) | ||
Rake::Task["db:schema:dump"].invoke | ||
end | ||
end | ||
|
||
desc 'Migrate the database to the latest version' | ||
task :migrate => :'migrate:load' do | ||
Sequel::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil) | ||
Rake::Task["db:schema:dump"].invoke | ||
end | ||
|
||
desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n' | ||
task :rollback => :'migrate:load' do | ||
step = ENV['STEP'] ? ENV['STEP'].to_i : 1 | ||
Sequel::Migrator.rollback('db/migrate/', step) | ||
Rake::Task["db:schema:dump"].invoke | ||
end | ||
|
||
desc 'Pushes the schema to the next version. Specify the number of steps with STEP=n' | ||
task :forward => :'migrate:load' do | ||
step = ENV['STEP'] ? ENV['STEP'].to_i : 1 | ||
Sequel::Migrator.forward('db/migrate/', step) | ||
Rake::Task["db:schema:dump"].invoke | ||
end | ||
|
||
desc 'Load the seed data from db/seeds.rb' | ||
task :seed => :environment do | ||
seed_file = File.join(Rails.root, 'db', 'seeds.rb') | ||
load(seed_file) if File.exist?(seed_file) | ||
end | ||
|
||
desc 'Create the database, load the schema, and initialize with the seed data' | ||
task :setup => [ 'db:create', 'db:migrate', 'db:seed' ] | ||
|
||
desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.' | ||
task :reset => [ 'db:drop', 'db:setup' ] | ||
|
||
namespace :test do | ||
task :prepare => ['db:reset'] | ||
end | ||
end |