cassandra_migrations 0.0.1.pre0 → 0.0.1.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -77,9 +77,11 @@ private
77
77
  def self.connect_to_server
78
78
  load_config
79
79
 
80
+ Rails.logger.try(:info, "Connecting to Cassandra on #{config['host']}:#{config['port']}")
81
+
80
82
  begin
81
83
  self.client = Cql::Client.new(:host => config['host'], :port => config['port'])
82
- client.start!
84
+ client.connect
83
85
  rescue Cql::Io::ConnectionError => e
84
86
  raise Errors::ConnectionError, e.message
85
87
  end
@@ -0,0 +1,17 @@
1
+ # encoding : utf-8
2
+
3
+ # Em produção (como usamos o Passenger), vários processos ruby são criados através do fork
4
+ # do original. No UNIX, quando um fork é realizado, is file descriptors (arquivos, conexões a base de dados,
5
+ # sockets, e etc) são copiados em estado aberto para o processo filho. Se não reabrirmos a conexão com o cassandra
6
+ # no processo filho, quando ela for ser usada ela estará 'locked' pelo processo pai, o que resultará em deadlock.
7
+
8
+ # Mais explicações em: http://www.modrails.com/documentation/Users%20guide%20Apache.html#spawning_methods_explained
9
+ if defined?(PhusionPassenger)
10
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
11
+ if forked
12
+ CassandraMigrations::Cassandra.shutdown!
13
+ end
14
+ end
15
+ end
16
+
17
+ CassandraMigrations::Cassandra.start!
@@ -1,15 +1,14 @@
1
1
  # encoding : utf-8
2
2
 
3
- require 'cassandra_migrations/cassandra'
4
-
5
3
  class CassandraMigrations::Railtie < ::Rails::Railtie
6
4
 
7
- initializer "cassandra_migrations.start" do
8
- CassandraMigrations::Cassandra.start!
5
+ initializer "cassandra_migrations.initializer" do
6
+ require File.expand_path('railtie/initializer', File.dirname(__FILE__))
9
7
  end
10
8
 
11
9
  rake_tasks do
12
- Dir[File.expand_path("tasks/**/*.rake", File.dirname(__FILE__))].each do |file|
10
+ Dir[File.expand_path("railtie/**/*.rake", File.dirname(__FILE__))].each do |file|
11
+ # 'load' is used because 'require' is only for .rb files
13
12
  load file
14
13
  end
15
14
  end
@@ -2,4 +2,5 @@
2
2
  module CassandraMigrations
3
3
  end
4
4
 
5
+ require 'cassandra_migrations/cassandra'
5
6
  require 'cassandra_migrations/railtie' if defined?(Rails)
