ridgepole 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b78450efbe8c650197b9515c90abced3f6a6575
4
- data.tar.gz: 4e78c9e6a877414d75d58735ad8ebd2c0b2d090f
3
+ metadata.gz: 2e09733c0324e6f1e7aac45b4b27b1dbf8492114
4
+ data.tar.gz: ee817aee1eabf4f66821585b4e114daad6b1a41b
5
5
  SHA512:
6
- metadata.gz: 8be2793d3f8bc2f8083c5c50805dc180cec3e0ce451203d74f678f601aa2f24c5225d0fe0b81d142ba87bf40d879f9c54cbe7547c2af14612ec9ea4d451a3459
7
- data.tar.gz: f8e0160bb8ade13201edf404c30278705ec9d8fad1b2d3cbd6390b8739db82b6754ef6eda4ca281da7a6196b847d931299961f1a2b972754f736c8b1ec1f36b4
6
+ metadata.gz: 12cb083697c48bcd3b799f6686a32f91bdd62852d69fa235e9afabc790bf1b10e72b13357debd2a8d6b04d38fdaa1c850b407c04a7f622d75d3f282a747059a3
7
+ data.tar.gz: be9c09c97c28b2ee4614230c37e81cb63f5323e1f9f50d68ab0e3d6f2806df15287432ebe4e05d458f8a0a40047e6116ca17029716d26f73ac66cdf9e21943cd
data/bin/ridgepole CHANGED
@@ -42,6 +42,7 @@ ARGV.options do |opt|
42
42
  opt.on('-t', '--tables TABLES', Array) {|v| options[:tables] = v }
43
43
  opt.on('', '--ignore-tables TABLES', Array) {|v| options[:ignore_tables] = v.map {|i| Regexp.new(i) } }
44
44
  opt.on('', '--disable-mysql-unsigned') { options[:disable_mysql_unsigned] = true }
45
+ opt.on('' , '--log-file LOG_FILE') {|v| options[:log_file] = v }
45
46
  opt.on('' , '--verbose') { Ridgepole::Logger.verbose = true }
46
47
  opt.on('' , '--debug') { options[:debug] = true }
47
48
  opt.parse!
@@ -5,7 +5,15 @@ class Ridgepole::Delta
5
5
  end
6
6
 
7
7
  def migrate
8
- ActiveRecord::Schema.new.instance_eval(script)
8
+ if log_file = @options[:log_file]
9
+ result = ActiveRecord::Migration.record_time do
10
+ migrate0
11
+ end
12
+
13
+ open(log_file, 'wb') {|f| f.puts JSON.pretty_generate(result) }
14
+ else
15
+ migrate0
16
+ end
9
17
  end
10
18
 
11
19
  def script
@@ -36,6 +44,10 @@ class Ridgepole::Delta
36
44
 
37
45
  private
38
46
 
47
+ def migrate0
48
+ ActiveRecord::Schema.new.instance_eval(script)
49
+ end
50
+
39
51
  def append_create_table(table_name, attrs, buf)
40
52
  options = attrs[:options] || {}
41
53
  definition = attrs[:definition] || {}
@@ -12,7 +12,10 @@ class Ridgepole::Diff
12
12
  next unless target?(table_name)
13
13
 
14
14
  if (from_table_name = (to_attrs[:options] || {}).delete(:rename_from))
15
- next unless from.has_key?(from_table_name)
15
+ unless from.has_key?(from_table_name)
16
+ raise "Table `#{from_table_name}` not found"
17
+ end
18
+
16
19
  delta[:rename] ||= {}
17
20
  delta[:rename][table_name] = from_table_name
18
21
  from.delete(from_table_name)
@@ -50,7 +53,7 @@ class Ridgepole::Diff
50
53
  to = (to || {}).dup
51
54
  table_delta = {}
52
55
 
53
- scan_options_change(from[:options], to[:options], table_delta)
56
+ scan_options_change(table_name, from[:options], to[:options], table_delta)
54
57
  scan_definition_change(from[:definition], to[:definition], table_delta)
55
58
  scan_indices_change(from[:indices], to[:indices], table_delta)
56
59
 
@@ -60,8 +63,10 @@ class Ridgepole::Diff
60
63
  end
61
64
  end
62
65
 
63
- def scan_options_change(from, to, table_delta)
64
- Ridgepole::Logger.instance.warn('[WARNING] Table options cannot be changed')
66
+ def scan_options_change(table_name, from, to, table_delta)
67
+ unless from == to
68
+ Ridgepole::Logger.instance.warn("[WARNING] Table `#{table_name}` options cannot be changed")
69
+ end
65
70
  end
66
71
 
67
72
  def scan_definition_change(from, to, table_delta)
@@ -71,7 +76,10 @@ class Ridgepole::Diff
71
76
 
72
77
  to.dup.each do |column_name, to_attrs|
