Created
March 4, 2013 21:58
-
-
Save jnunemaker/5086063 to your computer and use it in GitHub Desktop.
db.rake tasks for cassanity + migrations.
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
# ****************************************************************************** | |
# NOTE: You will need to replace keyspace and migrations_path in the rake tasks | |
# below with whatever makes sense for your application. You will also need a | |
# task named environment that loads whatever is need to make cassanity available. | |
# ****************************************************************************** | |
namespace :db do | |
desc "Run any pending migrations." | |
task :migrate => :environment do | |
require 'cassanity/migrator' | |
keyspace = Cassanity::Client.new[:my_app] | |
migrations_path = Rails.root.join('db', 'migrate') | |
migrator = Cassanity::Migrator.new(keyspace, migrations_path) | |
if ENV["VERSION"] | |
version = ENV["VERSION"].to_i | |
direction = ENV.fetch('DIRECTION', :up).to_sym | |
migrator.migrate_to(version, direction) | |
else | |
migrator.migrate | |
end | |
end | |
desc "List pending migrations." | |
task :pending => :environment do | |
require 'cassanity/migrator' | |
keyspace = Cassanity::Client.new[:my_app] | |
migrations_path = Rails.root.join('db', 'migrate') | |
migrator = Cassanity::Migrator.new(keyspace, migrations_path) | |
pending = migrator.pending_migrations | |
display_migrations pending | |
end | |
desc "List all migrations." | |
task :migrations => :environment do | |
require 'cassanity/migrator' | |
keyspace = Cassanity::Client.new[:my_app] | |
migrations_path = Rails.root.join('db', 'migrate') | |
migrator = Cassanity::Migrator.new(keyspace, migrations_path) | |
display_migrations migrator.migrations | |
end | |
def display_migrations(migrations) | |
max_size = migrations.map(&:name).map(&:size).max + 1 | |
migrations.each do |migration| | |
display_migration migration, size: max_size | |
end | |
end | |
def display_migration(migration, options = {}) | |
size = options[:size] || migration.name.size | |
puts "- #{migration.name.ljust(size)} #{migration.version}" | |
end | |
desc "Create the keyspace" | |
task :create => :environment do | |
keyspace = Cassanity::Client.new[:my_app] | |
unless keyspace.exists? | |
puts "Creating keyspace #{keyspace.name}" | |
keyspace.create | |
end | |
end | |
desc "Drop the keyspace" | |
task :drop => :environment do | |
keyspace = Cassanity::Client.new[:my_app] | |
if keyspace.exists? | |
puts "Dropping keyspace #{keyspace.name}" | |
keyspace.drop | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think it would be good to use database.yml or some config to pass keyspace creation arguments. RF and strategy are highly dependent upon environment.