Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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
intervention.
  • Loading branch information...
commit ec0059220f7c6c6ca53c8554e195e2daa1120dfd 1 parent 4dca556
Mike 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
View
@@ -2,11 +2,45 @@
#Creates and migrates the database if it doesn't yet exist
#Implementation based on http://trevorturk.wordpress.com/2008/04/10/automatically-creating-loading-and-migrating-your-database/
+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 = ActiveSupport::OrderedHash.new
+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 = Proc.new 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
+ )
+end
+
+# 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.
begin
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
Rake::Task["db:create"].invoke
Rake::Task["db:migrate"].invoke
Rake::Task["db:seed"].invoke
Please sign in to comment.
Something went wrong with that request. Please try again.