pg_gnostic 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -24,7 +24,9 @@ To install pg_gnostic rake tasks u need add this lines into RAILS_ROOT/Rakefile
24
24
  * rake pg: pg:drop_views, pg:functions, pg:views and all in one pg:update
25
25
 
26
26
 
27
- == CHANGE LIST:
27
+ == CHANGE LOG:
28
+
29
+ 0.0.2 - backup/restore rakes
28
30
 
29
31
  == TODO
30
32
 
data/Rakefile CHANGED
@@ -28,7 +28,7 @@ PKG_FILES = FileList[ '[a-zA-Z]*', 'generators/**/*', 'lib/**/*', 'rails/**/*',
28
28
 
29
29
  spec = Gem::Specification.new do |s|
30
30
  s.name = "pg_gnostic"
31
- s.version = "0.0.1"
31
+ s.version = "0.0.2"
32
32
  s.author = "niquola"
33
33
  s.email = "niquola@gmail.com"
34
34
  s.homepage = "http://github.com/niquola/pg_gnostic"
@@ -1,5 +1,12 @@
1
1
  module PgGnostic
2
2
  class Config < KungFigure::Base
3
+
4
+ class Backup < KungFigure::Base
5
+ define_prop :path, 'db/backup'
6
+ define_prop :archive_command, 'gzip'
7
+ define_prop :unarchive_command, 'gunzip -c'
8
+ end
9
+
3
10
  class ViewModel < KungFigure::Base
4
11
  define_prop :nest_in_module,'Views'
5
12
  define_prop :prefix_view_name,'view_'
@@ -7,5 +14,6 @@ module PgGnostic
7
14
  nest_in_module ? "app/model/#{nest_in_module.underscore}" : "app/model"
8
15
  end
9
16
  end
17
+
10
18
  end
11
19
  end
@@ -1,17 +1,92 @@
1
- puts "loading task"
1
+ require 'fileutils'
2
+ require 'date'
3
+
2
4
  namespace :pg do
5
+ def get_backup_dir()
6
+ dir = PgGnostic.config.backup.path
7
+ dir = File.join(Rails.root,dir) unless dir =~ /^\//
8
+ dir
9
+ end
10
+
11
+ desc "Backup database"
12
+ task :backup=>:environment do
13
+ config = ActiveRecord::Base.configurations[RAILS_ENV || 'production']
14
+ backupdir=get_backup_dir
15
+ FileUtils.mkdir_p backupdir
16
+ date = DateTime.now.to_s.gsub(/[-:T+]/,'_')
17
+ backupfile = File.join(backupdir,"#{config["database"]}_#{date}.sql")
18
+
19
+ params={
20
+ 'host'=>config["host"],
21
+ 'port'=>config["port"],
22
+ 'username'=>config["username"]
23
+ }.map{|k,v| "--#{k}=#{v}" if v}.join(' ')
24
+ archiver = PgGnostic.config.backup.archive_command
25
+ puts "Backup #{config["database"]} to #{backupfile}"
26
+ command = "export PGPASSWORD=#{config["password"]} && pg_dump #{params} -w #{config["database"]} | #{archiver} > #{backupfile}"
27
+ puts "Execute: #{command}"
28
+ system command
29
+ system "ls -lh #{backupfile}"
30
+ end
31
+
32
+ desc "Restore database"
33
+ task :restore=>:environment do |args|
34
+
35
+ config = ActiveRecord::Base.configurations[RAILS_ENV || 'production']
36
+ @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8'
37
+ begin
38
+ ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
39
+ ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
40
+ ActiveRecord::Base.establish_connection(config)
41
+
42
+ file = ENV["PGBACKUP"]
43
+ config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
44
+ backupdir = get_backup_dir
45
+ if file && File.exists?(file)
46
+ file = file
47
+ elsif file && File.exists?(File.join(backupdir,file))
48
+ file = File.join(backupdir,file)
49
+ else
50
+ STDOUT.puts "Select which backup file to restore:"
51
+ files = Dir["#{backupdir}/*"].to_a
52
+ files.each_with_index{|f,i| STDOUT.puts "#{i}: #{f}"}
53
+ print "> "
54
+ num = STDIN.gets
55
+ file = files[num.to_i]
56
+ end
57
+ exit unless file && File.exists?(file)
58
+ puts "Restoring database #{config["database"]} from file #{file}"
59
+ params={
60
+ 'host'=>config["host"],
61
+ 'port'=>config["port"],
62
+ 'username'=>config["username"]
63
+ }.map{|k,v| "--#{k}=#{v}" if v}.join(' ')
64
+ unarchiver = PgGnostic.config.backup.unarchive_command
65
+ command = "export PGPASSWORD=#{config["password"]} && #{unarchiver} #{file} | psql #{params} -w #{config["database"]}"
66
+ puts "Execute: #{command}"
67
+ system command
68
+ puts "Restore complete!"
69
+
70
+ rescue
71
+ $stderr.puts "It seems database #{config["database"]} already exists. Please drop it by hands (rake db:drop) before restore backup."
72
+ $stderr.puts $!, *($!.backtrace)
73
+ end
74
+ end
75
+
3
76
  desc "drop views"
4
77
  task :drop_views=>:environment do
5
78
  #ActiveRecord::Base.logger=Logger.new STDOUT
6
79
  PgGnostic::ViewDefinition.load_declarations(File.join(RAILS_ROOT,'db','views'))
7
80
  PgGnostic::ViewDefinition.delete_all
8
81
  end
82
+
9
83
  desc "update views"
10
84
  task :views=>:environment do
11
85
  #ActiveRecord::Base.logger=Logger.new STDOUT
12
86
  PgGnostic::ViewDefinition.load_declarations File.join(RAILS_ROOT,'db','views')
13
87
  PgGnostic::ViewDefinition.update
14
88
  end
89
+
15
90
  desc "updates functions"
16
91
  task :functions => [:environment] do
17
92
  Dir["#{File.join(RAILS_ROOT,'db','functions')}/*.sql"].sort.each do |f|
@@ -24,6 +99,7 @@ namespace :pg do
24
99
  end
25
100
  end
26
101
  end
102
+
27
103
  desc "updates functions and views"
28
104
  task :update=>[:functions,:views] do
29
105
  end
data/test/config_test.rb CHANGED
@@ -3,15 +3,29 @@ require File.dirname(__FILE__) + '/test_helper.rb'
3
3
  class PgGnosticViewUtilsTest < ActiveSupport::TestCase
4
4
  def test_default_config
5
5
  assert_equal('Views',PgGnostic.config.view_model.nest_in_module)
6
+ assert_equal('db/backup',PgGnostic.config.backup.path)
7
+ assert_equal('gzip',PgGnostic.config.backup.archive_command)
8
+ assert_equal('gunzip -c',PgGnostic.config.backup.unarchive_command)
6
9
 
7
10
  PgGnostic.configure do
11
+
8
12
  view_model do
9
13
  nest_in_module 'OtherModule'
10
14
  end
15
+
16
+ backup do
17
+ path '/tmp/backups'
18
+ archive_command 'arch'
19
+ unarchive_command 'unarch'
20
+ end
21
+
11
22
  end
12
23
  assert_equal('OtherModule',PgGnostic.config.view_model.nest_in_module)
13
24
  assert_equal('view_',PgGnostic.config.view_model.prefix_view_name)
14
-
25
+ assert_equal('/tmp/backups',PgGnostic.config.backup.path)
26
+ assert_equal('arch',PgGnostic.config.backup.archive_command)
27
+ assert_equal('unarch',PgGnostic.config.backup.unarchive_command)
28
+
15
29
  PgGnostic.clear_config!
16
30
  end
17
31
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_gnostic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
5
10
  platform: ruby
6
11
  authors:
7
12
  - niquola
@@ -9,19 +14,23 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-10 00:00:00 +03:00
17
+ date: 2010-03-31 00:00:00 +04:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: kung_figure
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - "="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 0
30
+ - 2
23
31
  version: 0.0.2
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  description:
26
35
  email: niquola@gmail.com
27
36
  executables: []
@@ -70,18 +79,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
79
  requirements:
71
80
  - - ">="
72
81
  - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
73
84
  version: "0"
74
- version:
75
85
  required_rubygems_version: !ruby/object:Gem::Requirement
76
86
  requirements:
77
87
  - - ">="
78
88
  - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
79
91
  version: "0"
80
- version:
81
92
  requirements: []
82
93
 
83
94
  rubyforge_project:
84
- rubygems_version: 1.3.5
95
+ rubygems_version: 1.3.6
85
96
  signing_key:
86
97
  specification_version: 3
87
98
  summary: Rails plugin for postgres