ejhayes_standalone_migrations 2.2.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/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.2.0
@@ -0,0 +1,5 @@
1
+ *.sqlite3
2
+ Gemfile
3
+ .bundle/
4
+ bin/
5
+ Gemfile.lock
@@ -0,0 +1,9 @@
1
+ # $LOAD_PATH handling not need if the standalone_migrations
2
+ # gem is installed
3
+ lib = File.expand_path("../../lib", __FILE__)
4
+ $:.unshift lib unless $:.include?(lib)
5
+
6
+ require "standalone_migrations"
7
+ StandaloneMigrations::Tasks.load_tasks
8
+
9
+ # and that's it!
@@ -0,0 +1,15 @@
1
+ development:
2
+ adapter: sqlite3
3
+ database: db/development.sqlite3
4
+ pool: 5
5
+ timeout: 5000
6
+ test:
7
+ adapter: sqlite3
8
+ database: db/test.sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+ production:
12
+ adapter: sqlite3
13
+ database: db/production.sqlite3
14
+ pool: 5
15
+ timeout: 5000
@@ -0,0 +1,7 @@
1
+ class CreateTableAwesomeMigration < ActiveRecord::Migration
2
+ def up
3
+ end
4
+
5
+ def down
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ lib_path = File.expand_path("../", __FILE__)
2
+ $:.unshift lib_path unless $:.include?(lib_path)
3
+
4
+ require "rubygems"
5
+ require "rails"
6
+ require "active_record"
7
+
8
+ require "standalone_migrations/configurator"
9
+ require "standalone_migrations/generator"
10
+ require "standalone_migrations/callbacks"
11
+
12
+ railtie_app_path = "#{lib_path}/standalone_migrations/minimal_railtie_config"
13
+ APP_PATH = File.expand_path(railtie_app_path, __FILE__)
14
+
15
+ require "standalone_migrations/minimal_railtie_config"
16
+ require "standalone_migrations/tasks"
17
+
18
+ if !ENV["RAILS_ENV"]
19
+ ENV["RAILS_ENV"] = ENV["DB"] || ENV["RACK_ENV"] || Rails.env || "development"
20
+ end
@@ -0,0 +1,13 @@
1
+ module StandaloneMigrations
2
+ @@callbacks ||= []
3
+
4
+ def self.on_loaded(&block)
5
+ @@callbacks << block
6
+ end
7
+
8
+ def self.run_on_load_callbacks
9
+ @@callbacks.each do |callback|
10
+ callback.call
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,88 @@
1
+ require 'active_support/all'
2
+
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
+
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
30
+
31
+ def initialize(options = {})
32
+ defaults = {
33
+ :config => "db/config.yml",
34
+ :migrate_dir => "db/migrate",
35
+ :seeds => "db/seeds.rb",
36
+ :schema => "db/schema.rb"
37
+ }
38
+ @options = load_from_file(defaults.dup) || defaults.merge(options)
39
+ ENV['SCHEMA'] = ENV['SCHEMA'] || File.expand_path(schema)
40
+ end
41
+
42
+ def config
43
+ @options[:config]
44
+ end
45
+
46
+ def migrate_dir
47
+ @options[:migrate_dir]
48
+ end
49
+
50
+ def seeds
51
+ @options[:seeds]
52
+ end
53
+
54
+ def schema
55
+ @options[:schema]
56
+ end
57
+
58
+ def config_for_all
59
+ Configurator.load_configurations.dup
60
+ end
61
+
62
+ def config_for(environment)
63
+ config_for_all[environment]
64
+ end
65
+
66
+ private
67
+
68
+ def configuration_file
69
+ if !ENV['DATABASE']
70
+ ".standalone_migrations"
71
+ else
72
+ ".#{ENV['DATABASE']}.standalone_migrations"
73
+ end
74
+ end
75
+
76
+ def load_from_file(defaults)
77
+ return nil unless File.exists? configuration_file
78
+ config = YAML.load( ERB.new(IO.read(configuration_file)).result )
79
+ {
80
+ :config => config["config"] ? config["config"]["database"] : defaults[:config],
81
+ :migrate_dir => (config["db"] || {})["migrate"] || defaults[:migrate_dir],
82
+ :seeds => (config["db"] || {})["seeds"] || defaults[:seeds],
83
+ :schema => (config["db"] || {})["schema"] || defaults[:schema]
84
+ }
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,11 @@
1
+ # these generators are backed by rails' generators
2
+ require "rails/generators"
3
+ module StandaloneMigrations
4
+ class Generator
5
+ def self.migration(name, options="")
6
+ generator_params = [name] + options.split(" ")
7
+ Rails::Generators.invoke "active_record:migration", generator_params,
8
+ :destination_root => Rails.root
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module StandaloneMigrations
2
+ class StandaloneMigrations::MinimalRailtieConfig < Rails::Application
3
+ config.generators.options[:rails] = {:orm => :active_record}
4
+
5
+ config.generators.options[:active_record] = {
6
+ :migration => true,
7
+ :timestamps => true
8
+ }
9
+ end
10
+ end
@@ -0,0 +1,36 @@
1
+ module StandaloneMigrations
2
+ class Tasks
3
+ class << self
4
+ def configure
5
+ Deprecations.new.call
6
+ configurator = Configurator.new
7
+ paths = Rails.application.config.paths
8
+ paths.add "config/database", :with => configurator.config
9
+ paths.add "db/migrate", :with => configurator.migrate_dir
10
+ paths.add "db/seeds", :with => configurator.seeds
11
+ end
12
+
13
+ def load_tasks
14
+ configure
15
+
16
+ MinimalRailtieConfig.load_tasks
17
+ %w(
18
+ connection
19
+ environment
20
+ db/new_migration
21
+ ).each do
22
+ |task| load "standalone_migrations/tasks/#{task}.rake"
23
+ end
24
+ load "active_record/railties/databases.rake"
25
+ end
26
+ end
27
+ end
28
+
29
+ class Tasks::Deprecations
30
+ def call
31
+ if File.directory?('db/migrations')
32
+ puts "DEPRECATED move your migrations into db/migrate"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path("../../../standalone_migrations", __FILE__)
2
+ namespace :standalone do
3
+ task :connection do
4
+ configurator = StandaloneMigrations::Configurator.new
5
+ ActiveRecord::Base.establish_connection configurator.config_for(Rails.env)
6
+ StandaloneMigrations.run_on_load_callbacks
7
+ end
8
+ end
@@ -0,0 +1,19 @@
1
+ namespace :db do
2
+ desc "Creates a new migration file with the specified name"
3
+ task :new_migration, :name, :options do |t, args|
4
+ name = args[:name] || ENV['name']
5
+ options = args[:options] || ENV['options']
6
+
7
+ unless name
8
+ puts "Error: must provide name of migration to generate."
9
+ puts "For example: rake #{t.name} name=add_field_to_form"
10
+ abort
11
+ end
12
+
13
+ if options
14
+ StandaloneMigrations::Generator.migration name, options.gsub('/', ' ')
15
+ else
16
+ StandaloneMigrations::Generator.migration name
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ require File.expand_path("../../../standalone_migrations", __FILE__)
2
+ task :environment => ["standalone:connection"] do
3
+ end
@@ -0,0 +1,10 @@
1
+ # I really think we should deprecate this file, seems to me that is
2
+ # more explicit if the user require "standalone_migrations" and then
3
+ # call the load_tasks methods in his Rakefile. But this only a
4
+ # suggestion, and we can get rid of this comment if others on the
5
+ # project don't agree with that
6
+ #
7
+ # Ricardo Valeriano
8
+
9
+ require File.expand_path("../../standalone_migrations", __FILE__)
10
+ StandaloneMigrations::Tasks.load_tasks
@@ -0,0 +1,4 @@
1
+ $: << File.join(File.expand_path('../', __FILE__), 'lib')
2
+
3
+ require 'rake'
4
+ require 'standalone_migrations'
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ module StandaloneMigrations
4
+
5
+ describe "Callbacks" do
6
+
7
+ describe ".on_loaded" do
8
+
9
+ it "responds to on_loaded" do
10
+ StandaloneMigrations.should respond_to :on_loaded
11
+ end
12
+
13
+ it "responds to run_on_load_callbacks" do
14
+ StandaloneMigrations.should respond_to :run_on_load_callbacks
15
+ end
16
+
17
+ it "can pass a block do on_loaded" do
18
+ callback_was_called = false
19
+
20
+ StandaloneMigrations.on_loaded do
21
+ callback_was_called = true
22
+ end
23
+
24
+ # invoke the callbacks
25
+ StandaloneMigrations.run_on_load_callbacks
26
+
27
+ callback_was_called.should be_true
28
+ end
29
+
30
+ it "can pass multiple blocks to on_loaded" do
31
+ callback_count = 0
32
+
33
+ for i in 1..4
34
+ StandaloneMigrations.on_loaded do
35
+ callback_count += 1
36
+ end
37
+ end
38
+
39
+ StandaloneMigrations.run_on_load_callbacks
40
+
41
+ callback_count.should == 4
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,263 @@
1
+ require 'spec_helper'
2
+ require 'yaml'
3
+
4
+ module StandaloneMigrations
5
+ describe Configurator, "which allows define custom dirs and files to work with your migrations" do
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
+ let(:env_hash_other_db) do
18
+ {
19
+ "development" => { "adapter" => "mysql2", "database" => "database_name" },
20
+ "test" => { "adapter" => "mysql2", "database" => "database_name" },
21
+ "production" => {"adapter" => "mysql2", "database" => "database_name" }
22
+ }
23
+ end
24
+
25
+ before(:all) do
26
+ @original_dir = Dir.pwd
27
+ Dir.chdir( File.expand_path("../../", __FILE__) )
28
+ FileUtils.mkdir_p "tmp/db"
29
+ Dir.chdir "tmp"
30
+ File.open("db/config.yml", "w") do |f|
31
+ f.write env_hash.to_yaml
32
+ end
33
+ end
34
+
35
+ it "load the specific environment config" do
36
+ config = Configurator.new.config_for(:development)
37
+ config.should == env_hash["development"]
38
+ end
39
+
40
+ it "load the yaml with environment configurations" do
41
+ config = Configurator.new.config_for(:development)
42
+ config[:database].should == "db/development.sql"
43
+ end
44
+
45
+ it "allow access the original configuration hash (for all environments)" do
46
+ Configurator.new.config_for_all.should == env_hash
47
+ end
48
+
49
+ context "customizing the environments configuration dynamically" do
50
+
51
+ let(:configurator) { Configurator.new }
52
+ let(:new_config) { { 'sbrobous' => 'test' } }
53
+
54
+ before(:all) do
55
+ Configurator.environments_config do |env|
56
+ env.on "production" do
57
+ new_config
58
+ end
59
+ end
60
+ end
61
+
62
+ it "allow changes on the configuration hashes" do
63
+ configurator.config_for("production").should == new_config
64
+ end
65
+
66
+ it "return current configuration if block yielding returns nil" do
67
+ Configurator.environments_config do |env|
68
+ env.on "production" do
69
+ nil
70
+ end
71
+ end
72
+ configurator.config_for("production").should == new_config
73
+ end
74
+
75
+ it "pass the current configuration as block argument" do
76
+ Configurator.environments_config do |env|
77
+ env.on "production" do |current_config|
78
+ current_config.should == new_config
79
+ end
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ after(:all) do
86
+ Dir.chdir @original_dir
87
+ end
88
+
89
+ end
90
+
91
+ context "default values when .standalone_configurations is missing" do
92
+
93
+ let(:configurator) do
94
+ Configurator.new
95
+ end
96
+
97
+ it "use config/database.yml" do
98
+ configurator.config.should == 'db/config.yml'
99
+ end
100
+
101
+ it "use db/migrate dir" do
102
+ configurator.migrate_dir.should == 'db/migrate'
103
+ end
104
+
105
+ it "use db/seeds.rb" do
106
+ configurator.seeds.should == "db/seeds.rb"
107
+ end
108
+
109
+ it "use db/schema.rb" do
110
+ configurator.schema.should == "db/schema.rb"
111
+ end
112
+
113
+ end
114
+
115
+ context "passing configurations as a parameter" do
116
+ let(:args) do
117
+ {
118
+ :config => "custom/config/database.yml",
119
+ :migrate_dir => "custom/db/migrate",
120
+ :seeds => "custom/db/seeds.rb",
121
+ :schema => "custom/db/schema.rb"
122
+ }
123
+ end
124
+
125
+ let(:configurator) do
126
+ Configurator.new args
127
+ end
128
+
129
+ it "use custom config" do
130
+ configurator.config.should == args[:config]
131
+ end
132
+
133
+ it "use custom migrate dir" do
134
+ configurator.migrate_dir.should == args[:migrate_dir]
135
+ end
136
+
137
+ it "use custom seeds" do
138
+ configurator.seeds.should == args[:seeds]
139
+ end
140
+
141
+ it "use custom schema" do
142
+ configurator.schema.should == args[:schema]
143
+ end
144
+
145
+ end
146
+
147
+ context "using a .standalone_migrations file with configurations" do
148
+
149
+ before(:all) do
150
+ @original_dir = Dir.pwd
151
+ Dir.chdir File.expand_path("../", __FILE__)
152
+ end
153
+
154
+ let(:yaml_hash) do
155
+ {
156
+ "db" => {
157
+ "seeds" => "file/db/seeds.rb",
158
+ "migrate" => "file/db/migrate",
159
+ "schema" => "file/db/schema.rb"
160
+ },
161
+ "config" => {
162
+ "database" => "file/config/database.yml"
163
+ }
164
+ }
165
+ end
166
+
167
+ let(:yaml_hash_other_db) do
168
+ {
169
+ "db" => {
170
+ "seeds" => "db2/seeds.rb",
171
+ "migrate" => "db2/migrate",
172
+ "schema" => "db2/schema.rb"
173
+ },
174
+ "config" => {
175
+ "database" => "config/config_other.yml"
176
+ }
177
+ }
178
+ end
179
+
180
+ let(:configurator) do
181
+ file = ".standalone_migrations"
182
+ File.open(file, "w") { |file| file.write(yaml_hash.to_yaml) }
183
+ Configurator.new
184
+ end
185
+
186
+ context "with database environment variable passed" do
187
+
188
+ before(:all) do
189
+ ENV['DATABASE'] = "other_db"
190
+ end
191
+
192
+ let(:other_configurator) do
193
+ file_other_db = ".other_db.standalone_migrations"
194
+ File.open(file_other_db, "w") { |file| file.write(yaml_hash_other_db.to_yaml) }
195
+ Configurator.new
196
+ end
197
+
198
+ it "look up named dot file" do
199
+ other_configurator.config.should == yaml_hash_other_db['config']['database']
200
+ end
201
+
202
+ it "load config from named dot file" do
203
+ other_configurator.migrate_dir.should == 'db2/migrate'
204
+ end
205
+
206
+ after(:all) do
207
+ File.delete ".other_db.standalone_migrations"
208
+ ENV['DATABASE'] = nil
209
+ end
210
+
211
+ end
212
+
213
+ context "with some configurations missing" do
214
+
215
+ let(:yaml_hash) do
216
+ {
217
+ "config" => {
218
+ "database" => "file/config/database.yml"
219
+ },
220
+ "db" => {
221
+ "seeds" => "file/db/seeds.rb"
222
+ }
223
+ }
224
+ end
225
+
226
+ it "use default values for the missing configurations" do
227
+ configurator.migrate_dir.should == 'db/migrate'
228
+ end
229
+
230
+ it "use custom config from file" do
231
+ configurator.config.should == yaml_hash["config"]["database"]
232
+ end
233
+
234
+ it "use custom config value from partial configuration" do
235
+ configurator.seeds.should == yaml_hash["db"]["seeds"]
236
+ end
237
+
238
+ end
239
+
240
+ it "use custom config from file" do
241
+ configurator.config.should == yaml_hash["config"]["database"]
242
+ end
243
+
244
+ it "use custom migrate dir from file" do
245
+ configurator.migrate_dir.should == yaml_hash["db"]["migrate"]
246
+ end
247
+
248
+ it "use custom seeds from file" do
249
+ configurator.seeds.should == yaml_hash["db"]["seeds"]
250
+ end
251
+
252
+ it "use custom schema from file" do
253
+ configurator.schema.should == yaml_hash["db"]["schema"]
254
+ end
255
+
256
+ after(:all) do
257
+ File.delete ".standalone_migrations"
258
+ Dir.chdir @original_dir
259
+ end
260
+
261
+ end
262
+ end
263
+ end