brick 1.0.39 → 1.0.40

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: 965d31143248d85fd47cf2445a266fb9d26cc5eb814330cbdbfb37d50570b5a0
4
- data.tar.gz: d4f070e1c01b3922a610a4385fde6f6b4f345892d68d6b872e1294a33f4b1e61
3
+ metadata.gz: cf29d4af64b65c01ae68df45762b816403a1ebc0035e1221552f249ff3ba2ce9
4
+ data.tar.gz: 076cdb672d7c2137cc74421467d72491e9ea89751fab32015f427158fc367b45
5
5
  SHA512:
6
- metadata.gz: 70ea705efad428abb335e05569284caabe908c65efd679fffcd044894083626ca432366a65305bcd50506a7ca13169bac9cc2f7354728c3b8f23e4ed0aba7094
7
- data.tar.gz: b61096e6deadf323cc8784bcf93ae5faf72eae02fef3059b002c57bac035b30858da73bb59bd2f0572168a90b39062645580cd120e1d78665f5c909258727ad8
6
+ metadata.gz: ce17038567ddd9d6d9d9dce6eb71fd50612d167b56351898ef9b3f9441d1a7ef3ee4ec3a57f3f24e613829e6c8a2f59517fc0d4e9266f11166b92821310b0c06
7
+ data.tar.gz: 6a7bb38b009853594a8bf1ab28489bbc92d6300fcd0f8497c7950e81c6a62a4c6da2bd65840f93bd7afea40153727cb93914529120ecf9b0dceca39b21fef880
@@ -70,7 +70,11 @@ module ActiveRecord
70
70
  def self._brick_primary_key(relation = nil)
71
71
  return instance_variable_get(:@_brick_primary_key) if instance_variable_defined?(:@_brick_primary_key)
72
72
 
73
- pk = primary_key.is_a?(String) ? [primary_key] : primary_key || []
73
+ pk = begin
74
+ primary_key.is_a?(String) ? [primary_key] : primary_key || []
75
+ rescue
76
+ []
77
+ end
74
78
  # Just return [] if we're missing any part of the primary key. (PK is usually just "id")
75
79
  if relation && pk.present?
76
80
  @_brick_primary_key ||= pk.any? { |pk_part| !relation[:cols].key?(pk_part) } ? [] : pk
@@ -529,6 +529,7 @@ if (headerTop) {
529
529
  # 0..62 because Postgres column names are limited to 63 characters
530
530
  #{obj_name}, (descrips = @_brick_bt_descrip[bt.first][bt_class])[0..-2].map { |z| #{obj_name}.send(z.last[0..62]) }, (bt_id_col = descrips.last)
531
531
  )
532
+ bt_txt ||= \"<< Orphaned ID: #\{val} >>\" if val
532
533
  bt_id = #{obj_name}.send(*bt_id_col) if bt_id_col&.present? %>
