replicate 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/replicate CHANGED
@@ -6,12 +6,14 @@
6
6
  #/ expression. The --load form reads dump data from stdin and loads into the
7
7
  #/ current environment.
8
8
  #/
9
- #/ Options:
10
- #/ -r, --require Require the library. Often used with 'config/environment'.
9
+ #/ Mode selection:
11
10
  #/ -d, --dump Dump the repository and all related objects to stdout.
12
11
  #/ -l, --load Load dump file data from stdin.
13
- #/ -i, --keep-id Use replicated ids when loading dump file.
14
12
  #/
13
+ #/ Options:
14
+ #/ -r, --require Require the library. Often used with 'config/environment'.
15
+ #/ -i, --keep-id Use replicated ids when loading dump file.
16
+ #/ -f, --force Allow loading in production environments.
15
17
  #/ -v, --verbose Write more status output.
16
18
  #/ -q, --quiet Write less status output.
17
19
  $stderr.sync = true
@@ -23,10 +25,12 @@ verbose = false
23
25
  quiet = false
24
26
  keep_id = false
25
27
  out = $stdout
28
+ force = false
26
29
 
27
30
  # parse arguments
28
31
  file = __FILE__
29
32
  usage = lambda { exec "grep ^#/<'#{file}'|cut -c4-" }
33
+ original_argv = ARGV.dup
30
34
  ARGV.options do |opts|
31
35
  opts.on("-d", "--dump") { mode = :dump }
32
36
  opts.on("-l", "--load") { mode = :load }
@@ -34,20 +38,17 @@ ARGV.options do |opts|
34
38
  opts.on("-v", "--verbose") { verbose = true }
35
39
  opts.on("-q", "--quiet") { quiet = true }
36
40
  opts.on("-i", "--keep-id") { keep_id = true }
41
+ opts.on("--force") { force = true }
37
42
  opts.on_tail("-h", "--help", &usage)
38
43
  opts.parse!
39
44
  end
40
45
 
41
- # load rails environment and replicator lib.
46
+ # load replicate lib and setup AR
42
47
  require 'replicate'
43
-
44
48
  if defined?(ActiveRecord::Base)
49
+ Replicate::AR
45
50
  ActiveRecord::Base.replicate_id = keep_id
46
- # hack to enable AR query cache
47
- ActiveRecord::ConnectionAdapters::QueryCache.
48
- send :attr_writer, :query_cache, :query_cache_enabled
49
- ActiveRecord::Base.connection.send(:query_cache=, {})
50
- ActiveRecord::Base.connection.send(:query_cache_enabled=, true)
51
+ ActiveRecord::Base.connection.enable_query_cache!
51
52
  end
52
53
 
53
54
  # dump mode means we're reading records from the database here and writing to
@@ -64,9 +65,14 @@ if mode == :dump
64
65
  # load mode means we're reading objects from stdin and creating them under
65
66
  # the current environment.
66
67
  elsif mode == :load
67
- Replicate::Loader.new do |loader|
68
- loader.log_to $stderr, verbose, quiet
69
- loader.read $stdin
68
+ if Replicate.production_environment? && !force
69
+ abort "error: refusing to load in production environment\n" +
70
+ " manual override: #{File.basename($0)} --force #{original_argv.join(' ')}"
71
+ else
72
+ Replicate::Loader.new do |loader|
73
+ loader.log_to $stderr, verbose, quiet
74
+ loader.read $stdin
75
+ end
70
76
  end
71
77
 
72
78
  # mode not set means no -l or -d arg was given. show usage and bail.
data/lib/replicate.rb CHANGED
@@ -6,5 +6,19 @@ module Replicate
6
6
  autoload :Status, 'replicate/status'
7
7
  autoload :AR, 'replicate/active_record'
8
8
 
9
+ # Determine if this is a production looking environment. Used in bin/replicate
10
+ # to safeguard against loading in production.
11
+ def self.production_environment?
12
+ if defined?(Rails) && Rails.respond_to?(:env)
13
+ Rails.env.to_s == 'production'
14
+ elsif defined?(RAILS_ENV)
15
+ RAILS_ENV == 'production'
16
+ elsif ENV['RAILS_ENV']
17
+ ENV['RAILS_ENV'] == 'production'
18
+ elsif ENV['RACK_ENV']
19
+ ENV['RAILS_ENV'] == 'production'
20
+ end
21
+ end
22
+
9
23
  AR if defined?(::ActiveRecord::Base)
10
24
  end
@@ -1,3 +1,6 @@
1
+ require 'active_record'
2
+ require 'active_record/version'
3
+
1
4
  module Replicate
2
5
  # ActiveRecord::Base instance methods used to dump replicant objects for the
3
6
  # record and all 1:1 associations. This module implements the replicant_id
@@ -209,6 +212,7 @@ module Replicate
209
212
  end
210
213
  end
211
214
  end
215
+
212
216
  end
213
217
 
214
218
  # Special object used to dump the list of associated ids for a
@@ -249,8 +253,25 @@ module Replicate
249
253
  end
250
254
  end
251
255
 
256
+ # Backport connection.enable_query_cache! for Rails 2.x
257
+ require 'active_record/connection_adapters/abstract/query_cache'
258
+ query_cache = ::ActiveRecord::ConnectionAdapters::QueryCache
259
+ if !query_cache.methods.any? { |m| m.to_sym == :enable_query_cache! }
260
+ query_cache.module_eval do
261
+ attr_writer :query_cache, :query_cache_enabled
262
+
263
+ def enable_query_cache!
264
+ @query_cache ||= {}
265
+ @query_cache_enabled = true
266
+ end
267
+
268
+ def disable_query_cache!
269
+ @query_cache_enabled = false
270
+ end
271
+ end
272
+ end
273
+
252
274
  # Load active record and install the extension methods.
253
- require 'active_record'
254
275
  ::ActiveRecord::Base.send :include, InstanceMethods
255
276
  ::ActiveRecord::Base.send :extend, ClassMethods
256
277
  ::ActiveRecord::Base.replicate_associations = []
@@ -349,4 +349,9 @@ class ActiveRecordTest < Test::Unit::TestCase
349
349
  user = User.find(user.id)
350
350
  assert_equal timestamp, user.updated_at
351
351
  end
352
+
353
+ def test_enabling_active_record_query_cache
354
+ ActiveRecord::Base.connection.enable_query_cache!
355
+ ActiveRecord::Base.connection.disable_query_cache!
356
+ end
352
357
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: replicate
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
9
- version: "1.1"
8
+ - 2
9
+ version: "1.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ryan Tomayko