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/adapter/sqlite.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "fileutils"
|
2
4
|
require "ardb"
|
3
5
|
require "ardb/adapter/base"
|
@@ -5,23 +7,21 @@ require "ardb/adapter/base"
|
|
5
7
|
module Ardb::Adapter
|
6
8
|
class Sqlite < Ardb::Adapter::Base
|
7
9
|
def db_file_path
|
8
|
-
File.expand_path(
|
10
|
+
File.expand_path(database, config.root_path)
|
9
11
|
end
|
10
12
|
|
11
13
|
def validate!
|
12
|
-
if File.exist?(
|
13
|
-
raise RuntimeError, "`#{self.database}` already exists"
|
14
|
-
end
|
14
|
+
raise "`#{database}` already exists" if File.exist?(db_file_path)
|
15
15
|
end
|
16
16
|
|
17
17
|
def create_db
|
18
18
|
validate!
|
19
|
-
FileUtils.mkdir_p File.dirname(
|
20
|
-
ActiveRecord::Base.establish_connection(
|
19
|
+
FileUtils.mkdir_p File.dirname(db_file_path)
|
20
|
+
ActiveRecord::Base.establish_connection(connect_hash)
|
21
21
|
end
|
22
22
|
|
23
23
|
def drop_db
|
24
|
-
FileUtils.rm(
|
24
|
+
FileUtils.rm(db_file_path) if File.exist?(db_file_path)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
data/lib/ardb/adapter_spy.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "ardb"
|
2
4
|
require "ardb/adapter/base"
|
3
5
|
|
@@ -20,60 +22,60 @@ module Ardb
|
|
20
22
|
end
|
21
23
|
|
22
24
|
def create_db_called?
|
23
|
-
|
25
|
+
create_db_called_count > 0
|
24
26
|
end
|
25
27
|
|
26
28
|
def drop_db_called?
|
27
|
-
|
29
|
+
drop_db_called_count > 0
|
28
30
|
end
|
29
31
|
|
30
32
|
def drop_tables_called?
|
31
|
-
|
33
|
+
drop_tables_called_count > 0
|
32
34
|
end
|
33
35
|
|
34
36
|
def connect_db_called?
|
35
|
-
|
37
|
+
connect_db_called_count > 0
|
36
38
|
end
|
37
39
|
|
38
40
|
def migrate_db_called?
|
39
|
-
|
41
|
+
migrate_db_called_count > 0
|
40
42
|
end
|
41
43
|
|
42
44
|
def load_schema_called?
|
43
|
-
|
45
|
+
load_schema_called_count > 0
|
44
46
|
end
|
45
47
|
|
46
48
|
def dump_schema_called?
|
47
|
-
|
49
|
+
dump_schema_called_count > 0
|
48
50
|
end
|
49
51
|
|
50
52
|
# Overwritten `Adapter::Base` methods
|
51
53
|
|
52
|
-
def create_db(*
|
54
|
+
def create_db(*_args)
|
53
55
|
self.create_db_called_count += 1
|
54
56
|
end
|
55
57
|
|
56
|
-
def drop_db(*
|
58
|
+
def drop_db(*_args)
|
57
59
|
self.drop_db_called_count += 1
|
58
60
|
end
|
59
61
|
|
60
|
-
def drop_tables(*
|
62
|
+
def drop_tables(*_args)
|
61
63
|
self.drop_tables_called_count += 1
|
62
64
|
end
|
63
65
|
|
64
|
-
def connect_db(*
|
66
|
+
def connect_db(*_args)
|
65
67
|
self.connect_db_called_count += 1
|
66
68
|
end
|
67
69
|
|
68
|
-
def migrate_db(*
|
70
|
+
def migrate_db(*_args)
|
69
71
|
self.migrate_db_called_count += 1
|
70
72
|
end
|
71
73
|
|
72
|
-
def load_schema(*
|
74
|
+
def load_schema(*_args)
|
73
75
|
self.load_schema_called_count += 1
|
74
76
|
end
|
75
77
|
|
76
|
-
def dump_schema(*
|
78
|
+
def dump_schema(*_args)
|
77
79
|
self.dump_schema_called_count += 1
|
78
80
|
end
|
79
81
|
end
|
data/lib/ardb/cli.rb
CHANGED
@@ -1,23 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "ardb/cli/clirb"
|
2
4
|
require "ardb/cli/commands"
|
3
5
|
require "ardb/version"
|
4
6
|
|
5
7
|
module Ardb
|
6
8
|
class CLI
|
7
|
-
COMMANDS =
|
8
|
-
|
9
|
-
|
10
|
-
c
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
COMMANDS =
|
10
|
+
CommandSet.new{ |unknown|
|
11
|
+
InvalidCommand.new(unknown)
|
12
|
+
}.tap do |c|
|
13
|
+
c.add(ConnectCommand)
|
14
|
+
c.add(CreateCommand)
|
15
|
+
c.add(DropCommand)
|
16
|
+
c.add(GenerateMigrationCommand)
|
17
|
+
c.add(MigrateCommand)
|
18
|
+
c.add(MigrateUpCommand)
|
19
|
+
c.add(MigrateDownCommand)
|
20
|
+
c.add(MigrateForwardCommand)
|
21
|
+
c.add(MigrateBackwardCommand)
|
22
|
+
end
|
18
23
|
|
19
24
|
def self.run(args)
|
20
|
-
|
25
|
+
new.run(args)
|
21
26
|
end
|
22
27
|
|
23
28
|
def initialize(kernel = nil, stdout = nil, stderr = nil)
|
@@ -37,16 +42,16 @@ module Ardb
|
|
37
42
|
@stdout.puts cmd.command_help
|
38
43
|
rescue CLIRB::VersionExit
|
39
44
|
@stdout.puts Ardb::VERSION
|
40
|
-
rescue CLIRB::Error, ArgumentError, InvalidCommandError =>
|
41
|
-
display_debug(
|
42
|
-
@stderr.puts "#{
|
45
|
+
rescue CLIRB::Error, ArgumentError, InvalidCommandError => ex
|
46
|
+
display_debug(ex)
|
47
|
+
@stderr.puts "#{ex.message}\n\n"
|
43
48
|
@stdout.puts cmd.command_help
|
44
49
|
@kernel.exit 1
|
45
50
|
rescue CommandExitError
|
46
51
|
@kernel.exit 1
|
47
|
-
rescue
|
48
|
-
@stderr.puts "#{
|
49
|
-
@stderr.puts
|
52
|
+
rescue => ex
|
53
|
+
@stderr.puts "#{ex.class}: #{ex.message}"
|
54
|
+
@stderr.puts ex.backtrace.join("\n")
|
50
55
|
@kernel.exit 1
|
51
56
|
end
|
52
57
|
@kernel.exit 0
|
data/lib/ardb/cli/clirb.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
module Ardb; end
|
2
|
+
|
2
3
|
class Ardb::CLI
|
3
4
|
class CLIRB # Version 1.1.0, https://github.com/redding/cli.rb
|
4
5
|
Error = Class.new(RuntimeError);
|
@@ -18,13 +19,16 @@ class Ardb::CLI
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def option(*args); @options << Option.new(*args); end
|
22
|
+
|
21
23
|
def parse!(argv)
|
22
24
|
@args = (argv || []).dup.tap do |args_list|
|
23
25
|
begin; @parser.parse!(args_list)
|
24
26
|
rescue OptionParser::ParseError => err; raise Error, err.message; end
|
25
27
|
end; @data = @args + [@opts]
|
26
28
|
end
|
29
|
+
|
27
30
|
def to_s; @parser.to_s; end
|
31
|
+
|
28
32
|
def inspect
|
29
33
|
"#<#{self.class}:#{"0x0%x" % (object_id << 1)} @data=#{@data.inspect}>"
|
30
34
|
end
|
@@ -50,6 +54,7 @@ class Ardb::CLI
|
|
50
54
|
custom_abbrev || processed_name.gsub(/[^a-z]/, "").chars.first || "a"
|
51
55
|
]
|
52
56
|
end
|
57
|
+
|
53
58
|
def gvalinfo(v); v.kind_of?(Class) ? [nil,v] : [v,v.class]; end
|
54
59
|
end
|
55
60
|
end
|
data/lib/ardb/cli/commands.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "ardb"
|
2
4
|
require "ardb/cli/clirb"
|
3
|
-
require "much-
|
5
|
+
require "much-mixin"
|
4
6
|
|
5
7
|
module Ardb; end
|
8
|
+
|
6
9
|
class Ardb::CLI
|
7
10
|
InvalidCommandError = Class.new(ArgumentError)
|
8
11
|
CommandExitError = Class.new(RuntimeError)
|
@@ -15,12 +18,14 @@ class Ardb::CLI
|
|
15
18
|
@clirb = CLIRB.new
|
16
19
|
end
|
17
20
|
|
18
|
-
def new
|
21
|
+
def new
|
22
|
+
self
|
23
|
+
end
|
19
24
|
|
20
25
|
def run(argv)
|
21
26
|
@clirb.parse!([@name, argv].flatten.compact)
|
22
27
|
raise CLIRB::HelpExit if @name.to_s.empty?
|
23
|
-
raise InvalidCommandError, "\"#{
|
28
|
+
raise InvalidCommandError, "\"#{name}\" is not a command."
|
24
29
|
end
|
25
30
|
|
26
31
|
def command_help
|
@@ -32,19 +37,26 @@ class Ardb::CLI
|
|
32
37
|
end
|
33
38
|
|
34
39
|
module ValidCommand
|
35
|
-
include
|
40
|
+
include MuchMixin
|
36
41
|
|
37
|
-
|
38
|
-
def command_name
|
39
|
-
|
42
|
+
mixin_class_methods do
|
43
|
+
def command_name
|
44
|
+
raise NotImplementedError
|
45
|
+
end
|
46
|
+
|
47
|
+
def command_summary
|
48
|
+
""
|
49
|
+
end
|
40
50
|
end
|
41
51
|
|
42
|
-
|
52
|
+
mixin_instance_methods do
|
43
53
|
def initialize(&clirb_build)
|
44
54
|
@clirb = CLIRB.new(&clirb_build)
|
45
55
|
end
|
46
56
|
|
47
|
-
def clirb
|
57
|
+
def clirb
|
58
|
+
@clirb
|
59
|
+
end
|
48
60
|
|
49
61
|
def run(argv, stdout = nil, stderr = nil)
|
50
62
|
@clirb.parse!(argv)
|
@@ -52,14 +64,19 @@ class Ardb::CLI
|
|
52
64
|
@stderr = stderr || $stderr
|
53
65
|
end
|
54
66
|
|
55
|
-
def command_name
|
56
|
-
|
67
|
+
def command_name
|
68
|
+
self.class.command_name
|
69
|
+
end
|
70
|
+
|
71
|
+
def command_summary
|
72
|
+
self.class.command_summary
|
73
|
+
end
|
57
74
|
|
58
75
|
def command_help
|
59
|
-
"Usage: ardb #{
|
60
|
-
"Options: #{
|
76
|
+
"Usage: ardb #{command_name} [options]\n\n" \
|
77
|
+
"Options: #{clirb}\n" \
|
61
78
|
"Description:\n" \
|
62
|
-
" #{
|
79
|
+
" #{command_summary}"
|
63
80
|
end
|
64
81
|
end
|
65
82
|
end
|
@@ -67,8 +84,13 @@ class Ardb::CLI
|
|
67
84
|
class ConnectCommand
|
68
85
|
include ValidCommand
|
69
86
|
|
70
|
-
def self.command_name
|
71
|
-
|
87
|
+
def self.command_name
|
88
|
+
"connect"
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.command_summary
|
92
|
+
"Connect to the configured DB"
|
93
|
+
end
|
72
94
|
|
73
95
|
def run(argv, *args)
|
74
96
|
super
|
@@ -76,15 +98,21 @@ class Ardb::CLI
|
|
76
98
|
begin
|
77
99
|
Ardb.init(false)
|
78
100
|
Ardb.adapter.connect_db
|
79
|
-
@stdout.puts
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
rescue
|
84
|
-
@stderr.puts
|
85
|
-
|
86
|
-
|
87
|
-
|
101
|
+
@stdout.puts(
|
102
|
+
"connected to #{Ardb.config.adapter} "\
|
103
|
+
"db #{Ardb.config.database.inspect}",
|
104
|
+
)
|
105
|
+
rescue ActiveRecord::NoDatabaseError
|
106
|
+
@stderr.puts(
|
107
|
+
"error: database #{Ardb.config.database.inspect} does not exist",
|
108
|
+
)
|
109
|
+
rescue => ex
|
110
|
+
@stderr.puts ex
|
111
|
+
@stderr.puts ex.backtrace.join("\n")
|
112
|
+
@stderr.puts(
|
113
|
+
"error connecting to #{Ardb.config.database.inspect} database " \
|
114
|
+
"with #{Ardb.config.activerecord_connect_hash.inspect}",
|
115
|
+
)
|
88
116
|
raise CommandExitError
|
89
117
|
end
|
90
118
|
end
|
@@ -93,8 +121,13 @@ class Ardb::CLI
|
|
93
121
|
class CreateCommand
|
94
122
|
include ValidCommand
|
95
123
|
|
96
|
-
def self.command_name
|
97
|
-
|
124
|
+
def self.command_name
|
125
|
+
"create"
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.command_summary
|
129
|
+
"Create the configured DB"
|
130
|
+
end
|
98
131
|
|
99
132
|
def run(argv, *args)
|
100
133
|
super
|
@@ -102,12 +135,15 @@ class Ardb::CLI
|
|
102
135
|
begin
|
103
136
|
Ardb.init(false)
|
104
137
|
Ardb.adapter.create_db
|
105
|
-
@stdout.puts
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
138
|
+
@stdout.puts(
|
139
|
+
"created #{Ardb.config.adapter} db #{Ardb.config.database.inspect}",
|
140
|
+
)
|
141
|
+
rescue ActiveRecord::StatementInvalid
|
142
|
+
@stderr.puts(
|
143
|
+
"error: database #{Ardb.config.database.inspect} already exists",
|
144
|
+
)
|
145
|
+
rescue => ex
|
146
|
+
@stderr.puts ex
|
111
147
|
@stderr.puts "error creating #{Ardb.config.database.inspect} database"
|
112
148
|
raise CommandExitError
|
113
149
|
end
|
@@ -117,8 +153,13 @@ class Ardb::CLI
|
|
117
153
|
class DropCommand
|
118
154
|
include ValidCommand
|
119
155
|
|
120
|
-
def self.command_name
|
121
|
-
|
156
|
+
def self.command_name
|
157
|
+
"drop"
|
158
|
+
end
|
159
|
+
|
160
|
+
def self.command_summary
|
161
|
+
"Drop the configured DB"
|
162
|
+
end
|
122
163
|
|
123
164
|
def run(argv, *args)
|
124
165
|
super
|
@@ -126,12 +167,15 @@ class Ardb::CLI
|
|
126
167
|
begin
|
127
168
|
Ardb.init(true)
|
128
169
|
Ardb.adapter.drop_db
|
129
|
-
@stdout.puts
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
170
|
+
@stdout.puts(
|
171
|
+
"dropped #{Ardb.config.adapter} db #{Ardb.config.database.inspect}",
|
172
|
+
)
|
173
|
+
rescue ActiveRecord::NoDatabaseError
|
174
|
+
@stderr.puts(
|
175
|
+
"error: database #{Ardb.config.database.inspect} does not exist",
|
176
|
+
)
|
177
|
+
rescue => ex
|
178
|
+
@stderr.puts ex
|
135
179
|
@stderr.puts "error dropping #{Ardb.config.database.inspect} database"
|
136
180
|
raise CommandExitError
|
137
181
|
end
|
@@ -141,8 +185,13 @@ class Ardb::CLI
|
|
141
185
|
class GenerateMigrationCommand
|
142
186
|
include ValidCommand
|
143
187
|
|
144
|
-
def self.command_name
|
145
|
-
|
188
|
+
def self.command_name
|
189
|
+
"generate-migration"
|
190
|
+
end
|
191
|
+
|
192
|
+
def self.command_summary
|
193
|
+
"Generate a MIGRATION-NAME migration file"
|
194
|
+
end
|
146
195
|
|
147
196
|
def run(argv, *args)
|
148
197
|
super
|
@@ -154,50 +203,55 @@ class Ardb::CLI
|
|
154
203
|
migration = Ardb::Migration.new(Ardb.config, @clirb.args.first)
|
155
204
|
migration.save!
|
156
205
|
@stdout.puts "generated #{migration.file_path}"
|
157
|
-
rescue Ardb::Migration::NoIdentifierError =>
|
206
|
+
rescue Ardb::Migration::NoIdentifierError => ex
|
158
207
|
error = ArgumentError.new("MIGRATION-NAME must be provided")
|
159
|
-
error.set_backtrace(
|
208
|
+
error.set_backtrace(ex.backtrace)
|
160
209
|
raise error
|
161
|
-
rescue
|
162
|
-
@stderr.puts
|
163
|
-
@stderr.puts
|
210
|
+
rescue => ex
|
211
|
+
@stderr.puts ex
|
212
|
+
@stderr.puts ex.backtrace.join("\n")
|
164
213
|
@stderr.puts "error generating migration"
|
165
214
|
raise CommandExitError
|
166
215
|
end
|
167
216
|
end
|
168
217
|
|
169
218
|
def command_help
|
170
|
-
"Usage: ardb #{
|
171
|
-
"Options: #{
|
219
|
+
"Usage: ardb #{command_name} MIGRATION-NAME [options]\n\n" \
|
220
|
+
"Options: #{clirb}\n" \
|
172
221
|
"Description:\n" \
|
173
|
-
" #{
|
222
|
+
" #{command_summary}"
|
174
223
|
end
|
175
224
|
end
|
176
225
|
|
177
226
|
module MigrateCommandBehaviors
|
178
|
-
include
|
227
|
+
include MuchMixin
|
179
228
|
|
180
|
-
|
229
|
+
mixin_included do
|
181
230
|
include ValidCommand
|
182
231
|
end
|
183
232
|
|
184
|
-
|
185
|
-
def migrate
|
233
|
+
mixin_instance_methods do
|
234
|
+
def migrate
|
235
|
+
raise NotImplementedError
|
236
|
+
end
|
186
237
|
|
187
238
|
def run(argv, *args)
|
188
239
|
super
|
189
240
|
|
190
241
|
begin
|
191
242
|
Ardb.init(true)
|
192
|
-
|
243
|
+
migrate
|
193
244
|
Ardb.adapter.dump_schema unless ENV["ARDB_MIGRATE_NO_SCHEMA"]
|
194
|
-
rescue ActiveRecord::NoDatabaseError
|
195
|
-
@stderr.puts
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
@stderr.puts
|
200
|
-
@stderr.puts
|
245
|
+
rescue ActiveRecord::NoDatabaseError
|
246
|
+
@stderr.puts(
|
247
|
+
"error: database #{Ardb.config.database.inspect} does not exist",
|
248
|
+
)
|
249
|
+
rescue => ex
|
250
|
+
@stderr.puts ex
|
251
|
+
@stderr.puts ex.backtrace.join("\n")
|
252
|
+
@stderr.puts(
|
253
|
+
"error migrating #{Ardb.config.database.inspect} database",
|
254
|
+
)
|
201
255
|
raise CommandExitError
|
202
256
|
end
|
203
257
|
end
|
@@ -207,8 +261,13 @@ class Ardb::CLI
|
|
207
261
|
class MigrateCommand
|
208
262
|
include MigrateCommandBehaviors
|
209
263
|
|
210
|
-
def self.command_name
|
211
|
-
|
264
|
+
def self.command_name
|
265
|
+
"migrate"
|
266
|
+
end
|
267
|
+
|
268
|
+
def self.command_summary
|
269
|
+
"Migrate the configured DB"
|
270
|
+
end
|
212
271
|
|
213
272
|
def migrate
|
214
273
|
Ardb.adapter.migrate_db
|
@@ -216,43 +275,60 @@ class Ardb::CLI
|
|
216
275
|
end
|
217
276
|
|
218
277
|
module MigrateStyleBehaviors
|
219
|
-
include
|
278
|
+
include MuchMixin
|
220
279
|
|
221
|
-
|
280
|
+
mixin_included do
|
222
281
|
include MigrateCommandBehaviors
|
223
282
|
end
|
224
283
|
|
225
|
-
|
226
|
-
def command_style
|
284
|
+
mixin_class_methods do
|
285
|
+
def command_style
|
286
|
+
raise NotImplementedError
|
287
|
+
end
|
227
288
|
|
228
|
-
def command_name
|
229
|
-
|
289
|
+
def command_name
|
290
|
+
"migrate-#{command_style}"
|
291
|
+
end
|
292
|
+
|
293
|
+
def command_summary
|
294
|
+
"Migrate the configured DB #{command_style}"
|
295
|
+
end
|
230
296
|
end
|
231
297
|
|
232
|
-
|
298
|
+
mixin_instance_methods do
|
233
299
|
def migrate
|
234
|
-
Ardb.adapter.send(
|
300
|
+
Ardb.adapter.send(
|
301
|
+
"migrate_db_#{self.class.command_style}",
|
302
|
+
*migrate_args,
|
303
|
+
)
|
235
304
|
end
|
236
305
|
|
237
306
|
private
|
238
307
|
|
239
|
-
def migrate_args
|
308
|
+
def migrate_args
|
309
|
+
raise NotImplementedError
|
310
|
+
end
|
240
311
|
end
|
241
312
|
end
|
242
313
|
|
243
314
|
module MigrateDirectionBehaviors
|
244
|
-
include
|
315
|
+
include MuchMixin
|
245
316
|
|
246
|
-
|
317
|
+
mixin_included do
|
247
318
|
include MigrateStyleBehaviors
|
248
319
|
end
|
249
320
|
|
250
|
-
|
251
|
-
def command_style
|
252
|
-
|
321
|
+
mixin_class_methods do
|
322
|
+
def command_style
|
323
|
+
command_direction
|
324
|
+
end
|
325
|
+
|
326
|
+
def command_direction
|
327
|
+
raise NotImplementedError
|
328
|
+
end
|
253
329
|
end
|
254
330
|
|
255
|
-
|
331
|
+
mixin_instance_methods do
|
256
332
|
def initialize
|
257
333
|
super do
|
258
334
|
option(:target_version, "version to migrate to", value: String)
|
@@ -268,18 +344,23 @@ class Ardb::CLI
|
|
268
344
|
end
|
269
345
|
|
270
346
|
module MigrateStepDirectionBehaviors
|
271
|
-
include
|
347
|
+
include MuchMixin
|
272
348
|
|
273
|
-
|
349
|
+
mixin_included do
|
274
350
|
include MigrateStyleBehaviors
|
275
351
|
end
|
276
352
|
|
277
|
-
|
278
|
-
def command_style
|
279
|
-
|
353
|
+
mixin_class_methods do
|
354
|
+
def command_style
|
355
|
+
command_direction
|
356
|
+
end
|
357
|
+
|
358
|
+
def command_direction
|
359
|
+
raise NotImplementedError
|
360
|
+
end
|
280
361
|
end
|
281
362
|
|
282
|
-
|
363
|
+
mixin_instance_methods do
|
283
364
|
def initialize
|
284
365
|
super do
|
285
366
|
option(:steps, "number of migrations to migrate", value: 1)
|
@@ -297,30 +378,38 @@ class Ardb::CLI
|
|
297
378
|
class MigrateUpCommand
|
298
379
|
include MigrateDirectionBehaviors
|
299
380
|
|
300
|
-
def self.command_direction
|
381
|
+
def self.command_direction
|
382
|
+
"up"
|
383
|
+
end
|
301
384
|
end
|
302
385
|
|
303
386
|
class MigrateDownCommand
|
304
387
|
include MigrateDirectionBehaviors
|
305
388
|
|
306
|
-
def self.command_direction
|
389
|
+
def self.command_direction
|
390
|
+
"down"
|
391
|
+
end
|
307
392
|
end
|
308
393
|
|
309
394
|
class MigrateForwardCommand
|
310
395
|
include MigrateStepDirectionBehaviors
|
311
396
|
|
312
|
-
def self.command_direction
|
397
|
+
def self.command_direction
|
398
|
+
"forward"
|
399
|
+
end
|
313
400
|
end
|
314
401
|
|
315
402
|
class MigrateBackwardCommand
|
316
403
|
include MigrateStepDirectionBehaviors
|
317
404
|
|
318
|
-
def self.command_direction
|
405
|
+
def self.command_direction
|
406
|
+
"backward"
|
407
|
+
end
|
319
408
|
end
|
320
409
|
|
321
410
|
class CommandSet
|
322
411
|
def initialize(&unknown_cmd_block)
|
323
|
-
@lookup = Hash.new{ |
|
412
|
+
@lookup = Hash.new{ |_h, k| unknown_cmd_block.call(k) }
|
324
413
|
@names = []
|
325
414
|
@aliases = {}
|
326
415
|
@summaries = {}
|
@@ -329,7 +418,7 @@ class Ardb::CLI
|
|
329
418
|
def add(klass)
|
330
419
|
begin
|
331
420
|
cmd = klass.new
|
332
|
-
rescue
|
421
|
+
rescue
|
333
422
|
# don"t add any commands you can"t initialize
|
334
423
|
else
|
335
424
|
@lookup[cmd.command_name] = cmd
|
@@ -355,11 +444,11 @@ class Ardb::CLI
|
|
355
444
|
end
|
356
445
|
|
357
446
|
def to_s
|
358
|
-
max_name_size
|
447
|
+
max_name_size = @names.map(&:size).max || 0
|
359
448
|
|
360
|
-
@to_s ||= @names.map
|
449
|
+
@to_s ||= @names.map{ |n|
|
361
450
|
"#{n.ljust(max_name_size)} #{@summaries[n]}"
|
362
|
-
|
451
|
+
}.join("\n")
|
363
452
|
end
|
364
453
|
end
|
365
454
|
end
|