efdb 0.1.0
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.
- checksums.yaml +7 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +30 -0
- data/LICENSE +21 -0
- data/README.md +45 -0
- data/Rakefile +12 -0
- data/efdb.gemspec +28 -0
- data/exe/efdb +6 -0
- data/lib/efdb/cli/pg.rb +113 -0
- data/lib/efdb/cli.rb +29 -0
- data/lib/efdb/error_handling.rb +37 -0
- data/lib/efdb/rails_app.rb +38 -0
- data/lib/efdb/version.rb +5 -0
- data/lib/efdb.rb +7 -0
- metadata +67 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 3a4c7bd14b0f5d18b2eb3d463531f2701928c0d610233cf9865fb7934f2b4605
|
|
4
|
+
data.tar.gz: 63a967c7e18365e456c7c6e2b4cb70b84e3182d8769df2946e479e998258ab6d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fbd21004e6cb20febe57a5671028308b03f57cb62ace065dd65484e7b93590cbfcfb343d45cf6597fa022e2d230efff57dcf1e5babf9ff16dc1cac65deb6e915
|
|
7
|
+
data.tar.gz: 895cc517fe985e2a438b99971c30a097a2d7da2696cb4ee8b9d50069048dd358f0fbabea78206031bf134ea8e8e29d53df15276f0333f0d5b342a0cd2e3ea1a6
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
efdb (0.1.0)
|
|
5
|
+
thor (>= 1.0)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
minitest (5.27.0)
|
|
11
|
+
rake (13.4.2)
|
|
12
|
+
thor (1.5.0)
|
|
13
|
+
|
|
14
|
+
PLATFORMS
|
|
15
|
+
ruby
|
|
16
|
+
x86_64-linux
|
|
17
|
+
|
|
18
|
+
DEPENDENCIES
|
|
19
|
+
efdb!
|
|
20
|
+
minitest (~> 5.0)
|
|
21
|
+
rake (~> 13.0)
|
|
22
|
+
|
|
23
|
+
CHECKSUMS
|
|
24
|
+
efdb (0.1.0)
|
|
25
|
+
minitest (5.27.0) sha256=2d3b17f8a36fe7801c1adcffdbc38233b938eb0b4966e97a6739055a45fa77d5
|
|
26
|
+
rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701
|
|
27
|
+
thor (1.5.0) sha256=e3a9e55fe857e44859ce104a84675ab6e8cd59c650a49106a05f55f136425e73
|
|
28
|
+
|
|
29
|
+
BUNDLED WITH
|
|
30
|
+
4.0.16
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Effectiva studio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Efdb
|
|
2
|
+
|
|
3
|
+
Database helpers for Ruby on Rails apps. Currently supports PostgreSQL.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
# Gemfile
|
|
9
|
+
gem "efdb", group: :development
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Optionally create a binstub: `bundle binstub efdb` (gives you `bin/efdb`).
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
Run from your Rails app root. Connection settings are read from
|
|
17
|
+
`config/database.yml` for the current `RAILS_ENV`.
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
bin/efdb pg dump # dump to tmp/<database>-<timestamp>.dump
|
|
21
|
+
bin/efdb pg dump path/to/file.dump # dump to a specific file
|
|
22
|
+
bin/efdb pg restore # restore latest tmp/*.dump (asks for confirmation)
|
|
23
|
+
bin/efdb pg restore --file=x.dump # restore a specific file
|
|
24
|
+
bin/efdb pg dump --db=cache # use another database from database.yml (default: primary)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`export` and `import` are aliases for `dump` and `restore`.
|
|
28
|
+
Restoring in production requires an extra confirmation.
|
|
29
|
+
|
|
30
|
+
Requires `pg_dump` / `pg_restore` on your `PATH`.
|
|
31
|
+
|
|
32
|
+
### Debugging
|
|
33
|
+
|
|
34
|
+
Errors are printed in red. Pass `--verbose` (or set the `VERBOSE` environment
|
|
35
|
+
variable) to also print the backtrace:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
bin/efdb pg dump --verbose
|
|
39
|
+
VERBOSE=1 bin/efdb pg dump
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Development
|
|
43
|
+
|
|
44
|
+
Install dependencies with `bundle install` and run the test suite with
|
|
45
|
+
`bundle exec rake test`.
|
data/Rakefile
ADDED
data/efdb.gemspec
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/efdb/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "efdb"
|
|
7
|
+
spec.version = Efdb::VERSION
|
|
8
|
+
spec.authors = ["Effectiva studio"]
|
|
9
|
+
|
|
10
|
+
spec.summary = "Database helpers (PostgreSQL dump/restore) for Ruby on Rails apps"
|
|
11
|
+
spec.homepage = "https://github.com/EffectivaStudio/efdb"
|
|
12
|
+
spec.license = "MIT"
|
|
13
|
+
spec.required_ruby_version = ">= 2.6.0"
|
|
14
|
+
|
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
17
|
+
|
|
18
|
+
spec.files = Dir.chdir(__dir__) do
|
|
19
|
+
`git ls-files -z`.split("\x0").reject do |file|
|
|
20
|
+
file.start_with?(*%w[test/ .git])
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
spec.bindir = "exe"
|
|
24
|
+
spec.executables = ["efdb"]
|
|
25
|
+
spec.require_paths = ["lib"]
|
|
26
|
+
|
|
27
|
+
spec.add_dependency "thor", ">= 1.0"
|
|
28
|
+
end
|
data/exe/efdb
ADDED
data/lib/efdb/cli/pg.rb
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "yaml"
|
|
5
|
+
require "thor"
|
|
6
|
+
|
|
7
|
+
module Efdb
|
|
8
|
+
class CLI < Thor
|
|
9
|
+
# PostgreSQL helpers for Rails applications.
|
|
10
|
+
class Pg < Thor
|
|
11
|
+
extend ErrorHandling
|
|
12
|
+
|
|
13
|
+
CONNECTION_KEYS = %i[host port username password database].freeze
|
|
14
|
+
|
|
15
|
+
def self.exit_on_failure?
|
|
16
|
+
true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class_option :db, type: :string, default: "primary", desc: "Database name from config/database.yml"
|
|
20
|
+
|
|
21
|
+
desc "dump [DUMP_PATH]", "Dump database (defaults to tmp/<database>-<timestamp>.dump)"
|
|
22
|
+
def dump(dump_path = nil)
|
|
23
|
+
say_config
|
|
24
|
+
dump_path ||= default_dump_path
|
|
25
|
+
FileUtils.mkdir_p(File.dirname(dump_path.to_s))
|
|
26
|
+
|
|
27
|
+
run_pg "pg_dump", "--verbose", "--format=custom", *connection_args, "--file=#{dump_path}"
|
|
28
|
+
|
|
29
|
+
say dump_path.to_s, [:green, :bold]
|
|
30
|
+
end
|
|
31
|
+
map "export" => :dump
|
|
32
|
+
|
|
33
|
+
desc "restore", "Restore database from a dump (defaults to latest in tmp/)"
|
|
34
|
+
option :file, type: :string, desc: "Path to the .dump file"
|
|
35
|
+
def restore
|
|
36
|
+
dump_path = options[:file] || latest_dump
|
|
37
|
+
raise Thor::Error, "No dump found. Pass --file=path/to/file.dump" if dump_path.nil? || !File.exist?(dump_path.to_s)
|
|
38
|
+
|
|
39
|
+
say_config
|
|
40
|
+
say "Dump file: #{dump_path}"
|
|
41
|
+
say ""
|
|
42
|
+
return unless yes?("Do you want to continue? (y/N)", :yellow)
|
|
43
|
+
|
|
44
|
+
if rails.env == "production"
|
|
45
|
+
say "!" * 60, :red
|
|
46
|
+
say "!!! WARNING: YOU ARE RESTORING IN PRODUCTION !!!", [:red, :bold]
|
|
47
|
+
say "!!! THIS WILL DROP AND OVERWRITE THE LIVE DATABASE !!!", [:red, :bold]
|
|
48
|
+
say "!" * 60, :red
|
|
49
|
+
raise Thor::Error, "Aborted" unless yes?("Are you absolutely sure you want to continue? (y/N)", [:red, :bold])
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
run_pg "pg_restore", "--verbose", "--clean", "--if-exists", "--no-owner", "--no-privileges",
|
|
53
|
+
*connection_args, dump_path.to_s
|
|
54
|
+
end
|
|
55
|
+
map "import" => :restore
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def rails
|
|
60
|
+
@rails ||= Efdb::RailsApp.new
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def config
|
|
64
|
+
@config ||= begin
|
|
65
|
+
hash = rails.db_config(options[:db])
|
|
66
|
+
adapter = hash[:adapter].to_s
|
|
67
|
+
unless adapter.match?(/postg/i)
|
|
68
|
+
raise Thor::Error, "Database #{options[:db].inspect} uses #{adapter.inspect} adapter, expected PostgreSQL"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
hash.slice(*CONNECTION_KEYS)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def say_config
|
|
76
|
+
say "Rails env: #{rails.env}"
|
|
77
|
+
say config.reject { |key, _| key == :password }.transform_keys(&:to_s).to_yaml
|
|
78
|
+
say ""
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def default_dump_base_dir
|
|
82
|
+
rails.root.join("tmp", "efdb")
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def default_dump_path
|
|
86
|
+
default_dump_base_dir.join("#{config[:database]}-#{Time.now.to_i}.dump")
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def latest_dump
|
|
90
|
+
Dir.glob(default_dump_base_dir.join("#{config[:database]}*.dump").to_s).max_by { |path| File.mtime(path) }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def connection_args
|
|
94
|
+
{
|
|
95
|
+
host: "--host",
|
|
96
|
+
port: "--port",
|
|
97
|
+
database: "--dbname",
|
|
98
|
+
username: "--username"
|
|
99
|
+
}.reject { |key, _| config[key].to_s.empty? }.map { |key, flag| "#{flag}=#{config[key]}" }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def run_pg(*command)
|
|
103
|
+
env = { "PGPASSWORD" => config[:password]&.to_s }.compact
|
|
104
|
+
redacted = env.map { |key, _| "#{key}=[REDACTED]" }
|
|
105
|
+
|
|
106
|
+
say [*redacted, *command].join(" ")
|
|
107
|
+
return if system(env, *command)
|
|
108
|
+
|
|
109
|
+
raise Thor::Error, "#{command.first} failed (exit status: #{$?&.exitstatus.inspect})"
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
data/lib/efdb/cli.rb
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "thor"
|
|
4
|
+
require_relative "../efdb"
|
|
5
|
+
require_relative "error_handling"
|
|
6
|
+
require_relative "rails_app"
|
|
7
|
+
|
|
8
|
+
module Efdb
|
|
9
|
+
class CLI < Thor
|
|
10
|
+
def self.exit_on_failure?
|
|
11
|
+
true
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
extend ErrorHandling
|
|
15
|
+
|
|
16
|
+
desc "version", "Print efdb version"
|
|
17
|
+
def version
|
|
18
|
+
say Efdb::VERSION
|
|
19
|
+
end
|
|
20
|
+
map %w[-v --version] => :version
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
require_relative "cli/pg"
|
|
25
|
+
|
|
26
|
+
Efdb::CLI.class_eval do
|
|
27
|
+
desc "pg SUBCOMMAND", "PostgreSQL helpers (dump, restore)"
|
|
28
|
+
subcommand "pg", Efdb::CLI::Pg
|
|
29
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "thor"
|
|
4
|
+
require_relative "../efdb"
|
|
5
|
+
|
|
6
|
+
module Efdb
|
|
7
|
+
# Prints raised errors in red; with --verbose or VERBOSE=<anything> also prints the backtrace.
|
|
8
|
+
# Extend a Thor class with it.
|
|
9
|
+
module ErrorHandling
|
|
10
|
+
# Shell that prints errors in red (when the terminal supports colors).
|
|
11
|
+
class Shell < Thor::Base.shell
|
|
12
|
+
def error(statement)
|
|
13
|
+
super(set_color(statement, :red))
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.extended(base)
|
|
18
|
+
base.class_option :verbose, type: :boolean, default: false, desc: "Print backtraces on errors (or set VERBOSE=1)"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def start(given_args = ARGV, config = {})
|
|
22
|
+
verbose = verbose?(given_args)
|
|
23
|
+
config[:shell] ||= Shell.new
|
|
24
|
+
super
|
|
25
|
+
rescue Efdb::Error, Thor::Error => e
|
|
26
|
+
config[:shell].error(e.message)
|
|
27
|
+
warn e.backtrace.join("\n") if verbose && e.backtrace
|
|
28
|
+
exit 1
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def verbose?(args)
|
|
34
|
+
args.include?("--verbose") || !ENV["VERBOSE"].nil?
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Efdb
|
|
4
|
+
# Loads the host Rails application (from the current working directory)
|
|
5
|
+
# and exposes what the CLI needs from it.
|
|
6
|
+
class RailsApp
|
|
7
|
+
def initialize(root: Dir.pwd)
|
|
8
|
+
@root = root
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def load!
|
|
12
|
+
return if defined?(::Rails) && ::Rails.respond_to?(:application) && ::Rails.application
|
|
13
|
+
|
|
14
|
+
environment = File.join(@root, "config", "environment.rb")
|
|
15
|
+
raise Efdb::Error, "Rails app not found (missing #{environment}). Run efdb from your Rails app root." unless File.exist?(environment)
|
|
16
|
+
|
|
17
|
+
require environment
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def root
|
|
21
|
+
load!
|
|
22
|
+
::Rails.root
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def env
|
|
26
|
+
load!
|
|
27
|
+
::Rails.env.to_s
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def db_config(name)
|
|
31
|
+
load!
|
|
32
|
+
config = ::ActiveRecord::Base.configurations.configs_for(env_name: env, name: name, include_hidden: true)
|
|
33
|
+
raise Efdb::Error, "No database configuration named #{name.inspect} for #{env} environment" unless config
|
|
34
|
+
|
|
35
|
+
config.configuration_hash
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/efdb/version.rb
ADDED
data/lib/efdb.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: efdb
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Effectiva studio
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: thor
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '1.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '1.0'
|
|
26
|
+
executables:
|
|
27
|
+
- efdb
|
|
28
|
+
extensions: []
|
|
29
|
+
extra_rdoc_files: []
|
|
30
|
+
files:
|
|
31
|
+
- Gemfile
|
|
32
|
+
- Gemfile.lock
|
|
33
|
+
- LICENSE
|
|
34
|
+
- README.md
|
|
35
|
+
- Rakefile
|
|
36
|
+
- efdb.gemspec
|
|
37
|
+
- exe/efdb
|
|
38
|
+
- lib/efdb.rb
|
|
39
|
+
- lib/efdb/cli.rb
|
|
40
|
+
- lib/efdb/cli/pg.rb
|
|
41
|
+
- lib/efdb/error_handling.rb
|
|
42
|
+
- lib/efdb/rails_app.rb
|
|
43
|
+
- lib/efdb/version.rb
|
|
44
|
+
homepage: https://github.com/EffectivaStudio/efdb
|
|
45
|
+
licenses:
|
|
46
|
+
- MIT
|
|
47
|
+
metadata:
|
|
48
|
+
homepage_uri: https://github.com/EffectivaStudio/efdb
|
|
49
|
+
source_code_uri: https://github.com/EffectivaStudio/efdb
|
|
50
|
+
rdoc_options: []
|
|
51
|
+
require_paths:
|
|
52
|
+
- lib
|
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: 2.6.0
|
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '0'
|
|
63
|
+
requirements: []
|
|
64
|
+
rubygems_version: 4.0.16
|
|
65
|
+
specification_version: 4
|
|
66
|
+
summary: Database helpers (PostgreSQL dump/restore) for Ruby on Rails apps
|
|
67
|
+
test_files: []
|