pg_dumper 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pg_dumper.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # PgDumper
2
+
3
+ Abstraction layer between PostgreSQL utility pg_dump and Rails app.
4
+
5
+ ## Rake tasks
6
+ You can specify few ENV variables:
7
+ GZIP - to gzip sql dump with gzip utility
8
+ FILE - dump output (else asks interactively)
9
+ VERBOSE - verbose output
10
+ Z - pg_dump compression level
11
+
12
+ ### db:dump
13
+ Dumps database schema **with data**.
14
+
15
+ ### db:dump:schema
16
+ Dumps database schema **without data**.
17
+
18
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,12 @@
1
+ require 'pg_dumper'
2
+ require 'rails'
3
+
4
+ class PgDumper
5
+ class Railtie < Rails::Railtie
6
+ #railtie_name :pg_dumper # Deprecated
7
+
8
+ rake_tasks do
9
+ load "tasks/db_dump.rake"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ class PgDumper
2
+ VERSION = "0.0.2"
3
+ end
data/lib/pg_dumper.rb ADDED
@@ -0,0 +1,76 @@
1
+ class PgDumper
2
+ require 'pg_dumper/railtie' if defined?(Rails)
3
+
4
+ attr_reader :database
5
+ attr_reader :args
6
+
7
+ def initialize database
8
+ @database = database
9
+ @args = []
10
+ @options = {}
11
+ end
12
+
13
+ def run(mode = :silent)
14
+ @binary ||= find_executable
15
+
16
+ raise "ERROR: pg_dump executable not found" unless @binary.present?
17
+
18
+ options = {}
19
+
20
+ case mode
21
+ when :silent
22
+ options[:out] = "/dev/null"
23
+ end
24
+ system @binary, *args, database, options
25
+ end
26
+
27
+ def schema_only!
28
+ add_args "-s"
29
+ end
30
+
31
+ def recreate!
32
+ add_args "-c"
33
+ end
34
+
35
+ def data_only!
36
+ add_args "-a", '--disable-triggers'
37
+ add_args '-T', 'schema_migrations'
38
+ end
39
+
40
+ def compress! level=9
41
+ add_args '-Z', level if level.present?
42
+ end
43
+
44
+ def verbose!
45
+ add_args "-v"
46
+ end
47
+
48
+ def pretty!
49
+ add_args '--column-inserts'
50
+ end
51
+
52
+ def connection= opts
53
+ add_args '-p', opts['port'] if opts['port'].present?
54
+ add_args '-h', opts['host'] if opts['host'].present?
55
+ add_args '-U', opts['username'] if opts['host'].present?
56
+ end
57
+
58
+ def output= filename
59
+ add_args "-f", filename
60
+ end
61
+
62
+ private
63
+ def find_executable
64
+ [ENV['PG_DUMP'], %x{which pg_dump}.strip].each do |pg|
65
+ return pg if pg.present? && File.exists?(pg)
66
+ end
67
+ nil
68
+ end
69
+
70
+ def add_args(*args)
71
+ @args.push *args.map!(&:to_s)
72
+ @args.uniq!
73
+ end
74
+
75
+
76
+ end
@@ -0,0 +1,63 @@
1
+ namespace :db do
2
+ desc 'Dumps database (both data and schema) to FILE or asks for output'
3
+ task :dump => ["#{Rails.root}/config/database.yml", :environment] do
4
+ gzip = ENV['GZIP'].present?
5
+ db = ActiveRecord::Base.configurations[Rails.env]
6
+ backup_file = Time.now.strftime("backup_%Y%m%d%H%M%S.sql")
7
+ unless file = ENV['FILE']
8
+ print "Enter filename of dump [#{backup_file}#{'.gz' if gzip}]: "
9
+ input = STDIN.gets.chop
10
+ file = (input.blank?)? backup_file : input
11
+ end
12
+
13
+ dump = PgDumper.new db["database"]
14
+ dump.recreate!
15
+ dump.connection = db
16
+ dump.output = backup_file
17
+ dump.verbose! if ENV['VERBOSE'].present?
18
+ dump.compress! ENV['Z']
19
+
20
+ print "Creating backup to '#{file}'\n"
21
+
22
+ if dump.run
23
+ system 'gzip', '-v', file if gzip
24
+ print "Backup saved to #{file.inspect}\n"
25
+ else
26
+ print "Backup failed! Error status: #{$?}."
27
+ end
28
+
29
+ @backup_file = file
30
+ end
31
+
32
+ desc 'Dumps database (only data) to FILE or asks for output'
33
+ namespace :dump do
34
+ task :data => ["#{Rails.root}/config/database.yml", :environment] do
35
+ gzip = ENV['GZIP'].present?
36
+ db = ActiveRecord::Base.configurations[Rails.env]
37
+ backup_file = Time.now.strftime("backup_%Y%m%d%H%M%S.sql")
38
+ unless file = ENV['FILE']
39
+ print "Enter filename of dump [#{backup_file}#{'.gz' if gzip}]: "
40
+ input = STDIN.gets.chop
41
+ file = (input.blank?)? backup_file : input
42
+ end
43
+
44
+ dump = PgDumper.new db["database"]
45
+ dump.data_only!
46
+ dump.connection = db
47
+ dump.output = backup_file
48
+ dump.verbose! if ENV['VERBOSE'].present?
49
+ dump.compress! ENV['Z']
50
+
51
+ print "Creating backup to '#{file}'\n"
52
+
53
+ if dump.run
54
+ system 'gzip', '-v', file if gzip
55
+ print "Backup saved to #{file.inspect}\n"
56
+ else
57
+ print "Backup failed! Error status: #{$?}."
58
+ end
59
+
60
+ @backup_file = file
61
+ end
62
+ end
63
+ end
data/pg_dumper.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "pg_dumper/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "pg_dumper"
7
+ s.version = PgDumper::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["mikz"]
10
+ s.email = ["mikz@o2h.cz"]
11
+ s.homepage = ""
12
+ s.summary = %q{Adds Rake tasks to easily dump postgresql database with or without data}
13
+ s.description = %q{Provides abstraction layer between pg_dump utility and ruby. Also adds rake task to easily dump database with or without data.}
14
+
15
+ #s.rubyforge_project = "pg_dumper"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_dumper
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - mikz
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-01 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Provides abstraction layer between pg_dump utility and ruby. Also adds rake task to easily dump database with or without data.
22
+ email:
23
+ - mikz@o2h.cz
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README.md
34
+ - Rakefile
35
+ - lib/pg_dumper.rb
36
+ - lib/pg_dumper/railtie.rb
37
+ - lib/pg_dumper/version.rb
38
+ - lib/tasks/db_dump.rake
39
+ - pg_dumper.gemspec
40
+ has_rdoc: true
41
+ homepage: ""
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.7
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Adds Rake tasks to easily dump postgresql database with or without data
72
+ test_files: []
73
+