brick 1.0.55 → 1.0.56
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/brick/version_number.rb +1 -1
- data/lib/generators/brick/migrations_generator.rb +44 -7
- 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: b696c53c9d9d359d49de147b06b5342820ca230451c860d097f44bcae3b23bb0
|
4
|
+
data.tar.gz: 7b0f6ff365f918a97d64305eabf70fcfe78cf764514ca22b3731b780da04b22c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43ab6db28554fb7502d0f03267d2f5bab27d310fb988701d371232f6604090cd679767a12097a7c217401b05e938c787844ae11138a2c8143c1e675b93599656
|
7
|
+
data.tar.gz: 8d2e592ba27bba30e100ea23a2bf951d6f3861c422ef355bad7cd38e146c20c2cf53380aa7ee440f40aa2d111c7f33eadc8dc09f73d1689432fff071e04228c4
|
data/lib/brick/version_number.rb
CHANGED
@@ -87,6 +87,8 @@ module Brick
|
|
87
87
|
done = []
|
88
88
|
fks = {}
|
89
89
|
stuck = {}
|
90
|
+
indexes = {} # Track index names to make sure things are unique
|
91
|
+
versions = [] # Resulting versions to be used when updating the schema_migrations table
|
90
92
|
# Start by making migrations for fringe tables (those with no foreign keys).
|
91
93
|
# Continue layer by layer, creating migrations for tables that reference ones already done, until
|
92
94
|
# no more migrations can be created. (At that point hopefully all tables are accounted for.)
|
@@ -134,14 +136,29 @@ module Brick
|
|
134
136
|
', id: :bigserial'
|
135
137
|
else
|
136
138
|
", id: :#{SQL_TYPES[pkey_col_first] || pkey_col_first}" # Something like: id: :integer, primary_key: :businessentityid
|
137
|
-
end +
|
139
|
+
end +
|
140
|
+
(pkey_cols.first ? ", primary_key: :#{pkey_cols.first}" : '') +
|
141
|
+
(!is_4x_rails && (comment = relation&.fetch(:description, nil))&.present? ? ", comment: #{comment.inspect}" : '')
|
138
142
|
end
|
139
143
|
end
|
144
|
+
# Find the ActiveRecord class in order to see if the columns have comments
|
145
|
+
unless is_4x_rails
|
146
|
+
klass = begin
|
147
|
+
tbl.tr('.', '/').singularize.camelize.constantize
|
148
|
+
rescue StandardError
|
149
|
+
end
|
150
|
+
if klass
|
151
|
+
unless ActiveRecord::Migration.table_exists?(klass.table_name)
|
152
|
+
puts "WARNING: Unable to locate table #{klass.table_name} (for #{klass.name})."
|
153
|
+
klass = nil
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
140
157
|
# Refer to this table name as a symbol or dotted string as appropriate
|
141
|
-
|
142
|
-
mig << " def change\n return unless reverting? || !table_exists?(#{
|
158
|
+
tbl_code = tbl_parts.length == 1 ? ":#{tbl_parts.first}" : "'#{tbl}'"
|
159
|
+
mig << " def change\n return unless reverting? || !table_exists?(#{tbl_code})\n\n"
|
143
160
|
mig << " create_schema :#{schema} unless schema_exists?(:#{schema})\n" if schema
|
144
|
-
mig << " create_table #{
|
161
|
+
mig << " create_table #{tbl_code}#{id_option} do |t|\n"
|
145
162
|
possible_ts = [] # Track possible generic timestamps
|
146
163
|
add_fks = [] # Track foreign keys to add after table creation
|
147
164
|
relation[:cols].each do |col, col_type|
|
@@ -156,6 +173,9 @@ module Brick
|
|
156
173
|
|
157
174
|
sql_type ||= col_type.first
|
158
175
|
suffix = col_type[3] ? +', null: false' : +''
|
176
|
+
if !is_4x_rails && klass && (comment = klass.columns_hash.fetch(col, nil)&.comment)&.present?
|
177
|
+
suffix << ", comment: #{comment.inspect}"
|
178
|
+
end
|
159
179
|
# Determine if this column is used as part of a foreign key
|
160
180
|
if fk = fkey_cols.find { |assoc| col == assoc[:fk] }
|
161
181
|
to_table = fk[:inverse_table].split('.')
|
@@ -166,6 +186,18 @@ module Brick
|
|
166
186
|
add_fks << [to_table, column, ::Brick.relations[fk[:inverse_table]]]
|
167
187
|
else
|
168
188
|
suffix << ", type: :#{sql_type}" unless sql_type == key_type
|
189
|
+
# Will the resulting default index name be longer than what Postgres allows? (63 characters)
|
190
|
+
if (idx_name = ActiveRecord::Base.connection.index_name(tbl, {column: col})).length > 63
|
191
|
+
# Try to find a shorter name that hasn't been used yet
|
192
|
+
unless indexes.key?(shorter = idx_name[0..62]) ||
|
193
|
+
indexes.key?(shorter = idx_name.tr('_', '')[0..62]) ||
|
194
|
+
indexes.key?(shorter = idx_name.tr('aeio', '')[0..62])
|
195
|
+
puts "Unable to easily find unique name for index #{idx_name} that is shorter than 64 characters,"
|
196
|
+
puts "so have resorted to this GUID-based identifier: #{shorter = "#{tbl[0..25]}_#{::SecureRandom.uuid}"}."
|
197
|
+
end
|
198
|
+
suffix << ", index: { name: '#{shorter || idx_name}' }"
|
199
|
+
indexes[shorter || idx_name] = nil
|
200
|
+
end
|
169
201
|
mig << " t.references :#{fk[:assoc_name]}#{suffix}, foreign_key: { to_table: #{to_table} }\n"
|
170
202
|
end
|
171
203
|
else
|
@@ -190,11 +222,12 @@ module Brick
|
|
190
222
|
pk = (add_fk[2][:cols].key?(add_fk[1]) && add_fk[1]) || '???'
|
191
223
|
end
|
192
224
|
# to_table column
|
193
|
-
mig << " #{'# ' if is_commented}add_foreign_key #{
|
225
|
+
mig << " #{'# ' if is_commented}add_foreign_key #{tbl_code}, #{add_fk[0]}, column: :#{add_fk[1]}, primary_key: :#{pk}\n"
|
194
226
|
end
|
195
227
|
mig << " end\nend\n"
|
196
228
|
current_mig_time += 1.minute
|
197
|
-
|
229
|
+
versions << (version = current_mig_time.strftime('%Y%m%d%H%M00'))
|
230
|
+
File.open("#{mig_path}/#{version}_create_#{full_table_name}.rb", "w") { |f| f.write mig }
|
198
231
|
end
|
199
232
|
done.concat(fringe)
|
200
233
|
chosen -= done
|
@@ -209,13 +242,17 @@ module Brick
|
|
209
242
|
if mig_path.start_with?(cur_path = ::Rails.root.to_s)
|
210
243
|
pretty_mig_path = mig_path[cur_path.length..-1]
|
211
244
|
end
|
212
|
-
puts "*** Created #{done.length} migration files under #{pretty_mig_path || mig_path} ***"
|
245
|
+
puts "\n*** Created #{done.length} migration files under #{pretty_mig_path || mig_path} ***"
|
213
246
|
if (stuck_sorted = stuck_counts.to_a.sort { |a, b| b.last <=> a.last }).length.positive?
|
214
247
|
puts "-----------------------------------------"
|
215
248
|
puts "Unable to create migrations for #{stuck_sorted.length} tables#{
|
216
249
|
". Here's the top 5 blockers" if stuck_sorted.length > 5
|
217
250
|
}:"
|
218
251
|
pp stuck_sorted[0..4]
|
252
|
+
else # Successful, and now we can update the schema_migrations table accordingly
|
253
|
+
ActiveRecord::Base.execute_sql("INSERT INTO #{ActiveRecord::Base.schema_migrations_table_name} (version) VALUES #{
|
254
|
+
versions.map { |version| "('#{version}')" }.join(', ')
|
255
|
+
}")
|
219
256
|
end
|
220
257
|
end
|
221
258
|
|