brick 1.0.57 → 1.0.58
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 +32 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 508d41fa8b75089e0467ece56ab53fc78b651f3951eabe62d108a62fa8b0f35e
|
4
|
+
data.tar.gz: dcd419560d850fb2014aab3076a6a80f5a555320ac2c2183e3381a916d94dbc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49c90c7fbb1845e151f91b9df7f3b639d587e6239dae5e7f31306807a2b4dc7ac83436d78d46839accfea7c614fd2a94f2460443ea3daae424d280fae17cac77
|
7
|
+
data.tar.gz: 7dd02fd9ab902cfe6359b6be95101ec3a89c47d8b7bc71692ab4537f5ffeb3a24a4ee73063fc10b3d4197ce59fd5c94380f7534f66647d8c03dd512c6ef0c334
|
data/lib/brick/version_number.rb
CHANGED
@@ -84,8 +84,14 @@ module Brick
|
|
84
84
|
|
85
85
|
# Generate a list of tables that can be chosen
|
86
86
|
chosen = gets_list(list: tables, chosen: tables.dup)
|
87
|
+
schemas = chosen.each_with_object({}) do |v, s|
|
88
|
+
schema = if v.split('.').length > 1
|
89
|
+
v.first
|
90
|
+
end
|
91
|
+
s[schema] = nil if schema && [::Brick.default_schema, 'public'].exclude?(schema)
|
92
|
+
end
|
87
93
|
# Start the timestamps back the same number of minutes from now as expected number of migrations to create
|
88
|
-
current_mig_time = Time.now - chosen.length.minutes
|
94
|
+
current_mig_time = Time.now - (schemas.length + chosen.length).minutes
|
89
95
|
done = []
|
90
96
|
fks = {}
|
91
97
|
stuck = {}
|
@@ -124,9 +130,14 @@ module Brick
|
|
124
130
|
tbl_parts.first
|
125
131
|
end
|
126
132
|
end
|
133
|
+
unless built_schemas.key?(schema)
|
134
|
+
mig = +" def change\n create_schema(:#{schema}) unless schema_exists?(:#{schema})\n end\n"
|
135
|
+
migration_file_write(mig_path, "create_db_schema_#{schema}", current_mig_time += 1.minute, ar_version, mig)
|
136
|
+
built_schemas[schema] = nil
|
137
|
+
end
|
138
|
+
|
127
139
|
# %%% For the moment we're skipping polymorphics
|
128
140
|
fkey_cols = relation[:fks].values.select { |assoc| assoc[:is_bt] && !assoc[:polymorphic] }
|
129
|
-
mig = +"class Create#{(full_table_name = tbl_parts.join('_')).camelize} < ActiveRecord::Migration#{ar_version}\n"
|
130
141
|
# If the primary key is also used as a foreign key, will need to do id: false and then build out
|
131
142
|
# a column definition which includes :primary_key -- %%% also using a data type of bigserial or serial
|
132
143
|
# if this one has come in as bigint or integer.
|
@@ -163,8 +174,7 @@ module Brick
|
|
163
174
|
end
|
164
175
|
# Refer to this table name as a symbol or dotted string as appropriate
|
165
176
|
tbl_code = tbl_parts.length == 1 ? ":#{tbl_parts.first}" : "'#{tbl}'"
|
166
|
-
mig
|
167
|
-
mig << " create_schema :#{schema} unless reverting? || schema_exists?(:#{schema})\n" if schema
|
177
|
+
mig = +" def change\n return unless reverting? || !table_exists?(#{tbl_code})\n\n"
|
168
178
|
mig << " create_table #{tbl_code}#{id_option} do |t|\n"
|
169
179
|
possible_ts = [] # Track possible generic timestamps
|
170
180
|
add_fks = [] # Track foreign keys to add after table creation
|
@@ -217,7 +227,11 @@ module Brick
|
|
217
227
|
possible_ts.each { |ts| emit_column('timestamp', ts.first, nil) }
|
218
228
|
end
|
219
229
|
mig << " end\n"
|
220
|
-
|
230
|
+
if pk_is_also_fk
|
231
|
+
mig << " reversible do |dir|\n"
|
232
|
+
mig << " dir.up { execute('ALTER TABLE #{tbl} ADD PRIMARY KEY (#{pk_is_also_fk})') }\n"
|
233
|
+
mig << " end\n"
|
234
|
+
end
|
221
235
|
add_fks.each do |add_fk|
|
222
236
|
is_commented = false
|
223
237
|
# add_fk[2] holds the inverse relation
|
@@ -227,21 +241,16 @@ module Brick
|
|
227
241
|
# No official PK, but if coincidentally there's a column of the same name, take a chance on it
|
228
242
|
pk = (add_fk[2][:cols].key?(add_fk[1]) && add_fk[1]) || '???'
|
229
243
|
end
|
230
|
-
#
|
244
|
+
# to_table column
|
231
245
|
mig << " #{'# ' if is_commented}add_foreign_key #{tbl_code}, #{add_fk[0]}, column: :#{add_fk[1]}, primary_key: :#{pk}\n"
|
232
246
|
end
|
233
|
-
|
234
|
-
|
235
|
-
built_schemas[schema] = nil
|
236
|
-
end
|
237
|
-
mig << " end\nend\n"
|
238
|
-
current_mig_time += 1.minute
|
239
|
-
versions_to_create << (version = current_mig_time.strftime('%Y%m%d%H%M00')).split('_').first
|
240
|
-
File.open("#{mig_path}/#{version}_create_#{full_table_name}.rb", "w") { |f| f.write mig }
|
247
|
+
mig << " end\n"
|
248
|
+
versions_to_create << migration_file_write(mig_path, "create_#{tbl_parts.join('_')}", current_mig_time += 1.minute, ar_version, mig)
|
241
249
|
end
|
242
250
|
done.concat(fringe)
|
243
251
|
chosen -= done
|
244
252
|
end
|
253
|
+
|
245
254
|
stuck_counts = Hash.new { |h, k| h[k] = 0 }
|
246
255
|
chosen.each do |leftover|
|
247
256
|
puts "Can't do #{leftover} because:\n #{stuck[leftover].map do |snag|
|
@@ -285,5 +294,14 @@ module Brick
|
|
285
294
|
def emit_column(type, name, suffix)
|
286
295
|
" t.#{type.start_with?('numeric') ? 'decimal' : type} :#{name}#{suffix}\n"
|
287
296
|
end
|
297
|
+
|
298
|
+
def migration_file_write(mig_path, name, current_mig_time, ar_version, mig)
|
299
|
+
File.open("#{mig_path}/#{version = current_mig_time.strftime('%Y%m%d%H%M00')}_#{name}.rb", "w") do |f|
|
300
|
+
f.write "class #{name.camelize} < ActiveRecord::Migration#{ar_version}\n"
|
301
|
+
f.write mig
|
302
|
+
f.write "end\n"
|
303
|
+
end
|
304
|
+
version
|
305
|
+
end
|
288
306
|
end
|
289
307
|
end
|
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.
|
4
|
+
version: 1.0.58
|
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-08-
|
11
|
+
date: 2022-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|