brick 1.0.231 → 1.0.233

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: 6b36099e75ec27d9fa4522da84f319af2fb1c98997c629a5246c88a1e6ddcbe3
4
- data.tar.gz: '089a28701ee39956f0447801c70f9f044aefd06638ab575f3a6307690746eca4'
3
+ metadata.gz: 153b8d0a95771a92fad553394bb1dd51633a971e7909b3a32e6cf548dacb2cb9
4
+ data.tar.gz: '094c6476db206319239def080ffc9349a4e8ec35c915e23a3cf1db3a5d8b425d'
5
5
  SHA512:
6
- metadata.gz: 0b8ed54764ce53a403f47d0a42ff578445e9a4d03e60f973264bbafb07a8c6a7af8daf15736ceb172472d2919817d14673163b40fdfffbdd87d4511e759c3747
7
- data.tar.gz: '0326081b5c6e69ab4f7a6fe94784c73996117db6c715e3b158bc0cde744245e213e53a50e5b5d01bcd44b6cb24d55022548c2a1c83a53de5ccbca04f23c66b65'
6
+ metadata.gz: 54c76ebb11fead41ed9ae33fee09e2e6e4685aa12c50c073a9da72d1004d08c2065b441d2bf272d06e8aacc2de16401c74636c61f65048d30e6b9d0bd49add8a
7
+ data.tar.gz: 1c391fa9c4acfbb9ce327d9da410e23ee9c1b183145f1a636ca5be91ac48b369346859432982033f74cf50de99f7d4f9c2011e7111eeefe4830bd76e45d5b931
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 231
8
+ TINY = 233
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = nil
@@ -15,6 +15,7 @@ You need only #{usage == :migrations ? 'this scope:' : "these three scopes:
15
15
 
16
16
  Please provide your Airtable PAT:"
17
17
  pat = gets_password
18
+ pat = ENV['AIRTABLE_PAT'] if pat.blank?
18
19
  require 'net/http'
19
20
  # Generate a list of bases that can be chosen
20
21
  bases = https_get('https://api.airtable.com/v0/meta/bases', pat)
@@ -52,22 +53,22 @@ Please provide your Airtable PAT:"
52
53
  # Queue up to build associative table with two foreign keys
53
54
  camelized = (assoc_name = "#{tbl_name}_#{col_name}_#{frn_tbl}").camelize
54
55
  if associatives.keys.any? { |a| a.camelize == camelized }
55
- puts "Strangely have found two columns in \"#{table.name}\" with a name similar to \"#{col_name}\". Skipping this to avoid a conflict."
56
+ puts "Strangely have found two columns in \"#{table.name}\" with a name similar to \"#{col_name}\". Skipping one to avoid a conflict."
56
57
  next
57
58
 
58
59
  end
59
60
  associatives[assoc_name] = [col_name, frn_tbl, tbl_name]
60
- fks << [assoc_name, frn_tbl, frn_tbl, col_name.underscore, tbl_name]
61
+ fks << [assoc_name, frn_tbl, tbl_name, col_name.underscore, tbl_name]
61
62
  end
62
63
  end
63
64
  else
64
65
  # puts col['type']
65
66
  dt = case col['type']
66
- when 'singleLineText', 'url', 'singleSelect'
67
+ when 'singleLineText', 'url', 'email', 'singleSelect'
67
68
  'string'
68
69
  when 'multilineText'
69
70
  'text'
70
- when 'number'
71
+ when 'number', 'currency'
71
72
  'decimal'
72
73
  when 'checkbox'
73
74
  'boolean'
@@ -94,18 +95,15 @@ Please provide your Airtable PAT:"
94
95
  }
95
96
  end
96
97
  associatives.each do |k, v|
97
- pri_pk_col = relations[v[1]][:pkey]&.first&.last&.first
98
- frn_pk_col = relations[v[2]][:pkey]&.first&.last&.first
99
98
  pri_fk_name = "#{v[1]}_id"
100
- frn_fk_name = (frn_fk_name == pri_fk_name) ?
101
- "#{v[2]}_2_id" # Self-referencing N:M
102
- : "#{v[2]}_id" # Standard N:M
99
+ frn_fk_base = (v[1] == v[2]) ? "#{v[2]}_2" : v[2] # Accommodates self-referencing N:Ms
100
+ frn_fk_name = "#{frn_fk_base}_id"
103
101
  relations[k] = {
104
102
  pkey: { "#{k}_pkey" => ['id'] },
105
103
  cols: { 'id' => ['integer', nil, false, false] }
106
104
  }
107
- fks << [v[1], pri_fk_name, k, pri_fk_name.underscore]
108
- fks << [v[2], frn_fk_name, k, frn_fk_name.underscore]
105
+ fks << [v[1], pri_fk_name, k, v[0].underscore]
106
+ fks << [v[2], frn_fk_name, k, frn_fk_base.underscore]
109
107
  end
110
108
  fk_idx = 0
111
109
  fks.each do |pri_tbl, fk_col, frn_tbl, airtable_col, assoc_tbl|
@@ -142,7 +140,7 @@ Please provide your Airtable PAT:"
142
140
  end
143
141
 
144
142
  def sane_name(col_name)
145
- sane_table_name(col_name.gsub('&', 'and').tr('()?', ''))
143
+ sane_table_name(col_name.gsub('&', 'and').tr('()?', '').tr('/', '_'))
146
144
  end
147
145
 
148
146
  def sane_table_name(tbl_name)
@@ -183,7 +183,9 @@ module Brick
183
183
  # to_table column
