ardb 0.28.0 → 0.29.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -7
  2. data/Gemfile +19 -8
  3. data/README.md +252 -3
  4. data/ardb.gemspec +8 -7
  5. data/bin/ardb +1 -1
  6. data/lib/ardb.rb +55 -39
  7. data/lib/ardb/adapter/base.rb +35 -30
  8. data/lib/ardb/adapter/mysql.rb +2 -17
  9. data/lib/ardb/adapter/postgresql.rb +24 -30
  10. data/lib/ardb/adapter/sqlite.rb +4 -8
  11. data/lib/ardb/adapter_spy.rb +2 -16
  12. data/lib/ardb/cli.rb +15 -15
  13. data/lib/ardb/cli/clirb.rb +14 -17
  14. data/lib/ardb/cli/commands.rb +204 -114
  15. data/lib/ardb/db_tests.rb +2 -4
  16. data/lib/ardb/default_order_by.rb +3 -13
  17. data/lib/ardb/migration.rb +9 -13
  18. data/lib/ardb/record_spy.rb +7 -26
  19. data/lib/ardb/relation_spy.rb +0 -6
  20. data/lib/ardb/require_autoloaded_active_record_files.rb +172 -58
  21. data/lib/ardb/test_helpers.rb +2 -5
  22. data/lib/ardb/use_db_default.rb +4 -15
  23. data/lib/ardb/version.rb +1 -1
  24. data/script/determine_autoloaded_active_record_files.rb +14 -9
  25. data/test/helper.rb +4 -13
  26. data/test/support/factory.rb +2 -2
  27. data/test/support/postgresql/migrations/{.gitkeep → .keep} +0 -0
  28. data/test/support/postgresql/schema.rb +0 -1
  29. data/test/support/postgresql/setup_test_db.rb +15 -15
  30. data/test/system/.keep +0 -0
  31. data/test/unit/adapter/base_tests.rb +76 -52
  32. data/test/unit/adapter/mysql_tests.rb +2 -19
  33. data/test/unit/adapter/postgresql_tests.rb +14 -23
  34. data/test/unit/adapter/sqlite_tests.rb +3 -11
  35. data/test/unit/adapter_spy_tests.rb +2 -16
  36. data/test/unit/ardb_tests.rb +57 -44
  37. data/test/unit/cli_tests.rb +220 -158
  38. data/test/unit/db_tests_tests.rb +3 -6
  39. data/test/unit/default_order_by_tests.rb +4 -8
  40. data/test/unit/migration_tests.rb +11 -15
  41. data/test/unit/record_spy_tests.rb +17 -22
  42. data/test/unit/relation_spy_tests.rb +17 -46
  43. data/test/unit/test_helpers_tests.rb +3 -14
  44. data/test/unit/use_db_default_tests.rb +7 -11
  45. metadata +100 -83
  46. data/lib/ardb/has_slug.rb +0 -107
  47. data/lib/ardb/migration_helpers.rb +0 -77
  48. data/lib/ardb/pg_json.rb +0 -90
  49. data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +0 -13
  50. data/test/system/pg_json_tests.rb +0 -85
  51. data/test/unit/has_slug_tests.rb +0 -341
  52. data/test/unit/migration_helpers_tests.rb +0 -65
  53. data/test/unit/pg_json_tests.rb +0 -39
@@ -1,11 +1,10 @@
1
- require 'active_record'
2
- require 'ardb'
1
+ require "active_record"
2
+ require "ardb"
3
3
 
4
4
  # Use theses helpers in your test suite. They all generally assume Ardb has
5
5
  # already been initialized by calling `Ardb.init`.
6
6
 
7
7
  module Ardb
8
-
9
8
  module TestHelpers
10
9
  module_function
11
10
 
@@ -73,7 +72,5 @@ module Ardb
73
72
  true
74
73
  end
75
74
  end
76
-
77
75
  end
78
-
79
76
  end
@@ -1,22 +1,16 @@
1
- require 'much-plugin'
1
+ require "much-plugin"
2
2
 
