bin/ridgepole in ridgepole-0.2.1 vs bin/ridgepole in ridgepole-0.2.2
- old
+ new
@@ -15,10 +15,13 @@
config = nil
mode = nil
file = DEFAULT_FILENAME
output_file = '-'
split = false
+diff_files = nil
+diff_with_apply = false
+exit_code = 0
options = {
:dry_run => false,
:debug => false,
}
@@ -36,20 +39,34 @@
opt.on('-f', '--file FILE') {|v| file = v }
opt.on('', '--dry-run') { options[:dry_run] = true }
opt.on('-e', '--export') { set_mode[:export] }
opt.on('', '--split') {|v| split = true }
opt.on('', '--split-with-dir') {|v| split = :with_dir }
+ opt.on('-d', '--diff DSL1 DSL2') {|diff_arg1|
+ set_mode[:diff]
+ diff_arg2 = ARGV.first
+
+ if [diff_arg1, diff_arg2].any? {|i| i.nil? or i =~ /\A-/ }
+ puts opt.help
+ exit 1
+ end
+
+ ARGV.shift
+ diff_files = [diff_arg1, diff_arg2]
+ }
+ opt.on('', '--reverse') { options[:reverse] = true }
+ opt.on('', '--with-apply') { diff_with_apply = true }
opt.on('-o', '--output FILE') {|v| output_file = v }
opt.on('-t', '--tables TABLES', Array) {|v| options[:tables] = v }
opt.on('', '--ignore-tables TABLES', Array) {|v| options[:ignore_tables] = v.map {|i| Regexp.new(i) } }
opt.on('', '--disable-mysql-unsigned') { options[:disable_mysql_unsigned] = true }
opt.on('' , '--log-file LOG_FILE') {|v| options[:log_file] = v }
opt.on('' , '--verbose') { Ridgepole::Logger.verbose = true }
opt.on('' , '--debug') { options[:debug] = true }
opt.parse!
- unless config and mode
+ if not mode or ([:apply, :export].include?(mode) and not config) or (options[:with_apply] and not config)
puts opt.help
exit 1
end
rescue => e
$stderr.puts("[ERROR] #{e.message}")
@@ -59,13 +76,15 @@
begin
logger = Ridgepole::Logger.instance
logger.set_debug(options[:debug])
- config = open(config) {|f| f.read } if File.exist?(config)
- config = YAML.load(config)
- client = Ridgepole::Client.new(config, options)
+ if config
+ config = File.read(config) if File.exist?(config)
+ config = YAML.load(config)
+ client = Ridgepole::Client.new(config, options)
+ end
case mode
when :export
if split
logger.info('Export Schema')
@@ -113,11 +132,11 @@
msg = (options[:merge] ? 'Merge' : 'Apply') + " `#{file}`"
msg << ' (dry-run)' if options[:dry_run]
logger.info(msg)
- dsl = open(file) {|f| f.read }
+ dsl = File.read(file)
delta = client.diff(dsl, :path => file)
if options[:dry_run]
puts delta.script if delta.differ?
else
@@ -126,14 +145,40 @@
end
unless delta.differ?
logger.info('No change')
end
+ when :diff
+ diff_files = diff_files.map do |file|
+ if File.exist?(file)
+ file_ext = File.extname(file)
+ %w(.yml .yaml).include?(file_ext) ? YAML.load_file(file) : File.read(file)
+ else
+ YAML.load(file)
+ end
+ end
+
+ delta = Ridgepole::Client.diff(*diff_files, options)
+
+ if diff_with_apply
+ logger.verbose_info('# Update schema')
+
+ if delta.differ?
+ delta.migrate
+ else
+ logger.info('No change')
+ end
+ elsif delta.differ?
+ puts delta.script
+ exit_code = 1
+ end
end
rescue => e
if options[:debug]
raise e
else
$stderr.puts("[ERROR] #{e.message}")
exit 1
end
end
+
+exit exit_code