Forked from skyriverbend/rails_switch_branch.py
Last active
November 15, 2024 14:24
-
-
Save brysgo/9980344 to your computer and use it in GitHub Desktop.
Revisions
-
brysgo revised this gist
Apr 9, 2014 . 1 changed file with 1 addition and 1 deletion.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 @@ -5,7 +5,7 @@ NEW_BRANCH=$2 if [ $CHECKING_OUT_BRANCH -eq 1 ] then FILES_CHANGED=`git diff $OLD_BRANCH $NEW_BRANCH --name-status` MIGRATIONS_REMOVED=`echo "$FILES_CHANGED" | egrep 'D\tdb/migrate/([0-9]+)' | sort -r` MIGRATIONS_ADDED=`echo "$FILES_CHANGED" | egrep 'A\tdb/migrate/([0-9]+)'` # CHECK IF WE NEED TO DO A BUNDLE -
brysgo revised this gist
Apr 8, 2014 . 1 changed file with 8 additions and 0 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 @@ -7,6 +7,13 @@ if [ $CHECKING_OUT_BRANCH -eq 1 ] FILES_CHANGED=`git diff $OLD_BRANCH $NEW_BRANCH --name-status` MIGRATIONS_REMOVED=`echo "$FILES_CHANGED" | egrep 'D\tdb/migrate/([0-9]+)'` MIGRATIONS_ADDED=`echo "$FILES_CHANGED" | egrep 'A\tdb/migrate/([0-9]+)'` # CHECK IF WE NEED TO DO A BUNDLE BUNDLE_CHANGED=`echo "$FILES_CHANGED" | egrep 'M\tGemfile.lock'` if [ ! -z "$BUNDLE_CHANGED" ]; then echo "Your Gemfile.lock has changed, running bundle" bundle fi if [ ! -z "$MIGRATIONS_REMOVED" ]; then echo "Rolling back missing migrations" @@ -22,6 +29,7 @@ if [ $CHECKING_OUT_BRANCH -eq 1 ] rm "$migration" done bundle exec rake db:test:prepare git checkout db/schema.rb fi if [ ! -z "$MIGRATIONS_ADDED" ]; then -
brysgo revised this gist
Apr 4, 2014 . 2 changed files with 31 additions and 54 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 @@ -0,0 +1,31 @@ CHECKING_OUT_BRANCH=$3 OLD_BRANCH=$1 NEW_BRANCH=$2 if [ $CHECKING_OUT_BRANCH -eq 1 ] then FILES_CHANGED=`git diff $OLD_BRANCH $NEW_BRANCH --name-status` MIGRATIONS_REMOVED=`echo "$FILES_CHANGED" | egrep 'D\tdb/migrate/([0-9]+)'` MIGRATIONS_ADDED=`echo "$FILES_CHANGED" | egrep 'A\tdb/migrate/([0-9]+)'` if [ ! -z "$MIGRATIONS_REMOVED" ]; then echo "Rolling back missing migrations" for migration in $MIGRATIONS_REMOVED do if [ $migration == "D" ]; then continue fi git checkout "$OLD_BRANCH" -- "$migration" VERSION=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3` bundle exec rake db:migrate:down VERSION="$VERSION" git reset rm "$migration" done bundle exec rake db:test:prepare fi if [ ! -z "$MIGRATIONS_ADDED" ]; then echo "New migrations have been added running migrations" bundle exec rake db:migrate db:test:prepare fi fi 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,54 +0,0 @@ -
skyriverbend revised this gist
Dec 4, 2012 . 1 changed file with 1 addition and 1 deletion.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 @@ -51,4 +51,4 @@ print print '*** Successfully switched branches and migrated to "' + BRANCH_NAME + '"' print -
skyriverbend revised this gist
Nov 15, 2012 . 1 changed file with 1 addition and 0 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 @@ -13,6 +13,7 @@ 2. Discard any changes to the db/schema.rb file 3. Check out the other branch 4. Run any new migrations existing in the other branch 5. Update your test database TODO: - Check if git directory is dirty. If so, do not run. -
skyriverbend revised this gist
Nov 15, 2012 . 1 changed file with 5 additions and 2 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,15 @@ #!/usr/local/bin/python """ To use this script, you must be in the root directory of a Rails project that is using git. You should also make sure that your directory does not contain any uncommitted changes. Then run: $ python rails_switch_branch.py name_of_another_branch Running the above will do the following: 1. Roll back any migrations on your current branch which do not exist on the other branch 2. Discard any changes to the db/schema.rb file 3. Check out the other branch 4. Run any new migrations existing in the other branch -
skyriverbend renamed this gist
Nov 15, 2012 . 1 changed file with 6 additions and 6 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,18 +1,18 @@ #!/usr/local/bin/python """ To use this script, you must be in the root directory of a Rails project that is using git. You should also make sure that your directory does not contain any uncommitted changes. Then run: $ python rails_switch_branch.py name_of_another_branch Running the above will do the following: 1. Roll back any migrations on your current branch which do not exist on the other branch 2. Discard any changes to the db/schema.rb file 3. Check out the other branch 4. Run any new migrations existing in the other branch TODO: - Check if git directory is dirty. If so, do not run. """ import sys import subprocess -
skyriverbend revised this gist
Nov 15, 2012 . 1 changed file with 1 addition and 1 deletion.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 @@ -6,7 +6,7 @@ Running the above will do the following: 1. Roll back any migrations on your current branch which do not exist on the other branch 2. Discard any schema file changes 3. Switch to the new branch 4. Run the migrations on the new branch -
skyriverbend revised this gist
Nov 15, 2012 . 1 changed file with 11 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 @@ -1,15 +1,18 @@ #!/usr/local/bin/python """ Usage (must be in the root of a Rails project directory that is using git): $ python switch_rails_branch <other_branch> Running the above will do the following: 1. Roll back any migrations which do not exist on that branch 2. Discard any schema file changes 3. Switch to the new branch 4. Run the migrations on the new branch TODO: - Do not run if git directory is not clean """ import sys import subprocess @@ -19,16 +22,16 @@ print "*** Switching from current branch to: " + BRANCH_NAME files_changed = subprocess.check_output(("git diff " + BRANCH_NAME + " --name-status").split()) migrations = re.findall("A\\tdb/migrate/([0-9]+)", files_changed) for migration in reversed(migrations): sh_command = "bundle exec rake db:migrate:down VERSION=" + migration print "*** Running: " + sh_command print subprocess.call(sh_command.split()) print "*** Discarding any changes to db/schema.rb" print subprocess.call("git checkout db/schema.rb".split()) subprocess.call(("git checkout " + BRANCH_NAME).split()) @@ -43,5 +46,5 @@ subprocess.call("bundle exec rake db:test:prepare".split()) print print '*** Successfully switched branches and migrated to "' + BRANCH_NAME + '"' print -
skyriverbend revised this gist
Nov 15, 2012 . 1 changed file with 2 additions and 2 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 @@ -6,10 +6,10 @@ Running the above will do the following: 1. Roll back any migrations on the current branch which do not exist on the other branch 2. Discard any schema file changes 3. Switch to the new branch 4. Run any new migrations on the new branch """ import sys import subprocess -
skyriverbend revised this gist
Nov 15, 2012 . 1 changed file with 1 addition and 1 deletion.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 @@ -2,7 +2,7 @@ """ Usage (must be run in the root of a Rails project directory that is using git): $ python switch_rails_branch.py <other_branch> Running the above will do the following: -
skyriverbend created this gist
Nov 15, 2012 .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,47 @@ #!/usr/local/bin/python """ Usage (must be run in the root of a Rails project directory that is using git): $ python switch_rails_branch <other_branch> Running the above will do the following: 1. Roll back any migrations which do not exist on that branch 2. Discard any schema file changes 3. Switch to the new branch 4. Run the migrations on the new branch """ import sys import subprocess import re BRANCH_NAME = sys.argv[1] print "*** Switching from current branch to: " + BRANCH_NAME files_changed = subprocess.check_output(("git diff " + BRANCH_NAME + " --name-only").split()) migrations = re.findall("db/migrate/([0-9]+)", files_changed) for migration in reversed(migrations): sh_command = "bundle exec rake db:migrate:down VERSION=" + migration print "*** Running: " + sh_command print subprocess.call(sh_command.split()) print "*** Discarding db/schema.rb" print subprocess.call("git checkout db/schema.rb".split()) subprocess.call(("git checkout " + BRANCH_NAME).split()) print print "*** Running: bundle exec rake db:migrate" print subprocess.call("bundle exec rake db:migrate".split()) print "*** Running: bundle exec rake db:test:prepare" print subprocess.call("bundle exec rake db:test:prepare".split()) print print '*** Successfully migrated "' + BRANCH_NAME + '"' print