#!/usr/bin/env ruby require 'gli' require 'mince_migrator/version' require 'mince_migrator/list' require 'mince_migrator/cli_helper' include GLI::App include MinceMigrator::CliHelper program_desc 'Manages database migrations for ruby applications' version MinceMigrator.version desc 'Creates a migration' arg_name 'Name of migration' command :create do |c| c.action do |global_options,options,args| name = args.join(" ") create_migration(name) end end desc 'Lists all migrations and their statuses' command :list do |c| c.action do |global_options,options,args| list_migrations(MinceMigrator::List.new) end end desc 'Reverts a migration' arg_name 'Name of migration' command :revert do |c| c.action do |global_options,options,args| name = args.join(" ") revert_migration(name: name) end end desc 'Deletes all migrations that have already ran' command :delete_ran do |c| c.action do |global_options,options,args| list = MinceMigrator::List.new('ran') if list.all.any? list.all.each do |migration| delete_migration(migration: migration) end else puts "No migrations were found." end end end desc 'Deletes a migration' arg_name 'Name of migration' command :delete do |c| c.action do |global_options,options,args| name = args.join(" ") delete_migration(name: name) end end desc 'Runs a migration' arg_name 'Name of migration' command :run do |c| c.action do |global_options,options,args| name = args.join(" ") run_migration(name: name) end end desc 'Runs all migrations that have not yet been ran' command :run_all do |c| c.action do list = MinceMigrator::List.new('not ran') if list.all.any? list.all.each do |migration| run_migration(migration: migration) end else puts "There were no migrations to run." end end end desc 'Shows the details of a migration' arg_name 'Name of migration' command :show do |c| c.action do |global_options,options,args| name = args.join(" ") if args.any? show_migration(name) else puts "You must provide a name" end end end pre do |global,command,options,args| # Pre logic here # Return true to proceed; false to abort and not call the # chosen command # Use skips_pre before a command to skip this block # on that command only if File.exists?(MinceMigrator::Config.config_file) require MinceMigrator::Config.config_file end true end post do |global,command,options,args| # Post logic here # Use skips_post before a command to skip this # block on that command only end on_error do |exception| # Error logic here # return false to skip default error handling true end exit run(ARGV)