rails_database_await 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ab25417a8d7f0460b7309d17e44a5118681f9d38e6b595ff25e95b34ea8adb4b
4
+ data.tar.gz: f9001704cd318349fa8f9b6835059f3a35b84852ad876dfe9e3e74c22e58fc74
5
+ SHA512:
6
+ metadata.gz: 47fd4eb74da635b165841e3b0543e10e399ab1c14339bd088ff8228c990bb768357217b235585f3becdd4e00ba33538045a5728ee79012337ff4e41e1fd5196d
7
+ data.tar.gz: 69748ece0d638b938e612a4df81a956824bee6ed232abc5ef9da8828a8192002ac88fa41fd02099a5246200a3bee829035c8aa29109c29589f69a3085cb3f0b7
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-07-07
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Micke Lisinge
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,39 @@
1
+ # RailsDatabaseAwait
2
+
3
+ This gem was created to solve the problem when you might have to wait until the database is ready before continuing.
4
+ For example when running migrations in a kubernetes job while using Google Cloud SQL Auth Proxy as a sidecar.
5
+
6
+ ## Installation
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ $ bundle add rails_database_await
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ $ gem install rails_database_await
15
+
16
+ ## Usage
17
+
18
+ `bundle exec rails db:await`
19
+
20
+ Configure the number of times we should be trying to connect to the database by setting the `DATABASE_AWAIT_MAX_TRIES` (Defaults to 30).
21
+ Configure the number of seconds to wait between each time we try to connect to the database by setting the `DATABASE_AWAIT_WAIT_TIME` (Defaults to 1).
22
+
23
+ For example; To wait for the database to be ready before running migrations we can just do:
24
+
25
+ `bundle exec rails db:await db:migrate`
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/apoex/rails_database_await.
36
+
37
+ ## License
38
+
39
+ 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,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,13 @@
1
+ require "rails_database_await"
2
+ require "rails"
3
+
4
+ module RailsDatabaseAwait
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :rails_database_await
7
+
8
+ rake_tasks do
9
+ path = File.expand_path(__dir__)
10
+ load "#{path}/tasks/db/await.rake"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ MAX_TRIES = ENV.fetch("DATABASE_AWAIT_MAX_TRIES", 30).to_i
2
+ WAIT_TIME = ENV.fetch("DATABASE_AWAIT_WAIT_TIME", 1).to_i
3
+
4
+ namespace :db do
5
+ task await: :environment do
6
+ tries ||= 0
7
+ tries += 1
8
+
9
+ ActiveRecord::Base.connection
10
+ puts "Database available"
11
+ rescue PG::ConnectionBad, ActiveRecord::ConnectionNotEstablished
12
+ if tries >= MAX_TRIES
13
+ puts "#{MAX_TRIES} tries exceeded, exiting"
14
+ exit 1
15
+ end
16
+
17
+ puts "Database unavailable, waiting #{WAIT_TIME} second. #{MAX_TRIES - tries} tries left."
18
+
19
+ sleep WAIT_TIME
20
+ retry
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsDatabaseAwait
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rails_database_await/version"
4
+ require_relative "rails_database_await/railtie" if defined?(Rails)
5
+
6
+ module RailsDatabaseAwait
7
+ end
@@ -0,0 +1,4 @@
1
+ module RailsDatabaseAwait
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_database_await
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ApoEx
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ description: Await readiness of database, when using Google Cloud SQL Auth Proxy for
28
+ example.
29
+ email:
30
+ - tech-lead@apoex.se
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - CHANGELOG.md
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - lib/rails_database_await.rb
40
+ - lib/rails_database_await/railtie.rb
41
+ - lib/rails_database_await/tasks/db/await.rake
42
+ - lib/rails_database_await/version.rb
43
+ - sig/rails_database_await.rbs
44
+ homepage: https://github.com/apoex/rails_database_await
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ allowed_push_host: https://rubygems.org
49
+ homepage_uri: https://github.com/apoex/rails_database_await
50
+ source_code_uri: https://github.com/apoex/rails_database_await
51
+ changelog_uri: https://github.com/apoex/rails_database_await
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.6.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.4.1
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Await readiness of database, when using Google Cloud SQL Auth Proxy for example.
71
+ test_files: []