ridgepole 0.2.7 → 0.2.8

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: b195dc41a13b87e24223916ac26701adc7cc4cb2
4
- data.tar.gz: 4d3ffa051d81f4f5b3366afe3106b4f3b592bb3c
3
+ metadata.gz: 493879d39cd654fb10b76891dd5800b33c6b6f32
4
+ data.tar.gz: 8afa5e085f0866ad5edc4777162e0e18cb394943
5
5
  SHA512:
6
- metadata.gz: a77bd763ef07424ae3db9870732029ed4a167d336f8517e02e7a7ef99c2a6ab0e306c80fc698837bd27c3d5602f3be5fd57a961e96518713bbc71de88b5d1383
7
- data.tar.gz: ded7b3fd166e9b64d03e860d2b712ebade9d7bef5fa8eafebc873aafcca047c1ea03f5a5119032e758fa203b81998eda277ad3328c133471c1f248e843e6a82c
6
+ metadata.gz: 449d9189b3b6e5e4bb1c9f72136379f19ad623527295dd75ab431028d13ab5dc2400e2766dbe7179e0bbd7bde9321c118f35c10256ccca4e2f1cec6e2d25cd8c
7
+ data.tar.gz: 04883048e57550f4d0bbc93ed4caed8aa7583cff0124706914890becbc4a28db942aebfee6b38d9e12a72688c8599066a1bb33ac19571642e4d3de8f325c1c94
data/README.md CHANGED
@@ -94,6 +94,8 @@ $ ridgepole -c config.yml --apply --dry-run
94
94
  Apply `Schemafile` (dry-run)
95
95
  add_column("articles", "author", :text, {:after=>"text"})
96
96
 
97
+ # ALTER TABLE `articles` ADD `author` text AFTER `text`
98
+
97
99
  $ ridgepole -c config.yml --apply
98
100
  Apply `Schemafile`
99
101
  -- add_column("articles", "author", :text, {:after=>"text"})
data/bin/ridgepole CHANGED
@@ -31,6 +31,14 @@ set_mode = proc do |m|
31
31
  mode = m
32
32
  end
33
33
 
34
+ def noop_migrate(delta)
35
+ puts <<-EOS
36
+ #{delta.script}
37
+
38
+ #{delta.migrate(:noop => true).each_line.map {|i| "# #{i}"}.join}
39
+ EOS
40
+ end
41
+
34
42
  ARGV.options do |opt|
35
43
  begin
36
44
  opt.on('-c', '--config CONF_OR_FILE') {|v| config = v }
@@ -84,6 +92,8 @@ begin
84
92
  client = Ridgepole::Client.new(config, options)
85
93
  end
86
94
 
95
+ ActiveRecord::Base.logger = logger
96
+
87
97
  case mode
88
98
  when :export
89
99
  if split
@@ -138,7 +148,9 @@ begin
138
148
  delta = client.diff(dsl, :path => file)
139
149
 
140
150
  if options[:dry_run]
141
- puts delta.script if delta.differ?
151
+ if delta.differ?
152
+ noop_migrate(delta)
153
+ end
142
154
  else
143
155
  logger.verbose_info('# Update schema')
144
156
  delta.migrate
@@ -168,7 +180,7 @@ begin
168
180
  logger.info('No change')
169
181
  end
170
182
  elsif delta.differ?
171
- puts delta.script
183
+ noop_migrate(delta)
172
184
  exit_code = 1
173
185
  end
174
186
  end
data/lib/ridgepole.rb CHANGED
@@ -12,6 +12,7 @@ require 'ridgepole/delta'
12
12
  require 'ridgepole/diff'
13
13
  require 'ridgepole/dsl_parser'
14
14
  require 'ridgepole/dumper'
15
+ require 'ridgepole/execute_expander'
15
16
  require 'ridgepole/logger'
16
17
  require 'ridgepole/migration_ext'
17
18
  require 'ridgepole/schema_dumper_ext'
@@ -2,7 +2,7 @@ class Ridgepole::Client
2
2
  def initialize(conn_spec, options = {})
3
3
  @options = options
