public
Description: Ruby on Rails plugin to provide notification when you need to run migrations
Homepage: http://blog.hasmanythrough.com/2008/3/2/migration-concordance
Clone URL: git://github.com/joshsusser/migration_concordance.git
Search Repo:
joshsusser (author)
Sun Mar 02 12:37:29 -0800 2008
commit  42c9612f1851a2281aeb0b7e4bf2225308dd3e42
tree    524b96d91c9be9f6055dea1eee4476e65f935ccc
parent  302bec0d44291def3913882954edc56240f47b68
migration_concordance / lib / migration_concordance.rb
100644 58 lines (51 sloc) 1.863 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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