Skip to content

Commit

Permalink
Merge branch 'master' into rename-file
Browse files Browse the repository at this point in the history
  • Loading branch information
y-dashev committed May 22, 2024
2 parents 8b69a6f + c4a61ef commit dbb746d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Style/FrozenStringLiteralComment:
Enabled: false
7 changes: 4 additions & 3 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,5 +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
1 change: 1 addition & 0 deletions .too-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.0.1
7 changes: 6 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'open3', '~> 0.1.0'
gem 'getopt', '~> 1.5', '>= 1.5.1'
gem 'rake'
gem 'rubocop', require: false
6 changes: 3 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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
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
```

Expand Down
83 changes: 52 additions & 31 deletions pg_db_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,52 @@
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='

# rubocop:disable Metrics/MethodLength
def print_help
puts `
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.
`
end
# rubocop:enable Metrics/MethodLength

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],
Expand All @@ -14,54 +59,30 @@
['--password', '-p', GetoptLong::OPTIONAL_ARGUMENT]
)

# heroku db url
heroku_database_url = nil

user = nil
password = ''
db_name = ''

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'
user = arg
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
Expand Down

0 comments on commit dbb746d

Please sign in to comment.