{"id":13878250,"url":"https://github.com/planetscale/planetscale_rails","last_synced_at":"2025-12-24T10:29:47.499Z","repository":{"id":131856710,"uuid":"608641378","full_name":"planetscale/planetscale_rails","owner":"planetscale","description":"Make Rails schema migrations easy with PlanetScale","archived":false,"fork":false,"pushed_at":"2024-05-28T18:31:16.000Z","size":135,"stargazers_count":63,"open_issues_count":2,"forks_count":2,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-11-15T05:19:12.716Z","etag":null,"topics":["mysql","planetscale","rails","rake"],"latest_commit_sha":null,"homepage":"https://planetscale.com/blog/zero-downtime-rails-migrations-planetscale-rails-gem","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/planetscale.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-02T12:49:53.000Z","updated_at":"2024-09-20T05:29:50.000Z","dependencies_parsed_at":"2023-12-12T20:32:36.844Z","dependency_job_id":"131ed535-5f8d-4751-b91c-6164ea10f1e0","html_url":"https://github.com/planetscale/planetscale_rails","commit_stats":{"total_commits":24,"total_committers":3,"mean_commits":8.0,"dds":0.125,"last_synced_commit":"96e787e27e8b4e13809c69011ed0b1f9dbc2bd1b"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/planetscale%2Fplanetscale_rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/planetscale%2Fplanetscale_rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/planetscale%2Fplanetscale_rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/planetscale%2Fplanetscale_rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/planetscale","download_url":"https://codeload.github.com/planetscale/planetscale_rails/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226138849,"owners_count":17579496,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mysql","planetscale","rails","rake"],"created_at":"2024-08-06T08:01:43.996Z","updated_at":"2025-12-24T10:29:47.483Z","avatar_url":"https://github.com/planetscale.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# PlanetScale Rails (Vitess only)\n\n[![Gem Version](https://badge.fury.io/rb/planetscale_rails.svg)](https://badge.fury.io/rb/planetscale_rails)\n\nRake tasks for easily running Rails migrations against PlanetScale database branches.\n\nFor information on how to connect your Rails app to PlanetScale, please [see our guide here](https://planetscale.com/docs/tutorials/connect-rails-app).\n\n## Included rake tasks\n\nThe rake tasks allow you to use local MySQL for development. When you're ready to make a schema change, you can create a PlanetScale branch and run migrations\nagainst it. See [usage](#usage) for details.\n\n```\nrake psdb:migrate                    # Migrate the database for current environment\nrake psdb:rollback                   # Rollback primary database for current environment\nrake psdb:schema:load                # Load the current schema into the database\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngroup :development do\n  gem 'planetscale_rails'\nend\n```\n\nAnd then execute in your terminal:\n\n```\nbundle install\n```\n\nMake sure you have the [`pscale` CLI installed](https://github.com/planetscale/cli#installation).\n\n### Disable schema dump\n\nAdd the following to your `config/application.rb`.\n\n```ruby\nif ENV[\"DISABLE_SCHEMA_DUMP\"]\n  config.active_record.dump_schema_after_migration = false\nend\n```\n\nThis will allow the gem to disable schema dumping after running `psdb:migrate`.\n\n## Usage\n\n1. Using pscale, create a new branch. This command will create a local `.pscale.yml` file. You should add it to your `.gitignore`.\n\n```\npscale branch switch my-new-branch-name --database my-db-name --create --wait\n```\n\n**Tip:** In your database settings. Enable \"Automatically copy migration data.\" Select \"Rails\" as the migration framework. This will auto copy your `schema_migrations` table between branches.\n\n2. Run your schema migrations against the branch.\n\n```\nbundle exec rails psdb:migrate\n```\n\nIf you run multiple databases in your Rails app, you can specify the DB name.\n\n```\nbundle exec rails psdb:migrate:primary\n```\n\nIf you make a mistake, you can use `bundle exec rails psdb:rollback` to rollback the changes on your PlanetScale branch.\n\n3. Next, you can either open the Deploy Request via the UI. Or the CLI.\n\nVia CLI is:\n```\npscale deploy-request create database-name my-new-branch-name\n```\n\n4. To get your schema change to production, run the deploy request. Then, once it's complete, you can merge your code changes into your `main` branch in git and deploy your application code.\n\n## Usage with multiple keyspace (or sharded) databases\n\nThis gem supports working with multi-keyspace PlanetScale databases. When running migrations, Vitess will automatically route the schema change (DDL) to the correct keyspace based on the name of the table being altered.\n\n**Creating new tables**\n\nFor adding a new table, you must specify the `keyspace` for the table either as part of create_table or by setting an environment variable. When running `psdb:migrate`, this gem will create the table in the correct keyspace. When running the migration locally against plain MySQL,\nthe keyspace will be ignored and the table will be created as normal within the same database.\n\n```ruby\ncreate_table(:new_table, id: { type: :bigint, unsigned: true }, keyspace: \"keyspace-name\") do |t|\n  t.string :name\nend\n```\n\n_Optionally_: You can set a default keyspace for all new tables by setting `PLANETSCALE_DEFAULT_KEYSPACE` to the keyspace name. If a keyspace is not present in the `create_table` call, then the ENV var will be used when set.\n\nThe keyspace resolution follows this precedence:\n1. Explicit `keyspace:` option in create_table\n2. `PLANETSCALE_DEFAULT_KEYSPACE` environment variable\n3. No keyspace (standard behavior)\n\n## Using PlanetScale deploy requests vs `psdb:migrate` directly in production.\n\nPlanetScale's deploy requests [solve the schema change problem](https://planetscale.com/docs/learn/how-online-schema-change-tools-work). They make a normally high risk operation, safe. This is done by running your schema change using [Vitess's online schema change](https://vitess.io/docs/18.0/user-guides/schema-changes/) tools. Once the change is made, a deploy request is [also revertible without data loss](https://planetscale.com/blog/revert-a-migration-without-losing-data). None of this is possible when running `rails db:migrate` directly against your production database.\n\nWe recommend using GitHub Actions to automate the creation of PlanetScale branches and deploy requests. Then when you are ready to merge, you can run the deploy request before merging in your code.\n\n**When not to use deploy requests**\n\nIf your application has minimal data and schema changes are a low risk event, then running `psdb:migrate` directly against production is perfectly fine. As your datasize grows and your application becomes busier, the risk of schema changes increase and we highly recommend using the deploy request flow. It's the best way available to safely migrate your schema.\n\n## How to safely drop a column\n\nBefore dropping the column in your database, there are a couple steps to take in your application to safely remove the column without any risk to your production application.\n\n1. Remove all references to the column in your code.\n2. Add the column name to `self.ignored_columns += %w(column_name)` in the model. [More on ignored_columns](https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-ignored_columns-3D).\n3. Deploy this code to production. This ensures your application is no longer making use of the column in production and it's removed from the schema cache.\n4. Follow the PlanetScale deploy request flow to drop the column.\n\n## Usage with GitHub Actions\n\nSee the [GitHub Actions examples](actions-example.md) doc for ways to automate your schema migrations with PlanetScale + Actions.\n\n## Does this gem work with PlanetScale for Postgres?\n\nNo, this gem is currently for Vitess databases only. When using PlanetScale for Postgres we recommend running `RAILS_ENV=production rails db:migrate` before your production code deploys to make schema changes.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/planetscale/planetscale_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/planetscale/planetscale_rails/blob/main/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [Apache 2.0 License](https://opensource.org/license/apache-2-0/).\n\n## Code of Conduct\n\nEveryone interacting in the PlanetScale Rails project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/planetscale/planetscale_rails/blob/main/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplanetscale%2Fplanetscale_rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplanetscale%2Fplanetscale_rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplanetscale%2Fplanetscale_rails/lists"}