ardb 0.29.1 → 0.29.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.l.yml +8 -0
- data/.rubocop.yml +3 -0
- data/.t.yml +6 -0
- data/Gemfile +4 -2
- data/ardb.gemspec +9 -6
- data/bin/ardb +2 -0
- data/lib/ardb.rb +75 -61
- data/lib/ardb/adapter/base.rb +40 -19
- data/lib/ardb/adapter/mysql.rb +2 -0
- data/lib/ardb/adapter/postgresql.rb +36 -25
- data/lib/ardb/adapter/sqlite.rb +7 -7
- data/lib/ardb/adapter_spy.rb +16 -14
- data/lib/ardb/cli.rb +23 -18
- data/lib/ardb/cli/clirb.rb +5 -0
- data/lib/ardb/cli/commands.rb +184 -95
- data/lib/ardb/db_tests.rb +2 -0
- data/lib/ardb/default_order_by.rb +13 -11
- data/lib/ardb/migration.rb +7 -4
- data/lib/ardb/record_spy.rb +42 -38
- data/lib/ardb/relation_spy.rb +27 -25
- data/lib/ardb/require_autoloaded_active_record_files.rb +3 -1
- data/lib/ardb/test_helpers.rb +11 -9
- data/lib/ardb/use_db_default.rb +9 -7
- data/lib/ardb/version.rb +3 -1
- data/script/determine_autoloaded_active_record_files.rb +22 -20
- data/test/helper.rb +2 -0
- data/test/support/factory.rb +2 -1
- data/test/support/fake_schema.rb +3 -1
- data/test/support/postgresql/schema.rb +3 -1
- data/test/support/postgresql/setup_test_db.rb +3 -1
- data/test/support/relative_require_test_db_file.rb +1 -0
- data/test/support/require_test_db_file.rb +1 -0
- data/test/unit/adapter/base_tests.rb +9 -5
- data/test/unit/adapter/mysql_tests.rb +2 -0
- data/test/unit/adapter/postgresql_tests.rb +14 -14
- data/test/unit/adapter/sqlite_tests.rb +2 -0
- data/test/unit/adapter_spy_tests.rb +4 -1
- data/test/unit/ardb_tests.rb +28 -13
- data/test/unit/cli_tests.rb +47 -34
- data/test/unit/db_tests_tests.rb +4 -1
- data/test/unit/default_order_by_tests.rb +18 -13
- data/test/unit/migration_tests.rb +8 -5
- data/test/unit/record_spy_tests.rb +21 -14
- data/test/unit/relation_spy_tests.rb +28 -22
- data/test/unit/test_helpers_tests.rb +4 -1
- data/test/unit/use_db_default_tests.rb +16 -7
- metadata +27 -11
data/lib/ardb/test_helpers.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_record"
|
2
4
|
require "ardb"
|
3
5
|
|
@@ -6,7 +8,7 @@ require "ardb"
|
|
6
8
|
|
7
9
|
module Ardb
|
8
10
|
module TestHelpers
|
9
|
-
|
11
|
+
extend self
|
10
12
|
|
11
13
|
def drop_tables
|
12
14
|
Ardb.adapter.drop_tables
|
@@ -22,7 +24,7 @@ module Ardb
|
|
22
24
|
|
23
25
|
def create_db
|
24
26
|
@create_db ||= begin
|
25
|
-
|
27
|
+
create_db!
|
26
28
|
true
|
27
29
|
end
|
28
30
|
end
|
@@ -33,7 +35,7 @@ module Ardb
|
|
33
35
|
|
34
36
|
def drop_db
|
35
37
|
@drop_db ||= begin
|
36
|
-
|
38
|
+
drop_db!
|
37
39
|
true
|
38
40
|
end
|
39
41
|
end
|
@@ -44,7 +46,7 @@ module Ardb
|
|
44
46
|
|
45
47
|
def connect_db
|
46
48
|
@connect_db ||= begin
|
47
|
-
|
49
|
+
connect_db!
|
48
50
|
true
|
49
51
|
end
|
50
52
|
end
|
@@ -55,20 +57,20 @@ module Ardb
|
|
55
57
|
|
56
58
|
def migrate_db
|
57
59
|
@migrate_db ||= begin
|
58
|
-
|
60
|
+
migrate_db!
|
59
61
|
true
|
60
62
|
end
|
61
63
|
end
|
62
64
|
|
63
65
|
def reset_db!
|
64
|
-
|
65
|
-
|
66
|
-
|
66
|
+
drop_db!
|
67
|
+
create_db!
|
68
|
+
load_schema
|
67
69
|
end
|
68
70
|
|
69
71
|
def reset_db
|
70
72
|
@reset_db ||= begin
|
71
|
-
|
73
|
+
reset_db!
|
72
74
|
true
|
73
75
|
end
|
74
76
|
end
|
data/lib/ardb/use_db_default.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "much-mixin"
|
2
4
|
|
3
5
|
module Ardb
|
4
6
|
module UseDbDefault
|
5
|
-
include
|
7
|
+
include MuchMixin
|
6
8
|
|
7
|
-
|
9
|
+
mixin_included do
|
8
10
|
@ardb_use_db_default_attrs = []
|
9
11
|
|
10
12
|
around_create :ardb_allow_db_to_default_attrs
|
11
13
|
end
|
12
14
|
|
13
|
-
|
15
|
+
mixin_class_methods do
|
14
16
|
def ardb_use_db_default_attrs
|
15
17
|
@ardb_use_db_default_attrs
|
16
18
|
end
|
@@ -21,7 +23,7 @@ module Ardb
|
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
|
26
|
+
mixin_instance_methods do
|
25
27
|
private
|
26
28
|
|
27
29
|
def ardb_allow_db_to_default_attrs
|
@@ -29,13 +31,13 @@ module Ardb
|
|
29
31
|
# activerecord from adding the attr into the sql `INSERT`, which will
|
30
32
|
# make the DB default its value
|
31
33
|
unchanged_names = self.class.ardb_use_db_default_attrs.reject do |name|
|
32
|
-
|
34
|
+
send("#{name}_changed?")
|
33
35
|
end
|
34
36
|
unchanged_names.each{ |name| @attributes.delete(name) }
|
35
37
|
yield
|
36
38
|
# we have to go and fetch the attr value from the DB, otherwise
|
37
39
|
# activerecord doesn"t know the value that the DB used
|
38
|
-
scope = self.class.where(:
|
40
|
+
scope = self.class.where(id: id)
|
39
41
|
unchanged_names.each do |name|
|
40
42
|
@attributes[name] = scope.pluck(name).first
|
41
43
|
end
|
data/lib/ardb/version.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_record"
|
2
4
|
|
3
5
|
# this can be slow, this is one of the reasons this shouldn"t be done during
|
@@ -13,19 +15,19 @@ paths = Dir["#{gemspec.lib_dirs_glob}/**/*.rb"]
|
|
13
15
|
# generators fail when we try to require them. the others are pieces of active
|
14
16
|
# record we don"t use in a production environment
|
15
17
|
ignored_regexes = [
|
16
|
-
/
|
17
|
-
/
|
18
|
-
/
|
19
|
-
/
|
20
|
-
/
|
21
|
-
/
|
22
|
-
/
|
23
|
-
/
|
24
|
-
/
|
25
|
-
/
|
26
|
-
/
|
18
|
+
%r{rails/generators},
|
19
|
+
%r{active_record/railtie},
|
20
|
+
%r{active_record/migration},
|
21
|
+
%r{active_record/fixtures},
|
22
|
+
%r{active_record/fixture_set},
|
23
|
+
%r{active_record/schema},
|
24
|
+
%r{active_record/connection_adapters},
|
25
|
+
%r{active_record/test_case},
|
26
|
+
%r{active_record/test_databases},
|
27
|
+
%r{active_record/test_fixtures},
|
28
|
+
%r{active_record/coders/yaml_column},
|
27
29
|
# `destroy_association_async_job` requires `ActiveJob` to be required.
|
28
|
-
/
|
30
|
+
%r{active_record/destroy_association_async_job},
|
29
31
|
]
|
30
32
|
|
31
33
|
Result = Struct.new(:file, :state, :reason)
|
@@ -43,11 +45,11 @@ paths.sort.each do |full_path|
|
|
43
45
|
|
44
46
|
# see if it"s ignored
|
45
47
|
ignored_regexes.each do |regex|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
next unless relative_path =~ regex
|
49
|
+
|
50
|
+
result.state = :ignored
|
51
|
+
result.reason = "matched #{regex}"
|
52
|
+
break
|
51
53
|
end
|
52
54
|
if result.state == :ignored
|
53
55
|
ignored << result
|
@@ -56,14 +58,14 @@ paths.sort.each do |full_path|
|
|
56
58
|
|
57
59
|
# try requiring the file
|
58
60
|
begin
|
59
|
-
if result.state = require(relative_path)
|
61
|
+
if (result.state = require(relative_path))
|
60
62
|
needs_to_be_required << result
|
61
63
|
else
|
62
64
|
already_required << result
|
63
65
|
end
|
64
|
-
rescue LoadError, SyntaxError =>
|
66
|
+
rescue LoadError, SyntaxError => ex
|
65
67
|
result.state = :errored
|
66
|
-
result.reason = "#{
|
68
|
+
result.reason = "#{ex.class}: #{ex.message}"
|
67
69
|
errored << result
|
68
70
|
end
|
69
71
|
end
|
data/test/helper.rb
CHANGED
data/test/support/factory.rb
CHANGED
data/test/support/fake_schema.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "assert"
|
2
4
|
require "ardb"
|
3
5
|
|
@@ -6,7 +8,7 @@ class PostgresqlDbTests < Assert::Context
|
|
6
8
|
@orig_env_ardb_db_file = ENV["ARDB_DB_FILE"]
|
7
9
|
ActiveRecord::Base.logger = @orig_ar_loggerF
|
8
10
|
|
9
|
-
#
|
11
|
+
# we"re manually configuring ardb so we don"t need this to do anything
|
10
12
|
ENV["ARDB_DB_FILE"] = File.join(TEST_SUPPORT_PATH, "require_test_db_file")
|
11
13
|
|
12
14
|
@ardb_config = Ardb::Config.new.tap do |c|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "assert"
|
2
4
|
require "ardb/adapter/base"
|
3
5
|
|
@@ -118,7 +120,7 @@ class Ardb::Adapter::Base
|
|
118
120
|
|
119
121
|
class MigrateTests < UnitTests
|
120
122
|
setup do
|
121
|
-
Assert.stub(ActiveRecord::MigrationContext, :new) do |*args
|
123
|
+
Assert.stub(ActiveRecord::MigrationContext, :new) do |*args|
|
122
124
|
@fake_migration_context ||= FakeMigrationContext.new(*args)
|
123
125
|
end
|
124
126
|
end
|
@@ -257,7 +259,8 @@ class Ardb::Adapter::Base
|
|
257
259
|
|
258
260
|
class DumpRubySchemaTests < UnitTests
|
259
261
|
setup do
|
260
|
-
@config.schema_path =
|
262
|
+
@config.schema_path =
|
263
|
+
File.join(TMP_PATH, "testdb", "test_dump_ruby_schema")
|
261
264
|
FileUtils.rm_rf(File.dirname(@config.schema_path))
|
262
265
|
|
263
266
|
@schema_dumper_connection, @schema_dumper_file = [nil, nil]
|
@@ -276,9 +279,9 @@ class Ardb::Adapter::Base
|
|
276
279
|
end
|
277
280
|
|
278
281
|
should "dump a ruby schema file using `dump_ruby_schema`" do
|
279
|
-
assert_false File.
|
282
|
+
assert_false File.exist?(subject.ruby_schema_path)
|
280
283
|
subject.dump_ruby_schema
|
281
|
-
assert_true File.
|
284
|
+
assert_true File.exist?(subject.ruby_schema_path)
|
282
285
|
assert_equal @fake_connection, @schema_dumper_connection
|
283
286
|
assert_instance_of File, @schema_dumper_file
|
284
287
|
assert_equal subject.ruby_schema_path, @schema_dumper_file.path
|
@@ -321,7 +324,8 @@ class Ardb::Adapter::Base
|
|
321
324
|
end
|
322
325
|
|
323
326
|
class FakeConnectionPool
|
324
|
-
def with_connection(&block)
|
327
|
+
def with_connection(&block)
|
328
|
+
end
|
325
329
|
end
|
326
330
|
|
327
331
|
class FakeMigrationContext
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "assert"
|
2
4
|
require "ardb/adapter/postgresql"
|
3
5
|
|
@@ -17,43 +19,41 @@ class Ardb::Adapter::Postgresql
|
|
17
19
|
should "know its public connect hash" do
|
18
20
|
exp = subject.connect_hash.merge({
|
19
21
|
"database" => "postgres",
|
20
|
-
"schema_search_path" => "public"
|
22
|
+
"schema_search_path" => "public",
|
21
23
|
})
|
22
24
|
assert_equal exp, subject.public_connect_hash
|
23
25
|
end
|
24
26
|
|
25
27
|
should "complain if given a database name with non-word characters" do
|
26
28
|
@config.database = "#{Factory.string}-#{Factory.string}"
|
27
|
-
assert_raises(Ardb::ConfigurationError)
|
29
|
+
assert_raises(Ardb::ConfigurationError) do
|
28
30
|
Ardb::Adapter::Postgresql.new(@config)
|
29
|
-
|
31
|
+
end
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
35
|
class SQLSchemaTests < UnitTests
|
34
36
|
setup do
|
35
37
|
@env = {
|
36
|
-
"PGHOST"
|
37
|
-
"PGPORT"
|
38
|
-
"PGUSER"
|
39
|
-
"PGPASSWORD" => @adapter.connect_hash["password"]
|
38
|
+
"PGHOST" => @adapter.connect_hash["host"],
|
39
|
+
"PGPORT" => @adapter.connect_hash["port"],
|
40
|
+
"PGUSER" => @adapter.connect_hash["username"],
|
41
|
+
"PGPASSWORD" => @adapter.connect_hash["password"],
|
40
42
|
}
|
41
43
|
end
|
42
|
-
|
43
44
|
end
|
44
45
|
|
45
46
|
class LoadSQLSchemaTests < SQLSchemaTests
|
46
47
|
setup do
|
47
48
|
cmd_str = "psql -f \"#{@adapter.sql_schema_path}\" #{@adapter.database}"
|
48
49
|
@cmd_spy = CmdSpy.new
|
49
|
-
Assert.stub(Scmd, :new).with(cmd_str, :
|
50
|
+
Assert.stub(Scmd, :new).with(cmd_str, env: @env){ @cmd_spy }
|
50
51
|
end
|
51
52
|
|
52
53
|
should "run a command for loading a SQL schema using `load_sql_schema`" do
|
53
54
|
subject.load_sql_schema
|
54
55
|
assert_true @cmd_spy.run_called
|
55
56
|
end
|
56
|
-
|
57
57
|
end
|
58
58
|
|
59
59
|
class DumpSQLSchemaTests < SQLSchemaTests
|
@@ -61,14 +61,13 @@ class Ardb::Adapter::Postgresql
|
|
61
61
|
cmd_str = "pg_dump -i -s -x -O -f " \
|
62
62
|
"\"#{@adapter.sql_schema_path}\" #{@adapter.database}"
|
63
63
|
@cmd_spy = CmdSpy.new
|
64
|
-
Assert.stub(Scmd, :new).with(cmd_str, :
|
64
|
+
Assert.stub(Scmd, :new).with(cmd_str, env: @env){ @cmd_spy }
|
65
65
|
end
|
66
66
|
|
67
67
|
should "run a command for dumping a SQL schema using `dump_sql_schema`" do
|
68
68
|
subject.dump_sql_schema
|
69
69
|
assert_true @cmd_spy.run_called
|
70
70
|
end
|
71
|
-
|
72
71
|
end
|
73
72
|
|
74
73
|
class CmdSpy
|
@@ -82,7 +81,8 @@ class Ardb::Adapter::Postgresql
|
|
82
81
|
@run_called = true
|
83
82
|
end
|
84
83
|
|
85
|
-
def success
|
84
|
+
def success?
|
85
|
+
true
|
86
|
+
end
|
86
87
|
end
|
87
|
-
|
88
88
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "assert"
|
2
4
|
require "ardb/adapter_spy"
|
3
5
|
|
@@ -26,7 +28,8 @@ class Ardb::AdapterSpy
|
|
26
28
|
should have_accessors :dump_schema_called_count, :load_schema_called_count
|
27
29
|
should have_accessors :drop_db_called_count, :create_db_called_count
|
28
30
|
should have_accessors :connect_db_called_count, :migrate_db_called_count
|
29
|
-
should have_imeths :create_db_called?, :drop_db_called
|
31
|
+
should have_imeths :create_db_called?, :drop_db_called?
|
32
|
+
should have_imeths :drop_tables_called?
|
30
33
|
should have_imeths :connect_db_called?, :migrate_db_called?
|
31
34
|
should have_imeths :dump_schema_called?, :load_schema_called?
|
32
35
|
should have_imeths :create_db, :drop_db, :drop_tables
|
data/test/unit/ardb_tests.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "assert"
|
2
4
|
require "ardb"
|
3
5
|
|
@@ -114,7 +116,10 @@ module Ardb
|
|
114
116
|
subject.init
|
115
117
|
|
116
118
|
assert_equal subject.config.logger, ActiveRecord::Base.logger
|
117
|
-
assert_equal
|
119
|
+
assert_equal(
|
120
|
+
subject.config.default_timezone,
|
121
|
+
ActiveRecord::Base.default_timezone,
|
122
|
+
)
|
118
123
|
end
|
119
124
|
|
120
125
|
should "optionally establish an AR connection" do
|
@@ -132,7 +137,9 @@ module Ardb
|
|
132
137
|
should "raise a not initialized error using its adapter before init" do
|
133
138
|
subject.reset_adapter
|
134
139
|
assert_raises(NotInitializedError){ subject.adapter }
|
135
|
-
assert_raises(NotInitializedError)
|
140
|
+
assert_raises(NotInitializedError) do
|
141
|
+
subject.escape_like_pattern(Factory.string)
|
142
|
+
end
|
136
143
|
|
137
144
|
subject.init
|
138
145
|
assert_nothing_raised{ subject.adapter }
|
@@ -171,7 +178,7 @@ module Ardb
|
|
171
178
|
:password,
|
172
179
|
:pool,
|
173
180
|
:checkout_timeout,
|
174
|
-
:min_messages
|
181
|
+
:min_messages,
|
175
182
|
]
|
176
183
|
assert_equal exp, subject::ACTIVERECORD_ATTRS
|
177
184
|
end
|
@@ -199,7 +206,7 @@ module Ardb
|
|
199
206
|
end
|
200
207
|
subject{ @config }
|
201
208
|
|
202
|
-
should have_accessors
|
209
|
+
should have_accessors(*Ardb::Config::ACTIVERECORD_ATTRS)
|
203
210
|
should have_accessors :default_timezone, :logger, :root_path
|
204
211
|
should have_readers :schema_format
|
205
212
|
should have_writers :migrations_path, :schema_path
|
@@ -209,9 +216,17 @@ module Ardb
|
|
209
216
|
assert_equal :utc, subject.default_timezone
|
210
217
|
assert_instance_of Logger, subject.logger
|
211
218
|
assert_equal ENV["PWD"], subject.root_path
|
212
|
-
exp =
|
219
|
+
exp =
|
220
|
+
File.expand_path(
|
221
|
+
@config_class::DEFAULT_MIGRATIONS_PATH,
|
222
|
+
subject.root_path,
|
223
|
+
)
|
213
224
|
assert_equal exp, subject.migrations_path
|
214
|
-
exp =
|
225
|
+
exp =
|
226
|
+
File.expand_path(
|
227
|
+
@config_class::DEFAULT_SCHEMA_PATH,
|
228
|
+
subject.root_path,
|
229
|
+
)
|
215
230
|
assert_equal exp, subject.schema_path
|
216
231
|
assert_equal @config_class::RUBY_SCHEMA_FORMAT, subject.schema_format
|
217
232
|
end
|
@@ -250,11 +265,11 @@ module Ardb
|
|
250
265
|
end
|
251
266
|
|
252
267
|
should "know its activerecord connection hash" do
|
253
|
-
attrs_and_values = @config_class::ACTIVERECORD_ATTRS.map
|
254
|
-
value = [Factory.string,
|
268
|
+
attrs_and_values = @config_class::ACTIVERECORD_ATTRS.map{ |attr_name|
|
269
|
+
value = [Factory.string, nil].sample
|
255
270
|
subject.send("#{attr_name}=", value)
|
256
|
-
[attr_name.to_s, value]
|
257
|
-
|
271
|
+
[attr_name.to_s, value] unless value.nil?
|
272
|
+
}.compact
|
258
273
|
assert_equal Hash[attrs_and_values], subject.activerecord_connect_hash
|
259
274
|
end
|
260
275
|
|
@@ -285,7 +300,7 @@ module Ardb
|
|
285
300
|
:root_path,
|
286
301
|
:schema_format,
|
287
302
|
:migrations_path,
|
288
|
-
:schema_path
|
303
|
+
:schema_path,
|
289
304
|
]
|
290
305
|
attrs.each do |attr_name|
|
291
306
|
subject.send("#{attr_name}=", Factory.string)
|
@@ -324,7 +339,7 @@ module Ardb
|
|
324
339
|
"postgresql",
|
325
340
|
"postgres",
|
326
341
|
"mysql",
|
327
|
-
"mysql2"
|
342
|
+
"mysql2",
|
328
343
|
]
|
329
344
|
assert_equal exp, subject::VALID_ADAPTERS
|
330
345
|
end
|
@@ -333,7 +348,7 @@ module Ardb
|
|
333
348
|
adapter_key, exp_adapter_class = [
|
334
349
|
["sqlite", Ardb::Adapter::Sqlite],
|
335
350
|
["postgresql", Ardb::Adapter::Postgresql],
|
336
|
-
["mysql", Ardb::Adapter::Mysql]
|
351
|
+
["mysql", Ardb::Adapter::Mysql],
|
337
352
|
].sample
|
338
353
|
@config.adapter = adapter_key
|
339
354
|
|