3
3
  module Ardb
4
-
5
4
  module UseDbDefault
6
5
  include MuchPlugin
7
6
 
8
7
  plugin_included do
9
- extend ClassMethods
10
- include InstanceMethods
11
-
12
8
  @ardb_use_db_default_attrs = []
13
9
 
14
10
  around_create :ardb_allow_db_to_default_attrs
15
-
16
11
  end
17
12
 
18
- module ClassMethods
19
-
13
+ plugin_class_methods do
20
14
  def ardb_use_db_default_attrs
21
15
  @ardb_use_db_default_attrs
22
16
  end
@@ -25,11 +19,9 @@ module Ardb
25
19
  @ardb_use_db_default_attrs += attrs.map(&:to_s)
26
20
  @ardb_use_db_default_attrs.uniq!
27
21
  end
28
-
29
22
  end
30
23
 
31
- module InstanceMethods
32
-
24
+ plugin_instance_methods do
33
25
  private
34
26
 
35
27
  def ardb_allow_db_to_default_attrs
@@ -42,15 +34,12 @@ module Ardb
42
34
  unchanged_names.each{ |name| @attributes.delete(name) }
43
35
  yield
44
36
  # we have to go and fetch the attr value from the DB, otherwise
45
- # activerecord doesn't know the value that the DB used
37
+ # activerecord doesn"t know the value that the DB used
46
38
  scope = self.class.where(:id => self.id)
47
39
  unchanged_names.each do |name|
48
40
  @attributes[name] = scope.pluck(name).first
49
41
  end
50
42
  end
51
-
52
43
  end
53
-
54
44
  end
55
-
56
45
  end
@@ -1,3 +1,3 @@
1
1
  module Ardb
2
- VERSION = "0.28.0"
2
+ VERSION = "0.29.1"
3
3
  end
@@ -1,8 +1,8 @@
1
- require 'active_record'
1
+ require "active_record"
2
2
 
3
- # this can be slow, this is one of the reasons this shouldn't be done during
3
+ # this can be slow, this is one of the reasons this shouldn"t be done during
4
4
  # the startup of our apps
5
- gemspec = Gem.loaded_specs['activerecord']
5
+ gemspec = Gem.loaded_specs["activerecord"]
6
6
 
7
7
  puts "Looking at files in: "
8
8
  puts " #{gemspec.lib_dirs_glob.inspect}"
@@ -11,16 +11,21 @@ paths = Dir["#{gemspec.lib_dirs_glob}/**/*.rb"]
11
11
 
12
12
  # these are regexs for files we want to ignore requiring. for example,
13
13
  # generators fail when we try to require them. the others are pieces of active
14
- # record we don't use in a production environment
14
+ # record we don"t use in a production environment
15
15
  ignored_regexes = [
16
16
  /rails\/generators/,
17
17
  /active_record\/railtie/,
18
18
  /active_record\/migration/,
19
19
  /active_record\/fixtures/,
20
+ /active_record\/fixture_set/,
20
21
  /active_record\/schema/,
21
22
  /active_record\/connection_adapters/,
22
23
  /active_record\/test_case/,
23
- /active_record\/coders\/yaml_column/
24
+ /active_record\/test_databases/,
25
+ /active_record\/test_fixtures/,
26
+ /active_record\/coders\/yaml_column/,
27
+ # `destroy_association_async_job` requires `ActiveJob` to be required.
28
+ /active_record\/destroy_association_async_job/,
24
29
  ]
25
30
 
26
31
  Result = Struct.new(:file, :state, :reason)
@@ -31,12 +36,12 @@ ignored = []
31
36
  errored = []
32
37
 
33
38
  paths.sort.each do |full_path|
34
- relative_path_with_rb = full_path.gsub("#{gemspec.lib_dirs_glob}/", '')
35
- relative_path = relative_path_with_rb.gsub(/\.rb\z/, '')
39
+ relative_path_with_rb = full_path.gsub("#{gemspec.lib_dirs_glob}/", "")
40
+ relative_path = relative_path_with_rb.gsub(/\.rb\z/, "")
36
41
 
