dag_me 0.3.0 → 0.4.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/README.md +9 -0
- data/lib/dag_me/configuration.rb +23 -4
- data/lib/dag_me/ddl.rb +10 -8
- data/lib/dag_me/graph.rb +12 -0
- data/lib/dag_me/macro.rb +2 -2
- data/lib/dag_me/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 535847d509f0a7d65eb13927c894086c021addbc89017257bec5a41af5c0a547
|
|
4
|
+
data.tar.gz: 3c50f1a759fdbfb225897ee76cf1753073a2b24d1e3b6fa32f9c312b7cd3a7ae
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fdfb3514a36b5b892296b87113f04151881823f8102337f061425640252673b23e2ab3169cbaa2153dba09aedda4ce2fdf1b585b2f085e85492c100cc458db45
|
|
7
|
+
data.tar.gz: 489c29e4aaa52a208ca00f1df5766ef784e2e5e40597b155c3e8adc9ea60cef83a520318fc7957a12c534db4f036d1e903918295c0185f13eb3d99232fb5e987
|
data/README.md
CHANGED
|
@@ -150,6 +150,15 @@ end
|
|
|
150
150
|
Generated tables (`orbital.station_dag_edges`, `orbital.station_dag_paths`) and
|
|
151
151
|
functions land in the node table's schema; trigger names stay plain identifiers.
|
|
152
152
|
|
|
153
|
+
Long table names would push generated identifiers past PostgreSQL's 63-byte
|
|
154
|
+
limit; pass a custom prefix instead of letting it truncate:
|
|
155
|
+
|
|
156
|
+
```ruby
|
|
157
|
+
class MaterialTracking::ProductBillOfMaterial < ApplicationRecord
|
|
158
|
+
dag_me prefix: 'bom_dag' # bom_dag_edges, bom_dag_paths, bom_dag_lock, ...
|
|
159
|
+
end
|
|
160
|
+
```
|
|
161
|
+
|
|
153
162
|
## Multi-tenancy
|
|
154
163
|
|
|
155
164
|
```ruby
|
data/lib/dag_me/configuration.rb
CHANGED
|
@@ -6,7 +6,8 @@ module DagMe
|
|
|
6
6
|
|
|
7
7
|
attr_reader :model, :name, :maintain, :prefix, :schema, :edge_table, :paths_table, :scope_columns
|
|
8
8
|
|
|
9
|
-
def initialize(model:, name: nil, maintain: :postgresql_closure, scope: nil,
|
|
9
|
+
def initialize(model:, name: nil, maintain: :postgresql_closure, scope: nil, prefix: nil,
|
|
10
|
+
edge_table: nil, paths_table: nil)
|
|
10
11
|
unless MAINTAIN_MODES.include?(maintain)
|
|
11
12
|
raise ArgumentError, "maintain must be one of #{MAINTAIN_MODES.inspect}, got #{maintain.inspect}"
|
|
12
13
|
end
|
|
@@ -19,9 +20,12 @@ module DagMe
|
|
|
19
20
|
# identifier - trigger and temp-table names cannot carry a schema - and
|
|
20
21
|
# place generated tables and functions in the node table's schema.
|
|
21
22
|
@schema, base_table = split_schema(model.table_name)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@
|
|
23
|
+
# A custom prefix keeps generated identifiers under PostgreSQL's 63-byte
|
|
24
|
+
# limit when the derived one (long table names) would silently truncate.
|
|
25
|
+
@prefix = prefix&.to_s || [base_table.singularize, @name, 'dag'].compact.join('_')
|
|
26
|
+
validate_prefix!
|
|
27
|
+
@edge_table = edge_table || qualify("#{@prefix}_edges")
|
|
28
|
+
@paths_table = paths_table || qualify("#{@prefix}_paths")
|
|
25
29
|
# Ivar peek keeps class load DB-free; composite keys must be declared
|
|
26
30
|
# before the macro. Array === because AR seeds a BasicObject sentinel.
|
|
27
31
|
@composite_pk = model.instance_variable_defined?(:@primary_key) &&
|
|
@@ -137,6 +141,21 @@ module DagMe
|
|
|
137
141
|
|
|
138
142
|
private
|
|
139
143
|
|
|
144
|
+
# The longest generated suffix is "_edge_insert_check" (18 bytes); reject
|
|
145
|
+
# prefixes whose identifiers PostgreSQL would silently truncate at 63.
|
|
146
|
+
def validate_prefix!
|
|
147
|
+
unless /\A[a-z_][a-z0-9_]*\z/.match?(@prefix)
|
|
148
|
+
raise ArgumentError, "prefix must be a lowercase SQL identifier, got #{@prefix.inspect}"
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
max = 63 - '_edge_insert_check'.length
|
|
152
|
+
return if @prefix.length <= max
|
|
153
|
+
|
|
154
|
+
raise ArgumentError,
|
|
155
|
+
"prefix #{@prefix.inspect} is #{@prefix.length} bytes; identifiers would exceed " \
|
|
156
|
+
"PostgreSQL's 63-byte limit (max prefix: #{max}). Pass dag_me prefix: '...' with a shorter name."
|
|
157
|
+
end
|
|
158
|
+
|
|
140
159
|
def split_schema(table_name)
|
|
141
160
|
return table_name.split('.', 2) if table_name.include?('.')
|
|
142
161
|
|
data/lib/dag_me/ddl.rb
CHANGED
|
@@ -76,6 +76,16 @@ module DagMe
|
|
|
76
76
|
statements
|
|
77
77
|
end
|
|
78
78
|
|
|
79
|
+
# Graph#backfill_self_rows! reuses this to repair nodes inserted with
|
|
80
|
+
# triggers disabled (e.g. Rails fixture loading).
|
|
81
|
+
def backfill_self_rows_sql
|
|
82
|
+
<<~SQL
|
|
83
|
+
INSERT INTO #{config.paths_table} (#{list(anc_cols)}, #{list(desc_cols)}, min_depth, path_count#{scope_column_list})
|
|
84
|
+
SELECT #{list(pk_cols)}, #{list(pk_cols)}, 0, 1#{scope_column_list} FROM #{config.node_table}
|
|
85
|
+
ON CONFLICT DO NOTHING;
|
|
86
|
+
SQL
|
|
87
|
+
end
|
|
88
|
+
|
|
79
89
|
def uninstall_sql
|
|
80
90
|
[
|
|
81
91
|
"DROP TRIGGER IF EXISTS #{config.trigger_name('node_insert')} ON #{config.node_table};",
|
|
@@ -489,14 +499,6 @@ module DagMe
|
|
|
489
499
|
SQL
|
|
490
500
|
end
|
|
491
501
|
|
|
492
|
-
def backfill_self_rows_sql
|
|
493
|
-
<<~SQL
|
|
494
|
-
INSERT INTO #{config.paths_table} (#{list(anc_cols)}, #{list(desc_cols)}, min_depth, path_count#{scope_column_list})
|
|
495
|
-
SELECT #{list(pk_cols)}, #{list(pk_cols)}, 0, 1#{scope_column_list} FROM #{config.node_table}
|
|
496
|
-
ON CONFLICT DO NOTHING;
|
|
497
|
-
SQL
|
|
498
|
-
end
|
|
499
|
-
|
|
500
502
|
def truth_walk_sql
|
|
501
503
|
<<~SQL.chomp
|
|
502
504
|
WITH RECURSIVE walk(#{list(anc_cols)}, #{list(desc_cols)}, depth) AS (
|
data/lib/dag_me/graph.rb
CHANGED
|
@@ -56,6 +56,18 @@ module DagMe
|
|
|
56
56
|
self
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
+
# Restores missing self-rows for nodes inserted with triggers disabled -
|
|
60
|
+
# Rails fixture loading wraps inserts in disable_referential_integrity,
|
|
61
|
+
# which skips the node_insert trigger. Idempotent; meant for test setups.
|
|
62
|
+
def backfill_self_rows!
|
|
63
|
+
return self unless config.closure?
|
|
64
|
+
|
|
65
|
+
model.connection_pool.with_connection do |conn|
|
|
66
|
+
conn.execute(DDL.new(config).backfill_self_rows_sql)
|
|
67
|
+
end
|
|
68
|
+
self
|
|
69
|
+
end
|
|
70
|
+
|
|
59
71
|
# Rows where the stored closure disagrees with the recursive-CTE truth.
|
|
60
72
|
# Empty means healthy.
|
|
61
73
|
def validate
|
data/lib/dag_me/macro.rb
CHANGED
|
@@ -14,7 +14,7 @@ module DagMe
|
|
|
14
14
|
# relay.add_child(other, dag: :power)
|
|
15
15
|
# relay.comms_children
|
|
16
16
|
# Relay.dag(:power).rebuild!
|
|
17
|
-
def dag_me(name = nil, maintain: :postgresql_closure, scope: nil, edge_table: nil, paths_table: nil)
|
|
17
|
+
def dag_me(name = nil, maintain: :postgresql_closure, scope: nil, prefix: nil, edge_table: nil, paths_table: nil)
|
|
18
18
|
key = name&.to_sym || :default
|
|
19
19
|
unless include?(DagMe::Model)
|
|
20
20
|
class_attribute :dag_configs, instance_writer: false, instance_predicate: false, default: {}.freeze
|
|
@@ -22,7 +22,7 @@ module DagMe
|
|
|
22
22
|
end
|
|
23
23
|
raise ArgumentError, "#{self.name}: dag #{key.inspect} is already defined" if dag_configs.key?(key)
|
|
24
24
|
|
|
25
|
-
config = Configuration.new(model: self, name: name&.to_sym, maintain:, scope:, edge_table:, paths_table:)
|
|
25
|
+
config = Configuration.new(model: self, name: name&.to_sym, maintain:, scope:, prefix:, edge_table:, paths_table:)
|
|
26
26
|
self.dag_configs = dag_configs.merge(key => config).freeze
|
|
27
27
|
DagMe::Model.attach(self, config)
|
|
28
28
|
self
|
data/lib/dag_me/version.rb
CHANGED