Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add code to repo

  • Loading branch information...
commit 42c9612f1851a2281aeb0b7e4bf2225308dd3e42 1 parent 302bec0
@joshsusser authored
View
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Josh Susser <josh@hasmanythrough.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
28 README
@@ -0,0 +1,28 @@
+MigrationConcordance
+====================
+
+by Josh Susser, josh@hasmanythrough.com
+
+project page: http://github.com/joshsusser/migration_concordance
+project repo: git://github.com/joshsusser/migration_concordance.git
+
+This plugin extends Rails migrations to provide notification when you need to run migrations. It will detect both new migrations and modifications to previously run migrations. It is primarily of use for team development, but is also useful when deploying a release to a new environment to determine when migrations need to be run. This plugin does not run migrations automatically, but will notify you whenever you need to run them.
+
+To integrate with your application:
+
+1) Add the following line to the end of your config/environment.rb file:
+
+ puts ActiveRecord::Migrator.check_concordance
+
+2) Add the file db/migration_snapshot.yml to your SCM ignore list (for example, the svn:ignore property or .gitignore file). Putting the snapshot file under version control defeats the whole purpose of this feature.
+
+Any time you load your application's environment, it will indicate whether you need to run migrations to bring your schema into concordance with the migration files. This notification will happen when you run script/server, tests, the console, or any rake task that depends on the environment. If you want to check on migration status, you can run "rake environment" to print the notification.
+
+
+Background
+==========
+
+MigrationConcordance works by taking a snapshot of the state of an appliation's migrations every time you run "rake db:migrate". It creates a file in db/migration_snapshot.yml that holds a list of all current migrations and their SHA-1 hashes. Therefore, if you migrate to a migration that is not the latest (using rake db:migrate VERSION=<version>), you won't get any more notifications even if you aren't on the latest version. It assumes you know what you are doing and won't keep nagging you to migrate after you've migrated somewhere already.
+
+
+Copyright (c) 2008 Josh Susser, released under the MIT license
View
1  init.rb
@@ -0,0 +1 @@
+require 'migration_concordance'
View
7 install.rb
@@ -0,0 +1,7 @@
+puts ""
+puts "Migration Concordance plugin installed."
+puts "To integrate with your application, add the following line to the end of your config/environment.rb file:"
+puts " puts ActiveRecord::Migrator.check_concordance"
+puts ""
+puts "You should also add the file db/migration_snapshot.yml to your SCM ignore list."
+puts ""
View
57 lib/migration_concordance.rb
@@ -0,0 +1,57 @@
+module ActiveRecord
+ class Migrator
+ class << self
+
+ def check_concordance
+ case differs = self.current_differs_from_snapshot("db/migrate")
+ when false
+ "***** DB schema is in sync with migrations."
+ when 0
+ "***** DB schema state unknown. No migration snapshot to compare."
+ else
+ "***** DB schema needs to be migrated from: #{differs}"
+ end
+ end
+
+ def migrate_with_snapshot(migrations_path, target_version = nil)
+ if migrations_path =~ %r{vendor/plugins}
+ migrate_without_snapshot(migrations_path, target_version)
+ else
+ old_version = current_version rescue nil
+ migrate_without_snapshot(migrations_path, target_version)
+ if current_version != old_version
+ snapshot = generate_snapshot(migrations_path)
+ lines = YAML.dump(snapshot).split("\n").sort
+ File.open(snapshot_path, "w") { |f| f.puts(lines) }
+ end
+ end
+ end
+ alias_method_chain :migrate, :snapshot
+
+ def current_differs_from_snapshot(migrations_path)
+ if File.exists?(snapshot_path)
+ current = generate_snapshot(migrations_path)
+ snapshot = YAML.load_file(snapshot_path)
+ diff = current.diff(snapshot)
+ diff.keys.empty? ? false : diff.keys.sort.first
+ else
+ 0
+ end
+ end
+
+ def generate_snapshot(migrations_path)
+ files = Dir[File.join(migrations_path, "[0-9]*_*.rb")].collect { |n| File.basename(n) }
+ snapshot = {}
+ files.each do |file|
+ snapshot[File.basename(file, ".rb")] = Digest::SHA1.hexdigest(File.read(File.join(migrations_path, file)))
+ end
+ snapshot
+ end
+
+ def snapshot_path
+ File.join(RAILS_ROOT, "db", "migration_snapshot.yml")
+ end
+
+ end
+ end
+end
View
3  uninstall.rb
@@ -0,0 +1,3 @@
+puts ""
+puts "Don't forget to remove the call to ActiveRecord::Migrator.check_concordance from config/environment.rb"
+puts ""
Please sign in to comment.
Something went wrong with that request. Please try again.