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

Add Spree::Migration #5000

Closed
wants to merge 3 commits into from
Closed

Conversation

tvdeyen
Copy link
Member

@tvdeyen tvdeyen commented Mar 29, 2023

Summary

Uses the current Rails version the app is running to run migrations.

This makes sure, that

a) extensions that are running an older Rails version (we still support down to Rails 5.2) are able to run migrations that are introduced in later Rails versions.

b) the Solidus tables are created the same way as the rest of the host apps tables that most likely exist.

Most of our migrations or very old (some of them still run with Rails 4.2 version). This changes tables and timestamps to very old Rails defaults. Leading to a misconfigured and inconsistent database schema for new Rails apps.

Since this does not effect existing apps, it should be safe to do that.

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

Uses the current Rails version the app is running to run migrations.
This makes sure, that a) extensions that are running an older Rails
version (we still support down to Rails 5.2) are able to run migrations
that are introduced in later Rails versions. Also it makes sure that
the Solidus tables are created the same way as the rest of the host
apps tables that most likely exist. Most of our migrations or very old
(some of them still run with Rails 4.2 version). This changes tables
and timestamps to very old Rails defaults. Leading to a misconfigured
and inconsistent database schema for new Rails apps. Since this does
not effect existing apps, it should be safe to do that.
@tvdeyen tvdeyen requested a review from a team as a code owner March 29, 2023 14:00
@github-actions github-actions bot added the changelog:solidus_core Changes to the solidus_core gem label Mar 29, 2023
@tvdeyen tvdeyen added the type:enhancement Proposed or newly added feature label Mar 29, 2023
Need to use add_reference so that the column types
match. MySQL will rightfully complain otherwise.
@tvdeyen
Copy link
Member Author

tvdeyen commented Mar 29, 2023

Ha, this already revealed a bug with mismatching column types for foreign keys. A thing that MySQL will rightfully complain about and fail to install a fresh Solidus app.

@codecov
Copy link

codecov bot commented Mar 29, 2023

Codecov Report

Merging #5000 (96ca33f) into master (1c5adde) will decrease coverage by 0.03%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master    #5000      +/-   ##
==========================================
- Coverage   86.69%   86.66%   -0.03%     
==========================================
  Files         579      580       +1     
  Lines       14721    14725       +4     
==========================================
  Hits        12762    12762              
- Misses       1959     1963       +4     
Impacted Files Coverage Δ
core/lib/spree/migration.rb 0.00% <0.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@kennyadsl
Copy link
Member

@tvdeyen Thanks!

I'm trying to figure out an example of the difference between a migration on Rails 4.2 and 7 (any other versions are ok for the example). Can you help me?

@BenMorganIO
Copy link
Contributor

BenMorganIO commented Apr 14, 2023

I really don't like this because it doesn't provide guarantees. I think this could cause issues with the schema falling out of state as new features are coded in Solidus. Perhaps we should create a migration to get current existing tables up to the standards of future rails gem versions. I already have some code for that for timestamps.

https://gist.github.com/BenMorganIO/4874b7a1d37c465999535a08ef2c2927

@kennyadsl Differences would be:

  • setting the timestamps to not null
  • whether indexes are added or not when you do references (I think - this needs to be checked)
  • the type that's used for the primary/foreign key (bigint vs integer)

Some of these we can easily just go through as a laundry list and create a migration to do a mass changeover.

@kennyadsl
Copy link
Member

@BenMorganIO I didn't think about this approach, but I think it might make a lot of sense.

Still, I'm afraid we need to add this migration in a major release, at least for some of the changes mentioned. For example, adding indexes might require updating some inconsistent data to avoid the migration to fail.

@BenMorganIO
Copy link
Contributor

I agree, it should be in a major release. This will take about a day out of a devs time to work through on a large, older project, but not likely more than that.

Copy link
Contributor

@adammathys adammathys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennyadsl You can find all of the differences between migration versions here: https://github.com/rails/rails/blob/main/activerecord/lib/active_record/migration/compatibility.rb
Not the easiest thing to read, but that's where they're stored. A notable example, prior to Rails 5.1 primary keys were stored as integer columns. Afterwards, they defaulted to bigint. (You can see the logic swap back to integer in the V5_0 class.)

@@ -0,0 +1,4 @@
module Spree
class Migration < ActiveRecord::Migration[ActiveRecord::Migration.current_version]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried leaving this dynamic is going to cause some headaches in the future. While true that we don't need to worry about existing stores, since they've already copied in the migrations, any future changes to ActiveRecord::Migration will result in non-deterministic migrations for stores.

And possibly even more overhead for existing users. If we find ourselves needing to update old migrations to work with more recent versions of Rails, any stores that have already copied those migrations will likely need to also copy over our changes themselves. (Or just never run migrations again, but I don't think that's something we can unilaterally enforce.)

I think @BenMorganIO's approach is the more sustainable one. We should provide migrations that can help update existing tables to match the new defaults we'd get from running the migration on more modern versions of Rails. That will help ensure both existing and new Solidus stores have a consistent schema to work from.

@kennyadsl
Copy link
Member

Just for reference, I opened #5030, which is related to this.

@tvdeyen tvdeyen closed this Jun 12, 2023
@tvdeyen tvdeyen deleted the solidus-migration branch June 12, 2023 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem type:enhancement Proposed or newly added feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants