rhino-rails 4.2.0 → 4.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/MIT-LICENSE +21 -0
- data/lib/rhino/blueprint/sorter.rb +118 -0
- data/lib/rhino/commands/blueprint_command.rb +22 -0
- data/lib/rhino/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c15dd271d0b613f2effa9bb7c8d050b3214c74884e3021461150017f0f159af6
|
|
4
|
+
data.tar.gz: 5b4e5dcb736acc54674ad24e9d689cd93582e7ba0195554ef99e7735f2c33050
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 93c2de702533d23edcfe6b334aa0ffaf0f327efe6518a8c608e8072b4fe27862430d6591a651e730f198f9e9e8cf39456005464490a12137dadce641613ef268
|
|
7
|
+
data.tar.gz: 7fe767badf982f99dd3fca7b72c441929ee0eb336b72b117ed4a5296cdcaafacda6582a338025e6e611f1a0e87117d6d906469c8b01abc9339109d9da32c711a
|
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rhino
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rhino
|
|
4
|
+
module Blueprint
|
|
5
|
+
# Orders blueprints so that a referenced model's table is created before any
|
|
6
|
+
# model whose migration adds a foreign key to it (parents before children).
|
|
7
|
+
#
|
|
8
|
+
# Foreign keys are taken from +foreignId+ columns that carry a +foreign_model+
|
|
9
|
+
# mapping to another model in the same generation set. References that impose
|
|
10
|
+
# no ordering are ignored:
|
|
11
|
+
# - self-references (a model's FK to its own table — one migration),
|
|
12
|
+
# - references to models NOT in this set (e.g. Organization/User, whose
|
|
13
|
+
# tables are created by +rhino:install+, not the blueprint run).
|
|
14
|
+
#
|
|
15
|
+
# Uses Kahn's algorithm with a stable tie-break: among models with no
|
|
16
|
+
# remaining unmet dependency, the one earliest in the input order wins. The
|
|
17
|
+
# input is already alphabetical (by file name), so the output stays
|
|
18
|
+
# alphabetical wherever relationships don't force a reorder.
|
|
19
|
+
#
|
|
20
|
+
# A circular FK dependency (A -> B -> A) has no linear migration order. Such
|
|
21
|
+
# models are emitted in a deterministic best-effort order and reported via
|
|
22
|
+
# {#cycles} so the caller can warn (one side should be a nullable/deferred FK).
|
|
23
|
+
class Sorter
|
|
24
|
+
# Model names involved in a circular foreign-key dependency during the last
|
|
25
|
+
# {#sort} (empty when the dependency graph is acyclic).
|
|
26
|
+
attr_reader :cycles
|
|
27
|
+
|
|
28
|
+
def initialize
|
|
29
|
+
@cycles = []
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Re-order blueprints into a valid migration sequence (parents first).
|
|
33
|
+
#
|
|
34
|
+
# @param blueprints [Array<Hash>] normalized blueprints (each with a
|
|
35
|
+
# +:model+ name and +:columns+).
|
|
36
|
+
# @return [Array<Hash>] the blueprints, re-ordered.
|
|
37
|
+
def sort(blueprints)
|
|
38
|
+
@cycles = []
|
|
39
|
+
return blueprints.dup if blueprints.length < 2
|
|
40
|
+
|
|
41
|
+
by_model = {}
|
|
42
|
+
blueprints.each do |bp|
|
|
43
|
+
model = bp[:model]
|
|
44
|
+
by_model[model] ||= bp if model
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
dependents = {}
|
|
48
|
+
indegree = {}
|
|
49
|
+
by_model.each_key do |m|
|
|
50
|
+
dependents[m] = []
|
|
51
|
+
indegree[m] = 0
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
by_model.each do |model, bp|
|
|
55
|
+
seen = {}
|
|
56
|
+
dependency_models(bp).each do |ref|
|
|
57
|
+
next if ref == model || !by_model.key?(ref) || seen[ref]
|
|
58
|
+
|
|
59
|
+
seen[ref] = true
|
|
60
|
+
dependents[ref] << model
|
|
61
|
+
indegree[model] += 1
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
input_order = by_model.keys
|
|
66
|
+
|
|
67
|
+
# Record the models that actually participate in a cycle (reachable from
|
|
68
|
+
# themselves), in input order, so the caller can warn about the full cycle.
|
|
69
|
+
input_order.each do |model|
|
|
70
|
+
@cycles << model if reachable_from_self?(model, dependents)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
ordered = []
|
|
74
|
+
resolved = {}
|
|
75
|
+
while ordered.length < by_model.length
|
|
76
|
+
# Earliest-input model with all dependencies already emitted...
|
|
77
|
+
pick = input_order.find { |m| !resolved[m] && indegree[m].zero? }
|
|
78
|
+
# ...or, when a cycle blocks the graph, the earliest unresolved model
|
|
79
|
+
# (deterministic cycle-break; the cycle itself is reported via #cycles).
|
|
80
|
+
pick ||= input_order.find { |m| !resolved[m] }
|
|
81
|
+
|
|
82
|
+
ordered << by_model[pick]
|
|
83
|
+
resolved[pick] = true
|
|
84
|
+
dependents[pick].each { |child| indegree[child] -= 1 }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
ordered
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
# Whether +start+ can reach itself by following dependency edges — i.e. it
|
|
93
|
+
# participates in a circular foreign-key dependency. +adj+ maps a model to
|
|
94
|
+
# the models that reference it (its dependents).
|
|
95
|
+
def reachable_from_self?(start, adj)
|
|
96
|
+
stack = (adj[start] || []).dup
|
|
97
|
+
visited = {}
|
|
98
|
+
until stack.empty?
|
|
99
|
+
node = stack.pop
|
|
100
|
+
return true if node == start
|
|
101
|
+
next if visited[node]
|
|
102
|
+
|
|
103
|
+
visited[node] = true
|
|
104
|
+
(adj[node] || []).each { |n| stack << n }
|
|
105
|
+
end
|
|
106
|
+
false
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The model names this blueprint's migration adds foreign keys to, taken
|
|
110
|
+
# from its +foreignId+ columns that carry a +foreign_model+.
|
|
111
|
+
def dependency_models(blueprint)
|
|
112
|
+
(blueprint[:columns] || [])
|
|
113
|
+
.select { |c| c[:type] == "foreignId" && c[:foreign_model] }
|
|
114
|
+
.map { |c| c[:foreign_model] }
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -4,6 +4,7 @@ require "rhino/commands/base_command"
|
|
|
4
4
|
require "fileutils"
|
|
5
5
|
require "rhino/blueprint/blueprint_parser"
|
|
6
6
|
require "rhino/blueprint/blueprint_validator"
|
|
7
|
+
require "rhino/blueprint/sorter"
|
|
7
8
|
require "rhino/blueprint/manifest_manager"
|
|
8
9
|
require "rhino/blueprint/generators/policy_generator"
|
|
9
10
|
require "rhino/blueprint/generators/test_generator"
|
|
@@ -85,6 +86,27 @@ module Rhino
|
|
|
85
86
|
|
|
86
87
|
say " Found #{yaml_files.length} blueprint(s)", :cyan
|
|
87
88
|
|
|
89
|
+
# Order so a referenced model's table is migrated before any model that
|
|
90
|
+
# foreign-keys to it (parents before children). Migration timestamps are
|
|
91
|
+
# assigned in iteration order, so this is what makes the set runnable.
|
|
92
|
+
parsed_for_order = []
|
|
93
|
+
unparseable = []
|
|
94
|
+
yaml_files.each do |f|
|
|
95
|
+
parsed_for_order << { file: f, blueprint: parser.parse_model(f) }
|
|
96
|
+
rescue StandardError
|
|
97
|
+
unparseable << f
|
|
98
|
+
end
|
|
99
|
+
sorter = Rhino::Blueprint::Sorter.new
|
|
100
|
+
ordered = sorter.sort(parsed_for_order.map { |p| p[:blueprint] })
|
|
101
|
+
file_by_model = parsed_for_order.each_with_object({}) do |p, h|
|
|
102
|
+
h[p[:blueprint][:model]] ||= p[:file]
|
|
103
|
+
end
|
|
104
|
+
yaml_files = ordered.map { |bp| file_by_model[bp[:model]] }.compact + unparseable
|
|
105
|
+
if sorter.cycles.any?
|
|
106
|
+
say " ⚠ Circular foreign-key dependency among: #{sorter.cycles.join(', ')}. " \
|
|
107
|
+
"Migration order is best-effort — make one side nullable or add the FK in a later migration.", :yellow
|
|
108
|
+
end
|
|
109
|
+
|
|
88
110
|
# 3. Process each blueprint
|
|
89
111
|
is_multi_tenant = multi_tenant_enabled?
|
|
90
112
|
org_identifier = detect_org_identifier
|
data/lib/rhino/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rhino-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 4.2.
|
|
4
|
+
version: 4.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bruno Cipolla
|
|
@@ -186,6 +186,7 @@ executables: []
|
|
|
186
186
|
extensions: []
|
|
187
187
|
extra_rdoc_files: []
|
|
188
188
|
files:
|
|
189
|
+
- MIT-LICENSE
|
|
189
190
|
- README.md
|
|
190
191
|
- lib/rhino-rails.rb
|
|
191
192
|
- lib/rhino.rb
|
|
@@ -198,6 +199,7 @@ files:
|
|
|
198
199
|
- lib/rhino/blueprint/generators/seeder_generator.rb
|
|
199
200
|
- lib/rhino/blueprint/generators/test_generator.rb
|
|
200
201
|
- lib/rhino/blueprint/manifest_manager.rb
|
|
202
|
+
- lib/rhino/blueprint/sorter.rb
|
|
201
203
|
- lib/rhino/commands/base_command.rb
|
|
202
204
|
- lib/rhino/commands/blueprint_command.rb
|
|
203
205
|
- lib/rhino/commands/export_postman_command.rb
|