pvcglue_dbutils 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OThlYzM5MzY4OTI1NTZlOGY3ZWYzNjhjZDZhMTQxYWNlYjE5NmYwOQ==
5
+ data.tar.gz: !binary |-
6
+ ODg4YzFiNzk1Y2M1OTNlYjJiZTc4Y2ZiNTQyMGI0OGI0YTcxYmFjZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YWEwZjJhMTg4OWI0N2Q2MzExNGRkMmRmYTA0ZDBlMzhiMjdiNTU3MzU1NWFj
10
+ MmVkMDVlYWMwNzM4ZThiYmEzNWMwYzEzODhlOWU1ZWNlNjVhOGExZDE0ZTNj
11
+ NjY1N2JlYmJlYmM2YjQ1MTBkMmIxN2UzNGJmZjQzZWUyODI2ODg=
12
+ data.tar.gz: !binary |-
13
+ MWE0NjhmMjIxN2E5OTFjNjIwZjA2MmRiZDcwYzE0MzhlMWIyMTZiYjI0Y2Rj
14
+ MWU0MWE3NGMwZmQzODA3OTZjMDBmYjU5MDgwMDQyZDAzYWJjZmEzNDgzY2Ez
15
+ YmI5NjQ5ZjhmYjVhNzI0Nzc1MTg5ZDMwZTUzNzk2ZjNkYTRhODA=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pvcglue_dbutils.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andrew Lyric
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # PvcglueDbutils
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pvcglue_dbutils'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pvcglue_dbutils
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/pvcglue_dbutils/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
30
+
31
+ License
32
+ -------
33
+
34
+ MIT License
35
+
36
+ Permission is hereby granted, free of charge, to any person obtaining
37
+ a copy of this software and associated documentation files (the
38
+ "Software"), to deal in the Software without restriction, including
39
+ without limitation the rights to use, copy, modify, merge, publish,
40
+ distribute, sublicense, and/or sell copies of the Software, and to
41
+ permit persons to whom the Software is furnished to do so, subject to
42
+ the following conditions:
43
+
44
+ The above copyright notice and this permission notice shall be
45
+ included in all copies or substantial portions of the Software.
46
+
47
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
51
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
52
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
53
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ require 'pvcglue_dbutils'
2
+ require 'rails'
3
+ module PvcglueDbutils
4
+ class Railtie < Rails::Railtie
5
+ railtie_name :pvcglue_dbutils
6
+
7
+ rake_tasks do
8
+ load "tasks/pvcglue_dbutils.rake"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module PvcglueDbutils
2
+ VERSION = "0.5.2"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "pvcglue_dbutils/version"
2
+
3
+ module PvcglueDbutils
4
+ require 'pvcglue_dbutils/railtie' if defined?(Rails)
5
+ end
@@ -0,0 +1,213 @@
1
+ namespace :db do
2
+ desc "Rebuild (drop, create, migrate) db. Then restore (or seed) and clone the test db. Will restore db/rebuild.sql by default. Use `rake db:rebuild[filename]` to restore db/filename.sql. Use `rake db:rebuild[-seed]` to instead load seed data."
3
+ task :rebuild, [:filename] do |t, args|
4
+ raise "Nope...Ain't gonna do it. Your request to drop the PRODUCTION database is denied." if Rails.env.production?
5
+ db = Rails.configuration.database_configuration[Rails.env]
6
+ puts "DB=#{db["database"]}"
7
+ # if something fails and you are unable to access the db, you can drop it with the following:
8
+ # /usr/bin/dropdb 'store-dev'
9
+ # /usr/bin/dropdb 'store-test'
10
+ # modified from http://stackoverflow.com/a/5408501/444774
11
+
12
+ puts "Forcibly disconnecting other processes from database...(You may need to restart them.)"
13
+
14
+ require 'active_record/connection_adapters/postgresql_adapter'
15
+ module ActiveRecord
16
+ module ConnectionAdapters
17
+ class PostgreSQLAdapter < AbstractAdapter
18
+ def drop_database(name)
19
+ raise "Nope...Ain't gonna do it. Your request to drop the PRODUCTION database is denied." if Rails.env.production?
20
+ begin
21
+ psql_version_num = execute "select setting from pg_settings where name = 'server_version_num'"
22
+ if psql_version_num.values.first.first.to_i < 90200
23
+ #puts "version < 9.2"
24
+ psql_version_pid_name = 'procpid'
25
+ else
26
+ #puts "version >= 9.2"
27
+ psql_version_pid_name = 'pid'
28
+ end
29
+
30
+ execute <<-SQL
31
+ UPDATE pg_catalog.pg_database
32
+ SET datallowconn=false WHERE datname='#{name}'
33
+ SQL
34
+
35
+ execute <<-SQL
36
+ SELECT pg_terminate_backend(pg_stat_activity.#{psql_version_pid_name})
37
+ FROM pg_stat_activity
38
+ WHERE pg_stat_activity.datname = '#{name}';
39
+ SQL
40
+
41
+ execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}"
42
+ ensure
43
+ execute <<-SQL
44
+ UPDATE pg_catalog.pg_database
45
+ SET datallowconn=true WHERE datname='#{name}'
46
+ SQL
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ puts "Dropping the db..."
53
+ Rake::Task['db:drop'].invoke
54
+ puts "Creating the db..."
55
+ Rake::Task['db:create'].invoke
56
+
57
+ if args.filename == "-seed"
58
+ puts "Migrating the db..."
59
+ Rake::Task['db:migrate'].invoke
60
+ puts "Seeding the db..."
61
+ Rake::Task['db:seed'].invoke
62
+ else
63
+ args.with_defaults(:filename => "rebuild")
64
+ puts "Restoring dump: #{args.filename}..."
65
+ Rake::Task['db:restore'].invoke(args.filename)
66
+ end
67
+
68
+ if Rails.env.development?
69
+ puts "Cloning the test db..."
70
+ Rake::Task['db:test:prepare'].invoke
71
+ end
72
+ puts "Done with DB=#{db["database"]}"
73
+
74
+ end
75
+
76
+ desc "Reload schema, then seed. (Does not try to drop and recreate db.)"
77
+ task :reload, [:filename] => :environment do |t, args|
78
+ raise "Nope...Ain't gonna do it. Your request to drop the PRODUCTION database is denied." if Rails.env.production?
79
+ puts "Reloading the db..."
80
+ Rake::Task['db:schema:load'].invoke
81
+ if args.filename == "seed!"
82
+ puts "Seeding the db..."
83
+ Rake::Task['db:seed'].invoke
84
+ else
85
+ args.with_defaults(:filename => "rebuild")
86
+ puts "Restoring dump: #{args.filename}..."
87
+ Rake::Task['db:restore'].invoke(args.filename)
88
+ end
89
+ puts "Done."
90
+ end
91
+
92
+ desc 'dump database (without schema_migrations) to *.sql using pg_dump. Ex: `rake db:backup[filename]` (filename is optional, default = "rebuild"). Use `rake db:backup[rebuild]` to use dump as default for `rake db:rebuild`.'
93
+ task :backup_data_only, :filename do |t, args|
94
+ db = Rails.configuration.database_configuration[Rails.env]
95
+ #cmd = "pg_dump -Fc --no-acl --no-owner -h #{db["host"]} -p #{db["port"]} -U #{db["username"]} #{db["database"]} > #{Rails.root}/db/#{args[:filename]}.dump"
96
+ cmd = "pg_dump -Fp --column-inserts --no-acl --no-owner --data-only -h #{db["host"]} -p #{db["port"]}"
97
+ cmd += " -U #{db["username"]}" unless db["username"].blank?
98
+ #cmd += " --exclude-table=schema_migrations"
99
+ cmd += " --exclude-table=cacheinators -T cacheinators_id_seq"
100
+ cmd += " #{db["database"]} > #{path_with_default(args[:filename])}"
101
+ puts cmd
102
+ unless system({"PGPASSWORD" => db["password"]}, cmd)
103
+ puts "ERROR:"
104
+ puts $?.inspect
105
+ end
106
+ end
107
+
108
+ desc 'dump database (with schema) to *.sql using pg_dump. Ex: `rake db:backup[filename]` (filename is optional, default = "rebuild"). Use `rake db:backup[rebuild]` to use dump as default for `rake db:rebuild`.'
109
+ task :backup, :filename do |t, args|
110
+ db = Rails.configuration.database_configuration[Rails.env]
111
+ #cmd = "pg_dump -Fp --column-inserts --no-acl --no-owner --clean -h #{db["host"]} -p #{db["port"]}"
112
+ cmd = "pg_dump -Fp --column-inserts --no-acl --no-owner -h #{db["host"]} -p #{db["port"]}"
113
+ cmd += " -U #{db["username"]}" unless db["username"].blank?
114
+ #cmd += " --exclude-table=cacheinators -T cacheinators_id_seq"
115
+ cmd += " #{db["database"]} > #{path_with_default(args[:filename])}"
116
+ puts cmd
117
+ unless system({"PGPASSWORD" => db["password"]}, cmd)
118
+ puts "ERROR:"
119
+ puts $?.inspect
120
+ end
121
+ end
122
+
123
+ #task :restore_zzz, :filename do |t, args|
124
+ # desc 'restore database from sql file. Ex: `rake db:backup[filename]` (filename is optional, default = "dump").'
125
+ # args.with_defaults(:filename => 'dump')
126
+ # db = Rails.configuration.database_configuration[Rails.env]
127
+ # ap db
128
+ # #cmd = "pg_restore --verbose --clean --no-acl --no-owner -h #{db["host"]} -p #{db["port"]} -U #{db["username"]} -d #{db["database"]} #{Rails.root}/db/#{args[:filename]}.dump"
129
+ # cmd = "psql -h #{db["host"]} -p #{db["port"]} -U #{db["username"]} -d #{db["database"]} -f #{Rails.root}/db/#{args[:filename]}.sql"
130
+ # puts cmd
131
+ # raise "Does not compute...destroying the production database is illogical. If you are *REALLY* sure, use the command above." if Rails.env.production?
132
+ # if system({"PGPASSWORD" => db["password"]}, cmd)
133
+ # puts
134
+ # puts "*"*80
135
+ # puts "Don't forget to restart the Rails server!"
136
+ # puts "*"*80
137
+ # puts
138
+ # else
139
+ # fail "ERROR: "+$?.inspect
140
+ # end
141
+ #end
142
+
143
+ desc 'restore database from sql file. Ex: `rake db:backup[filename]` (filename is optional, default = "rebuild").'
144
+ task :restore, [:filename] => :environment do |t, args|
145
+ raise "Does not compute...destroying the production database is illogical." if Rails.env.production? && !destroy_prod?
146
+ begin
147
+ #ActiveRecord::Base.connection.execute("DELETE FROM schema_migrations;\n#{File.open(path_with_default(args[:filename])).read}")
148
+ ActiveRecord::Base.connection.execute("#{filter_sql(File.open(path_with_default(args[:filename])).read)}")
149
+ rescue ActiveRecord::StatementInvalid => e
150
+ puts "\n\n\n** DATABASE ERROR *************************************************************"
151
+ puts e.message.truncate(1000)
152
+ raise "*"*80+"\nDB Reload Error :(\n"+"*"*80
153
+ end
154
+ end
155
+
156
+ desc 'restore database from sql file. Ex: `rake db:backup[filename]` (filename is optional, default = "dump").'
157
+ task :info, [:filename] => :environment do |t, args|
158
+ db = Rails.configuration.database_configuration[Rails.env]
159
+ ap db
160
+ end
161
+
162
+
163
+ def path_with_default(basename)
164
+ basename = 'rebuild' if basename.blank?
165
+ basename = File.basename(basename, '.*') # remove everything except basename
166
+ if basename == 'rebuild' && File.exist?(basename_to_path("#{basename}-#{Rails.env}"))
167
+ return basename_to_path("#{basename}-#{Rails.env}")
168
+ end
169
+ basename_to_path(basename)
170
+ end
171
+
172
+ def basename_to_path(basename)
173
+ "#{Rails.root}/db/#{basename}.sql"
174
+ end
175
+
176
+
177
+ def destroy_prod?
178
+ puts "Are you *REALLY* sure you want to DESTROY the PRODUCTION database?"
179
+ puts "Type 'destroy production' if you are."
180
+ STDOUT.flush
181
+ input = STDIN.gets.chomp
182
+ if input.downcase == "destroy production"
183
+ puts "ok, going through with the it..."
184
+ return true
185
+ else
186
+ raise "Nope...Ain't gonna do it. Your request to drop the PRODUCTION database is denied."
187
+ end
188
+ end
189
+
190
+ def filter_sql(sql)
191
+ raise "Nope...Ain't gonna do it. Your request to drop the PRODUCTION database is denied." if Rails.env.production?
192
+ sql = "DROP SCHEMA IF EXISTS public CASCADE;\nCREATE SCHEMA public;\n" + sql
193
+ sql.gsub!("CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;", '')
194
+ sql.gsub!("COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';", '')
195
+ #sql.gsub!(/(DROP) (INDEX|SEQUENCE|TABLE|SCHEMA|EXTENSION|CONSTRAINT) (.*);$/,'\1 \2 IF EXISTS \3;')
196
+ #sql.gsub(/(DROP|ALTER) (INDEX|SEQUENCE|TABLE|SCHEMA|EXTENSION|CONSTRAINT) (.*);$/,'\1 \2 IF EXISTS \3;')
197
+ sql
198
+ end
199
+ end
200
+
201
+ =begin
202
+
203
+ DROP INDEX public.unique_schema_migrations;
204
+ ALTER TABLE ONLY public.vendors DROP CONSTRAINT vendors_pkey;
205
+ ALTER TABLE public.vendors ALTER COLUMN id DROP DEFAULT;
206
+ DROP SEQUENCE public.adjusters_id_seq;
207
+ DROP TABLE public.adjusters;
208
+ DROP EXTENSION plpgsql;
209
+ DROP SCHEMA public;
210
+ --
211
+ -- Name: public; Type: SCHEMA; Schema: -; Owner: -
212
+ --
213
+ =end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pvcglue_dbutils/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pvcglue_dbutils"
8
+ spec.version = PvcglueDbutils::VERSION
9
+ spec.authors = ["Andrew Lyric"]
10
+ spec.email = ["talyric@gmail.com"]
11
+ spec.summary = %q{Write a short summary. Required.}
12
+ spec.description = %q{Write a longer description. Optional.}
13
+ spec.homepage = "https://github.com/talyric/pvcglue_dbutils"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", '>= 1.3.0', '< 2.0'
22
+ spec.add_development_dependency "rake", '~> 10.1'
23
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pvcglue_dbutils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.2
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Lyric
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.0
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.0
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '10.1'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '10.1'
47
+ description: Write a longer description. Optional.
48
+ email:
49
+ - talyric@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/pvcglue_dbutils.rb
60
+ - lib/pvcglue_dbutils/railtie.rb
61
+ - lib/pvcglue_dbutils/version.rb
62
+ - lib/tasks/pvcglue_dbutils.rake
63
+ - pvcglue_dbutils.gemspec
64
+ homepage: https://github.com/talyric/pvcglue_dbutils
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.1
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Write a short summary. Required.
88
+ test_files: []