pgchief 0.4.0 → 0.5.1

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
  SHA256:
3
- metadata.gz: df59b4bbe15540e5d3b587f98c98ef71f2724f028299e88961766e2b6225ac2c
4
- data.tar.gz: f5b0f578ac0c0610e2f020f740f207826a1f4cd05de16e27e5da7c1af860a408
3
+ metadata.gz: 22c8374fdee4a24fde56bb1edd737694551b73288643b93078efa5f73c149a1b
4
+ data.tar.gz: bbebe5882166def3e96dc08804d695c4c2f9d629735173fbde82dcfcd70aaa1e
5
5
  SHA512:
6
- metadata.gz: f2e9611feb5db3d76d8e13ca3b153d1a0a387c90faadd249590c115b94d696da5d0b4d5b712db6d7a739b8f9b933fea1f40f9e6dc102fb064098b6ff3b568f58
7
- data.tar.gz: 863b4f71e694fb3e8ebde4d4b020d446d96a582a24b4e457d0d8cd42bc474780956d874a02944ea959ac689a3d33870374565871c19deea2bc31c750349aff8a
6
+ metadata.gz: 0fd0d65a33b1370a2dda33e4ecd06710b7140eefe911c4f3f9eadbe2bea81bd272a43b8bf013c472afa0cee035f785bd9336d8521b86210536cef43dffc944a8
7
+ data.tar.gz: 3314266704ccb6db85feffdd93a104441565bf89f034295f0ce29d4d3f6783e5ccc134964a409c5885066852f27975c062773b77c6a126cc159dbaace3d8bde3
data/CHANGELOG.md CHANGED
@@ -13,6 +13,23 @@ and this project will try its best to adhere to [Semantic Versioning](https://se
13
13
 
14
14
  ### Fixes
15
15
 
16
+ ## [0.5.1]
17
+
18
+ ### Changes
19
+
20
+ * Update README to note that libpq-dev is a required dependency in order to build
21
+
22
+ ### Fixes
23
+
24
+ * Remove `pry` from being required.
25
+
26
+ ## [0.5.0]
27
+
28
+ ### Additions
29
+
30
+ * Restore database from local file(s)
31
+ * Restore database from s3
32
+
16
33
  ## [0.4.0]
17
34
 
18
35
  ### Changes
@@ -91,7 +108,9 @@ and this project will try its best to adhere to [Semantic Versioning](https://se
91
108
  - Drop user ✅
92
109
  - List databases ✅
93
110
 
94
- [Unreleased]: https://github.com/jayroh/pgchief/compare/v0.4.0...HEAD
111
+ [Unreleased]: https://github.com/jayroh/pgchief/compare/v0.5.1...HEAD
112
+ [0.5.1]: https://github.com/jayroh/pgchief/releases/tag/v0.5.1
113
+ [0.5.0]: https://github.com/jayroh/pgchief/releases/tag/v0.5.0
95
114
  [0.4.0]: https://github.com/jayroh/pgchief/releases/tag/v0.4.0
96
115
  [0.3.1]: https://github.com/jayroh/pgchief/releases/tag/v0.3.1
97
116
  [0.3.0]: https://github.com/jayroh/pgchief/releases/tag/v0.3.0
data/README.md CHANGED
@@ -24,6 +24,7 @@ below for the feature check-list and current progress.
24
24
  ## Usage
25
25
 
26
26
  ```sh
27
+ # System dependency `libpq-dev` must be installed
27
28
  gem install pgchief
28
29
 
29
30
  # To initialize the config file at `~/.config/pgchief/config.toml`:
@@ -132,7 +133,7 @@ Give "rando-username" access to database(s):
132
133
  * [x] Display connection information
133
134
  * [x] Back up database locally
134
135
  * [x] Back up database to S3
135
- * [ ] Restore local database
136
- * [ ] Restore remote database @ S3
136
+ * [x] Restore local database
137
+ * [x] Restore remote database @ S3
137
138
  * [ ] Quickly back up via command line option
138
139
  * [ ] Quickly restore via command line option
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module Pgchief
6
+ module Command
7
+ # Command object to restore a database
8
+ class DatabaseRestore < Base
9
+ extend Forwardable
10
+
11
+ attr_reader :database, :filename
12
+
13
+ def_delegators :s3, :configured?, :client, :bucket, :path
14
+
15
+ def call
16
+ @database = params.first
17
+ @filename = params.last
18
+ raise Pgchief::Errors::DatabaseMissingError unless db_exists?
19
+
20
+ download! if configured?
21
+ restore!
22
+
23
+ "Database '#{database}' restored from #{filename}"
24
+ rescue PG::Error => e
25
+ "Error: #{e.message}"
26
+ ensure
27
+ conn.close
28
+ end
29
+
30
+ private
31
+
32
+ def download!
33
+ client.get_object(
34
+ bucket: bucket,
35
+ key: "#{path}#{filename}",
36
+ response_target: local_location
37
+ )
38
+ end
39
+
40
+ def restore!
41
+ `pg_restore --clean --no-owner --dbname=#{Pgchief::Config.pgurl}/#{database} #{local_location}`
42
+ end
43
+
44
+ def db_exists?
45
+ query = "SELECT 1 FROM pg_database WHERE datname = '#{database}'"
46
+ conn.exec(query).any?
47
+ end
48
+
49
+ def local_location
50
+ "#{Pgchief::Config.backup_dir}/#{filename}"
51
+ end
52
+
53
+ def s3
54
+ Pgchief::Config.s3
55
+ end
56
+ end
57
+ end
58
+ end
@@ -19,7 +19,6 @@ module Pgchief
19
19
  acl: "private",
20
20
  content_type: "application/octet-stream"
21
21
  )
22
- FileUtils.rm(local_location)
23
22
  end
24
23
 
25
24
  def s3_location
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module Pgchief
6
+ class Database
7
+ # Get a list of all backups for a given database
8
+ class Backups
9
+ extend Forwardable
10
+
11
+ def_delegators :s3, :bucket, :path, :client
12
+
13
+ def self.for(database, remote)
14
+ new(database, remote).for
15
+ end
16
+
17
+ attr_reader :database, :remote
18
+
19
+ def initialize(database, remote)
20
+ @database = database
21
+ @remote = remote
22
+ end
23
+
24
+ def for
25
+ remote ? remote_backups : local_backups
26
+ end
27
+
28
+ def remote_backups
29
+ @remote_backups ||= client.list_objects(
30
+ bucket: bucket,
31
+ prefix: "#{path}#{database}-"
32
+ ).contents
33
+ .map(&:key)
34
+ .sort
35
+ .last(3)
36
+ .reverse
37
+ .map { |f| File.basename(f) }
38
+ end
39
+
40
+ def local_backups
41
+ Dir["#{Pgchief::Config.backup_dir}#{database}-*.dump"]
42
+ .sort_by { |f| File.mtime(f) }
43
+ .reverse
44
+ .last(3)
45
+ .map { |f| File.basename(f) }
46
+ end
47
+
48
+ private
49
+
50
+ def s3
51
+ Pgchief::Config.s3
52
+ end
53
+ end
54
+ end
55
+ end
@@ -14,5 +14,9 @@ module Pgchief
14
14
  ensure
15
15
  conn.close
16
16
  end
17
+
18
+ def self.backups_for(database, remote: false)
19
+ Pgchief::Database::Backups.for(database, remote: remote)
20
+ end
17
21
  end
18
22
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Pgchief
4
4
  module Prompt
5
- # Class to prompt for which database to drop
5
+ # Class to prompt for which database to backup
6
6
  class BackupDatabase < Base
7
7
  def call
8
8
  database = prompt.select("Which database needs backing up?", Pgchief::Database.all)
@@ -10,7 +10,8 @@ module Pgchief
10
10
  "Create database",
11
11
  "Drop database",
12
12
  "Database List",
13
- "Backup database"
13
+ "Backup database",
14
+ "Restore database"
14
15
  ])