37
42
  result = Result.new(relative_path)
38
43
 
39
- # see if it's ignored
44
+ # see if it"s ignored
40
45
  ignored_regexes.each do |regex|
41
46
  if relative_path =~ regex
42
47
  result.state = :ignored
@@ -87,5 +92,5 @@ puts "\n"
87
92
 
88
93
  puts "Needs To Be Required:\n"
89
94
  needs_to_be_required.each do |result|
90
- puts "require '#{result.file}'"
95
+ puts "require \"#{result.file}\""
91
96
  end
@@ -7,19 +7,10 @@ $LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
7
7
  TEST_SUPPORT_PATH = File.expand_path("../support", __FILE__)
8
8
  TMP_PATH = File.expand_path("../../tmp", __FILE__)
9
9
 
10
- require 'logger'
10
+ require "logger"
11
11
  log_path = File.expand_path("../../log/test.log", __FILE__)
12
- TEST_LOGGER = Logger.new(File.open(log_path, 'w'))
12
+ TEST_LOGGER = Logger.new(File.open(log_path, "w"))
13
13
 
14
14
  # require pry for debugging (`binding.pry`)
15
- require 'pry'
16
- require 'test/support/factory'
17
-
18
- # 1.8.7 backfills
19
-
20
- # Array#sample
21
- if !(a = Array.new).respond_to?(:sample) && a.respond_to?(:choice)
22
- class Array
23
- alias_method :sample, :choice
24
- end
25
- end
15
+ require "pry"
16
+ require "test/support/factory"
@@ -1,10 +1,10 @@
1
- require 'assert/factory'
1
+ require "assert/factory"
2
2
 
3
3
  module Factory
4
4
  extend Assert::Factory
5
5
 
6
6
  def self.migration_id
7
- # identifiers need to be plural b/c af activesupport's pluralize
7
+ # identifiers need to be plural b/c af activesupport"s pluralize
8
8
  "#{Factory.string}_things"
9
9
  end
10
10
 
@@ -1,3 +1,2 @@
1
1
  ActiveRecord::Schema.define(:version => 0) do
2
-
3
2
  end
@@ -1,24 +1,24 @@
1
- require 'assert'
2
- require 'ardb'
1
+ require "assert"
2
+ require "ardb"
3
3
 
4
4
  class PostgresqlDbTests < Assert::Context
5
5
  setup do
6
- @orig_env_ardb_db_file = ENV['ARDB_DB_FILE']
6
+ @orig_env_ardb_db_file = ENV["ARDB_DB_FILE"]
7
7
  ActiveRecord::Base.logger = @orig_ar_loggerF
8
8
 
9
- # no-op, we're manually configuring ardb so we don't need this to do anything
10
- ENV['ARDB_DB_FILE'] = File.join(TEST_SUPPORT_PATH, 'require_test_db_file')
9
+ # no-op, we"re manually configuring ardb so we don"t need this to do anything
10
+ ENV["ARDB_DB_FILE"] = File.join(TEST_SUPPORT_PATH, "require_test_db_file")
11
11
 
12
12
  @ardb_config = Ardb::Config.new.tap do |c|
13
- c.adapter = 'postgresql'
14
- c.database = 'redding_ardb_test'
15
- c.encoding = 'unicode'
16
- c.min_messages = 'WARNING'
13
+ c.adapter = "postgresql"
14
+ c.database = "redding_ardb_test"
15
+ c.encoding = "unicode"
16
+ c.min_messages = "WARNING"
17
17
 
18
18
  c.logger = TEST_LOGGER
19
- c.root_path = File.join(TEST_SUPPORT_PATH, 'postgresql')
20
- c.migrations_path = 'migrations'
21
- c.schema_path = 'schema'
19
+ c.root_path = File.join(TEST_SUPPORT_PATH, "postgresql")
20
+ c.migrations_path = "migrations"
21
+ c.schema_path = "schema"
22
22
  c.schema_format = :ruby
