ardb 0.27.3 → 0.28.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +5 -1
- data/ardb.gemspec +3 -4
- data/lib/ardb.rb +135 -68
- data/lib/ardb/adapter/base.rb +39 -24
- data/lib/ardb/adapter/mysql.rb +1 -2
- data/lib/ardb/adapter/postgresql.rb +14 -12
- data/lib/ardb/adapter/sqlite.rb +3 -8
- data/lib/ardb/adapter_spy.rb +67 -87
- data/lib/ardb/cli.rb +11 -219
- data/lib/ardb/{clirb.rb → cli/clirb.rb} +2 -1
- data/lib/ardb/cli/commands.rb +275 -0
- data/lib/ardb/migration.rb +8 -6
- data/lib/ardb/migration_helpers.rb +1 -1
- data/lib/ardb/pg_json.rb +90 -0
- data/lib/ardb/version.rb +1 -1
- data/test/helper.rb +15 -3
- data/test/support/factory.rb +15 -0
- data/test/support/fake_schema.rb +5 -0
- data/test/support/postgresql/migrations/.gitkeep +0 -0
- data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +13 -0
- data/test/support/postgresql/schema.rb +3 -0
- data/test/support/postgresql/setup_test_db.rb +51 -0
- data/test/support/relative_require_test_db_file.rb +2 -0
- data/test/support/require_test_db_file.rb +1 -0
- data/test/system/pg_json_tests.rb +85 -0
- data/test/unit/adapter/base_tests.rb +104 -39
- data/test/unit/adapter/mysql_tests.rb +2 -1
- data/test/unit/adapter/postgresql_tests.rb +10 -9
- data/test/unit/adapter/sqlite_tests.rb +8 -3
- data/test/unit/adapter_spy_tests.rb +57 -66
- data/test/unit/ardb_tests.rb +323 -36
- data/test/unit/cli_tests.rb +193 -146
- data/test/unit/has_slug_tests.rb +9 -9
- data/test/unit/migration_helpers_tests.rb +18 -12
- data/test/unit/migration_tests.rb +18 -11
- data/test/unit/pg_json_tests.rb +39 -0
- data/test/unit/record_spy_tests.rb +1 -1
- data/test/unit/test_helpers_tests.rb +2 -6
- data/test/unit/use_db_default_tests.rb +2 -2
- metadata +29 -34
- data/lib/ardb/root_path.rb +0 -15
- data/test/unit/config_tests.rb +0 -58
File without changes
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'ardb/migration_helpers'
|
2
|
+
|
3
|
+
class CreatePgJsonMigrateTest < ActiveRecord::Migration
|
4
|
+
include Ardb::MigrationHelpers
|
5
|
+
|
6
|
+
def change
|
7
|
+
create_table :pg_json_test_records do |t|
|
8
|
+
t.json :json_attribute
|
9
|
+
end
|
10
|
+
add_column :pg_json_test_records, :jsonb_attribute, :jsonb
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'ardb'
|
3
|
+
|
4
|
+
class PostgresqlDbTests < Assert::Context
|
5
|
+
setup do
|
6
|
+
@orig_env_ardb_db_file = ENV['ARDB_DB_FILE']
|
7
|
+
ActiveRecord::Base.logger = @orig_ar_loggerF
|
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')
|
11
|
+
|
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'
|
17
|
+
|
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'
|
22
|
+
c.schema_format = :ruby
|
23
|
+
end
|
24
|
+
Assert.stub(Ardb, :config){ @ardb_config }
|
25
|
+
|
26
|
+
Ardb.init(false)
|
27
|
+
|
28
|
+
Ardb.adapter.drop_db
|
29
|
+
Ardb.adapter.create_db
|
30
|
+
Ardb.adapter.load_schema
|
31
|
+
Ardb.adapter.connect_db
|
32
|
+
end
|
33
|
+
teardown do
|
34
|
+
ActiveRecord::Base.logger = @orig_ar_logger
|
35
|
+
ENV['ARDB_DB_FILE'] = @orig_env_ardb_db_file
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
# useful when testing creating/dropping/migrating DBs
|
41
|
+
def silence_stdout
|
42
|
+
current_stdout = $stdout.dup
|
43
|
+
$stdout = File.new('/dev/null', 'w')
|
44
|
+
begin
|
45
|
+
yield
|
46
|
+
ensure
|
47
|
+
$stdout = current_stdout
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# this file is used to test that Ardb will require a db file when init
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'ardb/pg_json'
|
3
|
+
|
4
|
+
require 'json'
|
5
|
+
require 'test/support/postgresql/setup_test_db'
|
6
|
+
|
7
|
+
module Ardb; end
|
8
|
+
module Ardb::PgJson
|
9
|
+
|
10
|
+
class SystemTests < PostgresqlDbTests
|
11
|
+
desc "Ardb postgresql json shim"
|
12
|
+
setup do
|
13
|
+
@ardb_config.migrations_path = 'pg_json_migrations'
|
14
|
+
end
|
15
|
+
|
16
|
+
should "add support for postgresql json columns to migrations" do
|
17
|
+
# this should migrate the db, adding a record that has json/jsonb columns
|
18
|
+
assert_nothing_raised do
|
19
|
+
silence_stdout{ Ardb.adapter.migrate_db }
|
20
|
+
end
|
21
|
+
|
22
|
+
results = ActiveRecord::Base.connection.execute(
|
23
|
+
"SELECT column_name, data_type " \
|
24
|
+
"FROM INFORMATION_SCHEMA.COLUMNS " \
|
25
|
+
"WHERE table_name = 'pg_json_test_records'"
|
26
|
+
).to_a
|
27
|
+
exp = {
|
28
|
+
'column_name' => 'json_attribute',
|
29
|
+
'data_type' => 'json',
|
30
|
+
}
|
31
|
+
assert_includes exp, results
|
32
|
+
exp = {
|
33
|
+
'column_name' => 'jsonb_attribute',
|
34
|
+
'data_type' => 'jsonb'
|
35
|
+
}
|
36
|
+
assert_includes exp, results
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
class WithMigratedTableTests < SystemTests
|
42
|
+
setup do
|
43
|
+
silence_stdout{ Ardb.adapter.migrate_db }
|
44
|
+
@record_class = Class.new(ActiveRecord::Base) do
|
45
|
+
self.table_name = 'pg_json_test_records'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
should "add support for postgresql 'json' attributes on records" do
|
50
|
+
values = [Factory.string, Factory.integer, nil]
|
51
|
+
|
52
|
+
record = @record_class.new
|
53
|
+
assert_nil record.json_attribute
|
54
|
+
assert_nil record.jsonb_attribute
|
55
|
+
|
56
|
+
hash = Factory.integer(3).times.inject({}) do |h, n|
|
57
|
+
h.merge!(Factory.string => values.sample)
|
58
|
+
end
|
59
|
+
record.json_attribute = JSON.dump(hash)
|
60
|
+
record.jsonb_attribute = JSON.dump(hash)
|
61
|
+
assert_nothing_raised{ record.save! }
|
62
|
+
record.reload
|
63
|
+
assert_equal hash, JSON.load(record.json_attribute)
|
64
|
+
assert_equal hash, JSON.load(record.jsonb_attribute)
|
65
|
+
|
66
|
+
array = Factory.integer(3).times.map{ values.sample }
|
67
|
+
record.json_attribute = JSON.dump(array)
|
68
|
+
record.jsonb_attribute = JSON.dump(array)
|
69
|
+
assert_nothing_raised{ record.save! }
|
70
|
+
record.reload
|
71
|
+
assert_equal array, JSON.load(record.json_attribute)
|
72
|
+
assert_equal array, JSON.load(record.jsonb_attribute)
|
73
|
+
|
74
|
+
value = values.sample
|
75
|
+
record.json_attribute = JSON.dump(value)
|
76
|
+
record.jsonb_attribute = JSON.dump(value)
|
77
|
+
assert_nothing_raised{ record.save! }
|
78
|
+
record.reload
|
79
|
+
assert_equal value, JSON.load(record.json_attribute)
|
80
|
+
assert_equal value, JSON.load(record.jsonb_attribute)
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -1,34 +1,46 @@
|
|
1
1
|
require 'assert'
|
2
2
|
require 'ardb/adapter/base'
|
3
3
|
|
4
|
+
require 'ardb'
|
5
|
+
# This is needed by the schema dumper but it doesn't handle requiring it so we
|
6
|
+
# have to manually, otherwise this errors when you try to run the adapter base
|
7
|
+
# tests by themselves
|
8
|
+
require 'active_support/core_ext/class/attribute_accessors'
|
9
|
+
|
4
10
|
class Ardb::Adapter::Base
|
5
11
|
|
6
12
|
class UnitTests < Assert::Context
|
7
13
|
desc "Ardb::Adapter::Base"
|
8
14
|
setup do
|
9
|
-
@
|
15
|
+
@config = Factory.ardb_config
|
16
|
+
@adapter = Ardb::Adapter::Base.new(@config)
|
10
17
|
end
|
11
18
|
subject{ @adapter }
|
12
19
|
|
13
|
-
should have_readers :
|
14
|
-
should
|
20
|
+
should have_readers :config
|
21
|
+
should have_imeths :connect_hash, :database, :migrations_path
|
22
|
+
should have_imeths :schema_format, :ruby_schema_path, :sql_schema_path
|
15
23
|
should have_imeths :escape_like_pattern
|
16
24
|
should have_imeths :foreign_key_add_sql, :foreign_key_drop_sql
|
17
|
-
should have_imeths :create_db, :drop_db, :
|
25
|
+
should have_imeths :create_db, :drop_db, :drop_tables
|
26
|
+
should have_imeths :connect_db, :migrate_db
|
18
27
|
should have_imeths :load_schema, :load_ruby_schema, :load_sql_schema
|
19
28
|
should have_imeths :dump_schema, :dump_ruby_schema, :dump_sql_schema
|
20
29
|
|
21
|
-
should "know its config
|
22
|
-
assert_equal
|
30
|
+
should "know its config" do
|
31
|
+
assert_equal @config, subject.config
|
23
32
|
end
|
24
33
|
|
25
|
-
should "
|
26
|
-
assert_equal
|
34
|
+
should "demeter its config" do
|
35
|
+
assert_equal @config.activerecord_connect_hash, subject.connect_hash
|
36
|
+
assert_equal @config.database, subject.database
|
37
|
+
assert_equal @config.migrations_path, subject.migrations_path
|
38
|
+
assert_equal @config.schema_format, subject.schema_format
|
27
39
|
end
|
28
40
|
|
29
|
-
should "know its schema paths" do
|
30
|
-
assert_equal "#{
|
31
|
-
assert_equal "#{
|
41
|
+
should "know its ruby and sql schema paths" do
|
42
|
+
assert_equal "#{@config.schema_path}.rb", subject.ruby_schema_path
|
43
|
+
assert_equal "#{@config.schema_path}.sql", subject.sql_schema_path
|
32
44
|
end
|
33
45
|
|
34
46
|
should "know how to escape like patterns" do
|
@@ -73,21 +85,38 @@ class Ardb::Adapter::Base
|
|
73
85
|
assert_raises(NotImplementedError){ subject.dump_sql_schema }
|
74
86
|
end
|
75
87
|
|
88
|
+
should "know if its equal to another adapter" do
|
89
|
+
matching_adapter = Ardb::Adapter::Base.new(@config)
|
90
|
+
assert_equal matching_adapter, subject
|
91
|
+
|
92
|
+
non_matching_adapter = Ardb::Adapter::Base.new(Factory.ardb_config)
|
93
|
+
assert_not_equal non_matching_adapter, subject
|
94
|
+
end
|
95
|
+
|
76
96
|
end
|
77
97
|
|
78
98
|
class ConnectDbTests < UnitTests
|
79
99
|
desc "`connect_db`"
|
80
100
|
setup do
|
81
|
-
@
|
82
|
-
Assert.stub(ActiveRecord::Base, :
|
83
|
-
@
|
101
|
+
@ar_establish_connection_called_with = nil
|
102
|
+
Assert.stub(ActiveRecord::Base, :establish_connection) do |options|
|
103
|
+
@ar_establish_connection_called_with = options
|
104
|
+
end
|
105
|
+
|
106
|
+
@ar_connection_pool = FakeConnectionPool.new
|
107
|
+
Assert.stub(ActiveRecord::Base, :connection_pool){ @ar_connection_pool }
|
108
|
+
|
109
|
+
@ar_with_connection_called = false
|
110
|
+
Assert.stub(@ar_connection_pool, :with_connection) do
|
111
|
+
@ar_with_connection_called = true
|
84
112
|
end
|
85
113
|
|
86
114
|
@adapter.connect_db
|
87
115
|
end
|
88
116
|
|
89
|
-
should "
|
90
|
-
|
117
|
+
should "use activerecord to establish and then checkout a connection" do
|
118
|
+
assert_equal subject.connect_hash, @ar_establish_connection_called_with
|
119
|
+
assert_true @ar_with_connection_called
|
91
120
|
end
|
92
121
|
|
93
122
|
end
|
@@ -95,6 +124,9 @@ class Ardb::Adapter::Base
|
|
95
124
|
class MigrateDbTests < UnitTests
|
96
125
|
desc "`migrate_db`"
|
97
126
|
setup do
|
127
|
+
@orig_migrate_version_env_var = ENV['MIGRATE_VERSION']
|
128
|
+
@orig_migrate_query_env_var = ENV['MIGRATE_QUIET']
|
129
|
+
|
98
130
|
ENV["MIGRATE_VERSION"] = Factory.integer.to_s if Factory.boolean
|
99
131
|
ENV["MIGRATE_QUIET"] = Factory.boolean.to_s if Factory.boolean
|
100
132
|
|
@@ -105,6 +137,10 @@ class Ardb::Adapter::Base
|
|
105
137
|
|
106
138
|
@adapter.migrate_db
|
107
139
|
end
|
140
|
+
teardown do
|
141
|
+
ENV['MIGRATE_VERSION'] = @orig_migrate_version_env_var
|
142
|
+
ENV['MIGRATE_QUIET'] = @orig_migrate_query_env_var
|
143
|
+
end
|
108
144
|
|
109
145
|
should "add the ardb migration helper recorder to activerecord's command recorder" do
|
110
146
|
exp = Ardb::MigrationHelpers::RecorderMixin
|
@@ -112,7 +148,7 @@ class Ardb::Adapter::Base
|
|
112
148
|
end
|
113
149
|
|
114
150
|
should "set the activerecord migrator's migrations path" do
|
115
|
-
exp =
|
151
|
+
exp = subject.migrations_path
|
116
152
|
assert_equal exp, ActiveRecord::Migrator.migrations_path
|
117
153
|
end
|
118
154
|
|
@@ -123,7 +159,7 @@ class Ardb::Adapter::Base
|
|
123
159
|
|
124
160
|
should "call the activerecord migrator's migrate method" do
|
125
161
|
version = ENV.key?("MIGRATE_VERSION") ? ENV["MIGRATE_VERSION"].to_i : nil
|
126
|
-
exp = [
|
162
|
+
exp = [subject.migrations_path, version]
|
127
163
|
assert_equal exp, @migrator_called_with
|
128
164
|
end
|
129
165
|
|
@@ -135,18 +171,15 @@ class Ardb::Adapter::Base
|
|
135
171
|
@captured_stdout = ""
|
136
172
|
$stdout = StringIO.new(@captured_stdout)
|
137
173
|
|
138
|
-
@
|
139
|
-
|
140
|
-
@adapter = SchemaSpyAdapter.new
|
174
|
+
@adapter = SchemaSpyAdapter.new(@config)
|
141
175
|
end
|
142
176
|
teardown do
|
143
|
-
Ardb.config.schema_format = @orig_schema_format
|
144
177
|
$stdout = @orig_stdout
|
145
178
|
end
|
146
179
|
|
147
180
|
should "load a ruby schema using `load_schema` when the format is ruby" do
|
148
|
-
|
149
|
-
adapter = SchemaSpyAdapter.new
|
181
|
+
@config.schema_format = Ardb::Config::RUBY_SCHEMA_FORMAT
|
182
|
+
adapter = SchemaSpyAdapter.new(@config)
|
150
183
|
|
151
184
|
assert_false adapter.load_ruby_schema_called
|
152
185
|
assert_false adapter.load_sql_schema_called
|
@@ -156,8 +189,8 @@ class Ardb::Adapter::Base
|
|
156
189
|
end
|
157
190
|
|
158
191
|
should "load a SQL schema using `load_schema` when the format is sql" do
|
159
|
-
|
160
|
-
adapter = SchemaSpyAdapter.new
|
192
|
+
@config.schema_format = Ardb::Config::SQL_SCHEMA_FORMAT
|
193
|
+
adapter = SchemaSpyAdapter.new(@config)
|
161
194
|
|
162
195
|
assert_false adapter.load_ruby_schema_called
|
163
196
|
assert_false adapter.load_sql_schema_called
|
@@ -172,8 +205,8 @@ class Ardb::Adapter::Base
|
|
172
205
|
end
|
173
206
|
|
174
207
|
should "always dump a ruby schema using `dump_schema`" do
|
175
|
-
|
176
|
-
adapter = SchemaSpyAdapter.new
|
208
|
+
@config.schema_format = Ardb::Config::RUBY_SCHEMA_FORMAT
|
209
|
+
adapter = SchemaSpyAdapter.new(@config)
|
177
210
|
|
178
211
|
assert_false adapter.dump_ruby_schema_called
|
179
212
|
assert_false adapter.dump_sql_schema_called
|
@@ -181,8 +214,8 @@ class Ardb::Adapter::Base
|
|
181
214
|
assert_true adapter.dump_ruby_schema_called
|
182
215
|
assert_false adapter.dump_sql_schema_called
|
183
216
|
|
184
|
-
|
185
|
-
adapter = SchemaSpyAdapter.new
|
217
|
+
@config.schema_format = Ardb::Config::SQL_SCHEMA_FORMAT
|
218
|
+
adapter = SchemaSpyAdapter.new(@config)
|
186
219
|
|
187
220
|
assert_false adapter.dump_ruby_schema_called
|
188
221
|
adapter.dump_schema
|
@@ -190,8 +223,8 @@ class Ardb::Adapter::Base
|
|
190
223
|
end
|
191
224
|
|
192
225
|
should "dump a SQL schema using `dump_schema` when the format is sql" do
|
193
|
-
|
194
|
-
adapter = SchemaSpyAdapter.new
|
226
|
+
@config.schema_format = Ardb::Config::SQL_SCHEMA_FORMAT
|
227
|
+
adapter = SchemaSpyAdapter.new(@config)
|
195
228
|
|
196
229
|
assert_false adapter.dump_ruby_schema_called
|
197
230
|
adapter.dump_schema
|
@@ -207,13 +240,8 @@ class Ardb::Adapter::Base
|
|
207
240
|
|
208
241
|
class LoadRubySchemaTests < UnitTests
|
209
242
|
setup do
|
210
|
-
@
|
211
|
-
Ardb.config
|
212
|
-
|
213
|
-
@adapter = Ardb::Adapter::Base.new
|
214
|
-
end
|
215
|
-
teardown do
|
216
|
-
Ardb.config.schema_path = @orig_schema_path
|
243
|
+
@config.schema_path = File.join(TEST_SUPPORT_PATH, 'fake_schema')
|
244
|
+
@adapter = Ardb::Adapter::Base.new(@config)
|
217
245
|
end
|
218
246
|
|
219
247
|
should "load a ruby schema file using `load_ruby_schema`" do
|
@@ -227,6 +255,39 @@ class Ardb::Adapter::Base
|
|
227
255
|
|
228
256
|
end
|
229
257
|
|
258
|
+
class DumpRubySchemaTests < UnitTests
|
259
|
+
setup do
|
260
|
+
@config.schema_path = File.join(TMP_PATH, 'testdb', 'test_dump_ruby_schema')
|
261
|
+
FileUtils.rm_rf(File.dirname(@config.schema_path))
|
262
|
+
|
263
|
+
@schema_dumper_connection, @schema_dumper_file = [nil, nil]
|
264
|
+
Assert.stub(ActiveRecord::SchemaDumper, :dump) do |connection, file|
|
265
|
+
@schema_dumper_connection = connection
|
266
|
+
@schema_dumper_file = file
|
267
|
+
end
|
268
|
+
|
269
|
+
@fake_connection = FakeConnection.new
|
270
|
+
Assert.stub(ActiveRecord::Base, :connection){ @fake_connection }
|
271
|
+
|
272
|
+
@adapter = Ardb::Adapter::Base.new(@config)
|
273
|
+
end
|
274
|
+
teardown do
|
275
|
+
FileUtils.rm_rf(File.dirname(@config.schema_path))
|
276
|
+
end
|
277
|
+
|
278
|
+
should "dump a ruby schema file using `dump_ruby_schema`" do
|
279
|
+
assert_false File.exists?(subject.ruby_schema_path)
|
280
|
+
subject.dump_ruby_schema
|
281
|
+
assert_true File.exists?(subject.ruby_schema_path)
|
282
|
+
assert_equal @fake_connection, @schema_dumper_connection
|
283
|
+
assert_instance_of File, @schema_dumper_file
|
284
|
+
assert_equal subject.ruby_schema_path, @schema_dumper_file.path
|
285
|
+
end
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
class FakeConnection; end
|
290
|
+
|
230
291
|
class SchemaSpyAdapter < Ardb::Adapter::Base
|
231
292
|
attr_reader :load_ruby_schema_called, :dump_ruby_schema_called
|
232
293
|
attr_reader :load_sql_schema_called, :dump_sql_schema_called
|
@@ -260,4 +321,8 @@ class Ardb::Adapter::Base
|
|
260
321
|
end
|
261
322
|
end
|
262
323
|
|
324
|
+
class FakeConnectionPool
|
325
|
+
def with_connection(&block); end
|
326
|
+
end
|
327
|
+
|
263
328
|
end
|