db-rotator 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 950b76e1099d543a0d7e7ea90ef9f5821dd8b11d
4
- data.tar.gz: b51297f8b370983ec0445621d1e2f8f39c2b3334
3
+ metadata.gz: 548ebd4b7086d44bccfe3f6640b4dc4ac90c9b77
4
+ data.tar.gz: 8e64b24dcfb012bd525512314d69cdf716c5bfff
5
5
  SHA512:
6
- metadata.gz: 7d6ab0d69db0a1c288b82b8ae5b1468a6aee4e75ebe58f7418ac7b186534e6b81ef8307f3ae83cfb5234d7df14aa959969a0d84ceb2e8334ee37eb6ff8858e2a
7
- data.tar.gz: e449a83884b4a2441816db1388e8493afbaab46c612d592ea9c30b9c5208f6f01468dcdade65d4172ab59bfdba8eb9d1bfa1ef43c6a782137e19db12fb4485ca
6
+ metadata.gz: 6a98adb179de802fcabd20a9310a11ea513a99c2ea9b66c3e31cf73e2ae98d2d1a719374949c180457f323e0a4216c1a3bb6cc4ef13ca66e64785cfdf48a87df
7
+ data.tar.gz: 88132fb3cf019e39d93bedeeaadb96a1b21001cd702edd2b6908d98c9e21832fe6441a85487f59631e1927659a2d280fdf1ae5dffd568b7b5f00eb389954b5af
data/README.md CHANGED
@@ -60,3 +60,5 @@ Example: `scp hostname:/path/to/mysql/backups/backup_filename.sql.bz2`
60
60
  - **reasonable_diskspace** (-s). Rough estimate of temporary disk space required to import a typical dump, in GB. Ensures this amount of space is free before importing. Default: nil
61
61
  - **rails_db_yaml_path** (-y). Updates database name in your YAML file. Example: `/path/to/railsroot/config/database.yml` Default: nil
62
62
  - **rails_environments** (-e). In conjunction with -y, which rails envs to update DB name for. If passing multiple via command line, use a comma to separate, like `-e "development,staging"`. Default: ["development"]
63
+ - **on_success** (-S). Executes COMMAND on successful completion
64
+ - **on_failure** (-F). Executes COMMAND if there is an error
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "db-rotator"
3
- s.version = "0.0.3"
3
+ s.version = "0.1.0"
4
4
  s.summary = "Easy MySQL database rotation and pruning"
5
5
  s.description = "Easy MySQL database rotation and pruning"
6
6
  s.authors = ["Michael Nelson"]
@@ -6,12 +6,19 @@ class DBRotator
6
6
  def initialize(config)
7
7
  @config = config.config
8
8
  @schemas = []
9
+
9
10
  populate_schemas
11
+ rescue
12
+ on_failure $!
10
13
  end
11
14
 
12
15
  def rotate
13
16
  refresh
14
17
  update_db_yaml
18
+ on_success
19
+
20
+ rescue
21
+ on_failure $!
15
22
  end
16
23
 
17
24
  def refresh
@@ -28,6 +35,18 @@ class DBRotator
28
35
 
29
36
  private
30
37
 
38
+ def on_success
39
+ bash_exec "#{@config[:on_success]}" if @config[:on_success]
40
+ end
41
+
42
+ def on_failure(err)
43
+ if @config[:on_failure]
44
+ bash_exec "#{@config[:on_failure]} #{err.class.name} '#{err.message}'"
45
+ else
46
+ raise err
47
+ end
48
+ end
49
+
31
50
  def download_dump
32
51
  verbose_message "Downloading dump..."
33
52
  bash_exec "rm -f #{local_dump_path}"
@@ -19,6 +19,9 @@ class DBRotatorConfig
19
19
  reasonable_diskspace: [['-s', "--minimum-diskspace [GB]", "Rough estimate of temporary disk space required to import a typical dump, in GB."], nil],
20
20
  rails_db_yaml_path: [['-y', "--rails-db-yaml [PATH]", "Updates database name in your YAML file when given.", "Default: nil"], nil],
21
21
  rails_environments: [['-e', "--rails-db-environments [ENV1,ENV2]", Array, "In conjunction with -y, which rails envs to update DB name for. Default: development"], ["development"]],
22
+ on_success: [['-S', "--on_success [COMMAND]", "Executes command on successful completion."], nil],
23
+ on_failure: [['-F', "--on_failure [COMMAND]", "Executes command if there is an error."], nil],
24
+
22
25
 
23
26
  config_file: [['-f', "--config-file PATH", "Runs rotator with configuration from this .yml file."], nil],
24
27
  }
@@ -3,6 +3,7 @@ require_relative '../lib/db_rotator'
3
3
  require_relative '../lib/db_rotator_config'
4
4
 
5
5
  TEST_SCHEMA_PREFIX = "__minitest_"
6
+ TEST_MYSQL_COMMAND = "mysql -u root"
6
7
 
