Last active
August 29, 2015 14:19
-
-
Save nalabjp/fa95bdd314bf3525aa90 to your computer and use it in GitHub Desktop.
Rake Task for Ridgepole
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $stdout.sync = true | |
| DB_CONFIG = 'config/database.yml' | |
| SCHEMA_FILE = 'db/Schemafile' | |
| SCHEMA_DUMP_FILE = "#{SCHEMA_FILE}.dump" | |
| OPT_ENV = "--env #{Rails.env}" | |
| OPT_CONFIG = "--config #{DB_CONFIG}" | |
| OPT_FILE = "--file #{SCHEMA_FILE}" | |
| OPT_MYSQL_AWESOME = '--enable-mysql-awesome --mysql-awesome-unsigned-pk' | |
| OPT_APPLY = '--apply' | |
| OPT_EXPORT = '--export' | |
| OPT_OUTPUT_DUMP = "-o #{SCHEMA_DUMP_FILE}" | |
| OPT_DIFF = '--diff' | |
| OPT_DRY_RUN = '--dry-run' | |
| def exec_ridgepole(action, options = {}) | |
| command = "bundle exec ridgepole #{make_option(action, options)}" | |
| command = "TEST_ENV_NUMBER=#{ENV['TEST_ENV_NUMBER']} #{command}" if ENV['TEST_ENV_NUMBER'] | |
| if defined?(Bundler) | |
| Bundler.clean_system(command) | |
| else | |
| command = command.sub(/(bundle exec )/, '') | |
| system(command) | |
| end | |
| puts '---' | |
| puts "Executed command => `#{command}`" | |
| end | |
| def make_option(action, options = {}) | |
| send("#{action}_option", options) | |
| end | |
| def apply_option(options = {}) | |
| [OPT_ENV, OPT_CONFIG, OPT_FILE, OPT_MYSQL_AWESOME, OPT_APPLY].tap{ |arr| arr.push(OPT_DRY_RUN) if options[:dry_run] }.join(' ') | |
| end | |
| def export_option(options = {}) | |
| [OPT_ENV, OPT_CONFIG, OPT_MYSQL_AWESOME, OPT_EXPORT, options[:output]].join(' ') | |
| end | |
| def diff_option(options = {}) | |
| [OPT_ENV, OPT_CONFIG, OPT_MYSQL_AWESOME, OPT_DIFF, options[:diff]].join(' ') | |
| end | |
| namespace :ridgepole do | |
| desc '`ridgepole --apply` with requirements options' | |
| task apply: :environment do | |
| exec_ridgepole(:apply) | |
| Annotate::Migration.update_annotations if defined?(Annotate::Migration) | |
| end | |
| desc '`ridgepole --apply --dry-run` with requirements options' | |
| namespace :apply do | |
| task dry_run: :environment do | |
| exec_ridgepole(:apply, dry_run: true) | |
| end | |
| end | |
| desc '`ridgepole --export` with requirements options' | |
| task export: :environment do | |
| exec_ridgepole(:export, output: OPT_OUTPUT_DUMP) | |
| end | |
| desc '`rake db:drop`, `rake db:create` and `ridgepole --apply` with requirements options' | |
| task reset: :environment do | |
| ActiveRecord::Tasks::DatabaseTasks.drop_current | |
| ActiveRecord::Tasks::DatabaseTasks.create_current | |
| exec_ridgepole(:apply) | |
| end | |
| desc 'diff current schemafile and current db schema, if no argument exists)' | |
| task diff: :environment do | |
| diff_files = ENV.fetch('diff', "#{DB_CONFIG}, #{SCHEMA_FILE}").split(',').map(&:strip).join(' ') | |
| exec_ridgepole(:diff, diff: diff_files) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment