potluck-postgres 0.0.1

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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +16 -0
  3. data/README.md +31 -0
  4. data/lib/potluck/postgres.rb +132 -0
  5. metadata +112 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5350781163dc218410b42e790bedb5c2ca246572e1334672ea9471c81efb432c
4
+ data.tar.gz: fdd55c1e5b645f6f0c99df93b9761e8830776b52b32e5310cd0def002e5c4f51
5
+ SHA512:
6
+ metadata.gz: 9e9475b41d894b36972aa8481eb06dfc6d1d0497d88a6841dfe92b5179bbb205db7f1cf48995df077aed00dbbc49371c553ccc5914154e15ca9c5684ed4bb9fe
7
+ data.tar.gz: d6df0284807374eafa9d9e48bebed6f79f00cbeb1398f6564831c2e1be703720ca059fbc5a09e0d84819f4f63f9fa5e8ff4519fc3f113a54f6696fd069c8fe86
data/LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ Copyright 2021 Nate Pickens
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4
+ documentation files (the "Software"), to deal in the Software without restriction, including without
5
+ limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
6
+ the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
7
+ conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial
10
+ portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13
+ LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
14
+ EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
15
+ AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
16
+ OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Potluck - Postgres
2
+
3
+ An extension to the Potluck gem that provides some basic utilities for setting up and connecting to Postgres
4
+ databases, as well as control over the Postgres process.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your Gemfile:
9
+
10
+ ```ruby
11
+ gem('potluck-postgres')
12
+ ```
13
+
14
+ Or install manually on the command line:
15
+
16
+ ```bash
17
+ gem install potluck-postgres
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ [Coming soon.]
23
+
24
+ ## Contributing
25
+
26
+ Bug reports and pull requests are welcome on GitHub at https://github.com/npickens/potluck.
27
+
28
+ ## License
29
+
30
+ The gem is available as open source under the terms of the
31
+ [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('potluck')
4
+
5
+ module Potluck
6
+ class Postgres < Dish
7
+ attr_reader(:database)
8
+
9
+ def initialize(config, **args)
10
+ super(**args)
11
+
12
+ @config = config
13
+ end
14
+
15
+ def connect
16
+ (tries ||= 0) && (tries += 1)
17
+ @database = Sequel.connect(@config, logger: @logger)
18
+ rescue Sequel::DatabaseConnectionError => e
19
+ if (dud = Sequel::DATABASES.last)
20
+ dud.disconnect
21
+ Sequel.synchronize { Sequel::DATABASES.delete(dud) }
22
+ end
23
+
24
+ if e.message =~ /role .* does not exist/ && tries == 1
25
+ create_database_role
26
+ create_database
27
+ retry
28
+ elsif e.message =~ /database .* does not exist/ && tries == 1
29
+ create_database
30
+ retry
31
+ elsif (@is_local && tries < 3) && (e.message.include?('could not connect') ||
32
+ e.message.include?('the database system is starting up'))
33
+ sleep(1)
34
+ retry
35
+ elsif e.message.include?('could not connect')
36
+ abort("#{e.class}: #{e.message.strip}")
37
+ else
38
+ abort("#{e.class}: #{e.message.strip}\n #{e.backtrace.join("\n ")}")
39
+ end
40
+ end
41
+
42
+ def disconnect
43
+ @database&.disconnect
44
+ end
45
+
46
+ def create_database_role
47
+ tmp_config = @config.dup
48
+ tmp_config[:database] = 'postgres'
49
+ tmp_config[:username] = ENV['USER']
50
+ tmp_config[:password] = nil
51
+
52
+ begin
53
+ Sequel.connect(tmp_config, logger: @logger) do |database|
54
+ database.execute("CREATE ROLE #{@config[:username]} WITH LOGIN CREATEDB REPLICATION PASSWORD "\
55
+ "'#{@config[:password]}'")
56
+ end
57
+ rescue => e
58
+ @logger.error("#{e.class}: #{e.message.strip}\n #{e.backtrace.join("\n ")}\n")
59
+ abort("Could not create role '#{@config[:username]}'. Make sure database user '#{ENV['USER']}' "\
60
+ 'has permission to do so, or create it manually.')
61
+ end
62
+ end
63
+
64
+ def create_database
65
+ tmp_config = @config.dup
66
+ tmp_config[:database] = 'postgres'
67
+
68
+ begin
69
+ Sequel.connect(tmp_config, logger: @logger) do |database|
70
+ database.execute("CREATE DATABASE #{@config[:database]}")
71
+ end
72
+ rescue => e
73
+ @logger.error("#{e.class}: #{e.message.strip}\n #{e.backtrace.join("\n ")}\n")
74
+ abort("Could not create database '#{@config[:database]}'. Make sure database user "\
75
+ "'#{@config[:username]}' has permission to do so, or create it manually.")
76
+ end
77
+ end
78
+
79
+ def migrate(dir, steps = nil)
80
+ return unless File.directory?(dir)
81
+
82
+ Sequel.extension(:migration)
83
+
84
+ # Suppress Sequel schema migration table queries.
85
+ original_level = @logger.level
86
+ @logger.level = Logger::WARN
87
+
88
+ args = [Sequel::Model.db, dir, {allow_missing_migration_files: true}]
89
+ migrator = Sequel::TimestampMigrator.new(*args)
90
+
91
+ return if migrator.files.empty?
92
+
93
+ if steps
94
+ all = migrator.files.map { |f| File.basename(f) }
95
+ applied = migrator.applied_migrations
96
+ current = applied.last
97
+
98
+ return if applied.empty? && steps <= 0
99
+
100
+ index = [[0, (all.index(current) || -1) + steps].max, all.size].min
101
+ file = all[index]
102
+
103
+ args.last[:target] = migrator.send(:migration_version_from_file, file)
104
+ end
105
+
106
+ migrator = Sequel::TimestampMigrator.new(*args)
107
+ @logger.level = original_level
108
+ migrator.run
109
+ end
110
+
111
+ private
112
+
113
+ def self.plist
114
+ super(
115
+ <<~EOS
116
+ <key>ProgramArguments</key>
117
+ <array>
118
+ <string>/usr/local/opt/postgresql/bin/postgres</string>
119
+ <string>-D</string>
120
+ <string>/usr/local/var/postgres</string>
121
+ </array>
122
+ <key>WorkingDirectory</key>
123
+ <string>/usr/local</string>
124
+ <key>StandardOutPath</key>
125
+ <string>/usr/local/var/log/postgres.log</string>
126
+ <key>StandardErrorPath</key>
127
+ <string>/usr/local/var/log/postgres.log</string>
128
+ EOS
129
+ )
130
+ end
131
+ end
132
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: potluck-postgres
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nate Pickens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: potluck
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: sequel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.41'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.41'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 5.11.2
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: 6.0.0
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 5.11.2
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: 6.0.0
75
+ description: An extension to the Potluck gem that provides some basic utilities for
76
+ setting up and connecting to Postgres databases, as well as control over the Postgres
77
+ process.
78
+ email:
79
+ executables: []
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - LICENSE
84
+ - README.md
85
+ - lib/potluck/postgres.rb
86
+ homepage: https://github.com/npickens/potluck/tree/master/potluck-postgres
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ allowed_push_host: https://rubygems.org
91
+ homepage_uri: https://github.com/npickens/potluck/tree/master/potluck-postgres
92
+ source_code_uri: https://github.com/npickens/potluck/tree/master/potluck-postgres
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: 2.5.8
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.2.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A Ruby manager for Postgres.
112
+ test_files: []