4
4
  ActiveRecord::Base.establish_connection(conn_spec)
5
-
5
+ Ridgepole::ExecuteExpander.expand_execute(ActiveRecord::Base.connection)
6
6
  @dumper = Ridgepole::Dumper.new(@options)
7
7
  @parser = Ridgepole::DSLParser.new(@options)
8
8
  @diff = Ridgepole::Diff.new(@options)
@@ -4,15 +4,15 @@ class Ridgepole::Delta
4
4
  @options = options
5
5
  end
6
6
 
7
- def migrate
7
+ def migrate(options = {})
8
8
  if log_file = @options[:log_file]
9
9
  result = ActiveRecord::Migration.record_time do
10
- migrate0
10
+ migrate0(options)
11
11
  end
12
12
 
13
13
  open(log_file, 'wb') {|f| f.puts JSON.pretty_generate(result) }
14
14
  else
15
- migrate0
15
+ migrate0(options)
16
16
  end
17
17
  end
18
18
 
@@ -44,10 +44,32 @@ class Ridgepole::Delta
44
44
 
45
45
  private
46
46
 
47
- def migrate0
48
- ActiveRecord::Schema.new.instance_eval(script)
47
+ def migrate0(options = {})
48
+ if options[:noop]
49
+ disable_logging_orig = ActiveRecord::Migration.disable_logging
50
+
51
+ begin
52
+ ActiveRecord::Migration.disable_logging = true
53
+ buf = StringIO.new
54
+
55
+ callback = proc do |sql, name|
56
+ buf.puts sql if sql =~ /\A(CREATE|ALTER)\b/i
57
+ end
58
+
59
+ Ridgepole::ExecuteExpander.without_operation(callback) do
60
+ ActiveRecord::Schema.new.instance_eval(script)
61
+ end
62
+
63
+ buf.string.strip
64
+ ensure
65
+ ActiveRecord::Migration.disable_logging = disable_logging_orig
66
+ end
67
+ else
68
+ ActiveRecord::Schema.new.instance_eval(script)
69
+ end
49
70
  end
50
71
 
72
+
51
73
  def append_create_table(table_name, attrs, buf)
52
74
  options = attrs[:options] || {}
53
75
  definition = attrs[:definition] || {}
@@ -0,0 +1,42 @@
1
+ class Ridgepole::ExecuteExpander
2
+ class Stub
3
+ def method_missing(method_name, *args, &block)
4
+ # Nothing to do
5
+ end
6
+ end
7
+
8
+ cattr_accessor :noop, :instance_writer => false, :instance_reader => false
9
+ cattr_accessor :callback, :instance_writer => false, :instance_reader => false
10
+
11
+ class << self
12
+ def without_operation(callback = nil)
13
+ begin
14
+ self.noop = true
15
+ self.callback = callback
16
+ yield
17
+ ensure
18
+ self.noop = false
19
+ self.callback = nil
20
+ end
21
+ end
22
+
23
+ def expand_execute(connection)
24
+ return if connection.respond_to?(:execute_with_noop)
25
+
26
+ class << connection
27
+ def execute_with_noop(sql, name = nil)
28
+ if Ridgepole::ExecuteExpander.noop
29
+ if (callback = Ridgepole::ExecuteExpander.callback)
30
+ callback.call(sql, name)
31
+ end
32
+
33
+ Stub.new
34
+ else
35
+ execute_without_noop(sql, name)
36
+ end
37
+ end
38
+ alias_method_chain :execute, :noop
39
+ end
40
+ end
41
+ end # of class methods
42
+ end
@@ -2,10 +2,11 @@ require 'active_record/migration'
2
2
 
3
3
  class ActiveRecord::Migration
4
4
  cattr_accessor :time_recorder
5
+ cattr_accessor :disable_logging
5
6
 
6
7
  def write_with_logging(text = '')
7
8
  logger = Ridgepole::Logger.instance
8
- logger.info(text)
9
+ logger.info(text) unless self.disable_logging
9
10
  parse_text(text)
10
11
  end
11
12
  alias_method_chain :write, :logging
