handy 0.0.1 → 0.0.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.
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # handy provides follwing tools
2
+
3
+ ##`rake handy:db:restore file=xyz.sql.gz##
4
+
5
+ restores the data and structure from file
6
+
7
+
8
+ * cap production handy:db:pull
9
+
10
+
11
+ Copyright (c) 2010 Neeraj Singh. See LICENSE for details.
data/lib/handy/railtie.rb CHANGED
@@ -1,13 +1,11 @@
1
- module Handy
2
- class Engine < Rails::Engine
1
+ class Engine < Rails::Engine
2
+
3
+ initializer :after_initialize do
4
+ end
3
5
 
4
- initializer "handy.setup" do
5
- end
6
6
 
7
- rake_tasks do
8
- desc "lab99" do
9
- puts "You're in my_gem"
10
- end
11
- end
7
+ rake_tasks do
8
+ load 'handy/tasks.rb'
12
9
  end
10
+
13
11
  end
@@ -0,0 +1,111 @@
1
+ class Util
2
+ attr_accessor :username, :password, :database
3
+ def initialize(*args)
4
+ @username, @password, @database = *args
5
+ end
6
+
7
+ def self.retrieve_db_info(database_yml_file, env)
8
+ config = YAML.load_file(database_yml_file)
9
+ [ config[env]['database'], config[env]['user'], config[env]['password'] ]
10
+ end
11
+
12
+ def mysql_command
13
+ password.blank? ? "mysql -u #{user} #{database}" : "mysql -u #{user} -p'#{password}' #{database}"
14
+ end
15
+
16
+ def self.execute_cmd(cmd)
17
+ puts cmd
18
+ system cmd
19
+ end
20
+ end
21
+
22
+ namespace :handy do
23
+ namespace :db do
24
+
25
+ desc "Load schema and data from a local sql file."
26
+ task :restore => :environment do
27
+ puts "Usage: rake handy:db:restore file=xxxxxxxxx.sql[.gz]"
28
+ file_name = ENV['file']
29
+ raise "file was not supplied. Check Usage." unless file_name
30
+ restore_file = File.join(Rails.root, 'tmp', file_name)
31
+ raise "file was not found" unless File.exists?(restore_file)
32
+
33
+ db_info = Util.retrieve_db_info("#{Rails.root}/config/database.yml", Rails.env)
34
+ database, user, password = db_info
35
+ util = Util.new(db_info)
36
+
37
+ if restore_file =~ /\.gz/
38
+ puts "decompressing backup"
39
+ result = system("gzip -d #{restore_file}" )
40
+ raise("backup decompression failed. msg: #{$?}" ) unless result
41
+ end
42
+
43
+ cmd = "#{util.mysql_command} < #{restore_file.gsub('.gz','')}"
44
+
45
+ Util.execute_cmd(cmd)
46
+ puts "database has been restored"
47
+ end
48
+
49
+ desc "bakup database to a file"
50
+ task :backup => [:environment] do
51
+ desc <<-DESC
52
+ This task backups the database to a file. It will keep a maximum of 10 backed up copies.
53
+ The files are backed up at shared directory on remote server.
54
+ This task should be executed before any deploy to production.
55
+ Files are backed at Rails.root/../../shared/db_backups on the remote server
56
+ DESC
57
+
58
+ timestamp = ENV['timestamp'] || Time.zone.now.strftime("%Y-%m-%d-%H-%M-%S")
59
+ file_name = "#{timestamp}.sql"
60
+ backup_dir = File.join (Rails.root, 'tmp')
61
+
62
+ backup_file = File.join(backup_dir, "#{file_name}.gz")
63
+
64
+ FileUtils.mkdir_p(backup_dir) unless File.exists?(backup_dir) && File.directory?(backup_dir)
65
+
66
+ database, user, password = Util.retrieve_db_info("#{Rails.root}/config/database.yml", Rails.env)
67
+ cmd = "mysqldump --opt --skip-add-locks -u#{user} -p#{password} #{database} >> #{file_name}"
68
+ Util.execute_cmd(cmd)
69
+
70
+ #-c --stdout write on standard output, keep original files unchanged
71
+ #-q quite
72
+ #-9 best compression
73
+ sh "gzip -q9 #{file_name}"
74
+ sh "mv #{file_name}.gz #{backup_file}"
75
+ puts "Backup done at #{File.expand_path(backup_file)}"
76
+
77
+ end
78
+
79
+
80
+ desc "Copy production database to staging database. Or copy data from any database to any other datbase."
81
+ task :db2db => :environment do
82
+ puts "Usage: handy:db:db2db from_env=production to_env=staging"
83
+ from_env = ENV['from_env'] || 'production'
84
+ to_env = ENV['to_env'] || 'staging'
85
+ file_name = "#{Rails.root}/tmp/#{from_env}.data"
86
+ config_file = "#{Rails.root}/config/database.yml"
87
+
88
+ db_config = YAML.load_file(config_file)
89
+
90
+ from_user = db_config[from_env]['username']
91
+ from_password = db_config[from_env]['password']
92
+ from_database = db_config[from_env]['database']
93
+ from_params = "-Q --add-drop-table -O add-locks=FALSE -O lock-tables=FALSE"
94
+
95
+ cmd = "mysqldump -u #{from_user} -p#{from_password} #{from_params} #{from_database} > #{file_name} "
96
+ puts cmd
97
+ system cmd
98
+
99
+ to_username = db_config[to_env]['username']
100
+ to_password = db_config[to_env]['password']
101
+ to_database = db_config[to_env]['database']
102
+
103
+ cmd = "mysql -u #{to_username} -p#{to_password} #{to_database} < #{file_name}"
104
+ puts cmd
105
+ system cmd
106
+
107
+ puts "#{to_env} database has been restored with #{from_env} database"
108
+ end
109
+
110
+ end
111
+ end
data/lib/handy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Handy
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Neeraj Singh
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-01 00:00:00 -04:00
18
+ date: 2010-10-07 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -27,13 +27,14 @@ extensions: []
27
27
 
28
28
  extra_rdoc_files:
29
29
  - LICENSE
30
- - README.rdoc
30
+ - README.md
31
31
  files:
32
32
  - LICENSE
33
- - README.rdoc
33
+ - README.md
34
34
  - Rakefile
35
35
  - lib/handy.rb
36
36
  - lib/handy/railtie.rb
37
+ - lib/handy/tasks.rb
37
38
  - lib/handy/version.rb
38
39
  - test/helper.rb
39
40
  - test/test_handy.rb
data/README.rdoc DELETED
@@ -1,17 +0,0 @@
1
- = handy
2
-
3
- Description goes here.
4
-
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
- * Send me a pull request. Bonus points for topic branches.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2010 Neeraj Singh. See LICENSE for details.