Skip to content

Instantly share code, notes, and snippets.

@MichalBryxi
Last active December 16, 2015 22:39
Show Gist options
  • Save MichalBryxi/5508598 to your computer and use it in GitHub Desktop.
Save MichalBryxi/5508598 to your computer and use it in GitHub Desktop.
Rails ActiveRecord migrations enhanced by possibility to run PHP migrations. Useful when you use ActiveRecord migrations for your (PHP) application, but you also need to use your application sources code classes in the migration process.
##### Our Migration class db/iw_migration.rb ######
# IW_Migration adds possibility to run PHP migrations in classic
# rails ActiveRecord stack. It will receive PHP class name which will be
# executed instead of regular AR migration.
class IW_Migration < ActiveRecord::Migration
# This will hold for us PHP class name
@migration_class = nil
# This will hold for us caller stamp from it's filename
@migration_stamp = nil
# Call this from your migration to set which class should be migrated
def self.migration_class(my_class)
@migration_class = my_class
@migration_stamp = caller.first[/[0-9]{14}/]
end
# Constructs PHP migration command
# - method - determines which method on our class is called, usually baseUp / baseDown
def self.get_command(method)
dst_app = ENV['IW_DEPLOY_TO']
customer = ENV['customer']
starter_script = "#{dst_app}/portal/bin/job-starter.php "
service="IW_Bin_Migration_#{@migration_stamp}_#{@migration_class}.php";
return "php #{starter_script} -jobname=#{service} -service=#{service} -customer=#{customer} -skiplock=yes -method=baseUp"
end
# Runs PHP migration
# - method - determines which method on our class is called, usually baseUp / baseDown
def self.run_migration(method)
command = self.get_command(method)
puts `#{command}`
if $?.exitstatus != 0
raise "Migration \"#{@migration_class}\" failed"
end
end
def self.up
self.run_migration('baseUp')
end
def self.down
self.run_migration('baseDown')
end
end
##### Sample migration db/migrate/20130502132000_active_record_test.rb #####
require './db/iw_migration'
class ActiveRecordTest < IW_Migration
migration_class "Test"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment