ardb 0.27.3 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
data/lib/ardb/adapter/sqlite.rb
CHANGED
@@ -1,18 +1,13 @@
|
|
1
|
-
require 'pathname'
|
2
1
|
require 'fileutils'
|
3
2
|
require 'ardb'
|
4
3
|
require 'ardb/adapter/base'
|
5
4
|
|
6
|
-
|
5
|
+
module Ardb::Adapter
|
7
6
|
|
8
7
|
class Sqlite < Base
|
9
8
|
|
10
9
|
def db_file_path
|
11
|
-
|
12
|
-
path.to_s
|
13
|
-
else
|
14
|
-
Ardb.config.root_path.join(path).to_s
|
15
|
-
end
|
10
|
+
File.expand_path(self.database, self.config.root_path)
|
16
11
|
end
|
17
12
|
|
18
13
|
def validate!
|
@@ -24,7 +19,7 @@ class Ardb::Adapter
|
|
24
19
|
def create_db
|
25
20
|
validate!
|
26
21
|
FileUtils.mkdir_p File.dirname(self.db_file_path)
|
27
|
-
ActiveRecord::Base.establish_connection(self.
|
22
|
+
ActiveRecord::Base.establish_connection(self.connect_hash)
|
28
23
|
end
|
29
24
|
|
30
25
|
def drop_db
|
data/lib/ardb/adapter_spy.rb
CHANGED
@@ -1,112 +1,92 @@
|
|
1
|
-
require '
|
1
|
+
require 'ardb'
|
2
|
+
require 'ardb/adapter/base'
|
2
3
|
|
3
4
|
module Ardb
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
class AdapterSpy < Ardb::Adapter::Base
|
7
|
+
|
8
|
+
attr_accessor :drop_tables_called_count
|
9
|
+
attr_accessor :dump_schema_called_count, :load_schema_called_count
|
10
|
+
attr_accessor :drop_db_called_count, :create_db_called_count
|
11
|
+
attr_accessor :connect_db_called_count, :migrate_db_called_count
|
12
|
+
|
13
|
+
def initialize(config = nil)
|
14
|
+
super(config || Ardb::Config.new)
|
15
|
+
@drop_tables_called_count = 0
|
16
|
+
@dump_schema_called_count = 0
|
17
|
+
@load_schema_called_count = 0
|
18
|
+
@drop_db_called_count = 0
|
19
|
+
@create_db_called_count = 0
|
20
|
+
@connect_db_called_count = 0
|
21
|
+
@migrate_db_called_count = 0
|
13
22
|
end
|
14
23
|
|
15
|
-
|
16
|
-
|
24
|
+
def create_db_called?
|
25
|
+
self.create_db_called_count > 0
|
17
26
|
end
|
18
27
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
attr_accessor :dump_schema_called_count, :load_schema_called_count
|
23
|
-
attr_accessor :drop_db_called_count, :create_db_called_count
|
24
|
-
attr_accessor :connect_db_called_count, :migrate_db_called_count
|
25
|
-
|
26
|
-
def drop_tables_called_count
|
27
|
-
@drop_tables_called_count ||= 0
|
28
|
-
end
|
29
|
-
|
30
|
-
def drop_tables_called?
|
31
|
-
self.drop_tables_called_count > 0
|
32
|
-
end
|
33
|
-
|
34
|
-
def drop_tables(*args, &block)
|
35
|
-
self.drop_tables_called_count += 1
|
36
|
-
end
|
37
|
-
|
38
|
-
def dump_schema_called_count
|
39
|
-
@dump_schema_called_count ||= 0
|
40
|
-
end
|
41
|
-
|
42
|
-
def dump_schema_called?
|
43
|
-
self.dump_schema_called_count > 0
|
44
|
-
end
|
45
|
-
|
46
|
-
def dump_schema(*args, &block)
|
47
|
-
self.dump_schema_called_count += 1
|
48
|
-
end
|
49
|
-
|
50
|
-
def load_schema_called_count
|
51
|
-
@load_schema_called_count ||= 0
|
52
|
-
end
|
28
|
+
def drop_db_called?
|
29
|
+
self.drop_db_called_count > 0
|
30
|
+
end
|
53
31
|
|
54
|
-
|
55
|
-
|
56
|
-
|
32
|
+
def drop_tables_called?
|
33
|
+
self.drop_tables_called_count > 0
|
34
|
+
end
|
57
35
|
|
58
|
-
|
59
|
-
|
60
|
-
|
36
|
+
def connect_db_called?
|
37
|
+
self.connect_db_called_count > 0
|
38
|
+
end
|
61
39
|
|
62
|
-
|
63
|
-
|
64
|
-
|
40
|
+
def migrate_db_called?
|
41
|
+
self.migrate_db_called_count > 0
|
42
|
+
end
|
65
43
|
|
66
|
-
|
67
|
-
|
68
|
-
|
44
|
+
def load_schema_called?
|
45
|
+
self.load_schema_called_count > 0
|
46
|
+
end
|
69
47
|
|
70
|
-
|
71
|
-
|
72
|
-
|
48
|
+
def dump_schema_called?
|
49
|
+
self.dump_schema_called_count > 0
|
50
|
+
end
|
73
51
|
|
74
|
-
|
75
|
-
@create_db_called_count ||= 0
|
76
|
-
end
|
52
|
+
# Overwritten `Adapter::Base` methods
|
77
53
|
|
78
|
-
|
79
|
-
|
80
|
-
|
54
|
+
def foreign_key_add_sql
|
55
|
+
"FAKE ADD FOREIGN KEY SQL :from_table :from_column " \
|
56
|
+
":to_table :to_column :name"
|
57
|
+
end
|
81
58
|
|
82
|
-
|
83
|
-
|
84
|
-
|
59
|
+
def foreign_key_drop_sql
|
60
|
+
"FAKE DROP FOREIGN KEY SQL :from_table :from_column " \
|
61
|
+
":to_table :to_column :name"
|
62
|
+
end
|
85
63
|
|
86
|
-
|
87
|
-
|
88
|
-
|
64
|
+
def create_db(*args, &block)
|
65
|
+
self.create_db_called_count += 1
|
66
|
+
end
|
89
67
|
|
90
|
-
|
91
|
-
|
92
|
-
|
68
|
+
def drop_db(*args, &block)
|
69
|
+
self.drop_db_called_count += 1
|
70
|
+
end
|
93
71
|
|
94
|
-
|
95
|
-
|
96
|
-
|
72
|
+
def drop_tables(*args, &block)
|
73
|
+
self.drop_tables_called_count += 1
|
74
|
+
end
|
97
75
|
|
98
|
-
|
99
|
-
|
100
|
-
|
76
|
+
def connect_db(*args, &block)
|
77
|
+
self.connect_db_called_count += 1
|
78
|
+
end
|
101
79
|
|
102
|
-
|
103
|
-
|
104
|
-
|
80
|
+
def migrate_db(*args, &block)
|
81
|
+
self.migrate_db_called_count += 1
|
82
|
+
end
|
105
83
|
|
106
|
-
|
107
|
-
|
108
|
-
|
84
|
+
def load_schema(*args, &block)
|
85
|
+
self.load_schema_called_count += 1
|
86
|
+
end
|
109
87
|
|
88
|
+
def dump_schema(*args, &block)
|
89
|
+
self.dump_schema_called_count += 1
|
110
90
|
end
|
111
91
|
|
112
92
|
end
|
data/lib/ardb/cli.rb
CHANGED
@@ -1,22 +1,17 @@
|
|
1
|
-
require 'ardb'
|
2
|
-
require 'ardb/
|
1
|
+
require 'ardb/cli/clirb'
|
2
|
+
require 'ardb/cli/commands'
|
3
|
+
require 'ardb/version'
|
3
4
|
|
4
5
|
module Ardb
|
5
6
|
|
6
7
|
class CLI
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
COMMANDS = Hash.new{ |h, k| InvalidCommand.new(k) }.tap do |h|
|
15
|
-
h['connect'] = ConnectCommand
|
16
|
-
h['create'] = CreateCommand
|
17
|
-
h['drop'] = DropCommand
|
18
|
-
h['migrate'] = MigrateCommand
|
19
|
-
h['generate-migration'] = GenerateMigrationCommand
|
9
|
+
COMMANDS = CommandSet.new{ |unknown| InvalidCommand.new(unknown) }.tap do |c|
|
10
|
+
c.add(ConnectCommand, 'connect')
|
11
|
+
c.add(CreateCommand, 'create')
|
12
|
+
c.add(DropCommand, 'drop')
|
13
|
+
c.add(MigrateCommand, 'migrate')
|
14
|
+
c.add(GenerateMigrationCommand, 'generate-migration')
|
20
15
|
end
|
21
16
|
|
22
17
|
def self.run(args)
|
@@ -32,11 +27,10 @@ module Ardb
|
|
32
27
|
def run(args)
|
33
28
|
begin
|
34
29
|
$LOAD_PATH.push(Dir.pwd) unless $LOAD_PATH.include?(Dir.pwd)
|
35
|
-
Ardb.init(false) # don't establish a connection
|
36
30
|
|
37
31
|
cmd_name = args.shift
|
38
|
-
cmd = COMMANDS[cmd_name]
|
39
|
-
cmd.run
|
32
|
+
cmd = COMMANDS[cmd_name]
|
33
|
+
cmd.run(args)
|
40
34
|
rescue CLIRB::HelpExit
|
41
35
|
@stdout.puts cmd.help
|
42
36
|
rescue CLIRB::VersionExit
|
@@ -65,208 +59,6 @@ module Ardb
|
|
65
59
|
end
|
66
60
|
end
|
67
61
|
|
68
|
-
InvalidCommandError = Class.new(ArgumentError)
|
69
|
-
CommandExitError = Class.new(RuntimeError)
|
70
|
-
|
71
|
-
class InvalidCommand
|
72
|
-
|
73
|
-
attr_reader :name, :argv, :clirb
|
74
|
-
|
75
|
-
def initialize(name)
|
76
|
-
@name = name
|
77
|
-
@argv = []
|
78
|
-
@clirb = Ardb::CLIRB.new
|
79
|
-
end
|
80
|
-
|
81
|
-
def new(args)
|
82
|
-
@argv = [@name, args].flatten.compact
|
83
|
-
self
|
84
|
-
end
|
85
|
-
|
86
|
-
def run
|
87
|
-
@clirb.parse!(@argv)
|
88
|
-
raise CLIRB::HelpExit if @clirb.args.empty? || @name.to_s.empty?
|
89
|
-
raise InvalidCommandError, "'#{self.name}' is not a command."
|
90
|
-
end
|
91
|
-
|
92
|
-
def help
|
93
|
-
"Usage: ardb [COMMAND] [options]\n\n" \
|
94
|
-
"Commands: #{COMMANDS.keys.sort.join(', ')}\n" \
|
95
|
-
"Options: #{@clirb}"
|
96
|
-
end
|
97
|
-
|
98
|
-
end
|
99
|
-
|
100
|
-
class ConnectCommand
|
101
|
-
|
102
|
-
attr_reader :clirb
|
103
|
-
|
104
|
-
def initialize(argv, stdout = nil, stderr = nil)
|
105
|
-
@argv = argv
|
106
|
-
@stdout = stdout || $stdout
|
107
|
-
@stderr = stderr || $stderr
|
108
|
-
|
109
|
-
@clirb = Ardb::CLIRB.new
|
110
|
-
@adapter = Ardb::Adapter.send(Ardb.config.db.adapter)
|
111
|
-
end
|
112
|
-
|
113
|
-
def run
|
114
|
-
@clirb.parse!(@argv)
|
115
|
-
begin
|
116
|
-
Ardb.init
|
117
|
-
@adapter.connect_db
|
118
|
-
@stdout.puts "connected to #{Ardb.config.db.adapter} db `#{Ardb.config.db.database}`"
|
119
|
-
rescue StandardError => e
|
120
|
-
@stderr.puts e
|
121
|
-
@stderr.puts e.backtrace.join("\n")
|
122
|
-
@stderr.puts "error connecting to #{Ardb.config.db.database.inspect} database " \
|
123
|
-
"with #{Ardb.config.db_settings.inspect}"
|
124
|
-
raise CommandExitError
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
def help
|
129
|
-
"Usage: ardb connect [options]\n\n" \
|
130
|
-
"Options: #{@clirb}"
|
131
|
-
end
|
132
|
-
|
133
|
-
end
|
134
|
-
|
135
|
-
class CreateCommand
|
136
|
-
|
137
|
-
attr_reader :clirb
|
138
|
-
|
139
|
-
def initialize(argv, stdout = nil, stderr = nil)
|
140
|
-
@argv = argv
|
141
|
-
@stdout = stdout || $stdout
|
142
|
-
@stderr = stderr || $stderr
|
143
|
-
|
144
|
-
@clirb = Ardb::CLIRB.new
|
145
|
-
@adapter = Ardb::Adapter.send(Ardb.config.db.adapter)
|
146
|
-
end
|
147
|
-
|
148
|
-
def run
|
149
|
-
@clirb.parse!(@argv)
|
150
|
-
begin
|
151
|
-
@adapter.create_db
|
152
|
-
@stdout.puts "created #{Ardb.config.db.adapter} db `#{Ardb.config.db.database}`"
|
153
|
-
rescue StandardError => e
|
154
|
-
@stderr.puts e
|
155
|
-
@stderr.puts "error creating #{Ardb.config.db.database.inspect} database"
|
156
|
-
raise CommandExitError
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
def help
|
161
|
-
"Usage: ardb create [options]\n\n" \
|
162
|
-
"Options: #{@clirb}"
|
163
|
-
end
|
164
|
-
|
165
|
-
end
|
166
|
-
|
167
|
-
class DropCommand
|
168
|
-
|
169
|
-
attr_reader :clirb
|
170
|
-
|
171
|
-
def initialize(argv, stdout = nil, stderr = nil)
|
172
|
-
@argv = argv
|
173
|
-
@stdout = stdout || $stdout
|
174
|
-
@stderr = stderr || $stderr
|
175
|
-
|
176
|
-
@clirb = Ardb::CLIRB.new
|
177
|
-
@adapter = Ardb::Adapter.send(Ardb.config.db.adapter)
|
178
|
-
end
|
179
|
-
|
180
|
-
def run
|
181
|
-
@clirb.parse!(@argv)
|
182
|
-
begin
|
183
|
-
@adapter.drop_db
|
184
|
-
@stdout.puts "dropped #{Ardb.config.db.adapter} db `#{Ardb.config.db.database}`"
|
185
|
-
rescue StandardError => e
|
186
|
-
@stderr.puts e
|
187
|
-
@stderr.puts "error dropping #{Ardb.config.db.database.inspect} database"
|
188
|
-
raise CommandExitError
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
def help
|
193
|
-
"Usage: ardb drop [options]\n\n" \
|
194
|
-
"Options: #{@clirb}"
|
195
|
-
end
|
196
|
-
|
197
|
-
end
|
198
|
-
|
199
|
-
class MigrateCommand
|
200
|
-
|
201
|
-
attr_reader :clirb
|
202
|
-
|
203
|
-
def initialize(argv, stdout = nil, stderr = nil)
|
204
|
-
@argv = argv
|
205
|
-
@stdout = stdout || $stdout
|
206
|
-
@stderr = stderr || $stderr
|
207
|
-
|
208
|
-
@clirb = Ardb::CLIRB.new
|
209
|
-
@adapter = Ardb::Adapter.send(Ardb.config.db.adapter)
|
210
|
-
end
|
211
|
-
|
212
|
-
def run
|
213
|
-
@clirb.parse!(@argv)
|
214
|
-
begin
|
215
|
-
Ardb.init
|
216
|
-
@adapter.migrate_db
|
217
|
-
@adapter.dump_schema unless ENV['ARDB_MIGRATE_NO_SCHEMA']
|
218
|
-
rescue StandardError => e
|
219
|
-
@stderr.puts e
|
220
|
-
@stderr.puts e.backtrace.join("\n")
|
221
|
-
@stderr.puts "error migrating #{Ardb.config.db.database.inspect} database"
|
222
|
-
raise CommandExitError
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
def help
|
227
|
-
"Usage: ardb migrate [options]\n\n" \
|
228
|
-
"Options: #{@clirb}"
|
229
|
-
end
|
230
|
-
|
231
|
-
end
|
232
|
-
|
233
|
-
class GenerateMigrationCommand
|
234
|
-
|
235
|
-
attr_reader :clirb
|
236
|
-
|
237
|
-
def initialize(argv, stdout = nil, stderr = nil)
|
238
|
-
@argv = argv
|
239
|
-
@stdout = stdout || $stdout
|
240
|
-
@stderr = stderr || $stderr
|
241
|
-
|
242
|
-
@clirb = Ardb::CLIRB.new
|
243
|
-
end
|
244
|
-
|
245
|
-
def run
|
246
|
-
@clirb.parse!(@argv)
|
247
|
-
begin
|
248
|
-
require "ardb/migration"
|
249
|
-
path = Ardb::Migration.new(@clirb.args.first).save!.file_path
|
250
|
-
@stdout.puts "generated #{path}"
|
251
|
-
rescue Ardb::Migration::NoIdentifierError => exception
|
252
|
-
error = ArgumentError.new("MIGRATION-NAME must be provided")
|
253
|
-
error.set_backtrace(exception.backtrace)
|
254
|
-
raise error
|
255
|
-
rescue StandardError => e
|
256
|
-
@stderr.puts e
|
257
|
-
@stderr.puts e.backtrace.join("\n")
|
258
|
-
@stderr.puts "error generating migration"
|
259
|
-
raise CommandExitError
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
def help
|
264
|
-
"Usage: ardb generate-migration [options] MIGRATION-NAME\n\n" \
|
265
|
-
"Options: #{@clirb}"
|
266
|
-
end
|
267
|
-
|
268
|
-
end
|
269
|
-
|
270
62
|
end
|
271
63
|
|
272
64
|
end
|