Skip to content

Commit

Permalink
feat: rake task to load DB from local file (#555)
Browse files Browse the repository at this point in the history
* add a rake task to load DB w/ local sql dump
* remove rake task to download from remote. It depends on a static s3://<wagner bucket>/dgidb/dgidb_latest.sql URI and I'm not really happy with maintaining that. I'd rather have a better way of dynamically pointing to the latest-dated dump. Also, I thought some of the data was supposed to be private but I could be wrong.
* slightly reconfigure the section for psql executable injection into the PATH. Drop macports support. Add homebrew (ARM64) support.
  • Loading branch information
jsstevenson authored Dec 11, 2024
1 parent 0e2fc97 commit 7e0df22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 42 deletions.
51 changes: 16 additions & 35 deletions server/lib/tasks/db_bootstrapping.rake
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace :dgidb do
data_submodule_path = File.join(Rails.root, 'data')
end
data_file = File.join(data_submodule_path, 'dgidb_v5_latest.sql')
version_file = File.join(Rails.root, 'VERSION')
database_name = Rails.configuration.database_configuration[Rails.env]['database']
host = Rails.configuration.database_configuration[Rails.env]['host']
username = Rails.configuration.database_configuration[Rails.env]['username']
Expand All @@ -22,22 +21,18 @@ namespace :dgidb do

desc 'set up path for macs running Postgres.app'
task :setup_path do
#special case for macs running Postgres.app
if RbConfig::CONFIG['host_os'] =~ /darwin/ && File.exist?( '/Applications/Postgres.app' )
puts 'Found Postgres.app'
ENV['PATH'] = "/Applications/Postgres.app/Contents/Versions/latest/bin:#{ENV['PATH']}"
if RbConfig::CONFIG['host_os'] =~ /darwin/
if File.exist?( '/Applications/Postgres.app' )
puts 'Using Postgres.app'
ENV['PATH'] = "/Applications/Postgres.app/Contents/Versions/latest/bin:#{ENV['PATH']}"
next
elsif File.exist?('/opt/homebrew/bin/psql')
puts 'Using Hombrew-installed psql'
ENV['PATH'] = "/opt/homebrew/bin/:#{ENV['PATH']}"
next
end
end

# MacPorts Handling
macports_postgres = Dir.glob( '/opt/local/lib/postgresql*/bin')
if RbConfig::CONFIG['host_os'] =~ /darwin/ && macports_postgres.any?
macports_postgres_path = macports_postgres.last
macports_postgres_version = File.basename(File.dirname(macports_postgres_path))
puts "Found MacPorts #{macports_postgres_version}"
ENV['PATH'] = "#{macports_postgres_path}:#{ENV['PATH']}"
end

# Homebrew Handling (TODO)
puts 'Warning: unable to ensure psql is available on $PATH'
end

desc 'create a dump of the current local database'
Expand All @@ -49,25 +44,11 @@ namespace :dgidb do
end
end

desc 'load the source controlled db dump and schema into the local db, blowing away what is currently there'
task load_local: ['setup_path', 'db:drop', 'db:create', 'db:structure:load'] do
download_data_dump(data_file)
system "psql -h #{host} -d #{database_name} -f #{data_file}"
Rails.cache.clear
end
desc 'load a local db dump and schema into the local db, blowing away what is currently there'
task :load_from_local, [:db_dump] => ['setup_path', 'db:drop', 'db:create', 'db:structure:load'] do |_t, args|
raise 'You must supply a path to a DB dump' unless args[:db_dump]

desc 'create a new data snapshot'
task :create_snapshot, [:message, :version_type] do |t, args|
args.with_defaults(version_type: :patch)
raise 'You must supply a commit message!' unless args[:message]
Rake::Task['dgidb:dump_local'].execute
in_git_stash do
pull_latest
new_version = update_version(version_file, args[:version_type].to_sym)
commit_db_update(data_submodule_path, data_file, args[:message])
commit_data_submodule_update(args[:message], data_submodule_path, version_file)
create_tag(new_version)
push_changes
end
system "psql -h #{host} -d #{database_name} -f #{args[:db_dump]}"
Rails.cache.clear
end
end
7 changes: 0 additions & 7 deletions server/lib/utils/snapshot_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ def update_data_submodule
system_or_die("git submodule update --init data")
end

def download_data_dump(destination)
unless Dir.exist? 'data'
Dir.mkdir('data')
end
system_or_die("wget -O #{destination} https://nch-igm-wagner-lab-public.s3.us-east-2.amazonaws.com/dgidb_v5_latest.sql")
end

private

def system_or_die(syscall)
Expand Down

0 comments on commit 7e0df22

Please sign in to comment.