73
78
  if (from_column_name = (to_attrs[:options] || {}).delete(:rename_from))
74
- next unless from.has_key?(from_column_name)
79
+ unless from.has_key?(from_column_name)
80
+ raise "Column `#{from_column_name}` not found"
81
+ end
82
+
75
83
  definition_delta[:rename] ||= {}
76
84
  definition_delta[:rename][column_name] = from_column_name
77
85
  from.delete(from_column_name)
@@ -1,9 +1,57 @@
1
1
  require 'active_record/migration'
2
2
 
3
3
  class ActiveRecord::Migration
4
+ cattr_accessor :time_recorder
5
+
4
6
  def write_with_logging(text = '')
5
7
  logger = Ridgepole::Logger.instance
6
8
  logger.info(text)
9
+ parse_text(text)
7
10
  end
8
11
  alias_method_chain :write, :logging
12
+
13
+ def parse_text(text)
14
+ return unless self.time_recorder
15
+
16
+ case text
17
+ when /\A--\s+(.+)\Z/
18
+ self.time_recorder.add_key($1)
19
+ when /\A\s+->\s+(\d+\.\d+)s\Z/
20
+ self.time_recorder.add_value($1.to_f)
21
+ end
22
+ end
23
+
24
+ def self.record_time
25
+ result = nil
26
+
27
+ begin
28
+ self.time_recorder = TimeRecorder.new
29
+ yield
30
+ result = self.time_recorder.result
31
+ ensure
32
+ self.time_recorder = nil
33
+ end
34
+
35
+ return result
36
+ end
37
+
38
+ class TimeRecorder
39
+ attr_reader :result
40
+
41
+ def initialize
42
+ @result = {}
43
+ end
44
+
45
+ def add_key(key)
46
+ @key = key
47
+ end
48
+
49
+ def add_value(value)
50
+ if @key
51
+ @result[@key] = value
52
+ end
53
+
54
+ @key = nil
55
+ end
56
+ end
9
57
  end
@@ -1,3 +1,3 @@
1
1
  module Ridgepole
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -29,7 +29,7 @@ describe 'Ridgepole::Client#diff -> migrate' do
29
29
  subject { client }
30
30
 
31
31
  it {
32
- expect(Ridgepole::Logger.instance).to receive(:warn).with('[WARNING] Table options cannot be changed')
32
+ expect(Ridgepole::Logger.instance).to receive(:warn).with('[WARNING] Table `employees` options cannot be changed')
33
33
  delta = subject.diff(expected_dsl)
34
34
  expect(delta.differ?).to be_false
35
35
  expect(subject.dump).to eq actual_dsl.undent.strip
@@ -149,4 +149,27 @@ describe 'Ridgepole::Client#diff -> migrate' do
149
149
  expect(subject.dump).to eq expected_dsl.undent.strip.gsub(/\s*,\s*rename_from:.*$/, '')
150
150
  }
151
151
  end
152
+
153
+ context 'when rename column (not found)' do
154
+ before { restore_tables }
155
+ subject { client }
156
+
157
+ let(:dsl) {
158
+ <<-RUBY
159
+ create_table "employees", primary_key: "emp_no", force: true do |t|
160
+ t.date "birth_date", null: false
161
+ t.string "first_name", limit: 14, null: false
162
+ t.string "last_name", limit: 16, null: false
163
+ t.string "gender", limit: 1, null: false, rename_from: 'age'
164
+ t.date "hire_date", null: false
165
+ end
166
+ RUBY
167
+ }
168
+
169
+ it {
170
+ expect {
171
+ subject.diff(dsl)
172
+ }.to raise_error('Column `age` not found')
173
+ }
174
+ end
152
175
  end
@@ -149,4 +149,27 @@ describe 'Ridgepole::Client#diff -> migrate' do
149
149
  expect(subject.dump).to eq expected_dsl.undent.strip.gsub(/, rename_from: 'employees'/, '')
150
150
  }
151
151
  end
152
+
153
+ context 'when rename table (not found)' do
154
+ before { restore_tables }
155
+ subject { client }
156
+
157
+ let(:dsl) {
158
+ <<-RUBY
159
+ create_table "employees", primary_key: "emp_no", force: true, rename_from: 'not_employees' do |t|
160
+ t.date "birth_date", null: false
161
+ t.string "first_name", limit: 14, null: false
162
+ t.string "last_name", limit: 16, null: false
163
+ t.string "gender", limit: 1, null: false
164
+ t.date "hire_date", null: false
165
+ end
166
+ RUBY
167
+ }
168
+
169
+ it {
170
+ expect {
171
+ subject.diff(dsl)
172
+ }.to raise_error('Table `not_employees` not found')
173
+ }
174
+ end
152
175
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ridgepole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-30 00:00:00.000000000 Z
11
+ date: 2014-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord