Skip to content

Instantly share code, notes, and snippets.

@jnunemaker jnunemaker/db.rake
Created Mar 4, 2013

Embed
What would you like to do?
db.rake tasks for cassanity + migrations.
# ******************************************************************************
# 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
@dr3s

This comment has been minimized.

Copy link

dr3s commented Dec 16, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.