Skip to content

Commit

Permalink
AUTO_TMP_DUMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Adeynack committed Aug 31, 2023
1 parent 5dfed5e commit 42cf53d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ rails db:pull_data
rails db:pull_assets
```

The following ENV values can be set to change their behavior.
The following ENV values can be set to change their behavior. Some of them exist as ENV so projects can set defaults for them in their `.env` or individual coders in their `.env.local` files (eg: automatically dump pulled data with `AUTO_TMP_DUMP=1`).

`HEROKU_APP=foo-staging` will pull data from the database of the `foo-staging` app on _Heroku_. By default, no app is given to the _Heroku_ CLI, which will result in it looking at the _GIT_ remotes for a _Heroku_ app there.

Expand All @@ -293,6 +293,8 @@ The following ENV values can be set to change their behavior.

`IGNORE_TABLES=foo,bar,asdf` will ignore tables `foo`, `bar`, and `asdf` while pulling data.

`AUTO_TMP_DUMP=1` will automatically invoke `db:tmp:dump` _Rake_ task after the data is pulled.

### Localizable Routes with Browser Locale Support

To localize a page via urls, this will help you tremendously.
Expand Down
43 changes: 33 additions & 10 deletions lib/shimmer/tasks/db.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

namespace :db do
desc "Downloads the app database from Heroku and imports it to the local database"
task pull_data: :environment do
desc "Set the ENV for database operations"
task set_db_env: :environment do
config = if Rails.version.to_f >= 7
ActiveRecord::Base.connection_db_config.configuration_hash.with_indifferent_access
else
Expand All @@ -13,19 +13,23 @@ namespace :db do
ENV["PGHOST"] = config["host"]
ENV["PGPORT"] = config["port"].to_s

database = ENV["DATABASE"].presence || config["database"]
database = "#{database}_#{Time.now.utc.strftime("%Y%m%d%H%M%S")}" if Shimmer::Config.instance.suffixed?

heroku_app = ENV["HEROKU_APP"].presence&.then { |app| "--app #{app}" }
exclude_table_part = ENV["IGNORE_TABLES"].to_s.split(",").filter(&:presence).join(";").presence&.then { |t| "--exclude-table-data '#{t}'" }
ENV["DATABASE"] ||= config["database"]
ENV["DATABASE"] = "#{ENV["DATABASE"]}_#{Time.now.utc.strftime("%Y%m%d%H%M%S")}" if Shimmer::Config.instance.suffixed?
ENV["EXCLUDE_TABLE_DATA_PART"] = ENV["IGNORE_TABLES"].to_s.split(",").filter(&:presence).join(";").presence&.then { |t| "--exclude-table-data '#{t}'" }
end

# TODO: Option to Auto-Dump locally
desc "Downloads the app database from Heroku and imports it to the local database"
task pull_data: :environment do
Rake::Task["db:set_db_env"].invoke
heroku_app_part = ENV["HEROKU_APP"].presence&.then { |app| "--app #{app}" }

# TODO: Try to automatically run post-pull task. eg: `Rake::Task[db:post_pull]&.invoke`

sh "dropdb --if-exists #{database}"
sh "heroku pg:pull DATABASE_URL #{heroku_app} #{exclude_table_part} #{database}"
sh "dropdb --if-exists #{ENV["DATABASE"]}"
sh "heroku pg:pull DATABASE_URL #{heroku_app_part} #{ENV["EXCLUDE_TABLE_DATA_PART"]} #{ENV["DATABASE"]}"
sh "rails db:environment:set"

Rake::Task["db:tmp:dump"].invoke if Shimmer::Config.instance.auto_tmp_dump?
end

desc "Downloads the app assets from AWS to directory `storage`."
Expand Down Expand Up @@ -62,4 +66,23 @@ namespace :db do
puts "No tables in database yet, skipping migration"
end
end

namespace :tmp do
desc "Dump the development database to the `tmp` folder of the project."
task :dump do
Rake::Task["db:set_db_env"].invoke
dump_filename = ENV["DUMP_NAME"].presence || ENV["DATABASE"]
sh "pg_dump --verbose --format=c #{ENV["DATABASE"]} #{ENV["EXCLUDE_TABLE_DATA_PART"]} --file=tmp/#{dump_filename}.dump"
end

desc "Restore the development database from the `tmp` folder of the project."
task :restore do
Rake::Task["db:set_db_env"].invoke
sh "dropdb --if-exists #{ENV["DATABASE"]}"
sh "createdb #{ENV["DATABASE"]}"
dump_filename = ENV["DUMP_NAME"].presence || ENV["DATABASE"]
sh "pg_restore --verbose --format=c --jobs=8 --disable-triggers --dbname=#{ENV["DATABASE"]} tmp/#{dump_filename}.dump"
sh "rake db:environment:set"
end
end
end

0 comments on commit 42cf53d

Please sign in to comment.