Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3e762d9
Use Ruby 2.4.1
bitaxis Oct 5, 2017
bdbd749
Update dependencies.
bitaxis Oct 5, 2017
fecbdc5
Version bump.
bitaxis Oct 5, 2017
0e0e99e
Add missing require.
bitaxis Oct 5, 2017
b4e954f
1. Add appraisal for Rails 5.1
bitaxis Oct 7, 2017
bbd9320
Replace 5.0.0.rc1 with 5.0 proper.
bitaxis Oct 7, 2017
f5d696b
Merge branch 'release/1.1.0'
bitaxis Oct 8, 2017
7f35635
Merge tag '1.1.0' into develop
bitaxis Oct 8, 2017
e39a4cc
Raise error by default in empty migration.
bitaxis Oct 10, 2017
f25046e
Add #execute_operation and #execute_suboperation.
bitaxis Oct 13, 2017
ed8f454
Make #execute_operation and #execute_suboperation public.
bitaxis Oct 13, 2017
ef3483f
1. Make execute helpers private.
bitaxis Oct 13, 2017
fccca0e
Merge branch 'feature/add-execute-operation-helpers' into develop
bitaxis Oct 13, 2017
fb9a17c
Version bump.
bitaxis Oct 13, 2017
7c94552
1. Add #execute_file.
bitaxis Oct 19, 2017
6e8ef21
1. Add #execute_text.
bitaxis Oct 19, 2017
0a7279a
Fix typo.
bitaxis Oct 19, 2017
7955d61
Merge branch 'release/1.2.0'
bitaxis Oct 22, 2017
3cc9d7e
Merge tag '1.2.0' into develop
bitaxis Oct 22, 2017
83672da
1. Improve !using_keyspace by adding begin ... ensure ... end.
bitaxis Apr 15, 2018
47fae60
Expose use_keyspace to migrations.
bitaxis Apr 15, 2018
4133e1f
Merge branch 'feature/improve-other-keyspace-support' into develop
bitaxis Apr 16, 2018
027f5c8
Version bump.
bitaxis Apr 16, 2018
ff81c16
1. Add comment for specific test that requires real Cassandra in orde…
bitaxis Apr 16, 2018
f3a1878
1. Ignore .byebug_history.
bitaxis Apr 17, 2018
0f519a2
Latest changes.
bitaxis Apr 17, 2018
1deade8
Remove need to have real Cassandra instance when checking that using …
bitaxis Apr 18, 2018
1ced70c
Merge branch 'release/1.3.0'
bitaxis Apr 18, 2018
80018a1
Forgot to update release date in gemspec.
bitaxis Apr 18, 2018
645d91c
Merge branch 'hotfix/1.3.1'
bitaxis Apr 18, 2018
edba016
Update cassandra_migrations.gemspec
hsgubert Apr 18, 2018
df98377
Adding @bitaxis as contributor
hsgubert Apr 18, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.gem
*.rbc
.bundle
.byebug_history
.config
.project
coverage
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.3.1
ruby-2.4.1
6 changes: 1 addition & 5 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
%w(3.2 4.0 4.2).each do |rails_version|
%w(3.2 4.0 4.2 5.0 5.1).each do |rails_version|
appraise "rails-#{rails_version}" do
gem "rails", "~> #{rails_version}.0"
end
end