7
8
  def dummy_config(config={})
8
9
  DBRotatorConfig.new.tap do |cfg|
@@ -10,7 +11,8 @@ def dummy_config(config={})
10
11
  db_prefix: TEST_SCHEMA_PREFIX,
11
12
  scp_command: "cp test/fixtures/basic_dump.sql.bz2",
12
13
 
13
- local_dump_destination: "/tmp"
14
+ local_dump_destination: "/tmp",
15
+ mysql_command: TEST_MYSQL_COMMAND,
14
16
  }.merge(config))
15
17
 
16
18
  cfg.add_default_values
@@ -2,9 +2,9 @@ require_relative '../test/helper'
2
2
 
3
3
  describe DBRotator do
4
4
  after do
5
- `mysql -B -e 'SHOW SCHEMAS;'`.split.each do |schema|
5
+ `#{TEST_MYSQL_COMMAND} -B -e 'SHOW SCHEMAS;'`.split.each do |schema|
6
6
  if /^#{TEST_SCHEMA_PREFIX}/.match(schema)
7
- `mysql -e "DROP SCHEMA #{schema}"`
7
+ `#{TEST_MYSQL_COMMAND} -e "DROP SCHEMA #{schema}"`
8
8
  end
9
9
  end
10
10
  end
@@ -23,22 +23,49 @@ describe DBRotator do
23
23
 
24
24
  Time.stub(:now, t1) do
25
25
  dbr.rotate
26
- `mysql -B -e 'SHOW SCHEMAS;'`.must_include(dbname_with(t1))
27
- `mysql -B -e 'SHOW TABLES FROM #{dbname_with(t1)};'`.must_include("test")
26
+ `#{TEST_MYSQL_COMMAND} -B -e 'SHOW SCHEMAS;'`.must_include(dbname_with(t1))
27
+ `#{TEST_MYSQL_COMMAND} -B -e 'SHOW TABLES FROM #{dbname_with(t1)};'`.must_include("test")
28
28
  end
29
29
 
30
30
  Time.stub(:now, t2) do
31
31
  dbr.rotate
32
- `mysql -B -e 'SHOW SCHEMAS;'`.must_include(dbname_with(t2))
33
- `mysql -B -e 'SHOW SCHEMAS;'`.must_include(dbname_with(t1))
32
+ `#{TEST_MYSQL_COMMAND} -B -e 'SHOW SCHEMAS;'`.must_include(dbname_with(t2))
33
+ `#{TEST_MYSQL_COMMAND} -B -e 'SHOW SCHEMAS;'`.must_include(dbname_with(t1))
34
34
  end
35
35
 
36
36
  Time.stub(:now, t3) do
37
37
  dbr.rotate
38
- `mysql -B -e 'SHOW SCHEMAS;'`.must_include(dbname_with(t3))
39
- `mysql -B -e 'SHOW SCHEMAS;'`.must_include(dbname_with(t2))
38
+ `#{TEST_MYSQL_COMMAND} -B -e 'SHOW SCHEMAS;'`.must_include(dbname_with(t3))
39
+ `#{TEST_MYSQL_COMMAND} -B -e 'SHOW SCHEMAS;'`.must_include(dbname_with(t2))
40
40
 
41
- `mysql -B -e 'SHOW SCHEMAS;'`.wont_include(dbname_with(t1))
41
+ `#{TEST_MYSQL_COMMAND} -B -e 'SHOW SCHEMAS;'`.wont_include(dbname_with(t1))
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "callbacks" do
47
+ before do
48
+ `test -e /tmp/success && rm /tmp/success`
49
+ `test -e /tmp/failure && rm /tmp/failure`
50
+ end
51
+
52
+ describe "on_success" do
53
+ it "calls on_success when specified" do
54
+ dbr = DBRotator.new(dummy_config(on_success: 'touch /tmp/success'))
55
+ dbr.rotate
56
+ FileTest.exists?('/tmp/success').must_equal true
57
+ FileTest.exists?('/tmp/failure').must_equal false
58
+ end
59
+ end
60
+
61
+ describe "on_failure" do
62
+ it "calls on_failure" do
63
+ dbr = DBRotator.new(dummy_config(
64
+ on_failure: 'touch /tmp/failure && true',
65
+ mysql_command: 'crap command 2> /dev/null'))
66
+ dbr.rotate
67
+ FileTest.exists?('/tmp/success').must_equal false
68
+ FileTest.exists?('/tmp/failure').must_equal true
42
69
  end
43
70
  end
44
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-rotator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Nelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-05 00:00:00.000000000 Z
11
+ date: 2014-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -65,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
65
  version: '0'
66
66
  requirements: []
67
67
  rubyforge_project:
68
- rubygems_version: 2.0.14
68
+ rubygems_version: 2.1.11
69
69
  signing_key:
70
70
  specification_version: 4
71
71
  summary: Easy MySQL database rotation and pruning