Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Plugins: automatic installation of migrations

At boot, if any engines are installed, any migrations in their db/schema
that do not exist in the main app will be copied over, so that newly
installed and upgraded plugins can install their tables without any user
  • Loading branch information...
commit ec0059220f7c6c6ca53c8554e195e2daa1120dfd 1 parent 4dca556
@mikldt mikldt authored
Showing with 34 additions and 0 deletions.
  1. +34 −0 config/initializers/01-db_create_load_or_migrate.rb
34 config/initializers/01-db_create_load_or_migrate.rb
@@ -2,11 +2,45 @@
#Creates and migrates the database if it doesn't yet exist
#Implementation based on
+require 'benchmark'
+# Plugin Migrations
+# Check all installed plugins for migrations, and install any that don't
+# exist already. Very similar to rake railties:install:migrations from
+# ActiveRecord. Benchmark comes in at around 62ms.
+# Inspired by rake railties:install:migrations from ActiveRecord.
+railties =
+Rails.application.railties.all do |railtie|
+ if railtie.respond_to?(:paths) && (path = railtie.paths['db/migrate'].first)
+ railties[railtie.railtie_name] = path
+ end
+ on_copy = do |name, migration, old_path|
+ puts "Copied migration #{migration.basename} from #{name}"
+ end
+ ActiveRecord::Migration.copy(
+ ActiveRecord::Migrator.migrations_paths.first,
+ railties,
+ :on_copy => on_copy
+ )
+# Database creation and migration
+# Notes:
+# - Is the rescue really needed?
+# - This is very fast, but does not account for the possibility of
+# unrun migrations with an older timestamp. Rails supports detection
+# and execution of those migrations with db:migrate, but the code
+# below does not.
current_version = ActiveRecord::Migrator.current_version
#Grab the timestamp from each migration filename, and run max() on the resulting array
highest_version = Dir.glob("#{Rails.root.to_s}/db/migrate/*.rb").map { |f| f.match(/\d+/).to_s.to_i}.max
if current_version == 0
+ require 'rake'
+ Concerto::Application.load_tasks

0 comments on commit ec00592

Please sign in to comment.
Something went wrong with that request. Please try again.