cairn 7.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,34 @@
1
+ module StandaloneMigrations
2
+ class Tasks
3
+ class << self
4
+ def configure(options = {})
5
+ Deprecations.new.call
6
+ Configurator.new options
7
+ end
8
+
9
+ def load_tasks(options = {})
10
+ configure(options)
11
+ Configurator.environments_config do |proxy|
12
+ ActiveRecord::Tasks::DatabaseTasks.database_configuration = proxy.configurations
13
+ end
14
+ MinimalRailtieConfig.load_tasks
15
+ %w(
16
+ connection
17
+ environment
18
+ db/new_migration
19
+ ).each do
20
+ |task| load "standalone_migrations/tasks/#{task}.rake"
21
+ end
22
+ load "active_record/railties/databases.rake"
23
+ end
24
+ end
25
+ end
26
+
27
+ class Tasks::Deprecations
28
+ def call
29
+ if File.directory?('db/migrations')
30
+ puts "DEPRECATED move your migrations into db/migrate"
31
+ end
32
+ end
33
+ end
34
+ 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,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
+ expect(StandaloneMigrations).to respond_to :on_loaded
11
+ end
12
+
13
+ it "responds to run_on_load_callbacks" do
14
+ expect(StandaloneMigrations).to 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
+ expect(callback_was_called).to 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
+ expect(callback_count).to eq(4)
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,261 @@
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
+
10
+ let(:env_hash_other_db) do
11
+ {
12
+ "development" => { "adapter" => "mysql2", "database" => "database_name" },
13
+ "test" => { "adapter" => "mysql2", "database" => "database_name" },
14
+ "production" => {"adapter" => "mysql2", "database" => "database_name" }
15
+ }
16
+ end
17
+
18
+ before(:all) do
19
+ @env_hash = {
20
+ "development" => { "adapter" => "sqlite3", "database" => "db/development.sql" },
21
+ "test" => { "adapter" => "sqlite3", "database" => "db/test.sql" },
22
+ "production" => {"adapter" => "sqlite3", "database" => ":memory:" }
23
+ }
24
+ @original_dir = Dir.pwd
25
+ Dir.chdir( File.expand_path("../../", __FILE__) )
26
+ FileUtils.mkdir_p "tmp/db"
27
+ Dir.chdir "tmp"
28
+ File.open("db/config.yml", "w") do |f|
29
+ f.write @env_hash.to_yaml
30
+ end
31
+ end
32
+
33
+ it "load the specific environment config" do
34
+ config = Configurator.new.config_for(:development)
35
+ expect(config).to eq(@env_hash["development"])
36
+ end
37
+
38
+ it "load the yaml with environment configurations" do
39
+ config = Configurator.new.config_for(:development)
40
+ expect(config["database"]).to eq("db/development.sql")
41
+ end
42
+
43
+ it "allow access the original configuration hash (for all environments)" do
44
+ expect(Configurator.new.config_for_all).to eq(@env_hash)
45
+ end
46
+
47
+ context "customizing the environments configuration dynamically" do
48
+
49
+ let(:configurator) { Configurator.new }
50
+
51
+ before(:all) do
52
+ @new_config = { 'sbrobous' => 'test' }
53
+ Configurator.environments_config do |env|
54
+ env.on "production" do
55
+ @new_config
56
+ end
57
+ end
58
+ end
59
+
60
+ it "allow changes on the configuration hashes" do
61
+ expect(configurator.config_for("production")).to eq(@new_config)
62
+ end
63
+
64
+ it "return current configuration if block yielding returns nil" do
65
+ Configurator.environments_config do |env|
66
+ env.on "production" do
67
+ nil
68
+ end
69
+ end
70
+ expect(configurator.config_for("production")).to eq(@new_config)
71
+ end
72
+
73
+ it "pass the current configuration as block argument" do
74
+ Configurator.environments_config do |env|
75
+ env.on "production" do |current_config|
76
+ expect(current_config).to eq(@new_config)
77
+ end
78
+ end
79
+ end
80
+
81
+ end
82
+
83
+ after(:all) do
84
+ Dir.chdir @original_dir
85
+ end
86
+
87
+ end
88
+
89
+ context "default values when .standalone_configurations is missing" do
90
+
91
+ let(:configurator) do
92
+ Configurator.new
93
+ end
94
+
95
+ it "use config/database.yml" do
96
+ expect(configurator.config).to eq('db/config.yml')
97
+ end
98
+
99
+ it "use db/migrate dir" do
100
+ expect(configurator.migrate_dir).to eq('db/migrate')
101
+ end
102
+
103
+ it "use db/seeds.rb" do
104
+ expect(configurator.seeds).to eq("db/seeds.rb")
105
+ end
106
+
107
+ it "use db/schema.rb" do
108
+ expect(configurator.schema).to end_with("db/schema.rb")
109
+ end
110
+
111
+ end
112
+
113
+ context "passing configurations as a parameter" do
114
+ let(:args) do
115
+ {
116
+ :config => "custom/config/database.yml",
117
+ :migrate_dir => "custom/db/migrate",
118
+ :seeds => "custom/db/seeds.rb",
119
+ :schema => "custom/db/schema.rb"
120
+ }
121
+ end
122
+
123
+ let(:configurator) do
124
+ Configurator.new args
125
+ end
126
+
127
+ it "use custom config" do
128
+ expect(configurator.config).to eq(args[:config])
129
+ end
130
+
131
+ it "use custom migrate dir" do
132
+ expect(configurator.migrate_dir).to eq(args[:migrate_dir])
133
+ end
134
+
135
+ it "use custom seeds" do
136
+ expect(configurator.seeds).to eq(args[:seeds])
137
+ end
138
+
139
+ it "use custom schema" do
140
+ expect(configurator.schema).to eq(args[:schema])
141
+ end
142
+
143
+ end
144
+
145
+ context "using a .standalone_migrations file with configurations" do
146
+
147
+ before(:all) do
148
+ @original_dir = Dir.pwd
149
+ Dir.chdir File.expand_path("../", __FILE__)
150
+ end
151
+
152
+ let(:yaml_hash) do
153
+ {
154
+ "db" => {
155
+ "seeds" => "file/db/seeds.rb",
156
+ "migrate" => "file/db/migrate",
157
+ "schema" => "file/db/schema.rb"
158
+ },
159
+ "config" => {
160
+ "database" => "file/config/database.yml"
161
+ }
162
+ }
163
+ end
164
+
165
+ let(:yaml_hash_other_db) do
166
+ {
167
+ "db" => {
168
+ "seeds" => "db2/seeds.rb",
169
+ "migrate" => "db2/migrate",
170
+ "schema" => "db2/schema.rb"
171
+ },
172
+ "config" => {
173
+ "database" => "config/config_other.yml"
174
+ }
175
+ }
176
+ end
177
+
178
+ let(:configurator) do
179
+ file = ".standalone_migrations"
180
+ File.open(file, "w") { |file| file.write(yaml_hash.to_yaml) }
181
+ Configurator.new
182
+ end
183
+
184
+ context "with database environment variable passed" do
185
+
186
+ before(:all) do
187
+ ENV['DATABASE'] = "other_db"
188
+ end
189
+
190
+ let(:other_configurator) do
191
+ file_other_db = ".other_db.standalone_migrations"
192
+ File.open(file_other_db, "w") { |file| file.write(yaml_hash_other_db.to_yaml) }
193
+ Configurator.new
194
+ end
195
+
196
+ it "look up named dot file" do
197
+ expect(other_configurator.config).to eq(yaml_hash_other_db['config']['database'])
198
+ end
199
+
200
+ it "load config from named dot file" do
201
+ expect(other_configurator.migrate_dir).to eq('db2/migrate')
202
+ end
203
+
204
+ after(:all) do
205
+ File.delete ".other_db.standalone_migrations"
206
+ ENV['DATABASE'] = nil
207
+ end
208
+
209
+ end
210
+
211
+ context "with some configurations missing" do
212
+
213
+ let(:yaml_hash) do
214
+ {
215
+ "config" => {
216
+ "database" => "file/config/database.yml"
217
+ },
218
+ "db" => {
219
+ "seeds" => "file/db/seeds.rb"
220
+ }
221
+ }
222
+ end
223
+
224
+ it "use default values for the missing configurations" do
225
+ expect(configurator.migrate_dir).to eq('db/migrate')
226
+ end
227
+
228
+ it "use custom config from file" do
229
+ expect(configurator.config).to eq(yaml_hash["config"]["database"])
230
+ end
231
+
232
+ it "use custom config value from partial configuration" do
233
+ expect(configurator.seeds).to eq(yaml_hash["db"]["seeds"])
234
+ end
235
+
236
+ end
237
+
238
+ it "use custom config from file" do
239
+ expect(configurator.config).to eq(yaml_hash["config"]["database"])
240
+ end
241
+
242
+ it "use custom migrate dir from file" do
243
+ expect(configurator.migrate_dir).to eq(yaml_hash["db"]["migrate"])
244
+ end
245
+
246
+ it "use custom seeds from file" do
247
+ expect(configurator.seeds).to eq(yaml_hash["db"]["seeds"])
248
+ end
249
+
250
+ it "use custom schema from file" do
251
+ expect(configurator.schema).to eq(yaml_hash["db"]["schema"])
252
+ end
253
+
254
+ after(:all) do
255
+ File.delete ".standalone_migrations"
256
+ Dir.chdir @original_dir
257
+ end
258
+
259
+ end
260
+ end
261
+ end