@@ -0,0 +1,88 @@
1
+ # encoding : utf-8
2
+ require 'spec_helper'
3
+
4
+ describe CassandraMigrations::Cassandra do
5
+
6
+ it "should initialize without client or config" do
7
+ CassandraMigrations::Cassandra.client.should be_nil
8
+ CassandraMigrations::Cassandra.config.should be_nil
9
+ end
10
+
11
+ before do
12
+ Rails.stub(:root).and_return Pathname.new("spec/fixtures")
13
+ Rails.stub(:env).and_return ActiveSupport::StringInquirer.new("development")
14
+ end
15
+
16
+ after do
17
+ CassandraMigrations::Cassandra.shutdown!
18
+ end
19
+
20
+ describe ".start!" do
21
+ it "should raise exception if config/cassandra.yaml is not found" do
22
+ Rails.stub(:root).and_return Pathname.new("wrong_path")
23
+
24
+ expect do
25
+ CassandraMigrations::Cassandra.start!
26
+ end.to raise_exception CassandraMigrations::Cassandra::Errors::MissingConfigurationError
27
+ end
28
+
29
+ it "should load config/cassandra.yaml environment specific part in self.config" do
30
+ begin
31
+ CassandraMigrations::Cassandra.start!
32
+ rescue
33
+ end
34
+
35
+ CassandraMigrations::Cassandra.config.should == {
36
+ 'host' => "127.0.0.1",
37
+ 'port' => 9042,
38
+ 'keyspace' => "cassandra_migrations_development",
39
+ 'replication' => {
40
+ 'class' => "SimpleStrategy",
41
+ 'replication_factor' => 1
42
+ }
43
+ }
44
+ end
45
+
46
+ it "should use host and port configurations to create cassandra client" do
47
+ cql_client_mock = Cql::Client.new
48
+ cql_client_mock.should_receive(:start!)
49
+ Cql::Client.should_receive(:new).with(:host => '127.0.0.1', :port => 9042).and_return(cql_client_mock)
50
+
51
+ begin
52
+ CassandraMigrations::Cassandra.start!
53
+ rescue
54
+ end
55
+ end
56
+
57
+ it "should raise exception if not able to connect to cassandra host" do
58
+ cql_client_mock = Cql::Client.new
59
+ cql_client_mock.stub(:start!).and_raise Cql::Io::ConnectionError
60
+ Cql::Client.stub(:new).and_return cql_client_mock
61
+
62
+ expect do
63
+ CassandraMigrations::Cassandra.start!
64
+ end.to raise_exception CassandraMigrations::Cassandra::Errors::ConnectionError
65
+ end
66
+
67
+ it "should automatically use configured keyspace" do
68
+ CassandraMigrations::Cassandra.should_receive(:use).with('cassandra_migrations_development')
69
+ CassandraMigrations::Cassandra.start!
70
+ end
71
+
72
+ it "should raise exception if configured keyspace does not exist" do
73
+ expect do
74
+ CassandraMigrations::Cassandra.start!
75
+ end.to raise_exception CassandraMigrations::Cassandra::Errors::UnexistingKeyspaceError
76
+ end
77
+ end
78
+
79
+ describe "query interface" do
80
+ it "should respond to query methods" do
81
+ CassandraMigrations::Cassandra.should respond_to :select
82
+ CassandraMigrations::Cassandra.should respond_to :write
83
+ CassandraMigrations::Cassandra.should respond_to :delete
84
+ CassandraMigrations::Cassandra.should respond_to :truncate
85
+ end
86
+ end
87
+
88
+ end
@@ -0,0 +1,13 @@
1
+ # encoding : utf-8
2
+ require 'spec_helper'
3
+
4
+ describe CassandraMigrations do
5
+ it "should define modules" do
6
+ defined?(CassandraMigrations).should be_true
7
+ defined?(CassandraMigrations::Railtie).should be_true
8
+ defined?(CassandraMigrations::Cassandra).should be_true
9
+ defined?(CassandraMigrations::Cassandra::Query).should be_true
10
+ defined?(CassandraMigrations::Cassandra::Errors).should be_true
11
+ defined?(CassandraMigrations::Cassandra::Migrator).should be_true
12
+ end
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassandra_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.pre0
4
+ version: 0.0.1.pre1
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - '='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.0.0.pre3
21
+ version: 1.0.0.pre5
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 1.0.0.pre3
29
+ version: 1.0.0.pre5
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: rake
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -98,13 +98,16 @@ executables: []
98
98
  extensions: []
99
99
  extra_rdoc_files: []
100
100
  files:
101
- - lib/cassandra_migrations/cassandra.rb
102
- - lib/cassandra_migrations/cassandra/errors.rb
101
+ - lib/cassandra_migrations.rb
103
102
  - lib/cassandra_migrations/cassandra/migrator.rb
104
103
  - lib/cassandra_migrations/cassandra/query.rb
105
- - lib/cassandra_migrations/tasks/cassandra.rake
104
+ - lib/cassandra_migrations/cassandra/errors.rb
105
+ - lib/cassandra_migrations/railtie/tasks.rake
106
+ - lib/cassandra_migrations/railtie/initializer.rb
107
+ - lib/cassandra_migrations/cassandra.rb
106
108
  - lib/cassandra_migrations/railtie.rb
107
- - lib/cassandra_migrations.rb
109
+ - spec/cassandra_migrations/cassandra_spec.rb
110
+ - spec/cassandra_migrations_spec.rb
108
111
  homepage: https://github.com/hsgubert/cassandra_migrations
109
112
  licenses: []
110
113
  post_install_message:
@@ -125,8 +128,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
128
  version: 1.8.0
126
129
  requirements: []
127
130
  rubyforge_project:
128
- rubygems_version: 1.8.24
131
+ rubygems_version: 1.8.25
129
132
  signing_key:
130
133
  specification_version: 3
131
134
  summary: Cassandra schema management for a multi-environment developer.
132
- test_files: []
135
+ test_files:
136
+ - spec/cassandra_migrations/cassandra_spec.rb
137
+ - spec/cassandra_migrations_spec.rb