ridgepole 0.7.4 → 0.7.5.beta

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
- SHA1:
3
- metadata.gz: 9e1f7e2e2dcdca2d9af34e642e60289ee5666551
4
- data.tar.gz: db223194fa6fff23ee8d8f5beeba4736f77a7f79
2
+ SHA256:
3
+ metadata.gz: c44addc9926210f15e87edf0411337f5c26d438eb9c56149f0836fb00816a40a
4
+ data.tar.gz: 612e706281e910142283d5db37112243bdcb4aeaef80e5b477525788103336fc
5
5
  SHA512:
6
- metadata.gz: 7d83dda15c6f6ba6374abfc165c688418d678d078d5ce1764f918c35209a3c67a1c61a04666b346be81076a4a19f6cd1622b6f876d79308c55401e9b14229706
7
- data.tar.gz: 15cf3590d6a87f73744136b3b9f5170abedf673ccce912b16d3f6440fd7632d79e11704b6e642c890fb4260d46f92a8b57fe086cd651fb90d0250c0695b23d44
6
+ metadata.gz: cae35726e3780ca82aa489ee7bc77cab33c1d70d25548c152c9aadad31482deaa7e4a638b901b2a627a91c1fa796803a08b4df934e66ac1aa758e351e4f44279
7
+ data.tar.gz: 9be70b10cd5ef421c2bbb48a44b48a3d2dab47b10e3d941a6323cefb14f804fa46ecc411dbfabfa748d750d03ddaabdb98d766156eb6485e754d51de04c0725a
data/.rubocop.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  AllCops:
2
2
  Exclude:
3
3
  - 'gemfiles/**/*'
4
+ - 'omnibus-ridgepole/**/*'
4
5
  TargetRubyVersion: 2.2
5
6
  Bundler/OrderedGems:
6
7
  Include:
@@ -34,5 +35,7 @@ Style/MethodMissingSuper:
34
35
  Style/MixinUsage:
35
36
  Exclude:
36
37
  - 'spec/**/*'
38
+ Style/TrailingCommaInHashLiteral:
39
+ EnforcedStyleForMultiline: consistent_comma
37
40
  Layout/ClosingHeredocIndentation:
38
41
  Enabled: false
data/.travis.yml CHANGED
@@ -6,10 +6,13 @@ cache:
6
6
  - bundler
7
7
  - apt
8
8
  rvm:
9
- - 2.2.9
10
9
  - 2.3.6
11
- - 2.4.3
12
- - 2.5.0
10
+ - 2.4.5
11
+ - 2.5.3
12
+ - 2.6.0-preview2
13
+ matrix:
14
+ allow_failures:
15
+ - rvm: 2.6.0-preview2
13
16
  before_script:
14
17
  - sudo service mysql stop
15
18
  - sudo service postgresql stop
