marty 1.0.14 → 1.0.15

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
  SHA1:
3
- metadata.gz: 9490119689a36383db480bd8b3f892904a08a3cf
4
- data.tar.gz: bc813657c712c00d9b26da1a20c7cb8332a0ffc8
3
+ metadata.gz: 443c5c011284c9a2ea3e10f62be61aca86b5ee27
4
+ data.tar.gz: c6bdf687627ec7971dabf549b7a8390c186a976a
5
5
  SHA512:
6
- metadata.gz: 3bfa1deaffa80e3a1587026420911090696aa1c5221921dbec985b894f1a9ae506234935d6f5dfbae51e4e9079556669c5c393bf76451087072e0867a326cb85
7
- data.tar.gz: ad7b61d596677b04ff6445122096103b520912022d79871254a881e8775ee5141728da76d141b3d77b3acfbf344539e027d4f21b54e8b8ec8f005e81246540f1
6
+ metadata.gz: 4cb6dc2818dc2990744feb4fc44a8d8d97f6637237555b40e5c00a9cfdaacbe9a93e3941c6c8c54bb3e21bbcd247cc6dbf4dfea4466cc0688248fc2217a97b7c
7
+ data.tar.gz: d15b0f44ed1d6825e38ac6626c27beb6b966741dade658d13eebf05f2001a214b6a20771bfc768983991309a411e16f883f566ceb66a052765772b5eb876431c
@@ -182,7 +182,74 @@ EOSQL
182
182
  end
183
183
  end
184
184
 
185
- private
185
+ def self.lines_to_crlf(lines)
186
+ lines.map do |line|
187
+ line.encode(line.encoding, :universal_newline => true).
188
+ encode(line.encoding, :crlf_newline => true)
189
+ end
190
+ end
191
+ def self.generate_sql_migrations(migrations_dir, sql_files_dir)
192
+ sd = Rails.root.join(sql_files_dir)
193
+ md = Rails.root.join(migrations_dir)
194
+ sql_files = Dir.glob("#{sd}/**/*.sql")
195
+ mig_files = Dir.glob("#{migrations_dir}/*.rb").map do |f|
196
+ m = /\A.*\/([0-9]+)_v([0-9]+)_sql_(.*)\.rb\z/.match(f)
197
+ { name: m[3],
198
+ timestamp: m[1],
199
+ version: m[2].to_i,
200
+ raw_sql: "#{md}/sql/#{m[1]}_v#{m[2]}_sql_#{m[3]}.sql"
201
+ }
202
+ end.group_by { |a| a[:name] }.each do |k, v|
203
+ v.sort! { |a, b| b[:version] <=> a[:version] }
204
+ end
205
+ time_now = Time.now.utc
206
+ gen_count = 0
207
+
208
+ sql_files.each do |sql|
209
+ base = File.basename(sql, ".sql")
210
+ existing = mig_files[base].first rescue nil
211
+ # must ensure CRLF line endings or SQL Server keep asking about line
212
+ # endings whenever you generating script
213
+ sql_lines = lines_to_crlf(File.open(sql, "r").readlines)
214
+ next if existing && sql_lines == File.open(existing[:raw_sql]).readlines
215
+
216
+ timestamp = (time_now + gen_count.seconds).strftime("%Y%m%d%H%M%S")
217
+ v = existing && existing[:version] + 1 || 1
218
+ klass = "v#{v}_sql_#{base}"
219
+ newbase = "#{timestamp}_#{klass}"
220
+ mig_name = File.join(md, "#{newbase}.rb")
221
+ sql_snap_literal = Rails.root.join(md, 'sql', "#{newbase}.sql")
222
+ sql_snap_call = "Rails.root.join('#{migrations_dir}', 'sql', '#{newbase}.sql')"
223
+
224
+ File.open(sql_snap_literal, "w") do |f|
225
+ f.print sql_lines.join
226
+ end
227
+ puts "creating #{newbase}.rb"
228
+
229
+ # only split on "GO" at the start of a line with optional whitespace
230
+ # before EOL. GO in comments could trigger this and will cause an error
231
+ File.open(mig_name, "w") do |f|
232
+ f.print <<OUT
233
+ class #{klass.camelcase} < ActiveRecord::Migration
234
+
235
+ def up
236
+ path = #{sql_snap_call}
237
+ batches = File.read(path).split(/^GO\\s*$/i)
238
+ batches.each { |batch| execute batch }
239
+ end
240
+
241
+ def down
242
+ announce('must rollback manually')
243
+ end
244
+
245
+ end
246
+ OUT
247
+ end
248
+ gen_count += 1
249
+ end
250
+ end
251
+
252
+ private
186
253
  def fk_opts(from, to, column)
187
254
  name = "fk_#{from}_#{to}_#{column}"
188
255
  if name.length > 63
data/lib/marty/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Marty
2
- VERSION = "1.0.14"
2
+ VERSION = "1.0.15"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marty
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.14
4
+ version: 1.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arman Bostani
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2016-11-18 00:00:00.000000000 Z
17
+ date: 2016-11-29 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: pg