postgressor 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ac951676c1041a11f52a59de5c58154648a6d77a0a4d5828b8ac4a8045c352d9
4
+ data.tar.gz: b87bcf598f561ab1385de799a705417bc7d153f560f47fee655f7067de6775d6
5
+ SHA512:
6
+ metadata.gz: e971443893ae079d788c7b598dc635c0e887218ace8eac059932bfe30969168c98e1ca363c677524ba89af9b0418276d78efe32725faeca4f642f8a26731847c
7
+ data.tar.gz: b4778abdf4caa5e930982bb324ebed27a29350d47cd09d28b14b5d2916025e146699e4c98a002be05f1e0166908356a5699db44db22f03803355f7fd6e4eb260
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.17.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in postgressor.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Victor Afanasev
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Postgressor
2
+
3
+ Get tired of typing the same commands over and over like creating Postgres user, database, creating and restoring database backups? Postgressor allow you to manage your (Postgres) application database and user within simple commands:
4
+
5
+ ```
6
+ $ postgressor -h
7
+ Commands:
8
+ postgressor createuser # Create app database user
9
+ postgressor dropuser # Drop app database user
10
+ postgressor createdb # Create app database
11
+ postgressor dropdb # Drop app database
12
+ postgressor dumpdb # Dump (backup) app database
13
+ postgressor restoredb # Restore app database from backup
14
+
15
+ postgressor --version, -v # Print the version
16
+ postgressor help [COMMAND] # Describe available commands or one specific command
17
+ ```
18
+
19
+ All what you need is DATABASE_URL in format like: `DATABASE_URL="postgres://app_user:app_user_pass@host/app_db_name"`. Also Postgressor automatically check `.env` file (if present) **to read DATABASE_URL from there.**
20
+
21
+ ## Installation
22
+
23
+ > Posgressor requires Ruby `>= 2.3.0`.
24
+
25
+ To install:
26
+
27
+ ```
28
+ $ gem install postgressor
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ > Use `VERBOSE=true` env option (used in examples below) to print postgres commands which will be executed
34
+
35
+ ### createuser and dropuser
36
+
37
+ To create user:
38
+
39
+ > You can provide `--superuser` option to create user as SUPERUSER
40
+
41
+ ```
42
+ $ DATABASE_URL="postgres://app_user:123456@localhost/app_db" VERBOSE=true postgressor createuser
43
+
44
+ Executing: sudo -i -u postgres psql -c CREATE USER app_user WITH CREATEDB LOGIN PASSWORD '123456';
45
+ CREATE ROLE
46
+ Created user app_user
47
+ ```
48
+
49
+ To drop user:
50
+
51
+ ```
52
+ $ DATABASE_URL="postgres://app_user:123456@localhost/app_db" VERBOSE=true postgressor dropuser
53
+
54
+ Executing: sudo -i -u postgres dropuser app_user
55
+ Dropped user app_user
56
+ ```
57
+
58
+ ### createdb and dropdb
59
+
60
+ To create database:
61
+
62
+ ```
63
+ $ DATABASE_URL="postgres://app_user:123456@localhost/app_db" VERBOSE=true postgressor createdb
64
+
65
+ Executing: PGPASSWORD=123456 createdb app_db -h localhost -U app_user
66
+ Created database app_db
67
+ ```
68
+
69
+ To drop database:
70
+
71
+ ```
72
+ $ DATABASE_URL="postgres://app_user:123456@localhost/app_db" VERBOSE=true postgressor dropdb
73
+
74
+ Executing: PGPASSWORD=123456 dropdb app_db -h localhost -U app_user
75
+ Dropped database app_db
76
+ ```
77
+
78
+ ### dumpdb and restoredb
79
+
80
+ To perform database backup to the current directory:
81
+
82
+ ```
83
+ $ DATABASE_URL="postgres://app_user:123456@localhost/app_db" VERBOSE=true postgressor dumpdb
84
+
85
+ Executing: PGPASSWORD=123456 pg_dump app_db -Fc --no-acl --no-owner -f app_db.dump -h localhost -U app_user
86
+ Dumped database app_db to app_db.dump file
87
+ ```
88
+
89
+ To restore database from backup file:
90
+
91
+ > Note: sometimes backup restore will fail if current user is not superuser, so there is option `--switch_to_superuser` to temporary switch user to SUPERUSER
92
+
93
+ > Note: recreate (drop and create) database before restore to omit some possible errors
94
+
95
+ ```
96
+ $ DATABASE_URL="postgres://app_user:123456@localhost/app_db" VERBOSE=true postgressor restoredb app_db.dump --switch_to_superuser
97
+
98
+ Executing: sudo -i -u postgres psql -c ALTER ROLE app_user SUPERUSER;
99
+ ALTER ROLE
100
+ Set user app_user to SUPERUSER
101
+ Executing: PGPASSWORD=123456 pg_restore app_db.dump -d app_db --no-acl --no-owner --verbose -h localhost -U app_user
102
+ pg_restore: connecting to database for restore
103
+ pg_restore: creating SCHEMA "public"
104
+ pg_restore: creating COMMENT "SCHEMA public"
105
+ pg_restore: creating EXTENSION "plpgsql"
106
+ pg_restore: creating COMMENT "EXTENSION plpgsql"
107
+ Restored database app_db from app_db.dump file
108
+ Executing: sudo -i -u postgres psql -c ALTER ROLE app_user NOSUPERUSER;
109
+ ALTER ROLE
110
+ Set user app_user to NOSUPERUSER
111
+ ```
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "postgressor"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/postgressor ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'postgressor'
4
+ require 'postgressor/cli'
5
+
6
+ Postgressor::CLI.start(ARGV)
@@ -0,0 +1,151 @@
1
+ require 'uri'
2
+ require 'thor'
3
+ require 'dotenv/load'
4
+
5
+ module Postgressor
6
+ class CLI < Thor
7
+ # https://www.postgresql.org/docs/current/app-createuser.html
8
+ desc "createuser", "Create app database user"
9
+ option :superuser, type: :string, banner: "Create user as superuser"
10
+ def createuser
11
+ preload!
12
+
13
+ # Use psql `CREATE USER` instead of `createuser` CLI command, to automatically provide user password:
14
+ is_superuser = "SUPERUSER" if options[:superuser]
15
+ psql_command = "CREATE USER #{@conf[:user]} WITH CREATEDB LOGIN #{is_superuser} PASSWORD '#{@conf[:password]}';"
16
+
17
+ command = %W(sudo -i -u postgres psql -c #{psql_command})
18
+ if execute command
19
+ say "Created user #{@conf[:user]}", :green
20
+ end
21
+ end
22
+
23
+ # https://www.postgresql.org/docs/current/app-dropuser.html
24
+ desc "dropuser", "Drop app database user"
25
+ def dropuser
26
+ preload!
27
+
28
+ command = %W(sudo -i -u postgres dropuser #{@conf[:user]})
29
+ if execute command
30
+ say "Dropped user #{@conf[:user]}", :green
31
+ end
32
+ end
33
+
34
+ ###
35
+
36
+ # https://www.postgresql.org/docs/current/app-createdb.html
37
+ desc "createdb", "Create app database"
38
+ def createdb
39
+ preload!
40
+
41
+ command = ["createdb", @conf[:db]] + @pg_cli_args
42
+ if execute command, with_env: true
43
+ say "Created database #{@conf[:db]}", :green
44
+ end
45
+ end
46
+
47
+ # https://www.postgresql.org/docs/current/app-dropdb.html
48
+ desc "dropdb", "Drop app database"
49
+ def dropdb
50
+ preload!
51
+
52
+ command = ["dropdb", @conf[:db]] + @pg_cli_args
53
+ if execute command, with_env: true
54
+ say "Dropped database #{@conf[:db]}", :green
55
+ end
56
+ end
57
+
58
+ ###
59
+
60
+ # https://www.postgresql.org/docs/current/app-pgdump.html
61
+ desc "dumpdb", "Dump (backup) app database"
62
+ def dumpdb
63
+ preload!
64
+
65
+ dump_file_name = "#{@conf[:db]}.dump"
66
+ command = %W(pg_dump #{@conf[:db]} -Fc --no-acl --no-owner -f #{dump_file_name}) + @pg_cli_args
67
+
68
+ if execute command, with_env: true
69
+ say "Dumped database #{@conf[:db]} to #{dump_file_name} file", :green
70
+ end
71
+ end
72
+
73
+ # https://www.postgresql.org/docs/current/app-pgrestore.html
74
+ desc "restoredb", "Restore app database from backup"
75
+ option :switch_to_superuser, type: :string, banner: "Temporary switch user to SUPERUSER while restoring db"
76
+ def restoredb(dump_file_path)
77
+ preload!
78
+
79
+ set_user_to_superuser if options[:switch_to_superuser]
80
+
81
+ command = %W(pg_restore #{dump_file_path} -d #{@conf[:db]} --no-acl --no-owner --verbose) + @pg_cli_args
82
+ if execute command, with_env: true
83
+ say "Restored database #{@conf[:db]} from #{dump_file_path} file", :green
84
+ end
85
+
86
+ set_user_to_nosuperuser if options[:switch_to_superuser]
87
+ end
88
+
89
+ ###
90
+
91
+ map %w[--version -v] => :__print_version
92
+ desc "--version, -v", "Print the version"
93
+ def __print_version
94
+ puts VERSION
95
+ end
96
+
97
+ private
98
+
99
+ def set_user_to_superuser
100
+ psql_command = "ALTER ROLE #{@conf[:user]} SUPERUSER;"
101
+ command = %W(sudo -i -u postgres psql -c #{psql_command})
102
+ if execute command
103
+ say "Set user #{@conf[:user]} to SUPERUSER", :green
104
+ end
105
+ end
106
+
107
+ def set_user_to_nosuperuser
108
+ psql_command = "ALTER ROLE #{@conf[:user]} NOSUPERUSER;"
109
+ command = %W(sudo -i -u postgres psql -c #{psql_command})
110
+ if execute command
111
+ say "Set user #{@conf[:user]} to NOSUPERUSER", :green
112
+ end
113
+ end
114
+
115
+ def execute(command, with_env: false)
116
+ if with_env
117
+ verbose_command = env.map { |k, v| "#{k}=#{v}" } + command
118
+ say("Executing: #{verbose_command.join(' ')}", :yellow) if ENV["VERBOSE"] == "true"
119
+
120
+ system env, *command
121
+ else
122
+ say("Executing: #{command.join(' ')}", :yellow) if ENV["VERBOSE"] == "true"
123
+ system *command
124
+ end
125
+ end
126
+
127
+ def env
128
+ { "PGPASSWORD" => @conf[:password] }
129
+ end
130
+
131
+ def preload!
132
+ url = ENV["DATABASE_URL"]
133
+ raise "Env variable DATABASE_URL is not provided" if url.nil? || url.strip.empty?
134
+
135
+ uri = URI.parse(url)
136
+ raise "DB adapter is not postgres" if uri.scheme != "postgres"
137
+
138
+ @conf = {
139
+ url: url,
140
+ db: uri.path.sub("/", ""),
141
+ host: uri.host,
142
+ port: uri.port,
143
+ user: uri.user,
144
+ password: uri.password
145
+ }
146
+
147
+ @pg_cli_args = ["-h", @conf[:host], "-U", @conf[:user]]
148
+ @pg_cli_args += ["-p", @conf[:port].to_s] if @conf[:port]
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,3 @@
1
+ module Postgressor
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "postgressor/version"
2
+
3
+ module Postgressor
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "postgressor/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "postgressor"
8
+ spec.version = Postgressor::VERSION
9
+ spec.authors = ["Victor Afanasev"]
10
+ spec.email = ["vicfreefly@gmail.com"]
11
+
12
+ spec.summary = "Manage your application Postgres user and database easily using simple CLI commands"
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/vifreefly/postgressor"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+
23
+ spec.bindir = "exe"
24
+ spec.executables = "postgressor"
25
+ spec.require_paths = ["lib"]
26
+ spec.required_ruby_version = ">= 2.3.0"
27
+
28
+ spec.add_dependency "thor"
29
+ spec.add_dependency "dotenv"
30
+ spec.add_development_dependency "bundler", "~> 1.17"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "minitest", "~> 5.0"
33
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postgressor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Victor Afanasev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dotenv
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.17'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.17'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: Manage your application Postgres user and database easily using simple
84
+ CLI commands
85
+ email:
86
+ - vicfreefly@gmail.com
87
+ executables:
88
+ - postgressor
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - exe/postgressor
101
+ - lib/postgressor.rb
102
+ - lib/postgressor/cli.rb
103
+ - lib/postgressor/version.rb
104
+ - postgressor.gemspec
105
+ homepage: https://github.com/vifreefly/postgressor
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 2.3.0
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.7.6
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Manage your application Postgres user and database easily using simple CLI
129
+ commands
130
+ test_files: []