From 2d924c8eeae721ee5afffcca669fa515590a9ab1 Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Wed, 27 Sep 2023 16:42:19 +0300 Subject: [PATCH 01/11] updating heroku db script --- Gemfile | 3 +- Gemfile.lock | 12 ++++++++ ruby_script.rb | 82 +++++++++++++++++++++++++++++++------------------- 3 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile index a224453..3d3da0d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1 @@ -gem 'open3', '~> 0.1.0' -gem 'getopt', '~> 1.5', '>= 1.5.1' \ No newline at end of file +gem 'open3', '~> 0.1.0' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..b9ca3b0 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,12 @@ +GEM + specs: + open3 (0.1.1) + +PLATFORMS + arm64-darwin-22 + +DEPENDENCIES + open3 (~> 0.1.0) + +BUNDLED WITH + 2.2.15 diff --git a/ruby_script.rb b/ruby_script.rb index 47acd41..80a5b29 100755 --- a/ruby_script.rb +++ b/ruby_script.rb @@ -4,7 +4,51 @@ require 'open3' require 'getoptlong' -# This script gets heroku database and applies it to the local project +DATABASE_URL_COMMAND = 'heroku config:get DATABASE_URL --remote=' + +def print_help + puts <<~EOF + Usage: #{File.basename(__FILE__)} [OPTIONS] + + Options: + -h, --help: + Show this help message. + + -a, --app_name APP_NAME: + Heroku app name needed for db URL. + + -n, --db_name DB_NAME: + Local database name. + + -u, --user USER: + Optional: Database user name. + + -p, --password PASSWORD: + Optional: Database password. + + EOF +end + +def run_command(command) + stdout, stderr, status = Open3.capture3(command) + raise "Error running command: #{command}\n#{stderr}" unless status.success? + + stdout.strip +end + +def backup_database(heroku_database_url, backup_file) + puts 'Backing up the Heroku database...' + command = "pg_dump #{heroku_database_url} > #{backup_file}" + run_command(command) + puts 'Backup completed!' +end + +def restore_database(db_name, backup_file) + puts 'Restoring the local database...' + command = "pg_restore -d #{db_name} --no-owner --no-privilege --data-only #{backup_file}" + run_command(command) + puts 'Restore completed!' +end opts = GetoptLong.new( ['--help', '-h', GetoptLong::NO_ARGUMENT], @@ -14,9 +58,7 @@ ['--password', '-p', GetoptLong::OPTIONAL_ARGUMENT] ) -# heroku db url heroku_database_url = nil - user = nil password = '' db_name = '' @@ -24,16 +66,8 @@ opts.each do |opt, arg| case opt when '--help' - puts <<~EOF - hello [OPTION] ...#{' '} - - -h, --help: - show help - - --app_name [app_name]: - heroku app name needed for db url - - EOF + print_help + exit(0) when '--password' password = arg when '--user' @@ -41,27 +75,13 @@ when '--db_name' db_name = arg when '--app_name' - if arg == '' - exit 0 - else - heroku_database_url = `heroku config:get DATABASE_URL --remote=#{arg}`.strip - end + heroku_database_url = run_command("#{DATABASE_URL_COMMAND}#{arg}") end end -return if heroku_database_url.nil? - -# Backup and restore database -def backup_database(heroku_database_url, backup_file) - puts 'Backing up the Heroku database...' - `pg_dump "#{heroku_database_url}" > "#{backup_file}"` - puts 'Backup completed!' -end - -def restore_database(db_name, backup_file) - puts 'Restoring the local database...' - `pg_restore -d "#{db_name}" --no-owner --no-privilege --data-only "#{backup_file}"` - puts 'Restore completed!' +unless heroku_database_url + puts 'Heroku database URL not found. Please provide a valid app name.' + exit(1) end # Generate a backup file name From cbe6062a57d6c795a16b99db98cc5d97df69d8bb Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Wed, 27 Sep 2023 16:44:07 +0300 Subject: [PATCH 02/11] Update Gemfile.lock --- Gemfile.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile.lock b/Gemfile.lock index b9ca3b0..97ac566 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,6 +4,7 @@ GEM PLATFORMS arm64-darwin-22 + x86_64-linux DEPENDENCIES open3 (~> 0.1.0) From 45520193625790ec817aa650e82a76b3d26ac5ae Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Wed, 27 Sep 2023 17:02:42 +0300 Subject: [PATCH 03/11] Changing the workflow --- .github/workflows/rubocop.yml | 2 ++ .github/workflows/ruby.yml | 3 +++ Gemfile | 2 ++ 3 files changed, 7 insertions(+) create mode 100644 .github/workflows/rubocop.yml diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml new file mode 100644 index 0000000..53400b4 --- /dev/null +++ b/.github/workflows/rubocop.yml @@ -0,0 +1,2 @@ +Style/FrozenStringLiteralComment: + Enabled: false \ No newline at end of file diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 3d479c6..6a87729 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -36,3 +36,6 @@ jobs: bundler-cache: true # runs 'bundle install' and caches installed gems automatically - name: Run tests run: bundle exec rake + + - name: Run RuboCop + run: bundle exec rubocop diff --git a/Gemfile b/Gemfile index 3d3da0d..78bc48f 100644 --- a/Gemfile +++ b/Gemfile @@ -1 +1,3 @@ +gem 'rake' +gem 'rubocop', require: false gem 'open3', '~> 0.1.0' \ No newline at end of file From 2310ea4b7a6e53cca3fafc20730ee01407c89693 Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Wed, 27 Sep 2023 17:03:45 +0300 Subject: [PATCH 04/11] Update Gemfile.lock --- Gemfile.lock | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index 97ac566..48efa81 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,35 @@ GEM specs: + ast (2.4.2) + base64 (0.1.1) + json (2.6.3) + language_server-protocol (3.17.0.3) open3 (0.1.1) + parallel (1.23.0) + parser (3.2.2.3) + ast (~> 2.4.1) + racc + racc (1.7.1) + rainbow (3.1.1) + rake (13.0.6) + regexp_parser (2.8.1) + rexml (3.2.6) + rubocop (1.56.1) + base64 (~> 0.1.1) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.2.2.3) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.28.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.29.0) + parser (>= 3.2.1.0) + ruby-progressbar (1.13.0) + unicode-display_width (2.4.2) PLATFORMS arm64-darwin-22 @@ -8,6 +37,8 @@ PLATFORMS DEPENDENCIES open3 (~> 0.1.0) + rake + rubocop BUNDLED WITH 2.2.15 From d211ddad2ce6e98e78e4376230ec516553dc9127 Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Wed, 27 Sep 2023 17:06:53 +0300 Subject: [PATCH 05/11] adding tool versions --- .github/workflows/ruby.yml | 2 +- .too-version | 1 + Gemfile | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 .too-version diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 6a87729..bf377f9 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -36,6 +36,6 @@ jobs: bundler-cache: true # runs 'bundle install' and caches installed gems automatically - name: Run tests run: bundle exec rake - + - name: Run RuboCop run: bundle exec rubocop diff --git a/.too-version b/.too-version new file mode 100644 index 0000000..a7e49f2 --- /dev/null +++ b/.too-version @@ -0,0 +1 @@ +ruby 3.0.1 diff --git a/Gemfile b/Gemfile index 78bc48f..bc0a7dc 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +source 'https://rubygems.org' + gem 'rake' gem 'rubocop', require: false gem 'open3', '~> 0.1.0' \ No newline at end of file From b8c073ef810d8f46d219d7b29cde82228019c09c Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Wed, 27 Sep 2023 17:36:34 +0300 Subject: [PATCH 06/11] Update Gemfile.lock --- Gemfile.lock | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 48efa81..54ef4d7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,10 +1,11 @@ GEM + remote: https://rubygems.org/ specs: ast (2.4.2) base64 (0.1.1) json (2.6.3) language_server-protocol (3.17.0.3) - open3 (0.1.1) + open3 (0.1.2) parallel (1.23.0) parser (3.2.2.3) ast (~> 2.4.1) @@ -14,7 +15,7 @@ GEM rake (13.0.6) regexp_parser (2.8.1) rexml (3.2.6) - rubocop (1.56.1) + rubocop (1.56.3) base64 (~> 0.1.1) json (~> 2.3) language_server-protocol (>= 3.17.0) From 264d8b0c1e33b7d2ab84cff0e15f2863ed5cc647 Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Wed, 27 Sep 2023 17:59:49 +0300 Subject: [PATCH 07/11] Update ruby.yml --- .github/workflows/ruby.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index bf377f9..d2c9133 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -34,8 +34,6 @@ jobs: with: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true # runs 'bundle install' and caches installed gems automatically - - name: Run tests - run: bundle exec rake - name: Run RuboCop run: bundle exec rubocop From 5631bb6fabea2a430bcc2901af2df6c7db2cdaca Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Wed, 27 Sep 2023 18:13:04 +0300 Subject: [PATCH 08/11] fix offenses --- Gemfile | 4 +++- ruby_script.rb | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index bc0a7dc..9accb4c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,7 @@ +# frozen_string_literal: true + source 'https://rubygems.org' +gem 'open3', '~> 0.1.0' gem 'rake' gem 'rubocop', require: false -gem 'open3', '~> 0.1.0' \ No newline at end of file diff --git a/ruby_script.rb b/ruby_script.rb index 80a5b29..7797adf 100755 --- a/ruby_script.rb +++ b/ruby_script.rb @@ -6,8 +6,9 @@ DATABASE_URL_COMMAND = 'heroku config:get DATABASE_URL --remote=' +# rubocop:disable Metrics/MethodLength def print_help - puts <<~EOF + puts ` Usage: #{File.basename(__FILE__)} [OPTIONS] Options: @@ -25,9 +26,9 @@ def print_help -p, --password PASSWORD: Optional: Database password. - - EOF + ` end +# rubocop:enable Metrics/MethodLength def run_command(command) stdout, stderr, status = Open3.capture3(command) From 2118668dec7e8da335a2956edfc7db2e96670246 Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Wed, 27 Sep 2023 18:14:47 +0300 Subject: [PATCH 09/11] Update ruby.yml --- .github/workflows/ruby.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index d2c9133..0f8391e 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -22,7 +22,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby-version: ['2.6', '2.7', '3.0'] + ruby-version: ['2.7', '3.0'] steps: - uses: actions/checkout@v3 From 7de55a43251890024aded7f95b684ff1b5412f84 Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Fri, 15 Dec 2023 17:51:41 +0200 Subject: [PATCH 10/11] Update README.MD --- README.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.MD b/README.MD index 76133b6..cd9ecfc 100644 --- a/README.MD +++ b/README.MD @@ -1,4 +1,4 @@ -Creating some ruby scripts +Ruby script so for importing heroku PG database to a project. In case you are using the 'fish' terminal you need to add the function with the path to the script From c4a61ef8bbfa7e8726a953571fbe693f0ac034c6 Mon Sep 17 00:00:00 2001 From: Yavor Dashev <58559918+y-dashev@users.noreply.github.com> Date: Wed, 22 May 2024 10:20:06 +0300 Subject: [PATCH 11/11] Update README.MD --- README.MD | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.MD b/README.MD index cd9ecfc..237eb27 100644 --- a/README.MD +++ b/README.MD @@ -5,8 +5,8 @@ In case you are using the 'fish' terminal you need to add the function with the Like so: ``` -function rubycode - [$your absolute path to - ruby_script.rb] $argv +function pg_db_import # or you can name it as you want + [$your absolute path to - pg_db_import.rb] $argv end ```