data/README.md CHANGED
@@ -89,6 +89,8 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
89
89
  * Support primary key adding/dropping ([issue#246](https://github.com/winebarrel/ridgepole/issues/246))
90
90
  * `>= 0.7.4`
91
91
  * Fix `add_foreign_key` options ([issue#250](https://github.com/winebarrel/ridgepole/issues/250))
92
+ * `>= 0.7.5`
93
+ * Fix polymorphic options ([pull#263](https://github.com/winebarrel/ridgepole/pull/263))
92
94
 
93
95
  ## Installation
94
96
 
@@ -104,6 +106,26 @@ Or install it yourself as:
104
106
 
105
107
  $ gem install ridgepole
106
108
 
109
+ ## Omnibus Package (deb/rpm)
110
+
111
+ see https://github.com/winebarrel/ridgepole/releases.
112
+
113
+ ### Install from rpm
114
+
115
+ ```sh
116
+ sudo dpkg -i ridgepole_x.x.x+xxx-x_amd64.deb
117
+ sudo apt install build-essential libmysqlclient-dev
118
+ sudo /opt/ridgepole/embedded/bin/gem install mysql2
119
+ ```
120
+
121
+ ### Install from rpm
122
+
123
+ ```sh
124
+ sudo yum install ridgepole-x.x.x+xxx-x.el7.x86_64.rpm
125
+ sudo yum install make gcc mariadb-devel
126
+ sudo /opt/ridgepole/embedded/bin/gem install mysql2
127
+ ```
128
+
107
129
  ## Help
108
130
  ```
109
131
  Usage: ridgepole [options]
data/bin/ridgepole CHANGED
@@ -24,7 +24,7 @@ COLUMN_TYPES = {
24
24
  float: :float,
25
25
  string: :string,
26
26
  text: :text,
27
- binary: :binary
27
+ binary: :binary,
28
28
  }.freeze
29
29
 
30
30
  config = nil
@@ -40,11 +40,12 @@ exit_code = 0
40
40
  options = {
41
41
  dry_run: false,
42
42
  debug: false,
43
- color: $stdout.tty?
43
+ color: $stdout.tty?,
44
44
  }
45
45
 
46
46
  set_mode = proc do |m|
47
47
  raise OptionParser::InvalidOption, 'More than one mode is specified' if mode
48
+
48
49
  mode = m
49
50
  end
50
51
 
@@ -85,6 +86,7 @@ ARGV.options do |opt|
85
86
  opt.on('', '--external-script SCRIPT') { |v| options[:external_script] = v }
86
87
  opt.on('', '--bulk-change') do
87
88
  raise OptionParser::InvalidOption, 'Cannot use `bulk-change` in `merge`' if options[:merge]
89
+
88
90
  options[:bulk_change] = true
89
91
  end
90
92
 
@@ -129,9 +131,7 @@ ARGV.options do |opt|
129
131
  opt.on('', '--create-table-with-index') { options[:create_table_with_index] = true }
130
132
 
131
133
  opt.on('', '--mysql-dump-auto-increment') do
132
- if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('5.1')
133
- raise OptionParser::InvalidOption, '`mysql-dump-auto-increment` is not available in `activerecord < 5.1`'
134
- end
134
+ raise OptionParser::InvalidOption, '`mysql-dump-auto-increment` is not available in `activerecord < 5.1`' if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('5.1')
135
135
 
136
136
  options[:mysql_dump_auto_increment] = true
137
137
  end
@@ -156,9 +156,7 @@ ARGV.options do |opt|
156
156
  rescue StandardError => e
157
157
  warn("[ERROR] #{e.message}")
158
158
 
159
- unless e.is_a?(OptionParser::ParseError)
160
- puts "\t" + e.backtrace.join("\n\t")
161
- end
159
+ puts "\t" + e.backtrace.join("\n\t") unless e.is_a?(OptionParser::ParseError)
162
160
 
163
161
  exit 1
164
162
  end
@@ -251,6 +249,13 @@ begin
251
249
  else
252
250
  File.open(diff_file)
253
251
  end
252
+ elsif Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1') # Ruby 2.6
253
+ YAML.safe_load(
254
+ diff_file,
255
+ permitted_classes: [],
256
+ permitted_symbols: [],
257
+ aliases: true
258
+ )
254
259
  else
255
260
  YAML.safe_load(diff_file, [], [], true)
256
261
  end
@@ -11,13 +11,18 @@ module Ridgepole
11
11
  parse_config_file(config)
12
12
  elsif (expanded = File.expand_path(config)) && File.exist?(expanded)
13
13
  parse_config_file(expanded)
14
+ elsif Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1') # Ruby 2.6
15
+ YAML.safe_load(
16
+ ERB.new(config).result,
17
+ permitted_classes: [],
18
+ permitted_symbols: [],
19
+ aliases: true
20
+ )
14
21
  else
15
22
  YAML.safe_load(ERB.new(config).result, [], [], true)
16
23
  end
17
24
 
18
- unless parsed_config.is_a?(Hash)
19
- parsed_config = parse_database_url(config)
20
- end
25
+ parsed_config = parse_database_url(config) unless parsed_config.is_a?(Hash)
21
26
 
22
27
  if parsed_config.key?(env.to_s)
23
28
  parsed_config.fetch(env.to_s)
@@ -30,23 +35,31 @@ module Ridgepole
30
35
 
31
36
  def parse_config_file(path)
32
37
  yaml = ERB.new(File.read(path)).result
33
- YAML.safe_load(yaml, [], [], true)
38
+
39
+ if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1') # Ruby 2.6
40
+ YAML.safe_load(
41
+ yaml,
42
+ permitted_classes: [],
43
+ permitted_symbols: [],
44
+ aliases: true
45
+ )
46
+ else
47
+ YAML.safe_load(yaml, [], [], true)
48
+ end
34
49
  end
35
50
 
36
51
  def parse_database_url(config)
37
52
  uri = URI.parse(config)
38
53
 
39
- if [uri.scheme, uri.user, uri.host, uri.path].any? { |i| i.nil? || i.empty? }
40
- raise "Invalid config: #{config.inspect}"
41
- end
54
+ raise "Invalid config: #{config.inspect}" if [uri.scheme, uri.user, uri.host, uri.path].any? { |i| i.nil? || i.empty? }
42
55
 
43
56
  {
44
- 'adapter' => uri.scheme,
57
+ 'adapter' => uri.scheme,
45
58
  'username' => uri.user,
46
59
  'password' => uri.password,
47
- 'host' => uri.host,
48
- 'port' => uri.port,
49
- 'database' => uri.path.sub(%r{\A/}, '')
60
+ 'host' => uri.host,
61
+ 'port' => uri.port,
62
+ 'database' => uri.path.sub(%r{\A/}, ''),
50
63
  }
51
64
  end
52
65
  end
@@ -6,22 +6,16 @@ module Ridgepole
6
6
  ActiveRecord::Base.establish_connection(conn_spec)
7
7
 
8
8
  # XXX: If the required processing in class method?
9
- if !@options.key?(:index_removed_drop_column) && (Ridgepole::DefaultsLimit.adapter == :postgresql)
10
- @options[:index_removed_drop_column] = true
11
- end
9
+ @options[:index_removed_drop_column] = true if !@options.key?(:index_removed_drop_column) && (Ridgepole::DefaultsLimit.adapter == :postgresql)
12
10
 
13
11
  Ridgepole::ExecuteExpander.expand_execute(ActiveRecord::Base.connection)
14
12
  @dumper = Ridgepole::Dumper.new(@options)
15
13
  @parser = Ridgepole::DSLParser.new(@options)
16
14
  @diff = Ridgepole::Diff.new(@options)
17
15
 
18
- if @options[:mysql_use_alter]
19
- require 'ridgepole/ext/abstract_mysql_adapter/use_alter_index'
20
- end
16
+ require 'ridgepole/ext/abstract_mysql_adapter/use_alter_index' if @options[:mysql_use_alter]
21
17
 
22
- if @options[:mysql_dump_auto_increment]
23
- require 'ridgepole/ext/abstract_mysql_adapter/dump_auto_increment'
24
- end
18
+ require 'ridgepole/ext/abstract_mysql_adapter/dump_auto_increment' if @options[:mysql_dump_auto_increment]
25
19
  end
26
20
 
27
21
  def dump(&block)
@@ -8,8 +8,8 @@ module Ridgepole
8
8
  float: 24,
9
9
  string: 255,
10
10
  text: 65_535,
11
- binary: 65_535
12
- }
11
+ binary: 65_535,
12
+ },
13
13
  }.freeze
14
14
 
15
15
  class << self
@@ -110,9 +110,7 @@ module Ridgepole
110
110
 
111
111
  begin
112
112
  with_pre_post_query(options) do
113
- unless script.empty?
114
- ActiveRecord::Schema.new.instance_eval(script, SCRIPT_NAME, 1)
115
- end
113
+ ActiveRecord::Schema.new.instance_eval(script, SCRIPT_NAME, 1) unless script.empty?
116
114
 
117
115
  execute_count = execute_sqls(options)
118
116
  end
@@ -311,9 +309,7 @@ execute "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name
311
309
  end
312
310
  end
313
311
 
314
- unless foreign_keys.empty?
315
- append_change_foreign_keys(table_name, foreign_keys, pre_buf_for_fk, post_buf_for_fk, @options)
316
- end
312
+ append_change_foreign_keys(table_name, foreign_keys, pre_buf_for_fk, post_buf_for_fk, @options) unless foreign_keys.empty?
317
313
 
318
314
  append_change_table_options(table_name, table_options, buf) if table_options
319
315
 
@@ -60,6 +60,7 @@ module Ridgepole
60
60
  next unless target?(table_name)
61
61
 
62
62
  next unless (from_table_name = (to_attrs[:options] || {}).delete(:renamed_from))
63
+
63
64
  from_table_name = from_table_name.to_s if from_table_name
64
65
 
65
66
  # Already renamed
@@ -112,9 +113,7 @@ module Ridgepole
112
113
  end
113
114
 
114
115
  [from, to].each do |table_attrs|
115
- if table_attrs.key?(:default) && table_attrs[:default].nil?
116
- table_attrs.delete(:default)
117
- end
116
+ table_attrs.delete(:default) if table_attrs.key?(:default) && table_attrs[:default].nil?
118
117
  end
119
118
 
120
119
  if Ridgepole::ConnectionAdapters.mysql?
@@ -176,9 +175,7 @@ module Ridgepole
176
175
  Ridgepole::DSLParser::TableDefinition::DEFAULT_PRIMARY_KEY_TYPE
177
176
  end
178
177
 
179
- if %i[integer bigint].include?(type) && !options.key?(:default) && !Ridgepole::ConnectionAdapters.postgresql?
180
- options[:auto_increment] = true
181
- end
178
+ options[:auto_increment] = true if %i[integer bigint].include?(type) && !options.key?(:default) && !Ridgepole::ConnectionAdapters.postgresql?
182
179
 
183
180
  { type: type, options: options }
184
181
  end
@@ -187,9 +184,7 @@ module Ridgepole
187
184
  normalize_column_options!(from_attrs, primary_key)
188
185
  normalize_column_options!(to_attrs, primary_key)
189
186
 
190
- unless compare_column_attrs(from_attrs, to_attrs)
191
- new_to_attrs = fix_change_column_options(table_name, from_attrs, to_attrs)
192
- end
187
+ new_to_attrs = fix_change_column_options(table_name, from_attrs, to_attrs) unless compare_column_attrs(from_attrs, to_attrs)
193
188
  new_to_attrs
194
189
  end
195
190
 
@@ -197,6 +192,7 @@ module Ridgepole
197
192
  from_column_attrs = convert_to_primary_key_attrs(from.slice(*PRIMARY_KEY_OPTIONS))
198
193
  to_column_attrs = convert_to_primary_key_attrs(to.slice(*PRIMARY_KEY_OPTIONS))
199
194
  return if from_column_attrs == to_column_attrs
195
+
200
196
  build_attrs_if_changed(to_column_attrs, from_column_attrs, table_name, primary_key: true)
201
197
  end
202
198
 
@@ -240,9 +236,7 @@ module Ridgepole
240
236
  added_size = 0
241
237
  to.reverse_each.with_index do |(column_name, to_attrs), i|
242
238
  if to_attrs[:options].delete(:after)
243
- if added_size != i
244
- @logger.warn("[WARNING] PostgreSQL doesn't support adding a new column except for the last position. #{table_name}.#{column_name} will be added to the last.")
245
- end
239
+ @logger.warn("[WARNING] PostgreSQL doesn't support adding a new column except for the last position. #{table_name}.#{column_name} will be added to the last.") if added_size != i
246
240
  added_size += 1
247
241
  end
248
242
  end
@@ -254,12 +248,11 @@ module Ridgepole
254
248
  definition_delta[:delete][column_name] = from_attrs
255
249
 
256
250
  next unless from_indices
251
+
257
252
  modified_indices = []
258
253
 
259
254
  from_indices.each do |name, attrs|
260
- if attrs[:column_name].is_a?(Array) && attrs[:column_name].delete(column_name)
261
- modified_indices << name
262
- end
255
+ modified_indices << name if attrs[:column_name].is_a?(Array) && attrs[:column_name].delete(column_name)
263
256
  end
264
257
 
265
258
  # In PostgreSQL, the index is deleted when the column is deleted
@@ -281,14 +274,13 @@ module Ridgepole
281
274
  def scan_column_rename(from, to, definition_delta)
282
275
  to.dup.each do |column_name, to_attrs|
283
276
  next unless (from_column_name = (to_attrs[:options] || {}).delete(:renamed_from))
277
+
284
278
  from_column_name = from_column_name.to_s if from_column_name
285
279
 
286
280
  # Already renamed
287
281
  next if from[column_name]
288
282
 
289
- unless from.key?(from_column_name)
290
- raise "Column `#{from_column_name}` not found"
291
- end
283
+ raise "Column `#{from_column_name}` not found" unless from.key?(from_column_name)
292
284
 
293
285
  definition_delta[:rename] ||= {}
294
286
  definition_delta[:rename][column_name] = from_column_name
@@ -388,9 +380,7 @@ module Ridgepole
388
380
  def columns_all_include?(expected_columns, actual_columns, table_options)
389
381
  return true unless expected_columns.is_a?(Array)
390
382
 
391
- if (table_options[:id] != false) && !table_options[:primary_key].is_a?(Array)
392
- actual_columns += [(table_options[:primary_key] || 'id').to_s]
393
- end
383
+ actual_columns += [(table_options[:primary_key] || 'id').to_s] if (table_options[:id] != false) && !table_options[:primary_key].is_a?(Array)
394
384
 
395
385
  expected_columns.all? { |i| actual_columns.include?(i) }
396
386
  end
@@ -426,9 +416,7 @@ module Ridgepole
426
416
  end
427
417
  end
428
418
 
429
- unless foreign_keys_delta.empty?
430
- table_delta[:foreign_keys] = foreign_keys_delta
431
- end
419
+ table_delta[:foreign_keys] = foreign_keys_delta unless foreign_keys_delta.empty?
432
420
  end
433
421
 
434
422
  # XXX: MySQL only?
@@ -450,9 +438,7 @@ module Ridgepole
450
438
  end
451
439
 
452
440
  if Ridgepole::ConnectionAdapters.mysql? && ActiveRecord::VERSION::STRING.start_with?('5.0.')
453
- if to_attrs[:options][:default].nil? && (to_attrs[:options][:null] == false)
454
- Ridgepole::Logger.instance.warn("[WARNING] Table `#{table_name}`: `default: nil` is ignored when `null: false`. Please apply twice")
455
- end
441
+ Ridgepole::Logger.instance.warn("[WARNING] Table `#{table_name}`: `default: nil` is ignored when `null: false`. Please apply twice") if to_attrs[:options][:default].nil? && (to_attrs[:options][:null] == false)
456
442
  end
457
443
 
458
444
  to_attrs
@@ -498,14 +484,12 @@ module Ridgepole
498
484
  definition = table_attr[:definition] || {}
499
485
 
500
486
  definition.each do |column_name, column_attrs|
501
- if column_name =~ /\w+_id\z/
502
- attrs_by_column[column_name] = column_attrs.dup
503
- end
487
+ attrs_by_column[column_name] = column_attrs.dup if column_name =~ /\w+_id\z/
504
488
  end
505
489
 
506
490
  relation_info[table_name] = {
507
491
  options: table_attr[:options] || {},
508
- columns: attrs_by_column
492
+ columns: attrs_by_column,
509
493
  }
510
494
  end
511
495
 
@@ -537,12 +521,12 @@ module Ridgepole
537
521
 
538
522
  parent_column_info = {
539
523
  type: table_options[:id] || @options[:check_relation_type].to_sym,
540
- unsigned: table_options[:unsigned]
524
+ unsigned: table_options[:unsigned],
541
525
  }
542
526
 
543
527
  child_column_info = {
544
528
  type: column_attrs[:type],
545
- unsigned: column_attrs.fetch(:options, {})[:unsigned]
529
+ unsigned: column_attrs.fetch(:options, {})[:unsigned],
546
530
  }
547
531
 
548
532
  [parent_column_info, child_column_info].each do |column_info|
@@ -551,11 +535,12 @@ module Ridgepole
551
535
  # for PostgreSQL
552
536
  column_info[:type] = {
553
537
  serial: :integer,
554
- bigserial: :bigint
538
+ bigserial: :bigint,
555
539
  }.fetch(column_info[:type], column_info[:type])
556
540
  end
557
541
 
558
542
  next unless parent_column_info != child_column_info
543
+
559
544
  parent_label = "#{parent_table}.id"
560
545
  child_label = "#{child_table}.#{column_name}"
561
546
  label_len = [parent_label.length, child_label.length].max
@@ -571,6 +556,7 @@ module Ridgepole
571
556
 
572
557
  def check_table_existence(definition)
573
558
  return unless @options[:tables]
559
+
574
560
  @options[:tables].each do |table_name|
575
561
  @logger.warn "[WARNING] '#{table_name}' definition is not found" unless definition.key?(table_name)
576
562
  end
@@ -15,17 +15,13 @@ module Ridgepole
15
15
 
16
16
  def check_orphan_index(definition)
17
17
  definition.each do |table_name, attrs|
18
- if attrs[:indices] && !(attrs[:definition])
19
- raise "Table `#{table_name}` to create the index is not defined: #{attrs[:indices].keys.join(',')}"
20
- end
18
+ raise "Table `#{table_name}` to create the index is not defined: #{attrs[:indices].keys.join(',')}" if attrs[:indices] && !(attrs[:definition])
21
19
  end
22
20
  end
23
21
 
24
22
  def check_orphan_foreign_key(definition)
25
23
  definition.each do |table_name, attrs|
26
- if attrs[:foreign_keys] && !(attrs[:definition])
27
- raise "Table `#{table_name}` to create the foreign key is not defined: #{attrs[:foreign_keys].keys.join(',')}"
28
- end
24
+ raise "Table `#{table_name}` to create the foreign key is not defined: #{attrs[:foreign_keys].keys.join(',')}" if attrs[:foreign_keys] && !(attrs[:definition])
29
25
  end
30
26
  end
31
27
  end
@@ -26,9 +26,7 @@ module Ridgepole
26
26
  table_name = table_name.to_s
27
27
  table_definition = TableDefinition.new(table_name, self)
28
28
 
29
- if options[:primary_key] && options[:primary_key].is_a?(Symbol)
30
- options[:primary_key] = options[:primary_key].to_s
31
- end
29
+ options[:primary_key] = options[:primary_key].to_s if options[:primary_key] && options[:primary_key].is_a?(Symbol)
32
30
  if options[:id] && TableDefinition::ALIAS_TYPES.key?(options[:id])
33
31
  type, type_default_opts = TableDefinition::ALIAS_TYPES[options[:id]]
34
32
  options[:id] = type
@@ -38,9 +36,7 @@ module Ridgepole
38
36
  yield(table_definition)
39
37
  @__definition[table_name] ||= {}
40
38
 
41
- if @__definition[table_name][:definition]
42
- raise "Table `#{table_name}` already defined"
43
- end
39
+ raise "Table `#{table_name}` already defined" if @__definition[table_name][:definition]
44
40
 
45
41
  @__definition[table_name][:definition] = table_definition.__definition
46
42
  options.delete(:force)
@@ -51,17 +47,13 @@ module Ridgepole
51
47
  table_name = table_name.to_s
52
48
  # Keep column_name for expression index support
53
49
  # https://github.com/rails/rails/pull/23393
54
- unless column_name.is_a?(String) && /\W/ === column_name # rubocop:disable Style/CaseEquality
55
- column_name = [column_name].flatten.map(&:to_s)
56
- end
50
+ column_name = [column_name].flatten.map(&:to_s) unless column_name.is_a?(String) && /\W/ === column_name # rubocop:disable Style/CaseEquality
57
51
  options[:name] = options[:name].to_s if options[:name]
58
52
  @__definition[table_name] ||= {}
59
53
  @__definition[table_name][:indices] ||= {}
60
54
  idx = options[:name] || column_name
61
55
 
62
- if @__definition[table_name][:indices][idx]
63
- raise "Index `#{table_name}(#{idx})` already defined"
64
- end
56
+ raise "Index `#{table_name}(#{idx})` already defined" if @__definition[table_name][:indices][idx]
65
57
 
66
58
  if options[:length].is_a?(Numeric)
67
59
  index_length = options[:length]
@@ -72,13 +64,11 @@ module Ridgepole
72
64
  end
73
65
  end
74
66
 
75
- if options[:length]
76
- options[:length] = options[:length].compact.symbolize_keys
77
- end
67
+ options[:length] = options[:length].compact.symbolize_keys if options[:length]
78
68
 
79
69
  @__definition[table_name][:indices][idx] = {
80
70
  column_name: column_name,
81
- options: options
71
+ options: options,
82
72
  }
83
73
  end
84
74
 
@@ -92,13 +82,11 @@ module Ridgepole
92
82
  @__definition[from_table][:foreign_keys] ||= {}
93
83
  idx = options[:name] || [from_table, to_table]
94
84
 
95
- if @__definition[from_table][:foreign_keys][idx]
96
- raise "Foreign Key `#{from_table}(#{idx})` already defined"
97
- end
85
+ raise "Foreign Key `#{from_table}(#{idx})` already defined" if @__definition[from_table][:foreign_keys][idx]
98
86
 
99
87
  @__definition[from_table][:foreign_keys][idx] = {
100
88
  to_table: to_table,
101
- options: options
89
+ options: options,
102
90
  }
103
91
  end
104
92
 
@@ -117,7 +105,7 @@ module Ridgepole
117
105
  def execute(sql, _name = nil, &cond)
118
106
  @__execute << {
119
107
  sql: sql,
120
- condition: cond
108
+ condition: cond,
121
109
  }
122
110
  end
123
111
  end
@@ -14,7 +14,7 @@ module Ridgepole
14
14
 
15
15
  @__definition[name] = {
16
16
  type: type,
17
- options: options
17
+ options: options,
18
18
  }
19
19
  end
20
20
 
@@ -87,7 +87,7 @@ module Ridgepole
87
87
  unsigned_integer: [:integer, { unsigned: true }],
88
88
  unsigned_bigint: [:bigint, { unsigned: true }],
89
89
  unsigned_float: [:float, { limit: 24, unsigned: true }],
90
- unsigned_decimal: [:decimal, { precision: 10, unsigned: true }]
90
+ unsigned_decimal: [:decimal, { precision: 10, unsigned: true }],
91
91
  }.freeze
92
92
 
93
93
  # XXX:
@@ -124,12 +124,15 @@ module Ridgepole
124
124
  def references(*args)
125
125
  options = args.extract_options!
126
126
  polymorphic = options.delete(:polymorphic)
127
+ polymorphic_options = polymorphic.is_a?(Hash) ? polymorphic : {}
128
+ # https://github.com/rails/rails/blob/5-2-1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L167
129
+ polymorphic_options.merge!(options.slice(:null, :first, :after))
127
130
  index_options = options.key?(:index) ? options.delete(:index) : true
128
131
  type = options.delete(:type) || DEFAULT_PRIMARY_KEY_TYPE
129
132
 
130
133
  args.each do |col|
131
134
  column("#{col}_id", type, options)
132
- column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
135
+ column("#{col}_type", :string, polymorphic_options) if polymorphic
133
136
  if index_options
134
137
  columns = polymorphic ? ["#{col}_type", "#{col}_id"] : ["#{col}_id"]
135
138
  index(columns, index_options.is_a?(Hash) ? index_options : {})
@@ -13,23 +13,20 @@ module Ridgepole
13
13
  if target_tables
14
14
  conn.data_sources.each do |tbl|
15
15
  next if target_tables.include?(tbl)
16
+
16
17
  ActiveRecord::SchemaDumper.ignore_tables << tbl
17
18
  end
18
19
  end
19
20
 
20
21
  if ignore_tables
21
22
  conn.data_sources.each do |tbl|
22
- if ignore_tables.any? { |i| i =~ tbl } && !(target_tables && target_tables.include?(tbl))
23
- ActiveRecord::SchemaDumper.ignore_tables << tbl
24
- end
23
+ ActiveRecord::SchemaDumper.ignore_tables << tbl if ignore_tables.any? { |i| i =~ tbl } && !(target_tables && target_tables.include?(tbl))
25
24
  end
26
25
  end
27
26
 
28
27
  stream = dump_from(conn)
29
28
 
30
- if target_tables || ignore_tables
31
- ActiveRecord::SchemaDumper.ignore_tables.clear
32
- end
29
+ ActiveRecord::SchemaDumper.ignore_tables.clear if target_tables || ignore_tables
33
30
 
34
31
  stream.string.lines.each_cons(2) do |first_line, second_line|
35
32
  if first_line.start_with?('# Could not dump')
@@ -44,9 +44,7 @@ module Ridgepole
44
44
  private
45
45
 
46
46
  def append_alter_extra(sql)
47
- if Ridgepole::ExecuteExpander.alter_extra && sql =~ /\AALTER\b/i
48
- sql = sql + ',' + Ridgepole::ExecuteExpander.alter_extra
49
- end
47
+ sql = sql + ',' + Ridgepole::ExecuteExpander.alter_extra if Ridgepole::ExecuteExpander.alter_extra && sql =~ /\AALTER\b/i
50
48
 
51
49
  sql
52
50
  end
@@ -28,13 +28,9 @@ module Ridgepole
28
28
  remove_prefix_and_suffix(foreign_key.to_table).inspect
29
29
  ]
30
30
 
31
- if foreign_key.column != @connection.foreign_key_column_for(foreign_key.to_table)
32
- parts << "column: #{foreign_key.column.inspect}"
33
- end
31
+ parts << "column: #{foreign_key.column.inspect}" if foreign_key.column != @connection.foreign_key_column_for(foreign_key.to_table)
34
32
 
35
- if foreign_key.custom_primary_key?
36
- parts << "primary_key: #{foreign_key.primary_key.inspect}"
37
- end
33
+ parts << "primary_key: #{foreign_key.primary_key.inspect}" if foreign_key.custom_primary_key?
38
34
 
39
35
  parts << "name: #{foreign_key.name.inspect}"
40
36
 
@@ -19,12 +19,14 @@ module Ridgepole
19
19
  ready = IO.select(files)
20
20
 
21
21
  next unless ready
22
+
22
23
  readable = ready[0]
23
24
 
24
25
  readable.each do |f|
25
26
  begin
26
27
  data = f.read_nonblock(1024)
27
28
  next if data.nil?
29
+
28
30
  data.chomp!
29
31
 
30
32
  if f == stderr
@@ -1,3 +1,3 @@
1
1
  module Ridgepole
2
- VERSION = '0.7.4'.freeze
2
+ VERSION = '0.7.5.beta'.freeze
3
3
  end
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ .bundle
3
+ .kitchen/
4
+ .kitchen.local.yml
5
+ vendor/bundle
6
+ pkg/*
7
+ .vagrant
8
+ bin/*
9
+ files/**/cache/
10
+ vendor/cookbooks
@@ -0,0 +1,25 @@
1
+ FROM centos:7
2
+
3
+ RUN yum install -y centos-release-scl && \
4
+ yum-config-manager --enable rhel-server-rhscl-7-rpms && \
5
+ yum install -y \
6
+ rh-ruby25-dev \
7
+ rh-ruby25-ruby-devel \
8
+ make \
9
+ gcc \
10
+ gcc-c++ \
11
+ git \
12
+ rpm-build
13
+
14
+ RUN scl enable rh-ruby25 -- \
15
+ gem install --no-document \
16
+ bundler \
17
+ omnibus
18
+
19
+ COPY Gemfile /
20
+ RUN scl enable rh-ruby25 -- \
21
+ bundle install
22
+
23
+ WORKDIR /omnibus-ridgepole
24
+
25
+ ENTRYPOINT ["/usr/bin/scl", "enable", "rh-ruby25", "--"]
@@ -0,0 +1,21 @@
1
+ FROM ubuntu:xenial
2
+
3
+ RUN apt-get update && \
4
+ apt-get install -y software-properties-common
5
+
6
+ RUN apt-add-repository -y ppa:brightbox/ruby-ng && \
7
+ apt-get update && \
8
+ apt-get install -y \
9
+ ruby2.5 \
10
+ ruby2.5-dev \
11
+ build-essential \
12
+ git
13
+
14
+ RUN gem install --no-document \
15
+ bundler \
16
+ omnibus
17
+
18
+ COPY Gemfile /
19
+ RUN bundle install
20
+
21
+ WORKDIR /omnibus-ridgepole
@@ -0,0 +1,24 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Install omnibus
4
+ gem 'omnibus', '~> 6.0'
5
+ gem 'omnibus-software', git: 'https://github.com/chef/omnibus-software.git'
6
+
7
+ gem 'rake'
8
+
9
+ # Use Chef's software definitions. It is recommended that you write your own
10
+ # software definitions, but you can clone/fork Chef's to get you started.
11
+ # gem 'omnibus-software', github: 'chef/omnibus-software'
12
+
13
+ # This development group is installed by default when you run `bundle install`,
14
+ # but if you are using Omnibus in a CI-based infrastructure, you do not need
15
+ # the Test Kitchen-based build lab. You can skip these unnecessary dependencies
16
+ # by running `bundle install --without development` to speed up build times.
17
+ group :development do
18
+ # Use Berkshelf for resolving cookbook dependencies
19
+ gem 'berkshelf', '~> 3.3'
20
+
21
+ # Use Test Kitchen with Vagrant for converging the build environment
22
+ gem 'test-kitchen', '~> 1.4'
23
+ gem 'kitchen-vagrant', '~> 0.18'
24
+ end
@@ -0,0 +1,15 @@
1
+ omnibus-ridgepole
2
+ -----------------
3
+
4
+ ## Installation
5
+
6
+ ```sh
7
+ bundle install
8
+ bundle exec rake docker:image:build
9
+ ```
10
+
11
+ ## Build omnibus-ridgepole
12
+
13
+ ```sh
14
+ bundle exec rake omnibus:build
15
+ ```
@@ -0,0 +1,38 @@
1
+ DOCKER_IMAGE_UBUNTU = 'omnibus-ridgepole-ubuntu'
2
+ DOCKER_IMAGE_CENTOS = 'omnibus-ridgepole-chentos'
3
+
4
+ namespace :docker do
5
+ namespace :image do
6
+ task build: ['build:ubuntu', 'build:centos']
7
+
8
+ namespace :build do
9
+ task :ubuntu do
10
+ sh 'docker', 'build', '-t', DOCKER_IMAGE_UBUNTU, '-f', 'Dockerfile.ubuntu', '.'
11
+ end
12
+
13
+ task :centos do
14
+ sh 'docker', 'build', '-t', DOCKER_IMAGE_CENTOS, '-f', 'Dockerfile.centos', '.'
15
+ end
16
+ end
17
+ end
18
+
19
+ namespace :omnibus do
20
+ task :build do
21
+ sh 'omnibus', 'build', 'ridgepole'
22
+ end
23
+ end
24
+ end
25
+
26
+ namespace :omnibus do
27
+ task build: ['build:ubuntu', 'build:centos']
28
+
29
+ namespace :build do
30
+ task :ubuntu do
31
+ sh 'docker', 'run', '-v', "#{pwd}:/omnibus-ridgepole", DOCKER_IMAGE_UBUNTU, 'bundle', 'exec', 'rake docker:omnibus:build'
32
+ end
33
+
34
+ task :centos do
35
+ sh 'docker', 'run', '-v', "#{pwd}:/omnibus-ridgepole", DOCKER_IMAGE_CENTOS, 'bundle', 'exec', 'rake docker:omnibus:build'
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+
4
+ name 'ridgepole'
5
+ maintainer 'winebarrel <sugawara@winebarrel.jp>'
6
+ homepage 'https://github.com/winebarrel/ridgepole'
7
+
8
+ # Defaults to C:/ridgepole on Windows
9
+ # and /opt/ridgepole on all other platforms
10
+ install_dir "#{default_root}/#{name}"
11
+
12
+ build_version JSON.parse(open('https://rubygems.org/api/v1/gems/ridgepole.json', &:read)).fetch('version')
13
+ build_iteration 1
14
+
15
+ dependency 'preparation'
16
+
17
+ # ridgepole dependencies/components
18
+ override :ruby, version: '2.5.1'
19
+ dependency 'ruby'
20
+ dependency 'rubygems'
21
+ dependency 'ridgepole'
22
+
23
+ # Version manifest file
24
+ dependency 'version-manifest'
25
+
26
+ exclude '**/.git'
27
+ exclude '**/bundler/git'
@@ -0,0 +1,13 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+
4
+ name 'ridgepole'
5
+
6
+ default_version JSON.parse(open('https://rubygems.org/api/v1/gems/ridgepole.json', &:read)).fetch('version')
7
+
8
+ license 'MIT'
9
+ skip_transitive_dependency_licensing true
10
+
11
+ build do
12
+ gem 'install ridgepole -N'
13
+ end
@@ -0,0 +1,54 @@
1
+ #
2
+ # This file is used to configure the ridgepole project. It contains
3
+ # some minimal configuration examples for working with Omnibus. For a full list
4
+ # of configurable options, please see the documentation for +omnibus/config.rb+.
5
+ #
6
+
7
+ # Build internally
8
+ # ------------------------------
9
+ # By default, Omnibus uses system folders (like +/var+ and +/opt+) to build and
10
+ # cache components. If you would to build everything internally, you can
11
+ # uncomment the following options. This will prevent the need for root
12
+ # permissions in most cases.
13
+ #
14
+ # Uncomment this line to change the default base directory to "local"
15
+ # -------------------------------------------------------------------
16
+ # base_dir './local'
17
+ #
18
+ # Alternatively you can tune the individual values
19
+ # ------------------------------------------------
20
+ # cache_dir './local/omnibus/cache'
21
+ # git_cache_dir './local/omnibus/cache/git_cache'
22
+ # source_dir './local/omnibus/src'
23
+ # build_dir './local/omnibus/build'
24
+ # package_dir './local/omnibus/pkg'
25
+ # package_tmp './local/omnibus/pkg-tmp'
26
+
27
+ # Disable git caching
28
+ # ------------------------------
29
+ # use_git_caching false
30
+
31
+ # Enable S3 asset caching
32
+ # ------------------------------
33
+ # use_s3_caching true
34
+ # s3_access_key ENV['AWS_ACCESS_KEY_ID']
35
+ # s3_secret_key ENV['AWS_SECRET_ACCESS_KEY']
36
+ # s3_profile ENV['AWS_S3_PROFILE']
37
+ # s3_bucket ENV['AWS_S3_BUCKET']
38
+
39
+ # Customize compiler bits
40
+ # ------------------------------
41
+ # solaris_compiler 'gcc'
42
+ # build_retries 5
43
+ # fetcher_read_timeout 120
44
+ # fetcher_retries 5
45
+
46
+ # Load additional software
47
+ # ------------------------------
48
+ # software_gems ['omnibus-software', 'my-company-software']
49
+ # local_software_dirs ['/path/to/local/software']
50
+
51
+ # Windows architecture defaults
52
+ # ------------------------------
53
+ windows_arch %w{x86 x64}.include?((ENV['OMNIBUS_WINDOWS_ARCH'] || '').downcase) ?
54
+ ENV['OMNIBUS_WINDOWS_ARCH'].downcase.to_sym : :x86
@@ -0,0 +1 @@
1
+ ln -sf /opt/ridgepole/embedded/bin/ridgepole /usr/bin
@@ -0,0 +1 @@
1
+ rm -f /usr/bin/ridgepole
@@ -168,6 +168,37 @@ describe 'Ridgepole::Client#diff -> migrate' do
168
168
  }
169
169
  end
170
170
 
171
+ context 'when use references with unsigned polymorphic (no change)' do
172
+ let(:actual_dsl) do
173
+ erbh(<<-ERB)
174
+ create_table "employees", primary_key: "emp_no", force: :cascade do |t|
175
+ t.<%= cond('>= 5.1','bigint', 'integer') %> "products_id", unsigned: true
176
+ t.string "products_type"
177
+ t.<%= cond('>= 5.1','bigint', 'integer') %> "user_id", unsigned: true
178
+ t.string "user_type"
179
+ t.index ["products_type", "products_id"]
180
+ t.index ["user_type", "user_id"]
181
+ end
182
+ ERB
183
+ end
184
+
185
+ let(:expected_dsl) do
186
+ <<-RUBY
187
+ create_table "employees", primary_key: "emp_no", force: :cascade do |t|
188
+ t.references :products, :user, unsigned: true, polymorphic: true
189
+ end
190
+ RUBY
191
+ end
192
+
193
+ before { subject.diff(actual_dsl).migrate }
194
+ subject { client }
195
+
196
+ it {
197
+ delta = subject.diff(expected_dsl)
198
+ expect(delta.differ?).to be_falsey
199
+ }
200
+ end
201
+
171
202
  context 'when use references (change)' do
172
203
  let(:actual_dsl) do
173
204
  erbh(<<-ERB)
data/spec/spec_helper.rb CHANGED
@@ -119,7 +119,7 @@ module SpecHelper
119
119
  host: TEST_PG_HOST,
120
120
  port: TEST_PG_PORT,
121
121
  username: TEST_PG_USER,
122
- password: TEST_PG_PASS
122
+ password: TEST_PG_PASS,
123
123
  }.merge(config)
124
124
  else
125
125
  {
@@ -128,7 +128,7 @@ module SpecHelper
128
128
  host: TEST_MYSQL_HOST,
129
129
  port: TEST_MYSQL_PORT,
130
130
  username: TEST_MYSQL_USER,
131
- password: TEST_MYSQL_PASS
131
+ password: TEST_MYSQL_PASS,
132
132
  }.merge(config)
133
133
  end
134
134
  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.7.4
4
+ version: 0.7.5.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-05 00:00:00.000000000 Z
11
+ date: 2018-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -272,6 +272,17 @@ files:
272
272
  - lib/ridgepole/schema_dumper_ext.rb
273
273
  - lib/ridgepole/schema_statements_ext.rb
274
274
  - lib/ridgepole/version.rb
275
+ - omnibus-ridgepole/.gitignore
276
+ - omnibus-ridgepole/Dockerfile.centos
277
+ - omnibus-ridgepole/Dockerfile.ubuntu
278
+ - omnibus-ridgepole/Gemfile
279
+ - omnibus-ridgepole/README.md
280
+ - omnibus-ridgepole/Rakefile
281
+ - omnibus-ridgepole/config/projects/ridgepole.rb
282
+ - omnibus-ridgepole/config/software/ridgepole.rb
283
+ - omnibus-ridgepole/omnibus.rb
284
+ - omnibus-ridgepole/package-scripts/ridgepole/postinst
285
+ - omnibus-ridgepole/package-scripts/ridgepole/postrm
275
286
  - ridgepole.gemspec
276
287
  - spec/cli_helper.rb
277
288
  - spec/erb_helper.rb
@@ -421,12 +432,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
421
432
  version: 2.2.7
422
433
  required_rubygems_version: !ruby/object:Gem::Requirement
423
434
  requirements:
424
- - - ">="
435
+ - - ">"
425
436
  - !ruby/object:Gem::Version
426
- version: '0'
437
+ version: 1.3.1
427
438
  requirements: []
428
439
  rubyforge_project:
429
- rubygems_version: 2.6.13
440
+ rubygems_version: 2.7.6
430
441
  signing_key:
431
442
  specification_version: 4
432
443
  summary: Ridgepole is a tool to manage DB schema.