migreazy 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/bin/migreazy +0 -176
  3. data/lib/migreazy.rb +177 -0
  4. data/migreazy.gemspec +46 -0
  5. metadata +5 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
data/bin/migreazy CHANGED
@@ -7,182 +7,6 @@ end
7
7
 
8
8
  require 'config/boot'
9
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
10
 
187
11
  action = ARGV.first
188
12
  action_class = begin
data/lib/migreazy.rb ADDED
@@ -0,0 +1,177 @@
1
+ require 'grit'
2
+
3
+ module Migreazy
4
+ @@db_connected = false
5
+
6
+ def self.ensure_db_connection
7
+ unless @@db_connected
8
+ db_config = YAML::load(IO.read("./config/database.yml"))
9
+ ActiveRecord::Base.establish_connection db_config['development']
10
+ @@db_connected = true
11
+ end
12
+ end
13
+
14
+ class Action
15
+ def initialize(args)
16
+ @args = args
17
+ if args.empty?
18
+ @source1 = Source::Database.new
19
+ @source2 = Source::WorkingCopy.new
20
+ elsif args.size == 1
21
+ @source1 = Source::Database.new
22
+ @source2 = Source.new_from_command_line_arg(args.first)
23
+ elsif args.size == 2
24
+ @source1 = Source.new_from_command_line_arg(args.first)
25
+ @source2 = Source.new_from_command_line_arg(args.last)
26
+ end
27
+ end
28
+
29
+ class Diff < Action
30
+ def run
31
+ left_only = (@source1.migrations - @source2.migrations).sort.reverse
32
+ right_only = (@source2.migrations - @source1.migrations).sort.reverse
33
+ unless left_only.empty? && right_only.empty?
34
+ puts(
35
+ sprintf("%-39s %-39s", @source1.description, @source2.description)
36
+ )
37
+ puts(
38
+ sprintf(
39
+ "%-39s %-39s", ("=" * @source1.description.length),
40
+ ("=" * @source2.description.length)
41
+ )
42
+ )
43
+ until (left_only.empty? && right_only.empty?)
44
+ side = if right_only.empty?
45
+ :left
46
+ elsif left_only.empty?
47
+ :right
48
+ elsif left_only.first > right_only.first
49
+ :left
50
+ else
51
+ :right
52
+ end
53
+ if side == :left
54
+ puts sprintf("%-39s", left_only.first.to_s)
55
+ left_only.shift
56
+ else
57
+ puts((" " * 39) + sprintf(" %-39s", right_only.first.to_s))
58
+ right_only.shift
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ class Down < Action
66
+ def run
67
+ successful_downs = []
68
+ missing_in_branch = @source1.migrations - @source2.migrations
69
+ if missing_in_branch.empty?
70
+ puts "No down migrations to run"
71
+ else
72
+ missing_in_branch.sort.reverse.each do |version|
73
+ file = Dir.entries("./db/migrate/").detect { |entry|
74
+ entry =~ /^#{version}.*\.rb$/
75
+ }
76
+ require "./db/migrate/#{file}"
77
+ file =~ /^#{version}_([_a-z0-9]*).rb/
78
+ $1.camelize.constantize.down
79
+ ActiveRecord::Base.connection.execute(
80
+ "delete from schema_migrations where version = '#{version}'"
81
+ )
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ class Find < Action
88
+ def initialize(args)
89
+ @args = args
90
+ @migration_number = args.first
91
+ end
92
+
93
+ def run
94
+ repo = Grit::Repo.new '.'
95
+ branches = repo.heads.select { |head|
96
+ (head.commit.tree / "db/migrate").contents.any? { |blob|
97
+ blob.name =~ /^0*#{@migration_number}/
98
+ }
99
+ }
100
+ puts "Migration #{@migration_number} found in " +
101
+ branches.map(&:name).join(', ')
102
+ end
103
+ end
104
+ end
105
+
106
+ class Source
107
+ def self.new_from_command_line_arg(arg)
108
+ if File.exist?(File.expand_path(arg))
109
+ TextFile.new arg
110
+ else
111
+ GitBranch.new arg
112
+ end
113
+ end
114
+
115
+ attr_reader :migrations
116
+
117
+ class Database < Source
118
+ def initialize
119
+ Migreazy.ensure_db_connection
120
+ @migrations = ActiveRecord::Base.connection.select_all(
121
+ "select version from schema_migrations"
122
+ ).map { |hash| hash['version'] }
123
+ end
124
+
125
+ def description
126
+ "Development DB"
127
+ end
128
+ end
129
+
130
+ class GitBranch < Source
131
+ def initialize(git_branch_name)
132
+ @git_branch_name = git_branch_name
133
+ repo = Grit::Repo.new '.'
134
+ head = repo.heads.detect { |h| h.name == @git_branch_name }
135
+ all_migrations = (head.commit.tree / "db/migrate").contents
136
+ @migrations = all_migrations.map { |blob|
137
+ blob.name.gsub(/^0*(\d+)_.*/, '\1')
138
+ }
139
+ end
140
+
141
+ def description
142
+ "Branch #{@git_branch_name}"
143
+ end
144
+ end
145
+
146
+ class TextFile < Source
147
+ def initialize(file)
148
+ @file = File.expand_path(file)
149
+ @migrations = []
150
+ File.read(@file).each_line do |line|
151
+ line.chomp!
152
+ if line.to_i.to_s == line
153
+ @migrations << line
154
+ end
155
+ end
156
+ end
157
+
158
+ def description
159
+ "File #{@file}"
160
+ end
161
+ end
162
+
163
+ class WorkingCopy < Source
164
+ def initialize
165
+ @migrations = Dir.entries("./db/migrate").select { |entry|
166
+ entry =~ /^\d+.*\.rb$/
167
+ }.map { |entry|
168
+ entry.gsub(/^0*(\d+)_.*/, '\1')
169
+ }
170
+ end
171
+
172
+ def description
173
+ "Working copy"
174
+ end
175
+ end
176
+ end
177
+ end
data/migreazy.gemspec ADDED
@@ -0,0 +1,46 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{migreazy}
8
+ s.version = "1.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Francis Hwang"]
12
+ s.date = %q{2011-08-20}
13
+ s.default_executable = %q{migreazy}
14
+ s.description = %q{migreazy helps manage Rails migrations across git branches.}
15
+ s.email = %q{sera@fhwang.net}
16
+ s.executables = ["migreazy"]
17
+ s.extra_rdoc_files = [
18
+ "README"
19
+ ]
20
+ s.files = [
21
+ "MIT-LICENSE",
22
+ "README",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "bin/migreazy",
26
+ "lib/migreazy.rb",
27
+ "migreazy.gemspec"
28
+ ]
29
+ s.homepage = %q{http://github.com/fhwang/migreazy}
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.6.2}
32
+ s.summary = %q{migreazy helps manage Rails migrations across git branches.}
33
+
34
+ if s.respond_to? :specification_version then
35
+ s.specification_version = 3
36
+
37
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
38
+ s.add_runtime_dependency(%q<grit>, [">= 0"])
39
+ else
40
+ s.add_dependency(%q<grit>, [">= 0"])
41
+ end
42
+ else
43
+ s.add_dependency(%q<grit>, [">= 0"])
44
+ end
45
+ end
46
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: migreazy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Francis Hwang
@@ -46,6 +46,8 @@ files:
46
46
  - Rakefile
47
47
  - VERSION
48
48
  - bin/migreazy
49
+ - lib/migreazy.rb
50
+ - migreazy.gemspec
49
51
  has_rdoc: true
50
52
  homepage: http://github.com/fhwang/migreazy
51
53
  licenses: []