ridgepole 0.8.0 → 0.8.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
  SHA256:
3
- metadata.gz: b24a10a4161326fb28338d3a5e6bbedea7a70d8c7425a070b7955212532b939e
4
- data.tar.gz: c95733535de966cf7c191b5c9c52c36d561315efd241f30665723d62e70bf280
3
+ metadata.gz: 0a3e5a9549db69b5028a0f9a8bc7cff951a3617ca7eced3c6be06f31bc373f4e
4
+ data.tar.gz: 756a16fecffa1a8c0f62a3f0a5d58a142377ca99509106865354ae6b8676317f
5
5
  SHA512:
6
- metadata.gz: 9126dc1fbd0ca8000bdcb6daf5ef5050ffe1524081e8b161f48783574c448f432e9f7e7c74832b2c0b9d04c9074313350753aefaa61860f9380d5e5e904ee369
7
- data.tar.gz: 062aa79acfcaa448720bffbc1f3f6973aee7302e9e9e24399270a5ac998c0b5ef656c8fc69424f7c44e1ddd50cb8a74031df2fed9be1977f9c899dd08cc056f4
6
+ metadata.gz: 6441491b72d7dcdfad0e74e58bfd6a950d3ed7876e9060e5812b86b013ddae9cd69d65f86ac1d7d32f4b046d0c1e2e126deade4ef891ab25954b4683d13bd32d
7
+ data.tar.gz: f30396aead19b62e243a2b135a27a2313ebbd05d429803ea8e0ee38c841f6729801a211cfa57e6d8f9b59fbef834f23d43d73d4f1684455bee19210f8eecce0e
data/README.md CHANGED
@@ -101,6 +101,8 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
101
101
  * Fix for `add_foreign_key(..., column: ,,,)` ([pull#278](https://github.com/winebarrel/ridgepole/pull/278))
102
102
  * `>= 0.8.0`
103
103
  * Support Rails 6.0
104
+ * `>= 0.8.1`
105
+ * Drop tables in an order considering foreign key constraints ([pull#284](https://github.com/winebarrel/ridgepole/pull/284))
104
106
  </details>
105
107
 
106
108
  ## Installation
@@ -42,7 +42,7 @@ module Ridgepole
42
42
  append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
43
43
  end
44
44
 
45
- (@delta[:delete] || {}).each do |table_name, attrs|
45
+ (@delta[:delete] || {}).reverse_each do |table_name, attrs|
46
46
  append_drop_table(table_name, attrs, buf)
47
47
  end
48
48
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'tsort'
4
+
3
5
  module Ridgepole
4
6
  class DSLParser
5
7
  def initialize(options = {})
@@ -10,7 +12,11 @@ module Ridgepole
10
12
  definition, execute = Context.eval(dsl, opts)
11
13
  check_orphan_index(definition)
12
14
  check_orphan_foreign_key(definition)
13
- [definition, execute]
15
+ sorted_definition = {}
16
+ DependencyGraph.new(definition).tsort_each do |table_name|
17
+ sorted_definition[table_name] = definition[table_name]
18
+ end
19
+ [sorted_definition, execute]
14
20
  end
15
21
 
16
22
  private
@@ -26,5 +32,22 @@ module Ridgepole
26
32
  raise "Table `#{table_name}` to create the foreign key is not defined: #{attrs[:foreign_keys].keys.join(',')}" if attrs[:foreign_keys] && !(attrs[:definition])
27
33
  end
28
34
  end
35
+
36
+ class DependencyGraph
37
+ include TSort
38
+
39
+ def initialize(definition)
40
+ @definition = definition
41
+ end
42
+
43
+ def tsort_each_child(table_name, &block)
44
+ keys = @definition[table_name].fetch(:foreign_keys, {})
45
+ keys.each_value { |v| block.call(v[:to_table]) }
46
+ end
47
+
48
+ def tsort_each_node(&block)
49
+ @definition.each_key(&block)
50
+ end
51
+ end
29
52
  end
30
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ridgepole
4
- VERSION = '0.8.0'
4
+ VERSION = '0.8.1'
5
5
  end
@@ -60,29 +60,29 @@ describe 'Ridgepole::Client#diff -> migrate' do
60
60
  context 'when drop fk when drop table' do
61
61
  let(:dsl) do
62
62
  erbh(<<-ERB)
63
- create_table "parent", <%= i cond('>= 5.1',id: :integer) %>, force: :cascade do |t|
63
+ create_table "0_parent", <%= i cond('>= 5.1',id: :integer) %>, force: :cascade do |t|
64
64
  end
65
65
 
66
- create_table "child", force: :cascade do |t|
67
- t.integer "parent_id"
68
- t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
66
+ create_table "1_child", force: :cascade do |t|
67
+ t.integer "0_parent_id"
68
+ t.index ["0_parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
69
69
  end
70
70
 
71
- add_foreign_key "child", "parent", name: "child_ibfk_1"
71
+ add_foreign_key "1_child", "0_parent", name: "child_ibfk_1"
72
72
  ERB
73
73
  end
74
74
 
75
75
  let(:sorted_dsl) do
76
76
  erbh(<<-ERB)
77
- create_table "child", force: :cascade do |t|
78
- t.integer "parent_id"
79
- t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
77
+ create_table "0_parent", <%= i cond('>= 5.1',id: :integer) %>, force: :cascade do |t|
80
78
  end
81
79
 
82
- create_table "parent", <%= i cond('>= 5.1',id: :integer) %>, force: :cascade do |t|
80
+ create_table "1_child", force: :cascade do |t|
81
+ t.integer "0_parent_id"
82
+ t.index ["0_parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
83
83
  end
84
84
 
85
- add_foreign_key "child", "parent", name: "child_ibfk_1"
85
+ add_foreign_key "1_child", "0_parent", name: "child_ibfk_1"
86
86
  ERB
87
87
  end
88
88
 
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.8.0
4
+ version: 0.8.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: 2019-08-17 00:00:00.000000000 Z
11
+ date: 2019-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -442,8 +442,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
442
442
  - !ruby/object:Gem::Version
443
443
  version: '0'
444
444
  requirements: []
445
- rubyforge_project:
446
- rubygems_version: 2.7.6
445
+ rubygems_version: 3.0.3
447
446
  signing_key:
448
447
  specification_version: 4
449
448
  summary: Ridgepole is a tool to manage DB schema.