23
23
  end
24
24
  Assert.stub(Ardb, :config){ @ardb_config }
@@ -31,8 +31,9 @@ class PostgresqlDbTests < Assert::Context
31
31
  Ardb.adapter.connect_db
32
32
  end
33
33
  teardown do
34
+ Ardb.reset_adapter
34
35
  ActiveRecord::Base.logger = @orig_ar_logger
35
- ENV['ARDB_DB_FILE'] = @orig_env_ardb_db_file
36
+ ENV["ARDB_DB_FILE"] = @orig_env_ardb_db_file
36
37
  end
37
38
 
38
39
  private
@@ -40,12 +41,11 @@ class PostgresqlDbTests < Assert::Context
40
41
  # useful when testing creating/dropping/migrating DBs
41
42
  def silence_stdout
42
43
  current_stdout = $stdout.dup
43
- $stdout = File.new('/dev/null', 'w')
44
+ $stdout = File.new("/dev/null", "w")
44
45
  begin
45
46
  yield
46
47
  ensure
47
48
  $stdout = current_stdout
48
49
  end
49
50
  end
50
-
51
51
  end
File without changes
@@ -1,14 +1,14 @@
1
- require 'assert'
2
- require 'ardb/adapter/base'
1
+ require "assert"
2
+ require "ardb/adapter/base"
3
3
 
4
- require 'ardb'
4
+ require "active_record/migration"
5
+ require "ardb"
5
6
  # This is needed by the schema dumper but it doesn't handle requiring it so we
6
7
  # have to manually, otherwise this errors when you try to run the adapter base
7
8
  # tests by themselves
8
- require 'active_support/core_ext/class/attribute_accessors'
9
+ require "active_support/core_ext/class/attribute_accessors"
9
10
 
10
11
  class Ardb::Adapter::Base
11
-
12
12
  class UnitTests < Assert::Context
13
13
  desc "Ardb::Adapter::Base"
14
14
  setup do
@@ -21,7 +21,6 @@ class Ardb::Adapter::Base
21
21
  should have_imeths :connect_hash, :database, :migrations_path
22
22
  should have_imeths :schema_format, :ruby_schema_path, :sql_schema_path
23
23
  should have_imeths :escape_like_pattern
24
- should have_imeths :foreign_key_add_sql, :foreign_key_drop_sql
25
24
  should have_imeths :create_db, :drop_db, :drop_tables
26
25
  should have_imeths :connect_db, :migrate_db
27
26
  should have_imeths :load_schema, :load_ruby_schema, :load_sql_schema
@@ -49,12 +48,15 @@ class Ardb::Adapter::Base
49
48
  "#{Factory.string}\\" \
50
49
  "#{Factory.string} " \
51
50
  "#{Factory.string}"
52
- exp = pattern.gsub("\\"){ "\\\\" }.gsub('%', "\\%").gsub('_', "\\_")
51
+ exp = pattern.gsub("\\"){ "\\\\" }.gsub("%", "\\%").gsub("_", "\\_")
53
52
  assert_equal exp, subject.escape_like_pattern(pattern)
53
+
54
+ pattern = Factory.string
55
+ assert_equal pattern, subject.escape_like_pattern(pattern)
54
56
  end
55
57
 
56
58
  should "allow using a custom escape char when escaping like patterns" do
57
- escape_char = '#'
59
+ escape_char = "#"
58
60
  pattern = "#{Factory.string}%" \
59
61
  "#{Factory.string}_" \
60
62
  "#{Factory.string}\\" \
@@ -62,15 +64,10 @@ class Ardb::Adapter::Base
62
64
  "#{Factory.string} " \
63
65
  "#{Factory.string}"
64
66
  exp = pattern.gsub(escape_char, "#{escape_char}#{escape_char}")
