lowdata 0.4.2 → 0.4.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27f2f5c6b645472ee8de9fe796bdef23493d50dc234998cb78c4a49d417f6d1b
4
- data.tar.gz: 2f1fbb112d7dbcc9be8053b660f88a7781196e6a8cfc7e1d23b479651fb0779c
3
+ metadata.gz: 2f0149153d8da8cf41bc22bcbc5b2b8300ec79d09b51e20946507d04abc98703
4
+ data.tar.gz: 7c01f0639be2ad330f2b9484e59e7d7acb9511dbc394342a66fdbfbfc4099cb0
5
5
  SHA512:
6
- metadata.gz: 37de426f6f987b98618492a48c37b060ff732e89b568cbe1ff6032e9805c28d28988deaa25cb236e0a7c2e951fb3d0ccda8dbd4a6b3af740732ec4f714af6b26
7
- data.tar.gz: a1b741111a08db463198e8d7edd9c84dc6e88ccac97505d00b8608745f176b8d8f527465e30c510e44534792c6e4066e1d694d95edd33bb03e3adbed384ac97f
6
+ metadata.gz: 00ee809389209fbe9a97a943343bce8db7c96b31ed70d77d4fdc0a9de2d687d04446f45205dfedce2ead010f267e1338a7ff7c76e6764e964d39b0c189ce9d47
7
+ data.tar.gz: 97c38055efc657810cbb7b31e4ad92db3700f2c9f31bff7baf37f418bba68f78f3b857087a45021526bc5396967e67aee8a708e564017614cf98a517c04c33e3
data/lib/nesting_tree.rb CHANGED
@@ -100,9 +100,10 @@ class LowData
100
100
  named.first if named.one?
101
101
  end
102
102
 
103
- # `user_id` nests as `user:`, like the association name in most ORMs.
103
+ # `user_id` nests as `user:`, like the association name in most ORMs, and a foreign key to a parent as its child.
104
104
  def one(parent:, child:, link:, key:)
105
105
  column, foreign_key = sole_columns(parent:, child:, link:)
106
+ key ||= child if link[:parent]
106
107
  key ||= column.to_s.end_with?('_id') ? column.to_s.delete_suffix('_id').to_sym : child
107
108
  [key, Nesting::Link.new(parent_column: column, child_column: foreign_key, many: false)]
108
109
  end
data/lib/schema.rb CHANGED
@@ -35,12 +35,10 @@ class LowData
35
35
  @tables ||= @database.tables
36
36
  end
37
37
 
38
- # Tables with a foreign key to or from the table.
38
+ # Tables with a foreign key to or from the table, or to its parent.
39
39
  def related_tables(table:)
40
40
  outgoing = foreign_keys(table:).map { |foreign_key| foreign_key[:table] }
41
- incoming = tables.select do |other|
42
- foreign_keys(table: other).any? { |foreign_key| foreign_key[:table] == table }
43
- end
41
+ incoming = tables.select { |other| links(from: other, to: table).any? }
44
42
 
45
43
  # Sorted, because databases list their tables in different orders, and error messages name the first.
46
44
  ((outgoing + incoming).uniq - [table]).sort
@@ -69,15 +67,31 @@ class LowData
69
67
  links(from: table, to: other).any? || links(from: other, to: table).any?
70
68
  end
71
69
 
72
- # Foreign keys on the `from` table that reference the `to` table, as their columns and the keys they reference.
70
+ # Foreign keys on the `from` table that reference the `to` table, as their columns and the keys they reference. A
71
+ # foreign key to the `to` table's parent links to it too, by its primary key, which has the parent's ids.
73
72
  def links(from:, to:)
74
- foreign_keys(table: from).select { |foreign_key| foreign_key[:table] == to }.map do |foreign_key|
73
+ own = foreign_keys(table: from).select { |foreign_key| foreign_key[:table] == to }.map do |foreign_key|
75
74
  { columns: foreign_key[:columns], keys: foreign_key[:key] || primary_key(table: to) }
76
75
  end
76
+ own + parent_links(from:, to:)
77
+ end
78
+
79
+ # The table a table's primary key is a foreign key to, its parent, like content for posts whose id is content's id.
80
+ def parent(table:)
81
+ foreign_keys(table:).find { |foreign_key| foreign_key[:columns] == primary_key(table:) }&.dig(:table)
77
82
  end
78
83
 
79
84
  private
80
85
 
86
+ # Foreign keys on the `from` table to the `to` table's parent, as links to the `to` table's primary key.
87
+ def parent_links(from:, to:)
88
+ parent = parent(table: to)
89
+ return [] if parent.nil? || [to, parent].include?(from)
90
+
91
+ foreign_keys(table: from).select { |foreign_key| foreign_key[:table] == parent }
92
+ .map { |foreign_key| { columns: foreign_key[:columns], keys: primary_key(table: to), parent: } }
93
+ end
94
+
81
95
  def backwards_rename_message(table:, name:, column:)
82
96
  "#{table} has no #{column} column. To rename #{name}, write #{column}: :#{name}"
83
97
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class LowData
4
- VERSION = '0.4.2'
4
+ VERSION = '0.4.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lowdata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi