db-evolve 0.2.1 → 0.2.2
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 +28 -31
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 968ab94490eb008b3eedcd6b83ad0042cc4812f5
|
4
|
+
data.tar.gz: 5c41d69b55474bb81f0fb7829d6fd5604d5bb193
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 059920a90809c21cf69c76183c433649f12a8425069ff1ad428858176fe693c3e6a43e019fd8dbb168492aad499f89771bfcd22ca7e707bb5050b788ec8f81ee
|
7
|
+
data.tar.gz: 1f9adcf24f2c40df0f427651c5f1e05db406863b89251ce74eb4394af8bbf4d8ea08acfd7b228a5a4e35029065404b0cc64535ae1472af13cab106a3fcd69222
|
data/lib/tasks/db.rb
CHANGED
@@ -47,33 +47,36 @@ namespace :db do
|
|
47
47
|
|
48
48
|
end
|
49
49
|
|
50
|
+
|
50
51
|
$i_nagged = false
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
else
|
59
|
-
config = HashWithIndifferentAccess.new ActiveRecord::Base.connection_config
|
60
|
-
unless $i_nagged || noop || Rails.env=='development'
|
61
|
-
puts "Your database.yml file does not contain an entry for '#{config_name}', so we're using '#{Rails.env}'. This works if your database user has permission to edit your schema, but this is not recommended outside of development. For more information visit: https://github.com/keredson/ruby-db-evolve/blob/master/README.md#schema-change-permissions"
|
62
|
-
$i_nagged = true
|
63
|
-
end
|
64
|
-
end
|
53
|
+
$db_username = HashWithIndifferentAccess.new(Rails.configuration.database_configuration[Rails.env])[:username] || ENV['USER'] || ENV['USERNAME']
|
54
|
+
|
55
|
+
def get_connection_config
|
56
|
+
config_name = "#{Rails.env}_dbevolve"
|
57
|
+
if Rails.configuration.database_configuration[config_name].present?
|
58
|
+
config = Rails.configuration.database_configuration[config_name]
|
65
59
|
else
|
66
|
-
|
60
|
+
unless $i_nagged || Rails.env=='development'
|
61
|
+
puts "Your database.yml file does not contain an entry for '#{config_name}', so we're using '#{Rails.env}'. This works if your database user has permission to edit your schema, but this is not recommended outside of development. For more information visit: https://github.com/keredson/ruby-db-evolve/blob/master/README.md#schema-change-permissions"
|
62
|
+
$i_nagged = true
|
63
|
+
end
|
64
|
+
config = Rails.configuration.database_configuration[Rails.env]
|
67
65
|
end
|
66
|
+
return config
|
67
|
+
end
|
68
|
+
|
69
|
+
def build_pg_connection_config
|
70
|
+
config = HashWithIndifferentAccess.new get_connection_config
|
68
71
|
config.delete(:adapter)
|
69
72
|
config.delete(:pool)
|
70
73
|
config[:dbname] = config.delete(:database)
|
71
74
|
config[:user] = config.delete(:username) || ENV['USER'] || ENV['USERNAME']
|
72
|
-
return config
|
75
|
+
return config
|
73
76
|
end
|
74
77
|
|
75
|
-
def
|
76
|
-
return PG::Connection.open(
|
78
|
+
def build_pg_connection
|
79
|
+
return PG::Connection.open(build_pg_connection_config)
|
77
80
|
end
|
78
81
|
|
79
82
|
def do_evolve(noop, yes, nowait)
|
@@ -127,9 +130,8 @@ def do_evolve(noop, yes, nowait)
|
|
127
130
|
return
|
128
131
|
end
|
129
132
|
|
130
|
-
config = build_real_connection_config to_exec: true
|
131
133
|
puts "Connecting to database:"
|
132
|
-
|
134
|
+
build_pg_connection_config.each do |k,v|
|
133
135
|
v = "*" * v.length if k.present? && k.to_s=='password'
|
134
136
|
puts "\t#{k} => #{v}"
|
135
137
|
end
|
@@ -146,7 +148,7 @@ def do_evolve(noop, yes, nowait)
|
|
146
148
|
end
|
147
149
|
end
|
148
150
|
puts
|
149
|
-
conn =
|
151
|
+
conn = build_pg_connection
|
150
152
|
to_run.each do |sql|
|
151
153
|
puts SQLColor.colorize(sql)
|
152
154
|
conn.exec(sql)
|
@@ -197,13 +199,7 @@ def calc_index_changes(existing_indexes, schema_indexes, table_renames, rename_c
|
|
197
199
|
return to_run
|
198
200
|
end
|
199
201
|
|
200
|
-
def get_db_username
|
201
|
-
username = ActiveRecord::Base.connection_config[:username] || ENV['USER'] || ENV['USERNAME']
|
202
|
-
return username
|
203
|
-
end
|
204
|
-
|
205
202
|
def calc_perms_changes schema_tables, noop
|
206
|
-
username = get_db_username
|
207
203
|
users = ($check_perms_for.map { |user| ActiveRecord::Base::sanitize(user) }).join ","
|
208
204
|
database = ActiveRecord::Base.connection_config[:database]
|
209
205
|
sql = %{
|
@@ -213,7 +209,7 @@ def calc_perms_changes schema_tables, noop
|
|
213
209
|
and grantee in (#{users})
|
214
210
|
and table_schema='public';
|
215
211
|
}
|
216
|
-
results =
|
212
|
+
results = build_pg_connection.exec(sql)
|
217
213
|
existing_perms = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = Set.new } }
|
218
214
|
results.each do |row|
|
219
215
|
existing_perms[row['grantee']][row['table_name']].add(row['privilege_type'])
|
@@ -241,6 +237,7 @@ IgnoreTables = Set.new ["schema_migrations"]
|
|
241
237
|
def load_existing_tables()
|
242
238
|
existing_tables = {}
|
243
239
|
existing_indexes = []
|
240
|
+
ActiveRecord::Base.establish_connection(get_connection_config)
|
244
241
|
connection = ActiveRecord::Base.connection
|
245
242
|
connection.tables.sort.each do |tbl|
|
246
243
|
next if IgnoreTables.include? tbl
|
@@ -265,7 +262,7 @@ class Table
|
|
265
262
|
end
|
266
263
|
|
267
264
|
def grant *args, to: nil
|
268
|
-
to =
|
265
|
+
to = $db_username if to.nil?
|
269
266
|
$check_perms_for.add(to)
|
270
267
|
args.each do |arg|
|
271
268
|
@perms_for_user[to] |= check_perm(arg)
|
@@ -273,7 +270,7 @@ class Table
|
|
273
270
|
end
|
274
271
|
|
275
272
|
def revoke *args, from: nil
|
276
|
-
from =
|
273
|
+
from = $db_username if from.nil?
|
277
274
|
$check_perms_for.add(from)
|
278
275
|
args.each do |arg|
|
279
276
|
@perms_for_user[from] -= check_perm(arg)
|
@@ -369,7 +366,7 @@ def check_perm perm
|
|
369
366
|
end
|
370
367
|
|
371
368
|
def grant(*perms, to: nil)
|
372
|
-
to =
|
369
|
+
to = $db_username if to.nil?
|
373
370
|
$check_perms_for.add(to)
|
374
371
|
perms.each do |perm|
|
375
372
|
$default_perms_for[to] |= check_perm(perm)
|
@@ -377,7 +374,7 @@ def grant(*perms, to: nil)
|
|
377
374
|
end
|
378
375
|
|
379
376
|
def revoke(*perms, from: nil)
|
380
|
-
from =
|
377
|
+
from = $db_username if from.nil?
|
381
378
|
$check_perms_for.add(from)
|
382
379
|
perms.each do |perm|
|
383
380
|
$default_perms_for[from] -= check_perm(perm)
|
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.2.
|
4
|
+
version: 0.2.2
|
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-03-
|
11
|
+
date: 2016-03-03 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.
|