Skip to content

Instantly share code, notes, and snippets.

@Zooip
Last active February 15, 2024 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zooip/8f1c54b67ee5d4fa7c6d0113d97d3e7c to your computer and use it in GitHub Desktop.
Save Zooip/8f1c54b67ee5d4fa7c6d0113d97d3e7c to your computer and use it in GitHub Desktop.
Reset schema.rb from main
#!/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