cequel-migrations-rails 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -63,6 +63,21 @@ column family in it called `schema_migrations` run the following command:
63
63
  rake cequel:create
64
64
  ```
65
65
 
66
+ #### Strategy & Strategy Options
67
+
68
+ Before `cequel:create` will work the `strategy_class` and any
69
+ `strategy_options` need to be set per environment in the `config/cequel.yml`
70
+ config as seen in the example below.
71
+
72
+ ```
73
+ development:
74
+ host: '127.0.0.1:9160'
75
+ keyspace: capture_api_dev
76
+ strategy_class: SimpleStrategy
77
+ strategy_options:
78
+ replication_factor: 1
79
+ ```
80
+
66
81
  ### Drop Database
67
82
 
68
83
  If you have already created the database and you want to drop it to start from
@@ -0,0 +1,45 @@
1
+ module Cequel
2
+ module Migrations
3
+ module Rails
4
+ class KeyspaceManager
5
+ attr_reader :db
6
+
7
+ def initialize
8
+ @db = CassandraCQL::Database.new(self.class.cequel_env_conf['host'])
9
+ end
10
+
11
+ def self.cequel_env_conf
12
+ YAML::load(File.open(File.join(::Rails.root,"config", "cequel.yml")))[::Rails.env]
13
+ end
14
+
15
+ def create_keyspace
16
+ db.execute(build_create_keyspace_cmd(self.class.cequel_env_conf['keyspace'], self.class.cequel_env_conf['strategy_class'], self.class.cequel_env_conf['strategy_options']))
17
+ end
18
+
19
+ def use_keyspace
20
+ db.execute("USE #{self.class.cequel_env_conf['keyspace']}")
21
+ end
22
+
23
+ def drop_keyspace
24
+ db.execute("DROP KEYSPACE #{self.class.cequel_env_conf['keyspace']}")
25
+ end
26
+
27
+ private
28
+
29
+ def build_create_keyspace_cmd(keyspace_name, strategy_class_name, strategy_options)
30
+ strat_opts_array = []
31
+ if strategy_options
32
+ strategy_options.each_pair do |k,v|
33
+ strat_opts_array << "strategy_options:#{k.to_s} = #{v}"
34
+ end
35
+ end
36
+ cql_cmd = ["CREATE KEYSPACE #{keyspace_name} WITH strategy_class = '#{strategy_class_name}'"]
37
+ if !strat_opts_array.empty?
38
+ cql_cmd << strat_opts_array.join(" AND ")
39
+ end
40
+ cql_cmd.join(" AND ")
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -4,37 +4,25 @@ require 'shearwater/cassandra_cql_backend'
4
4
  namespace :cequel do
5
5
  desc "Create the cequel specified cassandra keystore for the current environment"
6
6
  task :create => :environment do
7
- # Read in the cequel config for the current Rails environment
8
- cequel_env_conf = YAML::load(File.open(File.join(::Rails.root,"config", "cequel.yml")))[Rails.env]
9
-
10
- # Create a CQL connection to use as the migrations backend.
11
- db = CassandraCQL::Database.new(cequel_env_conf['host'])
12
- db.execute("CREATE KEYSPACE #{cequel_env_conf['keyspace']} WITH strategy_class = 'SimpleStrategy' AND strategy_options:replication_factor = 1")
13
- db.execute("USE #{cequel_env_conf['keyspace']}")
14
- db.execute("CREATE COLUMNFAMILY schema_migrations (version bigint PRIMARY KEY, migrated_at timestamp)")
7
+ keyspace_manager = Cequel::Migrations::Rails::KeyspaceManager.new
8
+ keyspace_manager.create_keyspace
9
+ keyspace_manager.use_keyspace
10
+ keyspace_manager.db.execute("CREATE COLUMNFAMILY schema_migrations (version bigint PRIMARY KEY, migrated_at timestamp)")
15
11
  end
16
12
 
17
13
  desc "Drop the cequel specified cassandra keystore for the current environment"
18
14
  task :drop => :environment do
19
- # Read in the cequel config for the current Rails environment
20
- cequel_env_conf = YAML::load(File.open(File.join(::Rails.root,"config", "cequel.yml")))[Rails.env]
21
-
22
- # Create a CQL connection to use as the migrations backend.
23
- db = CassandraCQL::Database.new(cequel_env_conf['host'])
24
- db.execute("DROP KEYSPACE #{cequel_env_conf['keyspace']}")
15
+ keyspace_manager = Cequel::Migrations::Rails::KeyspaceManager.new
16
+ keyspace_manager.drop_keyspace
25
17
  end
26
18
 
27
19
  desc "Migrate the cassandra store"
28
20
  task :migrate => :environment do
29
- # Read in the cequel config for the current Rails environment
30
- cequel_env_conf = YAML::load(File.open(File.join(::Rails.root,"config", "cequel.yml")))[Rails.env]
31
-
32
- # Create a CQL connection to use as the migrations backend.
33
- db = CassandraCQL::Database.new(cequel_env_conf['host'])
34
- db.execute("USE #{cequel_env_conf['keyspace']}")
21
+ keyspace_manager = Cequel::Migrations::Rails::KeyspaceManager.new
22
+ keyspace_manager.use_keyspace
35
23
 
36
24
  # Create the migrator
37
- backend = Shearwater::CassandraCqlBackend.new(db)
25
+ backend = Shearwater::CassandraCqlBackend.new(keyspace_manager.db)
38
26
  migrations_directory = ::Rails.root.join('cequel', 'migrate')
39
27
  migrator = Shearwater::Migrator.new(migrations_directory, backend)
40
28
 
@@ -44,15 +32,11 @@ namespace :cequel do
44
32
 
45
33
  desc "Rollback to the previous migration version by 1 step"
46
34
  task :rollback => :environment do
47
- # Read in the cequel config for the current Rails environment
48
- cequel_env_conf = YAML::load(File.open(File.join(::Rails.root,"config", "cequel.yml")))[Rails.env]
49
-
50
- # Create a CQL connection to use as the migrations backend.
51
- db = CassandraCQL::Database.new(cequel_env_conf['host'])
52
- db.execute("USE #{cequel_env_conf['keyspace']}")
35
+ keyspace_manager = Cequel::Migrations::Rails::KeyspaceManager.new
36
+ keyspace_manager.use_keyspace
53
37
 
54
38
  # Create the migrator
55
- backend = Shearwater::CassandraCqlBackend.new(db)
39
+ backend = Shearwater::CassandraCqlBackend.new(keyspace_manager.db)
56
40
  migrations_directory = ::Rails.root.join('cequel', 'migrate')
57
41
  migrator = Shearwater::Migrator.new(migrations_directory, backend)
58
42
 
@@ -68,5 +52,4 @@ namespace :cequel do
68
52
  Rake::Task["cequel:create"].invoke
69
53
  Rake::Task["cequel:migrate"].invoke
70
54
  end
71
-
72
55
  end
@@ -1,7 +1,7 @@
1
1
  module Cequel
2
2
  module Migrations
3
3
  module Rails
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,6 +1,7 @@
1
1
  require "cassandra-cql"
2
2
 
3
3
  require "cequel-migrations-rails/version"
4
+ require "cequel-migrations-rails/keyspace_manager"
4
5
  require "cequel/migration"
5
6
 
6
7
  module Cequel
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Cequel::Migration do
4
+
4
5
  let(:migration_class) { class FooMigration < Cequel::Migration; end; FooMigration }
5
6
  let(:migration) { migration_class.new }
6
7
 
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ # - creation of keyspace
4
+ # - drop keyspace
5
+ # - migrate
6
+ # - rollback
7
+ #
8
+
9
+ describe Cequel::Migrations::Rails::KeyspaceManager do
10
+ describe "#new" do
11
+ it "creates a CQL connection" do
12
+ Cequel::Migrations::Rails::KeyspaceManager.stub(:cequel_env_conf).and_return({ 'host' => 'some host' })
13
+ CassandraCQL::Database.should_receive(:new).with('some host')
14
+ Cequel::Migrations::Rails::KeyspaceManager.new
15
+ end
16
+ end
17
+
18
+ describe "#create_keyspace" do
19
+ it "send cql to create the specified keyspace" do
20
+ Cequel::Migrations::Rails::KeyspaceManager.stub(:cequel_env_conf).and_return({ 'host' => 'some host', 'keyspace' => 'aoeuoae', 'strategy_class' => 'stratclass' })
21
+ db = mock('db')
22
+ CassandraCQL::Database.stub(:new).and_return(db)
23
+ subject.stub(:build_create_keyspace_cmd).and_return('xxx')
24
+ db.should_receive(:execute).with('xxx')
25
+ subject.create_keyspace
26
+ end
27
+ end
28
+
29
+ describe "#build_create_keyspace_cmd" do
30
+ it "build the cql command string to create a keyspace" do
31
+ Cequel::Migrations::Rails::KeyspaceManager.stub(:cequel_env_conf).and_return({ 'host' => 'some host' })
32
+ CassandraCQL::Database.stub(:new)
33
+ subject.send(:build_create_keyspace_cmd, 'somename_keyspace_name', 'StrategyClass', { :replication_factor => 1 }).should == "CREATE KEYSPACE somename_keyspace_name WITH strategy_class = 'StrategyClass' AND strategy_options:replication_factor = 1"
34
+ end
35
+
36
+ it "builds the cql command string to create a keyspace without strategy_options" do
37
+ Cequel::Migrations::Rails::KeyspaceManager.stub(:cequel_env_conf).and_return({ 'host' => 'some host' })
38
+ CassandraCQL::Database.stub(:new)
39
+ subject.send(:build_create_keyspace_cmd, 'somename_keyspace_name', 'StrategyClass', nil).should == "CREATE KEYSPACE somename_keyspace_name WITH strategy_class = 'StrategyClass'"
40
+ end
41
+ end
42
+
43
+ describe "#use_keyspace" do
44
+ it "send the cql command to use the keyspace from the cequel.yml" do
45
+ Cequel::Migrations::Rails::KeyspaceManager.stub(:cequel_env_conf).and_return({ 'host' => 'some host', 'keyspace' => 'my_keyspace' })
46
+ db = mock('db')
47
+ CassandraCQL::Database.stub(:new).and_return(db)
48
+ db.should_receive(:execute).with('USE my_keyspace')
49
+ subject.use_keyspace
50
+ end
51
+ end
52
+
53
+ describe "#drop_keyspace" do
54
+ it "sends the cql comand to drop the keyspace from the cequel.yml" do
55
+ Cequel::Migrations::Rails::KeyspaceManager.stub(:cequel_env_conf).and_return({ 'host' => 'some host', 'keyspace' => 'my_keyspace' })
56
+ db = mock('db')
57
+ CassandraCQL::Database.stub(:new).and_return(db)
58
+ db.should_receive(:execute).with('DROP KEYSPACE my_keyspace')
59
+ subject.drop_keyspace
60
+ end
61
+ end
62
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cequel-migrations-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-03 00:00:00.000000000 Z
12
+ date: 2012-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shearwater
@@ -90,6 +90,7 @@ files:
90
90
  - Rakefile
91
91
  - cequel-migrations-rails.gemspec
92
92
  - lib/cequel-migrations-rails.rb
93
+ - lib/cequel-migrations-rails/keyspace_manager.rb
93
94
  - lib/cequel-migrations-rails/tasks/migrations.rake
94
95
  - lib/cequel-migrations-rails/version.rb
95
96
  - lib/cequel/migration.rb
@@ -97,6 +98,7 @@ files:
97
98
  - lib/generators/cequel/migration/templates/migration.rb
98
99
  - lib/generators/cequel/migrations/install/install_generator.rb
99
100
  - lib/generators/cequel/migrations/install/templates/cequel/migrate/.gitkeep
101
+ - spec/lib/cequel-migrations-rails/cql_manager_spec.rb
100
102
  - spec/lib/cequel/migration_spec.rb
101
103
  - spec/spec_helper.rb
102
104
  homepage: http://github.com/cyphactor/cequel-migrations-rails
@@ -124,5 +126,6 @@ signing_key:
124
126
  specification_version: 3
125
127
  summary: A Rails plugin that provides migration support for Cequel.
126
128
  test_files:
129
+ - spec/lib/cequel-migrations-rails/cql_manager_spec.rb
127
130
  - spec/lib/cequel/migration_spec.rb
128
131
  - spec/spec_helper.rb