db-evolve 0.3.5 → 0.3.6
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 +4 -4
- data/lib/tasks/db.rb +38 -39
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0ce9f6bef328a319d0049de5acb60cf9237f004
|
4
|
+
data.tar.gz: 612f563637bd68c9014ef6fcd442ce6ca7b4ba65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a0b61222546474c60c28b1333ecb819c79d25a9bc406cbb333924af5c8b550a29d25b7b5d75da56d9292edb898062d786fbcfab3825e6be0414ea22efcf708e
|
7
|
+
data.tar.gz: f04a64664b193312bf30b85cc8a850a7eef17afd555b9ea7886ee89f4ab0a7433302b1244918e1fa222f5ed1d90fbeab373e6d443c219fab4ec51259cfd800a2
|
data/lib/tasks/db.rb
CHANGED
@@ -19,15 +19,15 @@ namespace :db do
|
|
19
19
|
|
20
20
|
desc "Diff your database against your schema.rb and offer SQL to bring your database up to date."
|
21
21
|
task :evolve, [:arg1,:arg2] => :environment do |t, args|
|
22
|
-
|
23
|
-
argv = [ args[:arg1], args[:arg2] ]
|
22
|
+
|
23
|
+
argv = [ args[:arg1], args[:arg2] ]
|
24
24
|
noop = argv.include? "noop"
|
25
25
|
nowait = argv.include? "nowait"
|
26
26
|
yes = argv.include? "yes"
|
27
|
-
|
27
|
+
|
28
28
|
# confirm our shim is in place before we load schema.rb
|
29
29
|
# lest we accidentally drop and reload their database!
|
30
|
-
ActiveRecord::Schema.iloaded
|
30
|
+
ActiveRecord::Schema.iloaded
|
31
31
|
|
32
32
|
do_evolve(noop, yes, nowait)
|
33
33
|
|
@@ -84,7 +84,7 @@ def do_evolve(noop, yes, nowait)
|
|
84
84
|
server_version = load_server_version()
|
85
85
|
|
86
86
|
require_relative 'db_mock'
|
87
|
-
|
87
|
+
|
88
88
|
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.existing_tables = existing_tables
|
89
89
|
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.server_version = server_version
|
90
90
|
|
@@ -106,7 +106,7 @@ def do_evolve(noop, yes, nowait)
|
|
106
106
|
to_run += commands
|
107
107
|
rename_cols_by_table[ntn] = rename_cols
|
108
108
|
end
|
109
|
-
|
109
|
+
|
110
110
|
to_run += calc_index_changes(existing_indexes, $schema_indexes, renames, rename_cols_by_table)
|
111
111
|
|
112
112
|
to_run += calc_fk_changes($foreign_keys, Set.new(existing_tables.keys), renames)
|
@@ -116,7 +116,7 @@ def do_evolve(noop, yes, nowait)
|
|
116
116
|
to_run += sql_drops(deletes)
|
117
117
|
|
118
118
|
# prompt and execute
|
119
|
-
|
119
|
+
|
120
120
|
if to_run.empty?
|
121
121
|
if !noop
|
122
122
|
puts "\nYour database is up to date!"
|
@@ -141,7 +141,7 @@ def do_evolve(noop, yes, nowait)
|
|
141
141
|
v = "*" * v.length if k.present? && k.to_s=='password'
|
142
142
|
puts "\t#{k} => #{v}"
|
143
143
|
end
|
144
|
-
|
144
|
+
|
145
145
|
if !yes
|
146
146
|
print "Run this SQL? (type yes or no) "
|
147
147
|
end
|
@@ -179,33 +179,31 @@ def calc_index_changes(existing_indexes, schema_indexes, table_renames, rename_c
|
|
179
179
|
end
|
180
180
|
end
|
181
181
|
schema_indexes = Set.new schema_indexes
|
182
|
-
|
182
|
+
|
183
183
|
add_indexes = schema_indexes - existing_indexes
|
184
184
|
delete_indexes = existing_indexes - schema_indexes
|
185
185
|
|
186
|
-
$tmp_to_run = []
|
186
|
+
$tmp_to_run = []
|
187
187
|
|
188
188
|
connection = ActiveRecord::Base.connection
|
189
189
|
|
190
|
+
delete_indexes.each do |index|
|
191
|
+
table = index.delete(:table)
|
192
|
+
name = index[:name]
|
193
|
+
$tmp_to_run << "DROP INDEX IF EXISTS #{escape_table(name)}"
|
194
|
+
end
|
195
|
+
|
190
196
|
add_indexes.each do |index|
|
191
197
|
table = index.delete(:table)
|
192
198
|
columns = index.delete(:columns)
|
193
199
|
connection.add_index table, columns, index
|
194
200
|
end
|
195
|
-
|
196
|
-
to_run = $tmp_to_run
|
197
201
|
|
198
|
-
|
199
|
-
|
200
|
-
name = index[:name]
|
201
|
-
to_run << "DROP INDEX IF EXISTS #{escape_table(name)}"
|
202
|
+
if !$tmp_to_run.empty?
|
203
|
+
$tmp_to_run.unshift("\n-- update indexes")
|
202
204
|
end
|
203
205
|
|
204
|
-
|
205
|
-
to_run.unshift("\n-- update indexes")
|
206
|
-
end
|
207
|
-
|
208
|
-
return to_run
|
206
|
+
return $tmp_to_run
|
209
207
|
end
|
210
208
|
|
211
209
|
def calc_fk_changes(foreign_keys, existing_tables, renames)
|
@@ -214,11 +212,11 @@ def calc_fk_changes(foreign_keys, existing_tables, renames)
|
|
214
212
|
existing_tables_sql = (existing_tables.map {|tn| ActiveRecord::Base.sanitize(tn)}).join(',')
|
215
213
|
sql = %{
|
216
214
|
SELECT
|
217
|
-
tc.constraint_name, tc.table_name, kcu.column_name,
|
215
|
+
tc.constraint_name, tc.table_name, kcu.column_name,
|
218
216
|
ccu.table_name AS foreign_table_name,
|
219
|
-
ccu.column_name AS foreign_column_name
|
220
|
-
FROM
|
221
|
-
information_schema.table_constraints AS tc
|
217
|
+
ccu.column_name AS foreign_column_name
|
218
|
+
FROM
|
219
|
+
information_schema.table_constraints AS tc
|
222
220
|
JOIN information_schema.key_column_usage AS kcu
|
223
221
|
ON tc.constraint_name = kcu.constraint_name
|
224
222
|
JOIN information_schema.constraint_column_usage AS ccu
|
@@ -245,7 +243,7 @@ def calc_fk_changes(foreign_keys, existing_tables, renames)
|
|
245
243
|
foreign_keys = Set.new foreign_keys
|
246
244
|
add_fks = foreign_keys - existing_foreign_keys
|
247
245
|
delete_fks = existing_foreign_keys - foreign_keys
|
248
|
-
|
246
|
+
|
249
247
|
rename_fks = []
|
250
248
|
delete_fks.each do |delete_fk|
|
251
249
|
dfk = delete_fk.clone
|
@@ -311,7 +309,7 @@ def calc_perms_changes schema_tables, noop
|
|
311
309
|
if !to_run.empty?
|
312
310
|
to_run.unshift("\n-- update permissions")
|
313
311
|
end
|
314
|
-
|
312
|
+
|
315
313
|
return to_run
|
316
314
|
end
|
317
315
|
|
@@ -328,7 +326,7 @@ def load_existing_tables()
|
|
328
326
|
columns = connection.columns(tbl)
|
329
327
|
existing_tables[tbl] = columns
|
330
328
|
connection.indexes(tbl).each do |i|
|
331
|
-
index = {:table => i.table, :name => i.name, :columns => i.columns, :unique => i.unique}
|
329
|
+
index = {:table => i.table, :name => i.name, :columns => i.columns, :unique => i.unique, using: i.using}
|
332
330
|
existing_indexes.append(index)
|
333
331
|
end
|
334
332
|
end
|
@@ -344,11 +342,11 @@ end
|
|
344
342
|
|
345
343
|
class Table
|
346
344
|
attr_accessor :name, :opts, :id, :columns, :perms_for_user
|
347
|
-
|
345
|
+
|
348
346
|
def initialize()
|
349
347
|
@columns = []
|
350
348
|
end
|
351
|
-
|
349
|
+
|
352
350
|
def grant *args, to: nil
|
353
351
|
to = $db_username if to.nil?
|
354
352
|
$check_perms_for.add(to)
|
@@ -356,7 +354,7 @@ class Table
|
|
356
354
|
@perms_for_user[to] |= check_perm(arg)
|
357
355
|
end
|
358
356
|
end
|
359
|
-
|
357
|
+
|
360
358
|
def revoke *args, from: nil
|
361
359
|
from = $db_username if from.nil?
|
362
360
|
$check_perms_for.add(from)
|
@@ -364,7 +362,7 @@ class Table
|
|
364
362
|
@perms_for_user[from] -= check_perm(arg)
|
365
363
|
end
|
366
364
|
end
|
367
|
-
|
365
|
+
|
368
366
|
def method_missing(method_sym, *arguments, &block)
|
369
367
|
c = Column.new
|
370
368
|
c.type = method_sym.to_s
|
@@ -394,8 +392,8 @@ class Table
|
|
394
392
|
c.opts = {}
|
395
393
|
end
|
396
394
|
@columns.append c
|
397
|
-
end
|
398
|
-
|
395
|
+
end
|
396
|
+
|
399
397
|
end
|
400
398
|
|
401
399
|
class Column
|
@@ -448,6 +446,8 @@ def add_index(table, columns, opts)
|
|
448
446
|
if !opts.has_key? :unique
|
449
447
|
opts[:unique] = false
|
450
448
|
end
|
449
|
+
# this is the default index type for mysql and postgres
|
450
|
+
opts[:using] = :btree unless opts.has_key?(:using)
|
451
451
|
$schema_indexes.append(opts)
|
452
452
|
end
|
453
453
|
|
@@ -623,7 +623,7 @@ def calc_column_changes(tbl, existing_cols, schema_cols)
|
|
623
623
|
end
|
624
624
|
end
|
625
625
|
end
|
626
|
-
|
626
|
+
|
627
627
|
to_run = []
|
628
628
|
|
629
629
|
pg_a = gen_pg_adapter()
|
@@ -636,7 +636,7 @@ def calc_column_changes(tbl, existing_cols, schema_cols)
|
|
636
636
|
end
|
637
637
|
to_run += $tmp_to_run
|
638
638
|
end
|
639
|
-
|
639
|
+
|
640
640
|
$tmp_to_run = []
|
641
641
|
rename_cols.each do |ecn, scn|
|
642
642
|
pg_a.rename_column(tbl, ecn, scn)
|
@@ -645,7 +645,7 @@ def calc_column_changes(tbl, existing_cols, schema_cols)
|
|
645
645
|
delete_cols.each do |cn|
|
646
646
|
to_run.append("ALTER TABLE #{escape_table(tbl)} DROP COLUMN #{escape_table(cn)}")
|
647
647
|
end
|
648
|
-
|
648
|
+
|
649
649
|
same_names = existing_col_names - delete_cols
|
650
650
|
same_names.each do |ecn|
|
651
651
|
$tmp_to_run = []
|
@@ -692,7 +692,7 @@ def calc_column_changes(tbl, existing_cols, schema_cols)
|
|
692
692
|
if !to_run.empty?
|
693
693
|
to_run.unshift("\n-- column changes for table #{tbl}")
|
694
694
|
end
|
695
|
-
|
695
|
+
|
696
696
|
return to_run, rename_cols
|
697
697
|
end
|
698
698
|
|
@@ -718,4 +718,3 @@ class String
|
|
718
718
|
end
|
719
719
|
|
720
720
|
$tmp_to_run = []
|
721
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db-evolve
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Derek Anderson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A diff/patch-esque tool to replace schema migrations in Ruby. See https://github.com/keredson/ruby-db-evolve
|
14
14
|
for details.
|
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
41
|
version: '0'
|
42
42
|
requirements: []
|
43
43
|
rubyforge_project:
|
44
|
-
rubygems_version: 2.
|
44
|
+
rubygems_version: 2.5.1
|
45
45
|
signing_key:
|
46
46
|
specification_version: 4
|
47
47
|
summary: Schema Evolution for Ruby
|