Class: MigrExt::RailsApplication
- Inherits:
-
Object
- Object
- MigrExt::RailsApplication
- Defined in:
- lib/migrext/rails.rb
Instance Method Summary collapse
- #create_migration!(version, name, content) ⇒ Object
- #current_migrations ⇒ Object
- #database_version ⇒ Object
- #eval_in_application(code) ⇒ Object
- #external_sources ⇒ Object
- #import_externals! ⇒ Object
-
#initialize(base_dir) ⇒ RailsApplication
constructor
A new instance of RailsApplication.
Constructor Details
#initialize(base_dir) ⇒ RailsApplication
Returns a new instance of RailsApplication.
8 9 10 |
# File 'lib/migrext/rails.rb', line 8 def initialize(base_dir) @base_dir = base_dir end |
Instance Method Details
#create_migration!(version, name, content) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/migrext/rails.rb', line 26 def create_migration!(version, name, content) filepath = Pathname.new("#@base_dir/db/migrate/#{version}_#{name.underscore}.rb") raise RuntimeError, "Migration #{version}:#{name} exist" if filepath.exist? filepath.dirname.mkpath filepath.open("w") {|f| f.write content } @current_migrations = nil end |
#current_migrations ⇒ Object
16 17 18 |
# File 'lib/migrext/rails.rb', line 16 def current_migrations @current_migrations ||= MigrExt::MigrationSet.new(self, nil, "#@base_dir/db/migrate").current_migrations end |
#database_version ⇒ Object
35 36 37 |
# File 'lib/migrext/rails.rb', line 35 def database_version eval_in_application "ActiveRecord::Migrator.current_version" end |
#eval_in_application(code) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/migrext/rails.rb', line 39 def eval_in_application(code) @drb_rails ||= begin require 'drb' rd, wr = IO.pipe @rails_pid = fork do rd.close Dir.chdir @base_dir def eval_code(code); eval code; end require "#{@base_dir}/config/environment.rb" DRb.start_service nil, self wr.print DRb.uri wr.close DRb.thread.join exit 1 # unreachable end wr.close at_exit do begin Process.kill 15, @rails_pid rescue Errno::ESRCH # Process was died some time ago. Ignore the error. true end end DRbObject.new(nil, rd.read) end @drb_rails.eval_code code end |
#external_sources ⇒ Object
20 21 22 23 24 |
# File 'lib/migrext/rails.rb', line 20 def external_sources @external_sources ||= begin eval_in_application("ActiveRecord::Migrator.external_sources") end end |