15
16
  scope = result == "Database List" ? "command" : "prompt"
16
17
 
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pgchief
4
+ module Prompt
5
+ # Class to prompt for which database to restore
6
+ class RestoreDatabase < Base
7
+ def call
8
+ database = prompt.select("Which database needs restoring?", Pgchief::Database.all)
9
+ local_file = prompt.select("Which backup file do you want to restore?", Pgchief::Database.backups_for(database))
10
+ result = Pgchief::Command::DatabaseRestore.call(database, local_file)
11
+
12
+ prompt.say result
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pgchief
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.1"
5
5
  end
data/lib/pgchief.rb CHANGED
@@ -11,18 +11,20 @@ require "pgchief/config/s3"
11
11
  require "pgchief/connection_string"
12
12
  require "pgchief/version"
13
13
  require "pgchief/database"
14
+ require "pgchief/database/backups"
14
15
  require "pgchief/user"
15
16
 
16
17
  require "pgchief/prompt/base"
17
- require "pgchief/prompt/start"
18
18
  require "pgchief/prompt/backup_database"
19
19
  require "pgchief/prompt/create_database"
20
20
  require "pgchief/prompt/create_user"
21
21
  require "pgchief/prompt/database_management"
22
22
  require "pgchief/prompt/drop_database"
23
23
  require "pgchief/prompt/drop_user"