appraise "rails-5.0.0.rc1" do
gem "rails", "~> 5.0.0.rc1"
end
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ Additionally, this project aims for compatiblity with Rails 3.2 and above. For t
* Henrique Gubert - @hsgubert
* Brian Sam-Bodden - @bsbodden
* Sid Tantia - @sstgithub
* Nathan Brazil - @bitaxis
* [and more...](https://github.com/hsgubert/cassandra_migrations/graphs/contributors)

## Acknowledgements
Expand Down
18 changes: 9 additions & 9 deletions cassandra_migrations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require 'cassandra_migrations/version'
Gem::Specification.new do |s|
s.name = 'cassandra_migrations'
s.version = CassandraMigrations::VERSION
s.date = '2016-06-14'
s.date = '2018-04-17'
s.license = 'MIT'
s.summary = 'Cassandra schema management for a multi-environment developer.'
s.description = 'A gem to manage Cassandra database schema for Rails. This gem offers migrations and environment specific databases out-of-the-box for Rails users.'
Expand All @@ -23,16 +23,16 @@ Gem::Specification.new do |s|
s.test_files = Dir['s/**/*_s.rb']

# s.add_dependency: Production dependencies
s.add_runtime_dependency 'cassandra-driver', '~> 3.0'
s.add_runtime_dependency 'rake', '~> 10'
s.add_runtime_dependency 'cassandra-driver', '~> 3.2'
s.add_runtime_dependency 'rake', '~> 12'
s.add_runtime_dependency 'rails', '>= 3.2'
s.add_runtime_dependency 'colorize', '~> 0.7.3'
s.add_runtime_dependency 'colorize', '~> 0.8'

# s.add_development_dependency: Development dependencies
s.add_development_dependency 'rspec', '~> 3.1', '>= 3.1.0'
s.add_development_dependency 'byebug', '~> 8.2'
s.add_development_dependency 'rspec', '~> 3.6'
s.add_development_dependency 'byebug', '~> 9.1'
s.add_development_dependency 'bundler', '~> 1.6'
s.add_development_dependency 'simplecov', '~> 0.9'
s.add_development_dependency 'coveralls', '~> 0.7'
s.add_development_dependency 'appraisal', '~> 1.0'
s.add_development_dependency 'simplecov', '~> 0.14'
s.add_development_dependency 'coveralls', '~> 0.8'
s.add_development_dependency 'appraisal', '~> 2.2'
end
8 changes: 0 additions & 8 deletions gemfiles/5.0.0.rc1.gemfile

This file was deleted.

2 changes: 1 addition & 1 deletion gemfiles/rails_3.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ source "https://rubygems.org"

gem "rails", "~> 3.2.0"

gemspec :path => "../"
gemspec path: "../"
2 changes: 1 addition & 1 deletion gemfiles/rails_4.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ source "https://rubygems.org"

gem "rails", "~> 4.0.0"

gemspec :path => "../"
gemspec path: "../"
2 changes: 1 addition & 1 deletion gemfiles/rails_4.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ source "https://rubygems.org"

gem "rails", "~> 4.2.0"

gemspec :path => "../"
gemspec path: "../"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

source "https://rubygems.org"

gem "rails", "~> 5.0.0.rc1"
gem "rails", "~> 5.0.0"

gemspec :path => "../"
gemspec path: "../"
7 changes: 7 additions & 0 deletions gemfiles/rails_5.1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "rails", "~> 5.1.0"

gemspec path: "../"
36 changes: 33 additions & 3 deletions lib/cassandra_migrations/cassandra.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,47 @@ def self.shutdown!
self.client = nil
end

##
# Use the keyspace specified in an additional keyspace environment of the config/cassandra.yml file.
# In order for this to work, such environemnts in the YAML file must have names conforming to the
# {prefix}-{environment} format, where {prefix} is some name, and {environment} is one of the main Rails
# environment names. For example, by defining foo1-development, foo1-test and foo1-production in
# config/cassandra.yml, a migration can invoke .use_keyspace("foo1") to target the keyspace
# specified therein, in accordance with the Rails environment names. This approach is better than
# the .using_keyspace method because it makes a migration targeting an additional keyspace usable
# in all Rails environments the application needs to run in.
# @param prefix [String] The prefix portion of an additional keyspace environment
def self.use_keyspace(prefix, &block)
env_name = "#{prefix.to_s}-#{Rails.env}"
keyspace = CassandraMigrations::Config.configurations[env_name].keyspace
puts " Use keyspace #{keyspace} from #{env_name} in config/cassandra.yml"
use(keyspace)
begin
# invoke the block in the other namespace
block.call
ensure
# always switch back to the main keyspace
use(Config.keyspace)
end
end

def self.using_keyspace(keyspace, &block)
puts " using_keyspace(#{keyspace})"
use(keyspace)
block.call
use(Config.keyspace)
begin
# invoke the block in the other namespace
block.call
ensure
# always switch back to the main keyspace
use(Config.keyspace)
end
end

def self.use(keyspace)
connect_to_server unless client

begin
client.use(keyspace)
client.use(keyspace.to_s)
rescue Exception => e # keyspace does not exist
puts "#{e} : #{e.message}"
raise Errors::UnexistingKeyspaceError, keyspace
Expand Down
27 changes: 25 additions & 2 deletions lib/cassandra_migrations/migration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf-8

require "benchmark"
require 'cassandra_migrations/migration/table_operations'
require 'cassandra_migrations/migration/column_operations'

Expand All @@ -14,6 +15,7 @@ class Migration
# Makes +execute+ method directly available to migrations
delegate :execute, :to => Cassandra
delegate :using_keyspace, :to => Cassandra
delegate :use_keyspace, :to => Cassandra

# Makes +up+ work if the method in the migration is defined with self.up
def up
Expand Down Expand Up @@ -65,7 +67,7 @@ def migrate(direction)
end

private

# Generates output labeled with name of migration and a line that goes up
# to 75 characters long in the terminal
def announce_migration(message)
Expand All @@ -81,10 +83,31 @@ def announce_operation(message)
def announce_suboperation(message)
puts " -> " + message
end

# Gets the name of the migration
def name
self.class.name
end

##
# Execute contents of a file.
def execute_file(path)
execute_text File.read(path)
end

##
# Announce and execute some CQL operation.
def execute_statement(statement)
announce_operation statement
execute statement
end

##
# Execute CQL text.
def execute_text(text)
text.strip.split(/;\s*$/).each do |statement|
execute_statement(statement + ";")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class <%= @migration_class_name %> < CassandraMigrations::Migration
def up

raise CassandraMigrations::Errors::MigrationDefinitionError, "Empty up migration!"
end

def down

raise CassandraMigrations::Errors::MigrationDefinitionError, "Empty down migration!"
end
end
2 changes: 1 addition & 1 deletion lib/cassandra_migrations/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CassandraMigrations
VERSION = '1.0.0'
VERSION = "1.3.1"
end
14 changes: 14 additions & 0 deletions spec/cassandra_migrations/cassandra_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,26 @@
end

it "should raise exception if configured keyspace does not exist" do
cql_client_mock = double('cql_client')
allow(Client).to receive(:connect).with(:hosts => ['127.0.0.1'], :port => 9042).and_return(cql_client_mock)
allow(cql_client_mock).to receive(:use).with('anything').and_raise(Cassandra::Errors::InvalidError)
expect {
CassandraMigrations::Cassandra.use('anything')
}.to raise_exception CassandraMigrations::Errors::UnexistingKeyspaceError
end
end

describe '.use_keyspace' do
it 'should set use the specified keyspace by environment yield to the block and then reset the keyspace' do
cql_client_mock = double('cql_client')
original_keyspace = CassandraMigrations::Config.keyspace
allow(Client).to receive(:connect).with(:hosts => ['127.0.0.1'], :port => 9042).and_return(cql_client_mock)
allow(cql_client_mock).to receive(:use).with('anything').and_return(nil)
allow(cql_client_mock).to receive(:use).with(original_keyspace).and_return(nil)
expect { |block| CassandraMigrations::Cassandra.use_keyspace('anything', &block) }.to yield_control
end
end

describe '.using_keyspace' do
it 'should set use the specified keyspace yield to the block and then reset the keyspace' do
cql_client_mock = double('cql_client')
Expand Down
32 changes: 32 additions & 0 deletions spec/fixtures/config/cassandra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,35 @@ production:
class: NetworkTopologyStrategy
datacenter1: 3
datacenter2: 2

anything-development:
hosts:
- 127.0.0.1
port: 9042
keyspace: cassandra_migrations_development
replication:
class: SimpleStrategy
replication_factor: 1

anything-test:
hosts:
- 127.0.0.1
port: 9042
keyspace: my_keyspace_test
replication:
class: 'SimpleStrategy'
replication_factor: 1

anything-production:
hosts:
- 'cass1.my_app.biz'
- 'cass2.my_app.biz'
- 'cass3.my_app.biz'
port: 9042
keyspace: cassandra_migrations_production
username: myappuser
password: password1
replication:
class: NetworkTopologyStrategy
datacenter1: 3
datacenter2: 2