65
- exp = exp.gsub('%', "#{escape_char}%").gsub('_', "#{escape_char}_")
67
+ exp = exp.gsub("%", "#{escape_char}%").gsub("_", "#{escape_char}_")
66
68
  assert_equal exp, subject.escape_like_pattern(pattern, escape_char)
67
69
  end
68
70
 
69
- should "not implement the foreign key sql meths" do
70
- assert_raises(NotImplementedError){ subject.foreign_key_add_sql }
71
- assert_raises(NotImplementedError){ subject.foreign_key_drop_sql }
72
- end
73
-
74
71
  should "not implement the create and drop db methods" do
75
72
  assert_raises(NotImplementedError){ subject.create_db }
76
73
  assert_raises(NotImplementedError){ subject.drop_db }
@@ -92,7 +89,6 @@ class Ardb::Adapter::Base
92
89
  non_matching_adapter = Ardb::Adapter::Base.new(Factory.ardb_config)
93
90
  assert_not_equal non_matching_adapter, subject
94
91
  end
95
-
96
92
  end
97
93
 
98
94
  class ConnectDbTests < UnitTests
@@ -111,58 +107,64 @@ class Ardb::Adapter::Base
111
107
  @ar_with_connection_called = true
112
108
  end
113
109
 
114
- @adapter.connect_db
110
+ subject.connect_db
115
111
  end
116
112
 
117
113
  should "use activerecord to establish and then checkout a connection" do
118
114
  assert_equal subject.connect_hash, @ar_establish_connection_called_with
119
115
  assert_true @ar_with_connection_called
120
116
  end
121
-
122
117
  end
123
118
 
124
- class MigrateDbTests < UnitTests
125
- desc "`migrate_db`"
119
+ class MigrateTests < UnitTests
126
120
  setup do
127
- @orig_migrate_version_env_var = ENV['MIGRATE_VERSION']
128
- @orig_migrate_query_env_var = ENV['MIGRATE_QUIET']
129
-
130
- ENV["MIGRATE_VERSION"] = Factory.integer.to_s if Factory.boolean
131
- ENV["MIGRATE_QUIET"] = Factory.boolean.to_s if Factory.boolean
132
-
133
- @migrator_called_with = []
134
- Assert.stub(ActiveRecord::Migrator, :migrate) do |*args|
135
- @migrator_called_with = args
121
+ Assert.stub(ActiveRecord::MigrationContext, :new) do |*args, &block|
122
+ @fake_migration_context ||= FakeMigrationContext.new(*args)
136
123
  end
137
-
138
- @adapter.migrate_db
139
- end
140
- teardown do
141
- ENV['MIGRATE_VERSION'] = @orig_migrate_version_env_var
142
- ENV['MIGRATE_QUIET'] = @orig_migrate_query_env_var
143
124
  end
144
125
 
145
- should "add the ardb migration helper recorder to activerecord's command recorder" do
146
- exp = Ardb::MigrationHelpers::RecorderMixin
147
- assert_includes exp, ActiveRecord::Migration::CommandRecorder
126
+ should "migrate the db with `migrate_db`" do
127
+ subject.migrate_db
128
+
129
+ assert_equal [subject.migrations_path], @fake_migration_context.init_with
130
+ assert_equal [nil], @fake_migration_context.up_called_with
148
131
  end
149
132
 
150
- should "set the activerecord migrator's migrations path" do
151
- exp = subject.migrations_path
152
- assert_equal exp, ActiveRecord::Migrator.migrations_path
133
+ should "migrate the db with `migrate_db_up`" do
134
+ subject.migrate_db_up
135
+ assert_equal [nil], @fake_migration_context.up_called_with
136
+
137
+ target = Factory.string
138
+ subject.migrate_db_up(target)
139
+ assert_equal [target], @fake_migration_context.up_called_with
153
140
  end
154
141
 
