pgchief 0.4.0 → 0.5.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
  SHA256:
3
- metadata.gz: df59b4bbe15540e5d3b587f98c98ef71f2724f028299e88961766e2b6225ac2c
4
- data.tar.gz: f5b0f578ac0c0610e2f020f740f207826a1f4cd05de16e27e5da7c1af860a408
3
+ metadata.gz: adccab6ee77468761bf0d05d12954f7b0360547b95f5295797b911ed80f731e2
4
+ data.tar.gz: 2a4ae39f43e0da9913f252e70e0583d0e21dd45e48931e94a62c309298d287f8
5
5
  SHA512:
6
- metadata.gz: f2e9611feb5db3d76d8e13ca3b153d1a0a387c90faadd249590c115b94d696da5d0b4d5b712db6d7a739b8f9b933fea1f40f9e6dc102fb064098b6ff3b568f58
7
- data.tar.gz: 863b4f71e694fb3e8ebde4d4b020d446d96a582a24b4e457d0d8cd42bc474780956d874a02944ea959ac689a3d33870374565871c19deea2bc31c750349aff8a
6
+ metadata.gz: 7dea359e077b5c473c7df21e53067954dee0c426df76b8a375a026a3976593dbdd4024c6582650a2fbe1404a34affb1030fe0065842065ad594119cd3f757ace
7
+ data.tar.gz: 3b16494cea09736f6b2c16e55d2c5054898a6f023bf06b8862585313617104dc858e98750397baf6b606126aaf2f5bb109f9f02fbf11699b281bdeefd6c41c38
data/CHANGELOG.md CHANGED
@@ -13,6 +13,13 @@ and this project will try its best to adhere to [Semantic Versioning](https://se
13
13
 
14
14
  ### Fixes
15
15
 
16
+ ## [0.5.0]
17
+
18
+ ### Additions
19
+
20
+ * Restore database from local file(s)
21
+ * Restore database from s3
22
+
16
23
  ## [0.4.0]
17
24
 
18
25
  ### Changes
data/README.md CHANGED
@@ -132,7 +132,7 @@ Give "rando-username" access to database(s):
132
132
  * [x] Display connection information
133
133
  * [x] Back up database locally
134
134
  * [x] Back up database to S3
135
- * [ ] Restore local database
136
- * [ ] Restore remote database @ S3
135
+ * [x] Restore local database
136
+ * [x] Restore remote database @ S3
137
137
  * [ ] Quickly back up via command line option
138
138
  * [ ] 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,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+ require "pry"
5
+
6
+ module Pgchief
7
+ class Database
8
+ # Get a list of all backups for a given database
9
+ class Backups
10
+ extend Forwardable
11
+
12
+ def_delegators :s3, :bucket, :path, :client
13
+
14
+ def self.for(database, remote)
15
+ new(database, remote).for
16
+ end
17
+
18
+ attr_reader :database, :remote
19
+
20
+ def initialize(database, remote)
21
+ @database = database
22
+ @remote = remote
23
+ end
24
+
25
+ def for
26
+ remote ? remote_backups : local_backups
27
+ end
28
+
29
+ def remote_backups
30
+ @remote_backups ||= client.list_objects(
31
+ bucket: bucket,
32
+ prefix: "#{path}#{database}-"
33
+ ).contents
34
+ .map(&:key)
35
+ .sort
36
+ .last(3)
37
+ .reverse
38
+ .map { |f| File.basename(f) }
39
+ end
40
+
41
+ def local_backups
42
+ Dir["#{Pgchief::Config.backup_dir}#{database}-*.dump"]
43
+ .sort_by { |f| File.mtime(f) }
44
+ .reverse
45
+ .last(3)
46
+ .map { |f| File.basename(f) }
47
+ end
48
+
49
+ private
50
+
51
+ def s3
52
+ Pgchief::Config.s3
53
+ end
54
+ end
55
+ end
56
+ 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.0"
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.0
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