Skip to content

Instantly share code, notes, and snippets.

@brysgo
Forked from skyriverbend/rails_switch_branch.py
Last active November 15, 2024 14:24
Show Gist options
  • Select an option

  • Save brysgo/9980344 to your computer and use it in GitHub Desktop.

Select an option

Save brysgo/9980344 to your computer and use it in GitHub Desktop.

Revisions

  1. brysgo revised this gist Apr 9, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion post_checkout_migrations.sh
    Original 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]+)'`
    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
  2. brysgo revised this gist Apr 8, 2014. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions post_checkout_migrations.sh
    Original 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
  3. brysgo revised this gist Apr 4, 2014. 2 changed files with 31 additions and 54 deletions.
    31 changes: 31 additions & 0 deletions post_checkout_migrations.sh
    Original 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
    54 changes: 0 additions & 54 deletions rails_switch_branch.py
    Original file line number Diff line number Diff line change
    @@ -1,54 +0,0 @@
    #!/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
    5. Update your test database
    TODO:
    - Check if git directory is dirty. If so, do not run.
    """
    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-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())

    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 switched branches and migrated to "' + BRANCH_NAME + '"'
    print
  4. @skyriverbend skyriverbend revised this gist Dec 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_switch_branch.py
    Original file line number Diff line number Diff line change
    @@ -51,4 +51,4 @@

    print
    print '*** Successfully switched branches and migrated to "' + BRANCH_NAME + '"'
    print
    print
  5. @skyriverbend skyriverbend revised this gist Nov 15, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rails_switch_branch.py
    Original 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.
  6. @skyriverbend skyriverbend revised this gist Nov 15, 2012. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions rails_switch_branch.py
    Original 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:
    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
    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
  7. @skyriverbend skyriverbend renamed this gist Nov 15, 2012. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions switch_rails_branch.py → rails_switch_branch.py
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,18 @@
    #!/usr/local/bin/python
    """
    Usage (must be in the root of a Rails project directory that is using git):
    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 switch_rails_branch <other_branch>
    $ 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 schema file changes
    3. Switch to the new branch
    4. Run the migrations on the new 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:
    - Do not run if git directory is not clean
    - Check if git directory is dirty. If so, do not run.
    """
    import sys
    import subprocess
  8. @skyriverbend skyriverbend revised this gist Nov 15, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion switch_rails_branch.py
    Original 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 which do not exist on that branch
    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
  9. @skyriverbend skyriverbend revised this gist Nov 15, 2012. 1 changed file with 11 additions and 8 deletions.
    19 changes: 11 additions & 8 deletions switch_rails_branch.py
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,18 @@
    #!/usr/local/bin/python
    """
    Usage (must be run in the root of a Rails project directory that is using git):
    Usage (must be in the root of a Rails project directory that is using git):
    $ python switch_rails_branch.py <other_branch>
    $ python switch_rails_branch <other_branch>
    Running the above will do the following:
    1. Roll back any migrations on the current branch which do not exist on the other branch
    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 any new migrations on 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-only").split())
    migrations = re.findall("db/migrate/([0-9]+)", files_changed)
    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 db/schema.rb"
    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 migrated "' + BRANCH_NAME + '"'
    print '*** Successfully switched branches and migrated to "' + BRANCH_NAME + '"'
    print
  10. @skyriverbend skyriverbend revised this gist Nov 15, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions switch_rails_branch.py
    Original 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 which do not exist on that branch
    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 the migrations on the new branch
    4. Run any new migrations on the new branch
    """
    import sys
    import subprocess
  11. @skyriverbend skyriverbend revised this gist Nov 15, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion switch_rails_branch.py
    Original 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 <other_branch>
    $ python switch_rails_branch.py <other_branch>
    Running the above will do the following:
  12. @skyriverbend skyriverbend created this gist Nov 15, 2012.
    47 changes: 47 additions & 0 deletions switch_rails_branch.py
    Original 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