155
- should "set the activerecord migration's verbose attr" do
156
- exp = ENV["MIGRATE_QUIET"].nil?
157
- assert_equal exp, ActiveRecord::Migration.verbose
142
+ should "migrate the db with `migrate_db_down`" do
143
+ subject.migrate_db_down
144
+ assert_equal [nil], @fake_migration_context.down_called_with
145
+
146
+ target = Factory.string
147
+ subject.migrate_db_down(target)
148
+ assert_equal [target], @fake_migration_context.down_called_with
158
149
  end
159
150
 
160
- should "call the activerecord migrator's migrate method" do
161
- version = ENV.key?("MIGRATE_VERSION") ? ENV["MIGRATE_VERSION"].to_i : nil
162
- exp = [subject.migrations_path, version]
163
- assert_equal exp, @migrator_called_with
151
+ should "migrate the db with `migrate_db_forward`" do
152
+ subject.migrate_db_forward
153
+ assert_equal [1], @fake_migration_context.forward_called_with
154
+
155
+ steps = Factory.integer(3)
156
+ subject.migrate_db_forward(steps)
157
+ assert_equal [steps], @fake_migration_context.forward_called_with
164
158
  end
165
159
 
160
+ should "migrate the db with `migrate_db_backward`" do
161
+ subject.migrate_db_backward
162
+ assert_equal [1], @fake_migration_context.rollback_called_with
163
+
164
+ steps = Factory.integer(3)
165
+ subject.migrate_db_backward(steps)
166
+ assert_equal [steps], @fake_migration_context.rollback_called_with
167
+ end
166
168
  end
167
169
 
168
170
  class LoadAndDumpSchemaTests < UnitTests
@@ -235,12 +237,11 @@ class Ardb::Adapter::Base
235
237
  subject.load_schema
236
238
  assert_empty @captured_stdout
237
239
  end
238
-
239
240
  end
240
241
 
241
242
  class LoadRubySchemaTests < UnitTests
242
243
  setup do
243
- @config.schema_path = File.join(TEST_SUPPORT_PATH, 'fake_schema')
244
+ @config.schema_path = File.join(TEST_SUPPORT_PATH, "fake_schema")
244
245
  @adapter = Ardb::Adapter::Base.new(@config)
245
246
  end
246
247
 
@@ -252,12 +253,11 @@ class Ardb::Adapter::Base
252
253
  subject.load_ruby_schema
253
254
  assert_equal 2, FAKE_SCHEMA.load_count
254
255
  end
255
-
256
256
  end
257
257
 
258
258
  class DumpRubySchemaTests < UnitTests
259
259
  setup do
260
- @config.schema_path = File.join(TMP_PATH, 'testdb', 'test_dump_ruby_schema')
260
+ @config.schema_path = File.join(TMP_PATH, "testdb", "test_dump_ruby_schema")
261
261
  FileUtils.rm_rf(File.dirname(@config.schema_path))
262
262
 
263
263
  @schema_dumper_connection, @schema_dumper_file = [nil, nil]
@@ -283,7 +283,6 @@ class Ardb::Adapter::Base
283
283
  assert_instance_of File, @schema_dumper_file
284
284
  assert_equal subject.ruby_schema_path, @schema_dumper_file.path
285
285
  end
286
-
287
286
  end
288
287
 
289
288
  class FakeConnection; end
@@ -325,4 +324,29 @@ class Ardb::Adapter::Base
325
324
  def with_connection(&block); end
326
325
  end
327
326
 
327
+ class FakeMigrationContext
328
+ attr_reader :init_with
329
+ attr_reader :up_called_with, :down_called_with
330
+ attr_reader :forward_called_with, :rollback_called_with
331
+
332
+ def initialize(*args)
333
+ @init_with = args
334
+ end
335
+
336
+ def up(*args)
337
+ @up_called_with = args
338
+ end
339
+
340
+ def down(*args)
341
+ @down_called_with = args
342
+ end
343
+
344
+ def forward(*args)
345
+ @forward_called_with = args
346
+ end
347
+
348
+ def rollback(*args)
349
+ @rollback_called_with = args
350
+ end
351
+ end
328
352
  end