pg_tasks 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c836d8fe98ae36885760913a6f2b714b9aab5357
4
+ data.tar.gz: de74fe7101993253af9ff86ed5006e8b724f7a71
5
+ SHA512:
6
+ metadata.gz: 62c6bdea7f5012861c484d295f05dc57df2332145be0457e4c8920dbda1904bad6525bb07e5fb314816d00f7c1b9271748b15804c76c4cab8d5d52a44427c0f8
7
+ data.tar.gz: 62a5064c34861ceff2a6726ac1e828bf8feb3f6149c47782022c9df75a8765bc5f1914fe009f3b47f924270498d3fdd23bddafb5e3fea35dff2068ace6dd3421
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Thomas Schank
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'PgTasks'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+ Bundler::GemHelper.install_tasks
19
+
@@ -0,0 +1,10 @@
1
+ require 'pg_tasks'
2
+ require 'rails'
3
+ module PgTasks
4
+ class Railtie < Rails::Railtie
5
+ railtie_name :pg_tasks
6
+ rake_tasks do
7
+ load 'tasks/pg_tasks.rake'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module PgTasks
2
+ VERSION = "1.0.0"
3
+ end
data/lib/pg_tasks.rb ADDED
@@ -0,0 +1,136 @@
1
+ require 'active_record/tasks/database_tasks'
2
+ require 'active_record/tasks/postgresql_database_tasks'
3
+
4
+ module PgTasks
5
+
6
+ require 'pg_tasks/railtie' if defined?(Rails)
7
+
8
+ DEFAULT_BINARY_DATA_FILE_NAME = 'data.pgbin'
9
+ DEFAULT_BINARY_STRUCTURE_AND_DATA_FILE_NAME = 'structure_and_data.pgbin'
10
+
11
+ class << self
12
+
13
+ def reload!
14
+ load Rails.root.join(__FILE__)
15
+ end
16
+
17
+ %w(data_dump data_restore).each do |method_name|
18
+ define_method method_name do |filename = nil|
19
+ ActiveRecord::Tasks::DatabaseTasks \
20
+ .perform_pg_db_task_for_config_and_filename \
21
+ method_name, current_config,
22
+ filename_or_default_binary_data_file(filename)
23
+ end
24
+ end
25
+
26
+ %w(structure_and_data_dump structure_and_data_restore).each do |method_name|
27
+ define_method method_name do |filename = nil|
28
+ ActiveRecord::Tasks::DatabaseTasks \
29
+ .perform_pg_db_task_for_config_and_filename \
30
+ method_name, current_config,
31
+ filename_or_default_binary_structure_and_data_file(filename)
32
+ end
33
+ end
34
+
35
+ def truncate_tables
36
+ ActiveRecord::Base.connection.tap do |connection|
37
+ connection.tables.reject { |tn| tn == 'schema_migrations' }
38
+ .join(', ').tap do |tables|
39
+ connection.execute " TRUNCATE TABLE #{tables} CASCADE; "
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def current_config
47
+ ActiveRecord::Tasks::DatabaseTasks.current_config
48
+ end
49
+
50
+ def filename_or_default_binary_data_file(filename)
51
+ (filename.present? && filename) || \
52
+ File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir,
53
+ DEFAULT_BINARY_DATA_FILE_NAME)
54
+ end
55
+
56
+ def filename_or_default_binary_structure_and_data_file(filename)
57
+ (filename.present? && filename) || \
58
+ File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir,
59
+ DEFAULT_BINARY_STRUCTURE_AND_DATA_FILE_NAME)
60
+ end
61
+
62
+ end
63
+ end
64
+
65
+ module ActiveRecord
66
+ module Tasks
67
+ class PostgreSQLDatabaseTasks
68
+
69
+ def data_dump(filename)
70
+ set_psql_env
71
+ command = 'pg_dump -F c -a -T schema_migrations -x -O -f ' \
72
+ "#{Shellwords.escape(filename)} " \
73
+ "#{Shellwords.escape(configuration['database'])}"
74
+ raise 'Error during data_dump' unless Kernel.system(command)
75
+ $stdout.puts "The data of '#{configuration['database']} " \
76
+ "has been dumped to '#{filename}'"
77
+ end
78
+
79
+ def data_restore(filename)
80
+ set_psql_env
81
+ command = 'pg_restore --disable-triggers -a -x -O -d ' \
82
+ "#{Shellwords.escape(configuration['database'])} " \
83
+ "#{Shellwords.escape(filename)}"
84
+ raise 'Error during data_restore ' unless Kernel.system(command)
85
+ $stdout.puts "Data from '#{filename}' has been restored to \
86
+ '#{configuration['database']}'"
87
+ end
88
+
89
+ def structure_and_data_dump(filename)
90
+ set_psql_env
91
+ command = "pg_dump -F c -x -O -f \
92
+ #{Shellwords.escape(filename)} \
93
+ #{Shellwords.escape(configuration['database'])}"
94
+ unless Kernel.system(command)
95
+ raise 'Error during structure_and_data_dump'
96
+ else
97
+ $stdout.puts 'Structure and data of ' \
98
+ "'#{configuration['database']}' has been dumped to '#{filename}'"
99
+ end
100
+ end
101
+
102
+ def structure_and_data_restore(filename)
103
+ set_psql_env
104
+ command = 'pg_restore --disable-triggers -x -O -d ' \
105
+ "#{Shellwords.escape(configuration['database'])} " \
106
+ "#{Shellwords.escape(filename)}"
107
+ unless Kernel.system(command)
108
+ raise 'Error during structure_and_data_restore '
109
+ else
110
+ $stdout.puts "Structure and data of '#{configuration['database']}' " \
111
+ "has been restored to '#{filename}'"
112
+ end
113
+ end
114
+
115
+ end
116
+ end
117
+ end
118
+
119
+ module ActiveRecord
120
+ module Tasks
121
+ module DatabaseTasks
122
+
123
+ def perform_pg_db_task_for_config_and_filename(task_name, *arguments)
124
+ configuration = arguments.first
125
+ filename = arguments.delete_at 1
126
+ class_for_adapter(configuration['adapter']) \
127
+ .new(*arguments).send task_name, filename
128
+ rescue ActiveRecord::NoDatabaseError
129
+ $stderr.puts "Database '#{configuration['database']}' does not exist"
130
+ rescue Exception => error
131
+ $stderr.puts error, *(error.backtrace)
132
+ end
133
+
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,37 @@
1
+ require 'pg_tasks'
2
+
3
+ namespace :db do
4
+
5
+ namespace :pg do
6
+
7
+ task truncate_tables: [:environment, :load_config] do
8
+ PgTasks.truncate_tables
9
+ end
10
+
11
+ namespace :data do
12
+
13
+ task dump: [:environment, :load_config] do
14
+ PgTasks.data_dump ENV['FILE']
15
+ end
16
+
17
+ task restore: [:environment, :load_config] do
18
+ PgTasks.data_restore ENV['FILE']
19
+ end
20
+
21
+ end
22
+
23
+ namespace :structure_and_data do
24
+
25
+ task dump: [:environment, :load_config] do
26
+ PgTasks.structure_and_data_dump ENV['FILE']
27
+ end
28
+
29
+ task restore: [:environment, :load_config] do
30
+ PgTasks.structure_and_data_restore ENV['FILE']
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_tasks
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Schank
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 5.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 4.1.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 5.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: pg
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description:
48
+ email:
49
+ - DrTom@schank.ch
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - MIT-LICENSE
55
+ - Rakefile
56
+ - lib/pg_tasks.rb
57
+ - lib/pg_tasks/railtie.rb
58
+ - lib/pg_tasks/version.rb
59
+ - lib/tasks/pg_tasks.rake
60
+ homepage: https://github.com/DrTom/rails_pg-tasks
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.2
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: PostgreSQL Tasks and Functions for Ruby on Rails
84
+ test_files: []
85
+ has_rdoc: