Skip to content

Instantly share code, notes, and snippets.

@jimryan
Last active April 12, 2016 17:11

Revisions

  1. jimryan revised this gist Feb 7, 2014. 1 changed file with 15 additions and 11 deletions.
    26 changes: 15 additions & 11 deletions post-checkout
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ require 'yaml'
    # If this was a branch checkout
    if ARGV[2] == '1'
    def dump(database_name, branch_name)
    print "Dumping #{database_name}..."
    print "Saving state of #{database_name} on '#{branch_name}' branch..."

    if system(%[pg_dump -c -f "#{@dump_folder}/#{database_name}-#{branch_name}" #{database_name}])
    print "done!\n"
    @@ -20,13 +20,17 @@ if ARGV[2] == '1'
    system(%[psql #{database_name} < #{path} > /dev/null 2>&1])
    end

    def branch_from_refhead(ref)
    `git show-ref --heads | grep #{ref} | awk '{print $2}'`.strip.sub(/^refs\/heads\//, '')
    def branches_from_refhead(ref)
    `git show-ref --heads | grep #{ref} | awk '{print $2}'`.split("\n").map{ |b| b.sub(/^refs\/heads\//, '') }
    end

    # Get the source and destination branches
    @source_branch = branch_from_refhead(ARGV[0])
    @destination_branch = branch_from_refhead(ARGV[1])
    # Get the current (destination) branch
    @destination_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 == @destination_branch }

    @project_root = %x[git rev-parse --show-toplevel].strip
    @dump_folder = "#{@project_root}/.db_branch_dumps"
    @@ -42,9 +46,9 @@ if ARGV[2] == '1'
    end

    # Don't do anything if the source and destination branches are the same or nonexistent
    unless @source_branch == @destination_branch || [@source_branch, @destination_branch].any?{ |b| b == '' }
    # Dump database for source branch
    if dump(dev_database_name, @source_branch)
    unless @source_branches.include?(@destination_branch) || @source_branches.empty? || (@source_branches | [@destination_branch]).any?{ |b| b == '' }
    # Dump database for source branches
    if @source_branches.all? { |branch| dump(dev_database_name, branch) }
    # Restore dump from this branch, if it exists
    dump_path = "#{@dump_folder}/#{dev_database_name}-#{@destination_branch}"

    @@ -74,10 +78,10 @@ if ARGV[2] == '1'
    end
    else
    print "No DB dump for #{dev_database_name} on the '#{@destination_branch}' branch was found!\n"
    print "The state of your database has been saved for when you return to the '#{@source_branch}' branch, but its current state has been left unchanged. You are now free to make changes to it that are specific to this branch, and they will be saved when you checkout a different branch, then restored when you checkout this one again.\n"
    print "The state of your database has been saved for when you return to the '#{@source_branches.join('\' or \'')}' branch, but its current state has been left unchanged. You are now free to make changes to it that are specific to this branch, and they will be saved when you checkout a different branch, then restored when you checkout this one again.\n"
    end
    else
    print "Failed to dump database. Halting.\n"
    end
    end
    end
    end
  2. jimryan revised this gist Feb 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion post-checkout
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ if ARGV[2] == '1'

    # Don't do anything if the source and destination branches are the same or nonexistent
    unless @source_branch == @destination_branch || [@source_branch, @destination_branch].any?{ |b| b == '' }
    # Dump databases for source branch
    # Dump database for source branch
    if dump(dev_database_name, @source_branch)
    # Restore dump from this branch, if it exists
    dump_path = "#{@dump_folder}/#{dev_database_name}-#{@destination_branch}"
  3. jimryan revised this gist Feb 6, 2014. 2 changed files with 33 additions and 83 deletions.
    50 changes: 33 additions & 17 deletions post-checkout
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,10 @@ if ARGV[2] == '1'
    end
    end

    def restore(database_name, path)
    system(%[psql #{database_name} < #{path} > /dev/null 2>&1])
    end

    def branch_from_refhead(ref)
    `git show-ref --heads | grep #{ref} | awk '{print $2}'`.strip.sub(/^refs\/heads\//, '')
    end
    @@ -29,10 +33,8 @@ if ARGV[2] == '1'

    # Load Rails DB config and grab database name
    rails_db_config = YAML.load_file("#{@project_root}/config/database.yml")
    database_names = {
    'development' => rails_db_config['development']['database'],
    'test' => rails_db_config['test']['database']
    }
    dev_database_name = rails_db_config['development']['database']
    test_database_name = rails_db_config['test']['database']

    # Ensure dump directory exists
    unless Dir.exists?(@dump_folder)
    @@ -42,26 +44,40 @@ if ARGV[2] == '1'
    # Don't do anything if the source and destination branches are the same or nonexistent
    unless @source_branch == @destination_branch || [@source_branch, @destination_branch].any?{ |b| b == '' }
    # Dump databases for source branch
    if database_names.all? { |env, database_name| dump(database_name, @source_branch) }
    database_names.each do |env, database_name|
    # Restore dumps from this branch, if they exist
    dump_path = "#{@dump_folder}/#{database_name}-#{@destination_branch}"
    if dump(dev_database_name, @source_branch)
    # Restore dump from this branch, if it exists
    dump_path = "#{@dump_folder}/#{dev_database_name}-#{@destination_branch}"

    if File.exists?(dump_path)
    print "Restoring #{database_name} to its previous state on this branch..."
    if File.exists?(dump_path)
    print "Restoring #{dev_database_name} to its previous state on this branch..."

    if system(%[psql #{database_name} < #{dump_path} > /dev/null])
    print "done!\n"
    if restore(dev_database_name, dump_path)
    print "done!\n"

    # If we have a structure.sql file, restore that to the test database,
    # otherwise fall back to rake
    structure_sql = File.join(@project_root, 'db', 'structure.sql')

    if File.exists?(structure_sql)
    print "Restoring test database from structure.sql..."

    print restore(test_database_name, structure_sql) ? "done!\n" : "failed!\n"
    else
    print "failed!\n"
    print "Preparing test database..."

    system %[rake db:test:prepare]

    print "done!\n"
    end
    else
    print "No DB dump for #{database_name} on the '#{@destination_branch}' branch was found!\n"
    print "The state of your database has been saved for when you return to the '#{@source_branch}' branch, but its current state has been left unchanged. You are now free to make changes to it that are specific to this branch, and they will be saved when you checkout a different branch, then restored when you checkout this one again.\n"
    print "failed!\n"
    end
    else
    print "No DB dump for #{dev_database_name} on the '#{@destination_branch}' branch was found!\n"
    print "The state of your database has been saved for when you return to the '#{@source_branch}' branch, but its current state has been left unchanged. You are now free to make changes to it that are specific to this branch, and they will be saved when you checkout a different branch, then restored when you checkout this one again.\n"
    end
    else
    print "Failed to dump all databases. Halting.\n"
    print "Failed to dump database. Halting.\n"
    end
    end
    end
    end
    66 changes: 0 additions & 66 deletions post-checkout-using-rake-to-restore-test-db
    Original file line number Diff line number Diff line change
    @@ -1,66 +0,0 @@
    #!/usr/bin/env ruby

    require 'yaml'

    # If this was a branch checkout
    if ARGV[2] == '1'
    def dump(database_name, branch_name)
    print "Dumping #{database_name}..."

    if system(%[pg_dump -c -f "#{@dump_folder}/#{database_name}-#{branch_name}" #{database_name}])
    print "done!\n"
    true
    else
    print "failed!\n"
    false
    end
    end

    def branch_from_refhead(ref)
    `git show-ref --heads | grep #{ref} | awk '{print $2}'`.strip.sub(/^refs\/heads\//, '')
    end

    # Get the source and destination branches
    @source_branch = branch_from_refhead(ARGV[0])
    @destination_branch = branch_from_refhead(ARGV[1])

    @project_root = %x[git rev-parse --show-toplevel].strip
    @dump_folder = "#{@project_root}/.db_branch_dumps"

    # Load Rails DB config and grab database name
    rails_db_config = YAML.load_file("#{@project_root}/config/database.yml")
    database_name = rails_db_config['development']['database']

    # Ensure dump directory exists
    unless Dir.exists?(@dump_folder)
    Dir.mkdir @dump_folder
    end

    # Don't do anything if the source and destination branches are the same
    unless @source_branch == @destination_branch
    # Dump databases for source branch
    if dump(database_name, @source_branch)
    # Restore dumps from this branch, if they exist
    dump_path = "#{@dump_folder}/#{database_name}-#{@destination_branch}"

    if File.exists?(dump_path)
    print "Restoring #{database_name} to its previous state on this branch...\n"

    if system(%[psql #{database_name} < #{dump_path} > /dev/null])
    # Prepare test database
    print "Preparing test database...\n"
    system %[rake db:test:prepare]

    print "done!\n"
    else
    print "failed!\n"
    end
    else
    print "No DB dump for #{database_name} on the '#{@destination_branch}' branch was found!\n"
    print "The state of your database has been saved for when you return to the '#{@source_branch}' branch, but its current state has been left unchanged. You are now free to make changes to it that are specific to this branch, and they will be saved when you checkout a different branch, then restored when you checkout this one again.\n"
    end
    else
    print "Failed to dump all databases. Halting.\n"
    end
    end
    end
  4. jimryan revised this gist Feb 6, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions post-checkout
    Original file line number Diff line number Diff line change
    @@ -39,8 +39,8 @@ if ARGV[2] == '1'
    Dir.mkdir @dump_folder
    end

    # Don't do anything if the source and destination branches are the same
    unless @source_branch == @destination_branch
    # Don't do anything if the source and destination branches are the same or nonexistent
    unless @source_branch == @destination_branch || [@source_branch, @destination_branch].any?{ |b| b == '' }
    # Dump databases for source branch
    if database_names.all? { |env, database_name| dump(database_name, @source_branch) }
    database_names.each do |env, database_name|
    @@ -64,4 +64,4 @@ if ARGV[2] == '1'
    print "Failed to dump all databases. Halting.\n"
    end
    end
    end
    end
  5. jimryan revised this gist Feb 6, 2014. 1 changed file with 66 additions and 0 deletions.
    66 changes: 66 additions & 0 deletions post-checkout-using-rake-to-restore-test-db
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    #!/usr/bin/env ruby

    require 'yaml'

    # If this was a branch checkout
    if ARGV[2] == '1'
    def dump(database_name, branch_name)
    print "Dumping #{database_name}..."

    if system(%[pg_dump -c -f "#{@dump_folder}/#{database_name}-#{branch_name}" #{database_name}])
    print "done!\n"
    true
    else
    print "failed!\n"
    false
    end
    end

    def branch_from_refhead(ref)
    `git show-ref --heads | grep #{ref} | awk '{print $2}'`.strip.sub(/^refs\/heads\//, '')
    end

    # Get the source and destination branches
    @source_branch = branch_from_refhead(ARGV[0])
    @destination_branch = branch_from_refhead(ARGV[1])

    @project_root = %x[git rev-parse --show-toplevel].strip
    @dump_folder = "#{@project_root}/.db_branch_dumps"

    # Load Rails DB config and grab database name
    rails_db_config = YAML.load_file("#{@project_root}/config/database.yml")
    database_name = rails_db_config['development']['database']

    # Ensure dump directory exists
    unless Dir.exists?(@dump_folder)
    Dir.mkdir @dump_folder
    end

    # Don't do anything if the source and destination branches are the same
    unless @source_branch == @destination_branch
    # Dump databases for source branch
    if dump(database_name, @source_branch)
    # Restore dumps from this branch, if they exist
    dump_path = "#{@dump_folder}/#{database_name}-#{@destination_branch}"

    if File.exists?(dump_path)
    print "Restoring #{database_name} to its previous state on this branch...\n"

    if system(%[psql #{database_name} < #{dump_path} > /dev/null])
    # Prepare test database
    print "Preparing test database...\n"
    system %[rake db:test:prepare]

    print "done!\n"
    else
    print "failed!\n"
    end
    else
    print "No DB dump for #{database_name} on the '#{@destination_branch}' branch was found!\n"
    print "The state of your database has been saved for when you return to the '#{@source_branch}' branch, but its current state has been left unchanged. You are now free to make changes to it that are specific to this branch, and they will be saved when you checkout a different branch, then restored when you checkout this one again.\n"
    end
    else
    print "Failed to dump all databases. Halting.\n"
    end
    end
    end
  6. jimryan created this gist Feb 6, 2014.
    67 changes: 67 additions & 0 deletions post-checkout
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    #!/usr/bin/env ruby

    require 'yaml'

    # If this was a branch checkout
    if ARGV[2] == '1'
    def dump(database_name, branch_name)
    print "Dumping #{database_name}..."

    if system(%[pg_dump -c -f "#{@dump_folder}/#{database_name}-#{branch_name}" #{database_name}])
    print "done!\n"
    true
    else
    print "failed!\n"
    false
    end
    end

    def branch_from_refhead(ref)
    `git show-ref --heads | grep #{ref} | awk '{print $2}'`.strip.sub(/^refs\/heads\//, '')
    end

    # Get the source and destination branches
    @source_branch = branch_from_refhead(ARGV[0])
    @destination_branch = branch_from_refhead(ARGV[1])

    @project_root = %x[git rev-parse --show-toplevel].strip
    @dump_folder = "#{@project_root}/.db_branch_dumps"

    # Load Rails DB config and grab database name
    rails_db_config = YAML.load_file("#{@project_root}/config/database.yml")
    database_names = {
    'development' => rails_db_config['development']['database'],
    'test' => rails_db_config['test']['database']
    }

    # Ensure dump directory exists
    unless Dir.exists?(@dump_folder)
    Dir.mkdir @dump_folder
    end

    # Don't do anything if the source and destination branches are the same
    unless @source_branch == @destination_branch
    # Dump databases for source branch
    if database_names.all? { |env, database_name| dump(database_name, @source_branch) }
    database_names.each do |env, database_name|
    # Restore dumps from this branch, if they exist
    dump_path = "#{@dump_folder}/#{database_name}-#{@destination_branch}"

    if File.exists?(dump_path)
    print "Restoring #{database_name} to its previous state on this branch..."

    if system(%[psql #{database_name} < #{dump_path} > /dev/null])
    print "done!\n"
    else
    print "failed!\n"
    end
    else
    print "No DB dump for #{database_name} on the '#{@destination_branch}' branch was found!\n"
    print "The state of your database has been saved for when you return to the '#{@source_branch}' branch, but its current state has been left unchanged. You are now free to make changes to it that are specific to this branch, and they will be saved when you checkout a different branch, then restored when you checkout this one again.\n"
    end
    end
    else
    print "Failed to dump all databases. Halting.\n"
    end
    end
    end