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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tasks/db.rb +28 -31
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea154aecfe4a0ae8ae9975253ba9d140568caacf
4
- data.tar.gz: ab4746847692403c025bd15ec1cc2480ca3dc3a8
3
+ metadata.gz: 968ab94490eb008b3eedcd6b83ad0042cc4812f5
4
+ data.tar.gz: 5c41d69b55474bb81f0fb7829d6fd5604d5bb193
5
5
  SHA512:
6
- metadata.gz: 538855b9cdd6658d21054e37d00489f6518210fb2641a6acb38bf12a13baaa2baa84fae5bf76ec17a5e2970b72ff19c8bf0b439481f8848599062092c25bb37d
7
- data.tar.gz: ed4b7536727c1e151841ea72426fff431e7709813dd1160e7f6a54441af1e6b6fe5ff2c33411df874004896f3e58e6aa6c4b27f2c8ebc52c93dfcc41e0d17307
6
+ metadata.gz: 059920a90809c21cf69c76183c433649f12a8425069ff1ad428858176fe693c3e6a43e019fd8dbb168492aad499f89771bfcd22ca7e707bb5050b788ec8f81ee
7
+ data.tar.gz: 1f9adcf24f2c40df0f427651c5f1e05db406863b89251ce74eb4394af8bbf4d8ea08acfd7b228a5a4e35029065404b0cc64535ae1472af13cab106a3fcd69222
@@ -47,33 +47,36 @@ namespace :db do
47
47
 
48
48
  end
49
49
 
50
+
50
51
  $i_nagged = false
51
52
 
52
- def build_real_connection_config to_exec: false, noop: false
53
- require 'pg'
54
- if to_exec
55
- config_name = "#{Rails.env}_dbevolve"
56
- if Rails.configuration.database_configuration[config_name].present?
57
- config = HashWithIndifferentAccess.new Rails.configuration.database_configuration[config_name]
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
- config = HashWithIndifferentAccess.new ActiveRecord::Base.connection_config
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 build_real_connection to_exec: false, noop: false
76
- return PG::Connection.open(build_real_connection_config to_exec: to_exec, noop: noop)
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
- config.each do |k,v|
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 = build_real_connection to_exec: true
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 = build_real_connection(noop: noop).exec(sql)
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 = get_db_username if to.nil?
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 = get_db_username if from.nil?
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 = get_db_username if to.nil?
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 = get_db_username if from.nil?
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.1
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-01 00:00:00.000000000 Z
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.