Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update migration examples to showcase database-agnostic raw SQL execution #48085

Merged
merged 1 commit into from Apr 28, 2023

Conversation

wpank84
Copy link
Contributor

@wpank84 wpank84 commented Apr 27, 2023

Updated the raw SQL shown in the migration code examples of sections 3.10, 3.11, and 3.12.

The previous example would error out in SQLite due to SQLite not supporting adding constraints in the ALTER TABLE command.

Motivation / Background

Fixes #47941

Detail

This Pull Request changes the code examples of active_record_migrations.md in sections 3.10, 3.11, and 3.12.

@rails-bot rails-bot bot added the docs label Apr 27, 2023
@yahonda
Copy link
Member

yahonda commented Apr 28, 2023

Thanks for opening this pull request.

This section aims to explain how to make migrations reversible when the existing migration method is not reversible. However, these SQL statements in the pull request migration are already reversible using the `add_index' method.

### Using the `up`/`down` Methods
You can also use the old style of migration using `up` and `down` methods
instead of the `change` method.
The `up` method should describe the transformation you'd like to make to your
schema, and the `down` method of your migration should revert the
transformations done by the `up` method. In other words, the database schema
should be unchanged if you do an `up` followed by a `down`.
For example, if you create a table in the `up` method, you should drop it in the
`down` method. It is wise to perform the transformations in precisely the
reverse order they were made in the `up` method. The example in the `reversible`
section is equivalent to:

For example, the current example has been updated from foreign key to check constraint if Rails migration supports add_foreign_key' and remove_foreign_key'. Refer to #15606

Would you provide some SQL statements which are not supported by Rails change method and work for all supported databases like PostgreSQL, MySQL and SQLite3?

@wpank84
Copy link
Contributor Author

wpank84 commented Apr 28, 2023

@yahonda Appreciate the feedback! I went ahead and made a new commit using CREATE VIEW for the raw SQL which, to my knowledge, should satisfy those conditions.

@yahonda
Copy link
Member

yahonda commented Apr 28, 2023

It looks good. Would you squash your commits into a single commit?

@wpank84
Copy link
Contributor Author

wpank84 commented Apr 28, 2023

Squashed the commit to a single commit and should be good to go

@yahonda yahonda merged commit 2a7cd75 into rails:main Apr 28, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Active Record Migrations Guide contains invalid syntax for SQLite3
2 participants