Last active
February 15, 2024 15:30
-
-
Save Zooip/8f1c54b67ee5d4fa7c6d0113d97d3e7c to your computer and use it in GitHub Desktop.
Reset schema.rb from main
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 characters
#!/bin/bash | |
# Fetch last changes from origin | |
git fetch | |
# Copy schema.rb from main | |
git checkout origin/main -- db/schema.rb | |
# Restore database with main state (erase all local data) | |
bundle exec rake db:schema:load | |
# Build list of new migrations | |
migrations_list=$(git diff --name-only origin/main..HEAD -- db/migrate | xargs -n 1 basename | cut -d "_" -f 1 | paste -s -d, - | sed "s/\([^,]*\)/'\1'/g") | |
# Mark new migration as "down" if needed | |
bundle exec rails runner "ActiveRecord::Base.connection.execute(\"DELETE FROM schema_migrations WHERE version IN (${migrations_list})\")" | |
# Run new migrations | |
bundle exec rake db:migrate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment