migreazy 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +61 -0
  3. data/Rakefile +16 -0
  4. data/VERSION +1 -0
  5. data/bin/migreazy +196 -0
  6. metadata +84 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,61 @@
1
+ migreazy
2
+ ========
3
+
4
+ "It ain't easy bein' greazy in a world full of cleanliness."
5
+ -- Method Man
6
+
7
+ migreazy is a tool that helps manage git branches and Rails migrations. Right now it support three actions:
8
+
9
+ Diff
10
+ ====
11
+
12
+ Diff will compare two sets of migrations and tell you what the differences are.
13
+
14
+ Show the differences between my development DB and the git branch called "my_branch":
15
+ $ migreazy diff my_branch
16
+
17
+ Show the differences between my development DB and my working copy:
18
+ $ migreazy diff
19
+
20
+ Show the differences between the git branches "master" and "my_branch":
21
+ $ migreazy diff master my_branch
22
+
23
+ Down
24
+ ====
25
+
26
+ Down will apply down migrations to get your development DB to be a suitable state so you can switch branches. For example, let's say you have the branch "master" and the branch "my_branch", with the following migrations (using the old-fashioned migration numbers for purposes of illustration):
27
+
28
+ master my_branch
29
+ 1 1
30
+ 2
31
+ 3
32
+
33
+ If you're working in "my_branch", your development DB will have migrations 1 and 2, but not 3. If for some reason you have to go back to "master" to do a quick bugfix or something, you can do this:
34
+
35
+ $ migreazy down master # this rolls back migration 2
36
+ $ git checkout master
37
+ $ rake db:migrate # this runs migration 3
38
+
39
+ Find
40
+ ====
41
+
42
+ Find takes a migration number as an argument and digs through your git branches to see which branches contain this migration.
43
+
44
+ $ migreazy find 20090512132032
45
+
46
+ This can come in handy if you do a ton of branching and then occasionally make the mistake of switching a branch without first migrating down from that branch's migrations.
47
+
48
+ For example, let's say you're in master but you're getting test failures that you think are being caused by having your migrations out of sync. Here's how you might fix that:
49
+
50
+ $ migreazy diff
51
+ Missing in development DB:
52
+ (none)
53
+
54
+ Missing in working copy:
55
+ 20090512132032
56
+ $ migreazy find 20090512132032
57
+ Migration 20090512132032 found in my_branch
58
+ $ git checkout my_branch
59
+ $ migreazy down master
60
+
61
+ Copyright (c) 2009 Francis Hwang, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "migreazy"
5
+ gem.summary = %Q{migreazy helps manage Rails migrations across git branches.}
6
+ gem.description = %Q{migreazy helps manage Rails migrations across git branches.}
7
+ gem.email = "sera@fhwang.net"
8
+ gem.homepage = "http://github.com/fhwang/migreazy"
9
+ gem.authors = ["Francis Hwang"]
10
+ gem.add_dependency 'grit'
11
+ end
12
+ Jeweler::GemcutterTasks.new
13
+ rescue LoadError
14
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
15
+ end
16
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/bin/migreazy ADDED
@@ -0,0 +1,196 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ if ARGV.size < 1
4
+ puts "migreazy [diff|down|find]"
5
+ exit
6
+ end
7
+
8
+ require 'config/boot'
9
+ require 'config/environment'
10
+ require 'grit'
11
+
12
+ module Migreazy
13
+ @@db_connected = false
14
+
15
+ def self.ensure_db_connection
16
+ unless @@db_connected
17
+ db_config = YAML::load(IO.read("./config/database.yml"))
18
+ ActiveRecord::Base.establish_connection db_config['development']
19
+ @@db_connected = true
20
+ end
21
+ end
22
+
23
+ class Action
24
+ def initialize(args)
25
+ @args = args
26
+ if args.empty?
27
+ @migration_source1 = MigrationSource::Database.new
28
+ @migration_source2 = MigrationSource::WorkingCopy.new
29
+ elsif args.size == 1
30
+ @migration_source1 = MigrationSource::Database.new
31
+ @migration_source2 = MigrationSource.new_from_command_line_arg(
32
+ args.first
33
+ )
34
+ elsif args.size == 2
35
+ @migration_source1 = MigrationSource.new_from_command_line_arg(
36
+ args.first
37
+ )
38
+ @migration_source2 = MigrationSource.new_from_command_line_arg(
39
+ args.last
40
+ )
41
+ end
42
+ end
43
+
44
+ class Diff < Action
45
+ def run
46
+ missing_in_db =
47
+ @migration_source2.migrations - @migration_source1.migrations
48
+ puts "Missing in #{@migration_source1.description}:"
49
+ if missing_in_db.empty?
50
+ puts " (none)"
51
+ else
52
+ puts " #{missing_in_db.join(', ')}"
53
+ end
54
+ puts
55
+ missing_in_branch =
56
+ @migration_source1.migrations - @migration_source2.migrations
57
+ puts "Missing in #{@migration_source2.description}:"
58
+ if missing_in_branch.empty?
59
+ puts " (none)"
60
+ else
61
+ puts " #{missing_in_branch.join(', ')}"
62
+ end
63
+ end
64
+ end
65
+
66
+ class Down < Action
67
+ def run
68
+ successful_downs = []
69
+ missing_in_branch =
70
+ @migration_source1.migrations - @migration_source2.migrations
71
+ if missing_in_branch.empty?
72
+ puts "No down migrations to run"
73
+ else
74
+ past_migration_failed = false
75
+ missing_in_branch.sort.reverse.each do |migration|
76
+ if past_migration_failed
77
+ puts "Skipping down for #{migration} due to error in a previous migration"
78
+ else
79
+ output = `rake db:migrate:down VERSION=#{migration}`
80
+ puts output
81
+ if output =~ /^== .*: reverted \(\d*\.\d*s\)/
82
+ successful_downs << migration
83
+ else
84
+ unless past_migration_failed
85
+ puts "Skipping down for #{migration} due to error"
86
+ end
87
+ past_migration_failed = true
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ class Find < Action
96
+ def initialize(args)
97
+ @args = args
98
+ @migration_number = args.first
99
+ end
100
+
101
+ def run
102
+ repo = Grit::Repo.new '.'
103
+ branches = repo.heads.select { |head|
104
+ (head.commit.tree / "db/migrate").contents.any? { |blob|
105
+ blob.name =~ /^0*#{@migration_number}/
106
+ }
107
+ }
108
+ puts "Migration #{@migration_number} found in " +
109
+ branches.map(&:name).join(', ')
110
+ end
111
+ end
112
+ end
113
+
114
+ class MigrationSource
115
+ def self.new_from_command_line_arg(arg)
116
+ if File.exist?(File.expand_path(arg))
117
+ TextFile.new arg
118
+ else
119
+ GitBranch.new arg
120
+ end
121
+ end
122
+
123
+ attr_reader :migrations
124
+
125
+ class Database < MigrationSource
126
+ def initialize
127
+ Migreazy.ensure_db_connection
128
+ @migrations = ActiveRecord::Base.connection.select_all(
129
+ "select version from schema_migrations"
130
+ ).map { |hash| hash['version'] }
131
+ end
132
+
133
+ def description
134
+ "development DB"
135
+ end
136
+ end
137
+
138
+ class GitBranch < MigrationSource
139
+ def initialize(git_branch_name)
140
+ @git_branch_name = git_branch_name
141
+ repo = Grit::Repo.new '.'
142
+ head = repo.heads.detect { |h| h.name == @git_branch_name }
143
+ all_migrations = (head.commit.tree / "db/migrate").contents
144
+ @migrations = all_migrations.map { |blob|
145
+ blob.name.gsub(/^0*(\d+)_.*/, '\1')
146
+ }
147
+ end
148
+
149
+ def description
150
+ "branch #{@git_branch_name}"
151
+ end
152
+ end
153
+
154
+ class TextFile < MigrationSource
155
+ def initialize(file)
156
+ @file = File.expand_path(file)
157
+ @migrations = []
158
+ File.read(@file).each_line do |line|
159
+ line.chomp!
160
+ if line.to_i.to_s == line
161
+ @migrations << line
162
+ end
163
+ end
164
+ end
165
+
166
+ def description
167
+ "file #{@file}"
168
+ end
169
+ end
170
+
171
+ class WorkingCopy < MigrationSource
172
+ def initialize
173
+ @migrations = Dir.entries("./db/migrate").select { |entry|
174
+ entry =~ /^\d+.*\.rb$/
175
+ }.map { |entry|
176
+ entry.gsub(/^0*(\d+)_.*/, '\1')
177
+ }
178
+ end
179
+
180
+ def description
181
+ "working copy"
182
+ end
183
+ end
184
+ end
185
+ end
186
+
187
+ action = ARGV.first
188
+ action_class = begin
189
+ Migreazy::Action.const_get(action.capitalize)
190
+ rescue NameError
191
+ end
192
+ if action_class
193
+ action_class.new(ARGV[1..-1]).run
194
+ else
195
+ puts "'#{action}' is not a migreazy action."
196
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: migreazy
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Francis Hwang
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-20 00:00:00 -04:00
19
+ default_executable: migreazy
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: grit
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: migreazy helps manage Rails migrations across git branches.
36
+ email: sera@fhwang.net
37
+ executables:
38
+ - migreazy
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - MIT-LICENSE
45
+ - README
46
+ - Rakefile
47
+ - VERSION
48
+ - bin/migreazy
49
+ has_rdoc: true
50
+ homepage: http://github.com/fhwang/migreazy
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.6.2
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: migreazy helps manage Rails migrations across git branches.
83
+ test_files: []
84
+