@@ -1,3 +1,3 @@
1
1
  module Ridgepole
2
- VERSION = '0.2.7'
2
+ VERSION = '0.2.8'
3
3
  end
@@ -0,0 +1,104 @@
1
+ describe 'Ridgepole::Client#diff -> migrate' do
2
+ context 'when database is empty' do
3
+ let(:actual_dsl) { '' }
4
+ let(:expected_dsl) {
5
+ <<-RUBY
6
+ create_table "clubs", force: true do |t|
7
+ t.string "name", default: "", null: false
8
+ end
9
+
10
+ add_index "clubs", ["name"], name: "idx_name", unique: true, using: :btree
11
+
12
+ create_table "departments", primary_key: "dept_no", force: true do |t|
13
+ t.string "dept_name", limit: 40, null: false
14
+ end
15
+
16
+ add_index "departments", ["dept_name"], name: "dept_name", unique: true, using: :btree
17
+
18
+ create_table "dept_emp", id: false, force: true do |t|
19
+ t.integer "emp_no", null: false
20
+ t.string "dept_no", limit: 4, null: false
21
+ t.date "from_date", null: false
22
+ t.date "to_date", null: false
23
+ end
24
+
25
+ add_index "dept_emp", ["dept_no"], name: "dept_no", using: :btree
26
+ add_index "dept_emp", ["emp_no"], name: "emp_no", using: :btree
27
+
28
+ create_table "dept_manager", id: false, force: true do |t|
29
+ t.string "dept_no", limit: 4, null: false
30
+ t.integer "emp_no", null: false
31
+ t.date "from_date", null: false
32
+ t.date "to_date", null: false
33
+ end
34
+
35
+ add_index "dept_manager", ["dept_no"], name: "dept_no", using: :btree
36
+ add_index "dept_manager", ["emp_no"], name: "emp_no", using: :btree
37
+
38
+ create_table "employee_clubs", force: true do |t|
39
+ t.integer "emp_no", unsigned: true, null: false
40
+ t.integer "club_id", unsigned: true, null: false
41
+ end
42
+
43
+ add_index "employee_clubs", ["emp_no", "club_id"], name: "idx_emp_no_club_id", using: :btree
44
+
45
+ create_table "employees", primary_key: "emp_no", force: true do |t|
46
+ t.date "birth_date", null: false
47
+ t.string "first_name", limit: 14, null: false
48
+ t.string "last_name", limit: 16, null: false
49
+ t.string "gender", limit: 1, null: false
50
+ t.date "hire_date", null: false
51
+ end
52
+
53
+ create_table "salaries", id: false, force: true do |t|
54
+ t.integer "emp_no", null: false
55
+ t.integer "salary", null: false
56
+ t.date "from_date", null: false
57
+ t.date "to_date", null: false
58
+ end
59
+
60
+ add_index "salaries", ["emp_no"], name: "emp_no", using: :btree
61
+
62
+ create_table "titles", id: false, force: true do |t|
63
+ t.integer "emp_no", null: false
64
+ t.string "title", limit: 50, null: false
65
+ t.date "from_date", null: false
66
+ t.date "to_date"
67
+ end
68
+
69
+ add_index "titles", ["emp_no"], name: "emp_no", using: :btree
70
+ RUBY
71
+ }
72
+
73
+ subject { client }
74
+
75
+ it {
76
+ delta = subject.diff(expected_dsl)
77
+ expect(delta.differ?).to be_truthy
78
+ sql = delta.migrate(:noop => true)
79
+ expect(subject.dump).to eq actual_dsl
80
+
81
+ sql = sql.each_line.map {|i| i.strip }.join("\n")
82
+
83
+ expect(sql).to eq <<-SQL.strip_heredoc.strip
84
+ CREATE TABLE `clubs` (`id` int(10) unsigned DEFAULT NULL auto_increment PRIMARY KEY, `name` varchar(255) DEFAULT '' NOT NULL) ENGINE=InnoDB
85
+ CREATE UNIQUE INDEX `idx_name` USING btree ON `clubs` (`name`)
86
+ CREATE TABLE `departments` (`dept_no` int(10) unsigned DEFAULT NULL auto_increment PRIMARY KEY, `dept_name` varchar(40) NOT NULL) ENGINE=InnoDB
87
+ CREATE UNIQUE INDEX `dept_name` USING btree ON `departments` (`dept_name`)
88
+ CREATE TABLE `dept_emp` (`emp_no` int(10) NOT NULL, `dept_no` varchar(4) NOT NULL, `from_date` date NOT NULL, `to_date` date NOT NULL) ENGINE=InnoDB
89
+ CREATE INDEX `dept_no` USING btree ON `dept_emp` (`dept_no`)
90
+ CREATE INDEX `emp_no` USING btree ON `dept_emp` (`emp_no`)
91
+ CREATE TABLE `dept_manager` (`dept_no` varchar(4) NOT NULL, `emp_no` int(10) NOT NULL, `from_date` date NOT NULL, `to_date` date NOT NULL) ENGINE=InnoDB
92
+ CREATE INDEX `dept_no` USING btree ON `dept_manager` (`dept_no`)
93
+ CREATE INDEX `emp_no` USING btree ON `dept_manager` (`emp_no`)
94
+ CREATE TABLE `employee_clubs` (`id` int(10) unsigned DEFAULT NULL auto_increment PRIMARY KEY, `emp_no` int(10) unsigned NOT NULL, `club_id` int(10) unsigned NOT NULL) ENGINE=InnoDB
95
+ CREATE INDEX `idx_emp_no_club_id` USING btree ON `employee_clubs` (`emp_no`, `club_id`)
96
+ CREATE TABLE `employees` (`emp_no` int(10) unsigned DEFAULT NULL auto_increment PRIMARY KEY, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` varchar(1) NOT NULL, `hire_date` date NOT NULL) ENGINE=InnoDB
97
+ CREATE TABLE `salaries` (`emp_no` int(10) NOT NULL, `salary` int(10) NOT NULL, `from_date` date NOT NULL, `to_date` date NOT NULL) ENGINE=InnoDB
98
+ CREATE INDEX `emp_no` USING btree ON `salaries` (`emp_no`)
99
+ CREATE TABLE `titles` (`emp_no` int(10) NOT NULL, `title` varchar(50) NOT NULL, `from_date` date NOT NULL, `to_date` date) ENGINE=InnoDB
100
+ CREATE INDEX `emp_no` USING btree ON `titles` (`emp_no`)
101
+ SQL
102
+ }
103
+ end
104
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ridgepole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
@@ -117,6 +117,7 @@ files:
117
117
  - lib/ridgepole/diff.rb
118
118
  - lib/ridgepole/dsl_parser.rb
119
119
  - lib/ridgepole/dumper.rb
120
+ - lib/ridgepole/execute_expander.rb
120
121
  - lib/ridgepole/logger.rb
121
122
  - lib/ridgepole/migration_ext.rb
122
123
  - lib/ridgepole/schema_dumper_ext.rb
@@ -140,6 +141,7 @@ files:
140
141
  - spec/migrate/migrate_duplicate_table_spec.rb
141
142
  - spec/migrate/migrate_empty_spec.rb
142
143
  - spec/migrate/migrate_merge_mode_spec.rb
144
+ - spec/migrate/migrate_noop_spec.rb
143
145
  - spec/migrate/migrate_rename_column_spec.rb
144
146
  - spec/migrate/migrate_rename_table_spec.rb
145
147
  - spec/migrate/migrate_same_default_null_spec.rb
@@ -196,6 +198,7 @@ test_files:
196
198
  - spec/migrate/migrate_duplicate_table_spec.rb
197
199
  - spec/migrate/migrate_empty_spec.rb
198
200
  - spec/migrate/migrate_merge_mode_spec.rb
201
+ - spec/migrate/migrate_noop_spec.rb
199
202
  - spec/migrate/migrate_rename_column_spec.rb
200
203
  - spec/migrate/migrate_rename_table_spec.rb
201
204
  - spec/migrate/migrate_same_default_null_spec.rb