kreeti / kt-paperclip

Easy file attachment management for ActiveRecord
Other
275 stars 95 forks source link

include migration statements on ActiveRecord::Migration #125

Closed samrjenkins closed 10 months ago

samrjenkins commented 1 year ago

Currently, projects with old migration files containing Paperclip schema statements do not perform the same schema changes both before and after an ActiveRecord version upgrade.

Consider the following migration file which will add one datetime column using ActiveRecord's add_column and another datetime column as part of Paperclip's add_attachment (note that is is written as an ActiveRecord 6.1 migration).

class AddFooAttachmentToBars < ActiveRecord::Migration[6.1]
  def change
    add_column :bars, :baz, :datetime
    add_attachment :bars, :foo
  end
end

We would expect the two added datetime columns to be identical in everything but name.

When run on Rails 6.1.7.6 it produces the following schema entry as expected: baz and foo_updated_at both have the same format

  create_table "bars", force: :cascade do |t|
    t.datetime "baz"
    t.string "foo_content_type"
    t.string "foo_file_name"
    t.bigint "foo_file_size"
    t.datetime "foo_updated_at"
  end

When run on Rails 7.0.8 it produces the following schema entry: baz and foo_updated_at have different formats (note that foo_updated_at is no longer added with precision: nil as it would have in Rails 6)

  create_table "bars", force: :cascade do |t|
    t.datetime "baz", precision: nil
    t.string "foo_content_type"
    t.string "foo_file_name"
    t.bigint "foo_file_size"
    t.datetime "foo_updated_at"
  end

This is because Paperclip schema statements do not currently respect ActiveRecord::Migration::Compatibility versioning; the logic which ensures that schema statements from old migrations retain consistent behaviour when run within newer versions of ActiveRecord.


This pull request includes Paperclip::Schema::Statements on ActiveRecord::Migration to ensure that Paperclip schema statements are consistent with ActiveRecord::Migration::Compatibility versioning.

When run on Rails 6.1.7.6 it produces the following schema entry as expected: baz and foo_updated_at both have the same format

  create_table "bars", force: :cascade do |t|
    t.datetime "baz"
    t.string "foo_content_type"
    t.string "foo_file_name"
    t.bigint "foo_file_size"
    t.datetime "foo_updated_at"
  end

When run on Rails 7.0.8 it produces the following schema entry as expected: baz and foo_updated_at both have the same format

  create_table "bars", force: :cascade do |t|
    t.datetime "baz", precision: nil
    t.string "foo_content_type"
    t.string "foo_file_name"
    t.bigint "foo_file_size"
    t.datetime "foo_updated_at", precision: nil
  end
ssinghi commented 10 months ago

@samrjenkins can you please sign your commits

samrjenkins commented 10 months ago

@ssinghi I have rebased with signed commits