cequel-migrations-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p327@cequel-migrations-rails"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.16.20 (master)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ if [[ $- == *i* ]] # check for interactive shells
29
+ then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
30
+ else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
31
+ fi
32
+ else
33
+ # If the environment file has not yet been created, use the RVM CLI to select.
34
+ rvm --create use "$environment_id" || {
35
+ echo "Failed to create RVM environment '${environment_id}'."
36
+ return 1
37
+ }
38
+ fi
39
+
40
+ # If you use bundler, this might be useful to you:
41
+ # if [[ -s Gemfile ]] && {
42
+ # ! builtin command -v bundle >/dev/null ||
43
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
44
+ # }
45
+ # then
46
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
47
+ # gem install bundler
48
+ # fi
49
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
50
+ # then
51
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
52
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cequel-migrations-rails.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 ReachLocal Inc., Andrew De Ponte
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,119 @@
1
+ # Cequel Migrations Rails Plugin
2
+
3
+ This Rails plugin basically provides a migration setup very similar to the
4
+ standard ActiveRecord based migration system that normally exists in rails.
5
+ The BIG difference is that it is designed to support specifically migrations
6
+ for Cassandra data stores using Cequel.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'cequel-migrations-rails'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install cequel-migrations-rails
21
+
22
+ ## Usage
23
+
24
+ For information on how to use this gem in your rails app please refer to the
25
+ following. Note: You need to have the cequel gem setup and configured already
26
+ in your Rails app before using this gem.
27
+
28
+ ### Initial Setup
29
+
30
+ Before you can start using this gem you need to create the initial directory
31
+ structure in your Rails app for it. You can do this using the following
32
+ command:
33
+
34
+ ```
35
+ rails g cequel:migrations:install
36
+ ```
37
+
38
+ This creates the directory structure `cequel/migrate/` at the root of your
39
+ Rails application as the place to store your migration scripts.
40
+
41
+ ### Generate Migration File
42
+
43
+ To write a migration you first need to generate a migration file. This is done
44
+ with the following command where `<migration_name>` is replaced with the name
45
+ of the migration.
46
+
47
+ ```
48
+ rails g cequel:migration <migration_name>
49
+ ```
50
+
51
+ An example of the above command with a migration name would look as follows:
52
+
53
+ ```
54
+ rails g cequel:migration CreateUserColumnFamily
55
+ ```
56
+
57
+ ### Create Database
58
+
59
+ To create the database specified in your `config/cequel.yml` and create a
60
+ column family in it called `schema_migrations` run the following command:
61
+
62
+ ```
63
+ rake cequel:create
64
+ ```
65
+
66
+ ### Drop Database
67
+
68
+ If you have already created the database and you want to drop it to start from
69
+ scratch or something you can do so by running the following command:
70
+
71
+ ```
72
+ rake cequel:drop
73
+ ```
74
+
75
+ ### Migrate Database
76
+
77
+ If you database exists with a `schema_migrations` column family already
78
+ because you followed the directions above in the "Create Database" section you
79
+ can migrate the database with the following command:
80
+
81
+ ```
82
+ rake cequel:migrate
83
+ ```
84
+
85
+ ### Rollback Migrations
86
+
87
+ If you need to rollback a migration you do so with the following command:
88
+
89
+ ```
90
+ rake cequel:rollback
91
+ ```
92
+
93
+ If you need to rollback multiple migrations you can do so as follows, where 5
94
+ is the number of steps I wanted to rollback.
95
+
96
+ ```
97
+ STEPS=5 rake cequel:rollback
98
+ ```
99
+
100
+ ## Writing Migrations
101
+
102
+ When you generate a migration you may know that it sets up the migration class
103
+ with a base class of `Cequel::Migration`.
104
+
105
+ The `Cequel::Migration` class provides the `execute` method to be used inside
106
+ of your migrations `up` and `down` methods. The `execute` method takes a
107
+ string of CQL.
108
+
109
+ At the moment we currently only provide the `execute` method. However, we plan
110
+ to provide other helper functions like, `create_column_family`,
111
+ `drop_column_family`, etc.
112
+
113
+ ## Contributing
114
+
115
+ 1. Fork it
116
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
117
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
118
+ 4. Push to the branch (`git push origin my-new-feature`)
119
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cequel-migrations-rails/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "cequel-migrations-rails"
8
+ gem.version = Cequel::Migrations::Rails::VERSION
9
+ gem.authors = ["Andrew De Ponte"]
10
+ gem.email = ["cyphactor@gmail.com"]
11
+ gem.description = %q{Cequel migration support for Rails.}
12
+ gem.summary = %q{A Rails plugin that provides migration support for Cequel.}
13
+ gem.homepage = "http://github.com/cyphactor/cequel-migrations-rails"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency('shearwater', '>= 0.1.3')
21
+ gem.add_dependency('rails')
22
+ gem.add_dependency('cassandra-cql')
23
+ gem.add_development_dependency('rspec')
24
+ end
@@ -0,0 +1,16 @@
1
+ require "cassandra-cql"
2
+
3
+ require "cequel-migrations-rails/version"
4
+ require "cequel/migration"
5
+
6
+ module Cequel
7
+ module Migrations
8
+ module Rails
9
+ class Railtie < ::Rails::Railtie
10
+ rake_tasks do
11
+ load "cequel-migrations-rails/tasks/migrations.rake"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,72 @@
1
+ require 'shearwater'
2
+ require 'shearwater/cassandra_cql_backend'
3
+
4
+ namespace :cequel do
5
+ desc "Create the cequel specified cassandra keystore for the current environment"
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)")
15
+ end
16
+
17
+ desc "Drop the cequel specified cassandra keystore for the current environment"
18
+ 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']}")
25
+ end
26
+
27
+ desc "Migrate the cassandra store"
28
+ 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']}")
35
+
36
+ # Create the migrator
37
+ backend = Shearwater::CassandraCqlBackend.new(db)
38
+ migrations_directory = ::Rails.root.join('cequel', 'migrate')
39
+ migrator = Shearwater::Migrator.new(migrations_directory, backend)
40
+
41
+ # Migrate
42
+ migrator.migrate
43
+ end
44
+
45
+ desc "Rollback to the previous migration version by 1 step"
46
+ 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']}")
53
+
54
+ # Create the migrator
55
+ backend = Shearwater::CassandraCqlBackend.new(db)
56
+ migrations_directory = ::Rails.root.join('cequel', 'migrate')
57
+ migrator = Shearwater::Migrator.new(migrations_directory, backend)
58
+
59
+ steps = ENV['STEPS'] || 1
60
+
61
+ # Migrate
62
+ migrator.rollback(steps.to_i)
63
+ end
64
+
65
+ desc "Drop the keystore, recreate it and run the migrations"
66
+ task :reset do
67
+ Rake::Task["cequel:drop"].invoke
68
+ Rake::Task["cequel:create"].invoke
69
+ Rake::Task["cequel:migrate"].invoke
70
+ end
71
+
72
+ end
@@ -0,0 +1,7 @@
1
+ module Cequel
2
+ module Migrations
3
+ module Rails
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ module Cequel
2
+ class Migration
3
+ attr_reader :db
4
+
5
+ def initialize
6
+ @db = CassandraCQL::Database.new(self.class.cequel_env_conf['host'], { :keyspace => self.class.cequel_env_conf['keyspace'] })
7
+ end
8
+
9
+ def execute(cql_string)
10
+ db.execute(cql_string)
11
+ end
12
+
13
+ def self.cequel_env_conf
14
+ YAML::load(File.open(File.join(::Rails.root,"config", "cequel.yml")))[::Rails.env]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Cequel
2
+ module Generators
3
+ class MigrationGenerator < ::Rails::Generators::NamedBase
4
+ attr_reader :migration_class_name
5
+
6
+ def self.source_root
7
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
8
+ end
9
+
10
+ def create_migration
11
+ @migration_class_name = file_name.camelize
12
+ migration_timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
13
+ template 'migration.rb', File.join("cequel/migrate/#{migration_timestamp}_#{file_name.underscore}.rb")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ class <%= migration_class_name %> < Cequel::Migration
2
+ def up
3
+ end
4
+
5
+ def down
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ module Cequel
2
+ module Migrations
3
+ module Generators
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+ desc <<DESC
6
+ Description:
7
+ Setup cequel migrations directory structure.
8
+ DESC
9
+
10
+ def self.source_root
11
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
12
+ end
13
+
14
+ def copy_schema_files
15
+ directory 'cequel'
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cequel::Migration do
4
+ let(:migration_class) { class FooMigration < Cequel::Migration; end; FooMigration }
5
+ let(:migration) { migration_class.new }
6
+
7
+ describe "#new" do
8
+ it "create a cassandra-cql database connection for the host & keyspace specified in the environment's config" do
9
+ migration_class.stub(:cequel_env_conf).and_return({ 'host' => 'somehost', 'keyspace' => 'somekeyspace' })
10
+ CassandraCQL::Database.should_receive(:new).with('somehost', { :keyspace => 'somekeyspace' })
11
+ migration
12
+ end
13
+ end
14
+
15
+ describe "#execute" do
16
+ it "delegates to the cassandra-cql connection execute" do
17
+ migration_class.stub(:cequel_env_conf).and_return({ 'keyspace' => 'test keyspace', 'host' => '123.123.123.123' })
18
+ db = mock('db').as_null_object
19
+ CassandraCQL::Database.stub(:new).and_return(db)
20
+ db.should_receive(:execute).with("some cql string")
21
+ migration.execute("some cql string")
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'rails/all'
5
+
6
+ module CequelMigrationsRails
7
+ class Application < ::Rails::Application
8
+ end
9
+ end
10
+
11
+ require 'cequel-migrations-rails'
12
+
13
+ RSpec.configure do |config|
14
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cequel-migrations-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew De Ponte
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shearwater
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.1.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: cassandra-cql
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Cequel migration support for Rails.
79
+ email:
80
+ - cyphactor@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rvmrc
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - cequel-migrations-rails.gemspec
92
+ - lib/cequel-migrations-rails.rb
93
+ - lib/cequel-migrations-rails/tasks/migrations.rake
94
+ - lib/cequel-migrations-rails/version.rb
95
+ - lib/cequel/migration.rb
96
+ - lib/generators/cequel/migration/migration_generator.rb
97
+ - lib/generators/cequel/migration/templates/migration.rb
98
+ - lib/generators/cequel/migrations/install/install_generator.rb
99
+ - lib/generators/cequel/migrations/install/templates/cequel/migrate/.gitkeep
100
+ - spec/lib/cequel/migration_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: http://github.com/cyphactor/cequel-migrations-rails
103
+ licenses: []
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 1.8.24
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: A Rails plugin that provides migration support for Cequel.
126
+ test_files:
127
+ - spec/lib/cequel/migration_spec.rb
128
+ - spec/spec_helper.rb