standalone_migrations 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,40 +1,38 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activemodel (3.1.0.rc4)
5
- activesupport (= 3.1.0.rc4)
6
- bcrypt-ruby (~> 2.1.4)
4
+ activemodel (3.1.1)
5
+ activesupport (= 3.1.1)
7
6
  builder (~> 3.0.0)
8
7
  i18n (~> 0.6)
9
- activerecord (3.1.0.rc4)
10
- activemodel (= 3.1.0.rc4)
11
- activesupport (= 3.1.0.rc4)
12
- arel (~> 2.1.1)
13
- tzinfo (~> 0.3.27)
14
- activesupport (3.1.0.rc4)
8
+ activerecord (3.1.1)
9
+ activemodel (= 3.1.1)
10
+ activesupport (= 3.1.1)
11
+ arel (~> 2.2.1)
12
+ tzinfo (~> 0.3.29)
13
+ activesupport (3.1.1)
15
14
  multi_json (~> 1.0)
16
- arel (2.1.1)
17
- bcrypt-ruby (2.1.4)
15
+ arel (2.2.1)
18
16
  builder (3.0.0)
19
- diff-lcs (1.1.2)
17
+ diff-lcs (1.1.3)
20
18
  git (1.2.5)
21
19
  i18n (0.6.0)
22
- jeweler (1.6.2)
20
+ jeweler (1.6.4)
23
21
  bundler (~> 1.0)
24
22
  git (>= 1.2.5)
25
23
  rake
26
24
  multi_json (1.0.3)
27
- rake (0.9.2)
28
- rspec (2.6.0)
29
- rspec-core (~> 2.6.0)
30
- rspec-expectations (~> 2.6.0)
31
- rspec-mocks (~> 2.6.0)
32
- rspec-core (2.6.4)
33
- rspec-expectations (2.6.0)
25
+ rake (0.9.2.2)
26
+ rspec (2.7.0)
27
+ rspec-core (~> 2.7.0)
28
+ rspec-expectations (~> 2.7.0)
29
+ rspec-mocks (~> 2.7.0)
30
+ rspec-core (2.7.1)
31
+ rspec-expectations (2.7.0)
34
32
  diff-lcs (~> 1.1.2)
35
- rspec-mocks (2.6.0)
36
- sqlite3 (1.3.3)
37
- tzinfo (0.3.27)
33
+ rspec-mocks (2.7.0)
34
+ sqlite3 (1.3.4)
35
+ tzinfo (0.3.31)
38
36
 
39
37
  PLATFORMS
40
38
  ruby
data/README.markdown CHANGED
@@ -16,11 +16,13 @@ Install Ruby, RubyGems and a ruby-database driver (e.g. `gem install mysql`) the
16
16
 
17
17
  Add to `Rakefile` in your projects base directory:
18
18
 
19
- begin
20
- require 'tasks/standalone_migrations'
21
- rescue LoadError => e
22
- puts "gem install standalone_migrations to get db:migrate:* tasks! (Error: #{e})"
23
- end
19
+ ```ruby
20
+ begin
21
+ require 'tasks/standalone_migrations'
22
+ rescue LoadError => e
23
+ puts "gem install standalone_migrations to get db:migrate:* tasks! (Error: #{e})"
24
+ end
25
+ ```
24
26
 
25
27
  Add database configuration to `db/config.yml` in your projects base directory e.g.:
26
28
 
@@ -55,13 +57,15 @@ Add database configuration to `db/config.yml` in your projects base directory e.
55
57
 
56
58
  #### If you really want to, you can just execute raw SQL:
57
59
 
58
- def self.up
59
- execute "insert into foo values (123,'something');"
60
- end
60
+ ```ruby
61
+ def self.up
62
+ execute "insert into foo values (123,'something');"
63
+ end
61
64
 
62
- def self.down
63
- execute "delete from foo where field='something';"
64
- end
65
+ def self.down
66
+ execute "delete from foo where field='something';"
67
+ end
68
+ ```
65
69
 
66
70
  #### Even better, you can use the _generate_ task to create the initial migration ####
67
71
 