533
534
  <%= bt_id ? link_to(bt_txt, send(\"#\{bt_class.base_class.name.underscore.tr('/', '_')\}_path\".to_sym, bt_id)) : bt_txt %>
534
535
  <%#= Previously was: bt_obj = bt[1].first.first.find_by(bt[2] => val); link_to(bt_obj.brick_descrip, send(\"#\{bt[1].first.first.name.underscore\}_path\".to_sym, bt_obj.send(bt[1].first.first.primary_key.to_sym))) if bt_obj %>
@@ -610,7 +611,11 @@ end
610
611
  html_options = { prompt: \"Select #\{bt_name\}\" }
611
612
  html_options[:class] = 'dimmed' unless val %>
612
613
  <%= f.select k.to_sym, bt[3], { value: val || '^^^brick_NULL^^^' }, html_options %>
613
- <%= bt_obj = bt_class&.find_by(bt_pair[1] => val); link_to('⇛', send(\"#\{bt_class.base_class.name.underscore.tr('/', '_')\}_path\".to_sym, bt_obj.send(bt_class.primary_key.to_sym)), { class: 'show-arrow' }) if bt_obj %>
614
+ <%= if (bt_obj = bt_class&.find_by(bt_pair[1] => val))
615
+ link_to('⇛', send(\"#\{bt_class.base_class.name.underscore.tr('/', '_')\}_path\".to_sym, bt_obj.send(bt_class.primary_key.to_sym)), { class: 'show-arrow' })
616
+ elsif val
617
+ \"Orphaned ID: #\{val}\"
618
+ end %>
614
619
  <% else case #{model_name}.column_for_attribute(k).type
615
620
  when :string, :text %>
616
621
  <% if is_bcrypt?(val) # || .readonly? %>
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ if Object.const_defined?('::Rake::TaskManager')
4
+ namespace :brick do
5
+ desc 'Find any seemingly-orphaned records'
6
+ task orphans: :environment do
7
+ def class_pk(dotted_name, multitenant)
8
+ Object.const_get((multitenant ? [dotted_name.split('.').last] : dotted_name.split('.')).map { |nm| "::#{nm.singularize.camelize}" }.join).primary_key
9
+ end
10
+
11
+ schema_list = ((multi = ::Brick.config.schema_behavior[:multitenant]) && ::Brick.db_schemas.keys.sort) || []
12
+ if schema_list.length > 1
13
+ require 'fancy_gets'
14
+ include FancyGets
15
+ schema = gets_list(
16
+ list: schema_list,
17
+ chosen: multi[:schema_to_analyse]
18
+ )
19
+ elsif schema_list.length.positive?
20
+ schema = schema_list.first
21
+ end
22
+ ActiveRecord::Base.execute_sql("SET SEARCH_PATH = ?", schema) if schema
23
+ orphans = +''
24
+ ::Brick.relations.each do |k, v|
25
+ next if v.key?(:isView) || ::Brick.config.exclude_tables.include?(k) ||
26
+ !(pri_pk = v[:pkey].values.first&.first) ||
27
+ !(pri_pk = class_pk(k, multi))
28
+ v[:fks].each do |k1, v1|
29
+ next if v1[:is_bt] ||
30
+ !(for_rel = ::Brick.relations.fetch(v1[:inverse_table], nil)) ||
31
+ v1[:inverse]&.key?(:polymorphic) ||
32
+ !(for_pk = for_rel.fetch(:pkey, nil)&.values&.first&.first) ||
33
+ !(for_pk = class_pk(v1[:inverse_table], multi))
34
+ begin
35
+ ActiveRecord::Base.execute_sql(
36
+ "SELECT DISTINCT frn.#{v1[:fk]} AS pri_id, frn.#{for_pk} AS fk_id
37
+ FROM #{v1[:inverse_table]} AS frn
38
+ LEFT OUTER JOIN #{k} AS pri ON pri.#{pri_pk} = frn.#{v1[:fk]}
39
+ WHERE frn.#{v1[:fk]} IS NOT NULL AND pri.#{pri_pk} IS NULL
40
+ ORDER BY 1, 2"
41
+ ).each do |o|
42
+ orphans << "#{v1[:inverse_table]} #{o['fk_id']} refers to non-existant #{k} #{o['pri_id']}\n"
43
+ end
44
+ rescue StandardError => err
45
+ puts "Strange -- #{err.inspect}"
46
+ end
47
+ end
48
+ end
49
+ puts "For #{schema}:\n#{'=' * (schema.length + 5)}" if schema
50
+ if orphans.blank?
51
+ puts "No orphans!"
52
+ else
53
+ print orphans
54
+ end
55
+ end
56
+ end
57
+ end
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 39
8
+ TINY = 40
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = nil
data/lib/brick.rb CHANGED
@@ -481,6 +481,11 @@ require 'active_record'
481
481
  require 'active_record/relation'
482
482
  require 'active_record/relation/query_methods' if is_add_left_outer_join
483
483
 
484
+ # Rake tasks
485
+ class Railtie < Rails::Railtie
486
+ Dir.glob("#{File.expand_path(__dir__)}/brick/tasks/**/*.rake").each { |task| load task }
487
+ end
488
+
484
489
  # Major compatibility fixes for ActiveRecord < 4.2
485
490
  # ================================================
486
491
  ActiveSupport.on_load(:active_record) do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brick
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.39
4
+ version: 1.0.40
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-30 00:00:00.000000000 Z
11
+ date: 2022-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '7.2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: fancy_gets
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: appraisal
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -225,6 +239,7 @@ files:
225
239
  - lib/brick/join_array.rb
226
240
  - lib/brick/serializers/json.rb
227
241
  - lib/brick/serializers/yaml.rb
242
+ - lib/brick/tasks/orphans.rake
228
243
  - lib/brick/util.rb
229
244
  - lib/brick/version_number.rb
230
245
  - lib/generators/brick/USAGE