24
- require "pgchief/prompt/user_management"
25
24
  require "pgchief/prompt/grant_database_privileges"
25
+ require "pgchief/prompt/restore_database"
26
+ require "pgchief/prompt/start"
27
+ require "pgchief/prompt/user_management"
26
28
  require "pgchief/prompt/view_database_connection_string"
27
29
 
28
30
  require "pgchief/command"
@@ -33,6 +35,7 @@ require "pgchief/command/database_create"
33
35
  require "pgchief/command/database_drop"
34
36
  require "pgchief/command/database_list"
35
37
  require "pgchief/command/database_privileges_grant"
38
+ require "pgchief/command/database_restore"
36
39
  require "pgchief/command/retrieve_connection_string"
37
40
  require "pgchief/command/s3_upload"
38
41
  require "pgchief/command/store_connection_string"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgchief
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Oliveira
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-27 00:00:00.000000000 Z
11
+ date: 2024-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-s3
@@ -108,6 +108,7 @@ files:
108
108
  - lib/pgchief/command/database_drop.rb
109
109
  - lib/pgchief/command/database_list.rb
110
110
  - lib/pgchief/command/database_privileges_grant.rb
111
+ - lib/pgchief/command/database_restore.rb
111
112
  - lib/pgchief/command/retrieve_connection_string.rb
112
113
  - lib/pgchief/command/s3_upload.rb
113
114
  - lib/pgchief/command/store_connection_string.rb
@@ -118,6 +119,7 @@ files:
118
119
  - lib/pgchief/config/s3.rb
119
120
  - lib/pgchief/connection_string.rb
120
121
  - lib/pgchief/database.rb
122
+ - lib/pgchief/database/backups.rb
121
123
  - lib/pgchief/prompt.rb
122
124
  - lib/pgchief/prompt/backup_database.rb
123
125
  - lib/pgchief/prompt/base.rb
@@ -127,6 +129,7 @@ files:
127
129
  - lib/pgchief/prompt/drop_database.rb
128
130
  - lib/pgchief/prompt/drop_user.rb
129
131
  - lib/pgchief/prompt/grant_database_privileges.rb
132
+ - lib/pgchief/prompt/restore_database.rb
130
133
  - lib/pgchief/prompt/start.rb
131
134
  - lib/pgchief/prompt/user_management.rb
132
135
  - lib/pgchief/prompt/view_database_connection_string.rb