184
184
  mig << "#{add_fk[0]}, column: :#{add_fk[1]}, primary_key: :#{pk}\n"
185
185
  end
186
- if after_fks.length > 500
186
+ # If there are LOTS of foreign keys then give extra database maintenance info and initially
187
+ # disable the final FK migration by using a ".rbx" file extension.
188
+ fks_extension = if after_fks.length > 500
187
189
  minutes = (after_fks.length + 1000) / 1500
188
190
  mig << " if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'\n"
189
191
  mig << " puts 'NOTE: It could take around #{minutes} #{'minute'.pluralize(minutes)} on a FAST machine for Postgres to do all the final processing for these foreign keys. Please be patient!'\n"
@@ -194,14 +196,19 @@ module Brick
194
196
  execute('VACUUM FULL')
195
197
  execute('BEGIN TRANSACTION')
196
198
  end\n"
199
+ 'rbx'
200
+ else
201
+ 'rb'
197
202
  end
198
203
  mig << +" end\n"
199
204
  current_mig_time[0] += 1.minute
200
- versions_to_create << migration_file_write(mig_path, 'create_brick_fks.rbx', current_mig_time, ar_version, mig)
201
- puts "Have written out a final migration called 'create_brick_fks.rbx' which creates #{after_fks.length} foreign keys.
202
- This file extension (.rbx) will cause it not to run yet when you do a 'rails db:migrate'.
205
+ versions_to_create << migration_file_write(mig_path, "create_brick_fks.#{fks_extension}", current_mig_time, ar_version, mig)
206
+ puts "Have written out a final migration called 'create_brick_fks.#{fks_extension}' which creates #{after_fks.length} foreign keys."
207
+ if fks_extension == 'rbx'
208
+ puts " This file extension (.rbx) will cause it not to run yet when you do a 'rails db:migrate'.
203
209
  The idea here is to do all data loading first, and then rename that migration file back
204
210
  into having a .rb extension, and run a final db:migrate to put the foreign keys in place."
211
+ end
205
212
 
206
213
  when 'Create' # Show additional_references entries that can be added into brick.rb
207
214
  puts 'Place this block into your brick.rb file:'
@@ -161,14 +161,15 @@ module Brick
161
161
  if relation[:cols].keys.include?(col_name = ::Brick::AirtableApiCaller.sane_name(field.first))
162
162
  s[col_name] = obj['fields'][field.first]
163
163
  else # Consider N:M fks
164
- nm_fk = relation[:fks].find do |_k, fk1|
165
- relations[fk1[:assoc_tbl]]&.fetch(:fks, nil)&.find { |_k, fk2| fk2[:assoc_name] == col_name }
166
- end&.last
167
- if (t_table = nm_fk&.fetch(:inverse_table, nil))
164
+ hm_fk = relation[:fks].find { |_k, fk1| !fk1[:is_bt] && fk1[:assoc_name] == ::Brick::AirtableApiCaller.sane_name(field.first) }&.last
165
+ if (assoc_table = hm_fk&.fetch(:inverse_table, nil))
166
+ associative_fks = relations[assoc_table][:fks]
167
+ # near_side_fk = associative_fks.find { |_k, fk1| fk1[:is_bt] && fk1[:assoc_name] == ::Brick::AirtableApiCaller.sane_name(field.first) }&.last
168
+ far_side_fk = associative_fks.find { |_k, fk1| fk1[:is_bt] && fk1[:assoc_name] != ::Brick::AirtableApiCaller.sane_name(field.first) }&.last
168
169
  field.last.each do |nm_rec|
169
- nm_fk_col = nm_fk[:assoc_tbl]
170
- airtable_assoc_recids[t_table] << "#{nm_fk[:fk]}: #{nm_fk[:fk].singularize}_#{obj['id'][3..-1]}, " \
171
- "#{nm_fk_col}: #{nm_fk_col.singularize}_#{nm_rec[3..-1]}"
170
+ # Can trade out: hm_fk[:fk] for: near_side_fk[:inverse_table]
171
+ airtable_assoc_recids[assoc_table] << "#{hm_fk[:fk]}: #{hm_fk[:fk].singularize}_#{nm_rec[3..-1]}, " \
172
+ "#{far_side_fk[:assoc_name]}: #{far_side_fk[:inverse_table].singularize}_#{obj['id'][3..-1]}"
172
173
  end
173
174
  end
174
175
  end
@@ -183,7 +184,7 @@ module Brick
183
184
  is_empty = false
184
185
  # For Airtable, take off the "rec___" prefix
185
186
  pk_val = is_airtable ? airtable_id[3..-1] : brick_escape(obj.attributes_before_type_cast[pkey_cols.first])
186
- var_name = "#{tbl.gsub('.', '__')}_#{pk_val}"
187
+ var_name = "#{tbl.singularize.gsub('.', '__')}_#{pk_val}"
187
188
  fk_vals = []
188
189
  data = []
189
190
  updates = []
@@ -203,6 +204,7 @@ module Brick
203
204
  if fk
204
205
  inv_tbl = fk[:inverse_table].gsub('.', '__')
205
206
  fk_val = if is_airtable
207
+ inv_tbl = inv_tbl.singularize
206
208
  # Used to be: fk[:airtable_col]
207
209
  # Take off the "rec___" prefix
208
210
  obj.attributes_before_type_cast[fk[:assoc_name]]&.first&.[](3..-1)
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.231
4
+ version: 1.0.233
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-12 00:00:00.000000000 Z
11
+ date: 2025-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord