Last active
October 20, 2023 09:49
-
-
Save joelmoss/3574cef393a68a41efdbebe5110c1ad5 to your computer and use it in GitHub Desktop.
Revisions
-
joelmoss revised this gist
Oct 20, 2023 . 1 changed file with 9 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,7 @@ # rubocop:disable Metrics/LineLength require 'yaml' require 'digest' require 'erb' return if ENV['NO_DB_SWITCH'] == '1' @@ -20,7 +21,7 @@ def branches_from_refhead(ref) end end def safe_name(name) = Digest::MD5.hexdigest(name) # Get the current (destination) branch dest_branch = `git rev-parse --abbrev-ref HEAD`.strip @@ -50,26 +51,26 @@ def safe_name(name) = name.tr '/', '-' # Copy dev DB to source branches source_branches.each do |br| sbr = safe_name(br) print " -- Copying #{dev_db} to #{dev_db}_#{sbr} ..." if `psql -p #{dev_db_port} -tAc "SELECT 1 FROM pg_database WHERE datname='#{dev_db}_#{sbr}'"`.strip == '1' print ' (dropping existing one first)' system %(dropdb -p #{dev_db_port} -f #{dev_db}_#{sbr}) end system "createdb -p #{dev_db_port} -T #{dev_db} #{dev_db}_#{sbr}" puts ' DONE' end s_dest_branch = safe_name(dest_branch) if `psql -p #{dev_db_port} -tAc "SELECT 1 FROM pg_database WHERE datname='#{dev_db}_#{s_dest_branch}'"`.strip == '1' print " -- Dropping #{dev_db} ..." system %(dropdb -p #{dev_db_port} -f #{dev_db}) puts ' DONE' # Rename destination branch DB to dev DB print " -- Renaming #{dev_db}_#{s_dest_branch} to #{dev_db} ..." system %(psql -p #{dev_db_port} --command="ALTER DATABASE #{dev_db}_#{s_dest_branch} RENAME TO #{dev_db};" postgres > /dev/null) puts ' DONE' end -
joelmoss revised this gist
Oct 6, 2023 . 1 changed file with 11 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,6 +20,8 @@ def branches_from_refhead(ref) end end def safe_name(name) = name.tr '/', '-' # Get the current (destination) branch dest_branch = `git rev-parse --abbrev-ref HEAD`.strip @@ -47,25 +49,27 @@ def branches_from_refhead(ref) # Copy dev DB to source branches source_branches.each do |br| sbr = safe_name(br) print " -- Copying #{dev_db} to #{dev_db}__#{sbr} ..." if `psql -p #{dev_db_port} -tAc "SELECT 1 FROM pg_database WHERE datname='#{dev_db}__#{sbr}'"`.strip == '1' print ' (dropping existing one first)' system %(dropdb -p #{dev_db_port} -f #{dev_db}__#{sbr}) end system "createdb -p #{dev_db_port} -T #{dev_db} #{dev_db}__#{sbr}" puts ' DONE' end s_dest_branch = safe_name(dest_branch) if `psql -p #{dev_db_port} -tAc "SELECT 1 FROM pg_database WHERE datname='#{dev_db}__#{s_dest_branch}'"`.strip == '1' print " -- Dropping #{dev_db} ..." system %(dropdb -p #{dev_db_port} -f #{dev_db}) puts ' DONE' # Rename destination branch DB to dev DB print " -- Renaming #{dev_db}__#{s_dest_branch} to #{dev_db} ..." system %(psql -p #{dev_db_port} --command="ALTER DATABASE #{dev_db}__#{s_dest_branch} RENAME TO #{dev_db};" postgres > /dev/null) puts ' DONE' end -
joelmoss revised this gist
Oct 6, 2023 . 1 changed file with 19 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,17 @@ # frozen_string_literal: true # rubocop:disable Metrics/LineLength require 'yaml' require 'erb' return if ENV['NO_DB_SWITCH'] == '1' # Return if this is not a branch checkout. return if ARGV[2] != '1' def drop_existing_connections_to_database(database_name, port: 5432) system(%[psql -p #{port} --command="SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '#{database_name}' AND pid <> pg_backend_pid();" postgres > /dev/null]) end def branches_from_refhead(ref) @@ -26,8 +31,10 @@ def branches_from_refhead(ref) project_root = `git rev-parse --show-toplevel`.strip # Load Rails DB config and grab database name tpl = ERB.new(File.read("#{project_root}/config/database.yml")) rails_db_config = YAML.load(tpl.result, aliases: true) dev_db = rails_db_config['development']['database'] dev_db_port = rails_db_config['development'].fetch('port', 5432) # Don't do anything if the source and destination branches are the same or nonexistent return if source_branches.include?(dest_branch) || source_branches.empty? || @@ -36,28 +43,30 @@ def branches_from_refhead(ref) puts "Switching development database from #{source_branches.join(', ')} to #{dest_branch}..." # Drop connections drop_existing_connections_to_database dev_db, port: dev_db_port # Copy dev DB to source branches source_branches.each do |br| print " -- Copying #{dev_db} to #{dev_db}__#{br} ..." if `psql -p #{dev_db_port} -tAc "SELECT 1 FROM pg_database WHERE datname='#{dev_db}__#{br}'"`.strip == '1' print ' (dropping existing one first)' system %(dropdb -p #{dev_db_port} -f #{dev_db}__#{br}) end system "createdb -p #{dev_db_port} -T #{dev_db} #{dev_db}__#{br}" puts ' DONE' end if `psql -p #{dev_db_port} -tAc "SELECT 1 FROM pg_database WHERE datname='#{dev_db}__#{dest_branch}'"`.strip == '1' print " -- Dropping #{dev_db} ..." system %(dropdb -p #{dev_db_port} -f #{dev_db}) puts ' DONE' # Rename destination branch DB to dev DB print " -- Renaming #{dev_db}__#{dest_branch} to #{dev_db} ..." system %(psql -p #{dev_db_port} --command="ALTER DATABASE #{dev_db}__#{dest_branch} RENAME TO #{dev_db};" postgres > /dev/null) puts ' DONE' end # rubocop:enable Metrics/LineLength -
joelmoss revised this gist
Mar 13, 2023 . 1 changed file with 2 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,3 @@ require 'yaml' return if ENV['NO_DB_SWITCH'] == '1' @@ -46,7 +44,7 @@ def branches_from_refhead(ref) if `psql -tAc "SELECT 1 FROM pg_database WHERE datname='#{dev_db}__#{br}'"`.strip == '1' print ' (dropping existing one first)' system %(dropdb -f #{dev_db}__#{br}) end system "createdb -T #{dev_db} #{dev_db}__#{br}" @@ -55,7 +53,7 @@ def branches_from_refhead(ref) if `psql -tAc "SELECT 1 FROM pg_database WHERE datname='#{dev_db}__#{dest_branch}'"`.strip == '1' print " -- Dropping #{dev_db} ..." system %(dropdb -f #{dev_db}) puts ' DONE' # Rename destination branch DB to dev DB -
joelmoss created this gist
Oct 16, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ #!/usr/bin/env ruby require 'yaml' return if ENV['NO_DB_SWITCH'] == '1' # Return if this is not a branch checkout. return if ARGV[2] != '1' def drop_existing_connections_to_database(database_name) system(%[psql --command="SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '#{database_name}' AND pid <> pg_backend_pid();" postgres > /dev/null]) end def branches_from_refhead(ref) `git show-ref --heads | grep #{ref} | awk '{print $2}'`.split("\n").map do |b| b.sub(%r{^refs/heads/}, '') end end # Get the current (destination) branch dest_branch = `git rev-parse --abbrev-ref HEAD`.strip # Since we're just given a commit ID referencing the branch head we're coming from, # it could be at the head of multiple branches. We can assume the source isn't the same as the # destination branch, so we can remove that immediately. source_branches = branches_from_refhead(ARGV[0]).reject { |b| b == dest_branch } project_root = `git rev-parse --show-toplevel`.strip # Load Rails DB config and grab database name rails_db_config = YAML.load_file("#{project_root}/config/database.yml", aliases: true) dev_db = rails_db_config['development']['database'] # Don't do anything if the source and destination branches are the same or nonexistent return if source_branches.include?(dest_branch) || source_branches.empty? || (source_branches | [dest_branch]).any?('') puts "Switching development database from #{source_branches.join(', ')} to #{dest_branch}..." # Drop connections drop_existing_connections_to_database dev_db # Copy dev DB to source branches source_branches.each do |br| print " -- Copying #{dev_db} to #{dev_db}__#{br} ..." if `psql -tAc "SELECT 1 FROM pg_database WHERE datname='#{dev_db}__#{br}'"`.strip == '1' print ' (dropping existing one first)' system %(dropdb #{dev_db}__#{br}) end system "createdb -T #{dev_db} #{dev_db}__#{br}" puts ' DONE' end if `psql -tAc "SELECT 1 FROM pg_database WHERE datname='#{dev_db}__#{dest_branch}'"`.strip == '1' print " -- Dropping #{dev_db} ..." system %(dropdb #{dev_db}) puts ' DONE' # Rename destination branch DB to dev DB print " -- Renaming #{dev_db}__#{dest_branch} to #{dev_db} ..." system %(psql --command="ALTER DATABASE #{dev_db}__#{dest_branch} RENAME TO #{dev_db};" postgres > /dev/null) puts ' DONE' end