@@ -77,20 +81,22 @@ An example to create a Person table with 3 columns (and it will automatically ad
77
81
 
78
82
  This will create a migration in db/migrate/
79
83
 
80
- class CreatePerson < ActiveRecord::Migration
81
- def self.up
82
- create_table :Person do |t|
83
- t.string :first_name
84
- t.string :last_name
85
- t.integer :age
86
- t.timestamps
87
- end
88
- end
89
-
90
- def self.down
91
- drop_table :Person
92
- end
84
+ ```ruby
85
+ class CreatePerson < ActiveRecord::Migration
86
+ def self.up
87
+ create_table :Person do |t|
88
+ t.string :first_name
89
+ t.string :last_name
90
+ t.integer :age
91
+ t.timestamps
93
92
  end
93
+ end
94
+
95
+ def self.down
96
+ drop_table :Person
97
+ end
98
+ end
99
+ ```
94
100
 
95
101
  ### To apply your newest migration:
96
102
 
@@ -125,16 +131,76 @@ directory structure to work with, you can use a configuration file
125
131
  named .standalone_migrations in the root of your project containing
126
132
  the following:
127
133
 
128
- db:
129
- seeds: db/seeds.rb
130
- migrate: db/migrate
131
- schema: db/schema.rb
132
- config:
133
- database: db/config.yml
134
+ ```yaml
135
+ db:
136
+ seeds: db/seeds.rb
137
+ migrate: db/migrate
138
+ schema: db/schema.rb
139
+ config:
140
+ database: db/config.yml
141
+ ```
134
142
 
135
143
  These are the configurable options available. You can omit any of
136
144
  the keys and Standalone Migrations will assume the default values.
137
145
 
146
+ #### Changing environment config in runtime
147
+
148
+ If you are using Heroku or have to create or change your connection
149
+ configuration based on runtime aspects (maybe environment variables),
150
+ you can use the `StandaloneMigrations::Configurator.environments_config`
151
+ method. Check the usage example:
152
+
153
+ ```ruby
154
+ require 'tasks/standalone_migrations'
155
+
156
+ StandaloneMigrations::Configurator.environments_config do |env|
157
+
158
+ env.on "production" do
159
+
160
+ if (ENV['DATABASE_URL'])
161
+ db = URI.parse(ENV['DATABASE_URL'])
162
+ return {
163
+ :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
164
+ :host => db.host,
165
+ :username => db.user,
166
+ :password => db.password,
167
+ :database => db.path[1..-1],
168
+ :encoding => 'utf8'
169
+ }
170
+ end
171
+
172
+ nil
173
+ end
174
+
175
+ end
176
+ ```
177
+
178
+ You have to put this anywhere on your `Rakefile`. If you want to
179
+ change some configuration, call the #on method on the object
180
+ received as argument in your block passed to ::environments_config
181
+ method call. The #on method receives the key to the configuration
182
+ that you want to change within the block. The block should return
183
+ your new configuration hash or nil if you want the configuration
184
+ to stay the same.
185
+
186
+ Your logic to decide the new configuration need to access some data
187
+ in your current configuration? Then you should receive the configuration
188
+ in your block, like this:
189
+
190
+ ```ruby
191
+ require 'tasks/standalone_migrations'
192
+
193
+ StandaloneMigrations::Configurator.environments_config do |env|
194
+
195
+ env.on "my_custom_config" do |current_custom_config|
196
+ p current_custom_config
197
+ # => the values on your current "my_custom_config" environment
198
+ nil
199
+ end
200
+
201
+ end
202
+ ```
203
+
138
204
  Contributors
139
205
  ============
140
206
  - [Todd Huss](http://gabrito.com/)
data/Rakefile CHANGED
@@ -10,6 +10,8 @@ end
10
10
  task :specs => ["specs:nodb"]
11
11
  namespace :specs do
12
12
  require 'rspec/core/rake_task'
13
+
14
+ desc "only specs that don't use database connection"
13
15
  RSpec::Core::RakeTask.new "nodb" do |t|
14
16
  t.pattern = "spec/standalone_migrations/**/*_spec.rb"
15
17
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.4
1
+ 1.0.5
@@ -1,5 +1,32 @@
1
+ require 'active_support/all'
2
+
1
3
  module StandaloneMigrations
4
+
5
+ class InternalConfigurationsProxy
6
+
7
+ def initialize(configurations)
8
+ @configurations = configurations
9
+ end
10
+
11
+ def on(config_key)
12
+ if @configurations[config_key] && block_given?
13
+ @configurations[config_key] = yield(@configurations[config_key]) || @configurations[config_key]
14
+ end
15
+ @configurations[config_key]
16
+ end
17
+
18
+ end
19
+
2
20
  class Configurator
21
+ def self.load_configurations
22
+ @standalone_configs ||= Configurator.new.config
23
+ @environments_config ||= YAML.load(ERB.new(File.read(@standalone_configs)).result).with_indifferent_access
24
+ end
25
+
26
+ def self.environments_config
27
+ proxy = InternalConfigurationsProxy.new(load_configurations)
28
+ yield(proxy) if block_given?
29
+ end
3
30
 
4
31
  def initialize(options = {})
5
32
  defaults = {
@@ -8,7 +35,7 @@ module StandaloneMigrations
8
35
  :seeds => "db/seeds.rb",
9
36
  :schema => "db/schema.rb"
10
37
  }
11
- @options = load_from_file || defaults.merge(options)
38
+ @options = load_from_file(defaults.dup) || defaults.merge(options)
12
39
  end
13
40
 
14
41
  def config
@@ -27,20 +54,28 @@ module StandaloneMigrations
27
54
  @options[:schema]
28
55
  end
29
56
 
57
+ def config_for_all
58
+ Configurator.load_configurations.dup
59
+ end
60
+
61
+ def config_for(environment)
62
+ config_for_all[environment]
63
+ end
64
+
30
65
  private
31
66
 
32
67
  def configuration_file
33
68
  ".standalone_migrations"
34
69
  end
35
70
 
36
- def load_from_file
71
+ def load_from_file(defaults)
37
72
  return nil unless File.exists? configuration_file
38
73
  config = YAML.load( IO.read(configuration_file) )
39
74
  {
40
- :config => config["config"]["database"],
41
- :migrate_dir => config["db"]["migrate"],
42
- :seeds => config["db"]["seeds"],
43
- :schema => config["db"]["schema"]
75
+ :config => config["config"] ? config["config"]["database"] : defaults[:config],
76
+ :migrate_dir => config["db"] ? config["db"]["migrate"] : defaults[:migrate_dir],
77
+ :seeds => config["db"] ? config["db"]["seeds"] : defaults[:seeds],
78
+ :schema => config["db"] ? config["db"]["schema"] : defaults[:schema]
44
79
  }
45
80
  end
46
81
 
@@ -8,11 +8,9 @@ if File.directory?('db/migrations')
8
8
  puts "DEPRECATED move your migrations into db/migrate"
9
9
  end
10
10
 
11
- configurator = StandaloneMigrations::Configurator.new
12
-
13
- DB_CONFIG = YAML.load(
14
- ERB.new(File.read(configurator.config)).result
15
- ).with_indifferent_access
11
+ def standalone_configurator
12
+ @configurator ||= StandaloneMigrations::Configurator.new
13
+ end
16
14
 
17
15
  module Rails
18
16
  def self.env
@@ -29,19 +27,17 @@ module Rails
29
27
  s = "fake_app"
30
28
 
31
29
  def s.paths
32
- configurator = StandaloneMigrations::Configurator.new
33
-
34
30
  {
35
- "db/migrate" => [configurator.migrate_dir],
36
- "db/seeds.rb" => [configurator.seeds],
37
- "db/schema.rb" => [configurator.schema]
31
+ "db/migrate" => [standalone_configurator.migrate_dir],
32
+ "db/seeds.rb" => [standalone_configurator.seeds],
33
+ "db/schema.rb" => [standalone_configurator.schema]
38
34
  }
39
35
  end
40
36
 
41
37
  def s.config
42
38
  s = "fake_config"
43
39
  def s.database_configuration
44
- DB_CONFIG
40
+ standalone_configurator.config_for_all
45
41
  end
46
42
  s
47
43
  end
@@ -55,8 +51,8 @@ end
55
51
  task(:rails_env){}
56
52
 
57
53
  task(:environment) do
58
- ActiveRecord::Base.configurations = DB_CONFIG
59
- ActiveRecord::Base.establish_connection DB_CONFIG[Rails.env]
54
+ ActiveRecord::Base.configurations = standalone_configurator.config_for_all
55
+ ActiveRecord::Base.establish_connection standalone_configurator.config_for Rails.env
60
56
  end
61
57
 
62
58
  load 'active_record/railties/databases.rake'
@@ -86,7 +82,7 @@ eof
86
82
  end
87
83
 
88
84
  def configurator
89
- StandaloneMigrations::Configurator.new
85
+ standalone_configurator
90
86
  end
91
87
 
92
88
  def create_file file, contents
@@ -4,6 +4,82 @@ require 'yaml'
4
4
  module StandaloneMigrations
5
5
  describe Configurator, "which allows define custom dirs and files to work with your migrations" do
6
6
 
7
+ describe "environment yaml configuration loading" do
8
+
9
+ let(:env_hash) do
10
+ {
11
+ "development" => { "adapter" => "sqlite3", "database" => "db/development.sql" },
12
+ "test" => { "adapter" => "sqlite3", "database" => "db/test.sql" },
13
+ "production" => {"adapter" => "sqlite3", "database" => ":memory:" }
14
+ }
15
+ end
16
+
17
+ before(:all) do
18
+ @original_dir = Dir.pwd
19
+ Dir.chdir( File.expand_path("../../", __FILE__) )
20
+ FileUtils.mkdir_p "tmp/db"
21
+ Dir.chdir "tmp"
22
+ File.open("db/config.yml", "w") do |f|
23
+ f.write env_hash.to_yaml
24
+ end
25
+ end
26
+
27
+ it "load the specific environment config" do
28
+ config = Configurator.new.config_for(:development)
29
+ config.should == env_hash["development"]
30
+ end
31
+
32
+ it "load the yaml with environment configurations" do
33
+ config = Configurator.new.config_for(:development)
34
+ config[:database].should == "db/development.sql"
35
+ end
36
+
37
+ it "allow access the original configuration hash (for all environments)" do
38
+ Configurator.new.config_for_all.should == env_hash
39
+ end
40
+
41
+ context "customizing the environments configuration dynamically" do
42
+
43
+ let(:configurator) { Configurator.new }
44
+ let(:new_config) { { 'sbrobous' => 'test' } }
45
+
46
+ before(:all) do
47
+ Configurator.environments_config do |env|
48
+ env.on "production" do
49
+ new_config
50
+ end
51
+ end
52
+ end
53
+
54
+ it "allow changes on the configuration hashes" do
55
+ configurator.config_for("production").should == new_config
56
+ end
57
+
58
+ it "return current configuration if block yielding returns nil" do
59
+ Configurator.environments_config do |env|
60
+ env.on "production" do
61
+ nil
62
+ end
63
+ end
64
+ configurator.config_for("production").should == new_config
65
+ end
66
+
67
+ it "pass the current configuration as block argument" do
68
+ Configurator.environments_config do |env|
69
+ env.on "production" do |current_config|
70
+ current_config.should == new_config
71
+ end
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ after(:all) do
78
+ Dir.chdir @original_dir
79
+ end
80
+
81
+ end
82
+
7
83
  context "default values when .standalone_configurations is missing" do
8
84
 
9
85
  let(:configurator) do
@@ -87,6 +163,25 @@ module StandaloneMigrations
87
163
  Configurator.new
88
164
  end
89
165
 
166
+ context "with some configurations missing" do
167
+
168
+ let(:yaml_hash) do
169
+ {
170
+ "config" => {
171
+ "database" => "file/config/database.yml"
172
+ }
173
+ }
174
+ end
175
+
176
+ it "use default values for the missing configurations" do
177
+ configurator.migrate_dir.should == 'db/migrate'
178
+ end
179
+
180
+ it "use custom config from file" do
181
+ configurator.config.should == yaml_hash["config"]["database"]
182
+ end
183
+ end
184
+
90
185
  it "use custom config from file" do
91
186
  configurator.config.should == yaml_hash["config"]["database"]
92
187
  end
@@ -4,13 +4,13 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = "standalone_migrations"
8
- s.version = "1.0.4"
7
+ s.name = %q{standalone_migrations}
8
+ s.version = "1.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Todd Huss", "Michael Grosser"]
12
- s.date = "2011-11-04"
13
- s.email = "thuss@gabrito.com"
11
+ s.authors = [%q{Todd Huss}, %q{Michael Grosser}]
12
+ s.date = %q{2011-11-13}
13
+ s.email = %q{thuss@gabrito.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.markdown"
16
16
  ]
@@ -32,10 +32,10 @@ Gem::Specification.new do |s|
32
32
  "vendor/migration_helpers/init.rb",
33
33
  "vendor/migration_helpers/lib/migration_helper.rb"
34
34
  ]
35
- s.homepage = "http://github.com/thuss/standalone-migrations"
36
- s.require_paths = ["lib"]
37
- s.rubygems_version = "1.8.11"
38
- s.summary = "A thin wrapper to use Rails Migrations in non Rails projects"
35
+ s.homepage = %q{http://github.com/thuss/standalone-migrations}
36
+ s.require_paths = [%q{lib}]
37
+ s.rubygems_version = %q{1.8.7}
38
+ s.summary = %q{A thin wrapper to use Rails Migrations in non Rails projects}
39
39
 
40
40
  if s.respond_to? :specification_version then
41
41
  s.specification_version = 3
metadata CHANGED
@@ -1,46 +1,49 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: standalone_migrations
3
- version: !ruby/object:Gem::Version
4
- version: 1.0.4
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 1.0.5
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Todd Huss
9
9
  - Michael Grosser
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-11-04 00:00:00.000000000Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
13
+
14
+ date: 2011-11-13 00:00:00 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
16
17
  name: rake
17
- requirement: &70273003731020 !ruby/object:Gem::Requirement
18
+ requirement: &id001 !ruby/object:Gem::Requirement
18
19
  none: false
19
- requirements:
20
- - - ! '>='
21
- - !ruby/object:Gem::Version
22
- version: '0'
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
23
24
  type: :runtime
24
25
  prerelease: false
25
- version_requirements: *70273003731020
26
- - !ruby/object:Gem::Dependency
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
27
28
  name: activerecord
28
- requirement: &70273003730340 !ruby/object:Gem::Requirement
29
+ requirement: &id002 !ruby/object:Gem::Requirement
29
30
  none: false
30
- requirements:
31
- - - ! '>='
32
- - !ruby/object:Gem::Version
33
- version: '3'
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "3"
34
35
  type: :runtime
35
36
  prerelease: false
36
- version_requirements: *70273003730340
37
+ version_requirements: *id002
37
38
  description:
38
39
  email: thuss@gabrito.com
39
40
  executables: []
41
+
40
42
  extensions: []
41
- extra_rdoc_files:
43
+
44
+ extra_rdoc_files:
42
45
  - README.markdown
43
- files:
46
+ files:
44
47
  - Gemfile
45
48
  - Gemfile.lock
46
49
  - MIT-LICENSE
@@ -59,26 +62,30 @@ files:
59
62
  - vendor/migration_helpers/lib/migration_helper.rb
60
63
  homepage: http://github.com/thuss/standalone-migrations
61
64
  licenses: []
65
+
62
66
  post_install_message:
63
67
  rdoc_options: []
64
- require_paths:
68
+
69
+ require_paths:
65
70
  - lib
66
- required_ruby_version: !ruby/object:Gem::Requirement
71
+ required_ruby_version: !ruby/object:Gem::Requirement
67
72
  none: false
68
- requirements:
69
- - - ! '>='
70
- - !ruby/object:Gem::Version
71
- version: '0'
72
- required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
78
  none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
78
83
  requirements: []
84
+
79
85
  rubyforge_project:
80
- rubygems_version: 1.8.11
86
+ rubygems_version: 1.8.7
81
87
  signing_key:
82
88
  specification_version: 3
83
89
  summary: A thin wrapper to use Rails Migrations in non Rails projects
84
90
  test_files: []
91
+