Skip to content

Commit

Permalink
include migration statements on ActiveRecord::Migration (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
samrjenkins committed Dec 16, 2023
1 parent 0fbd26f commit 1c6dfe6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[UNRELEASED]
* Improvement: Support file extension names both as symbols and strings for :content_type_mappings
* Issue file delete only once per unique style when nullifying attachment or destroying an object. Avoids triggering a rate limit error on Google Cloud Storage.
* Paperclip schema statements are consistent with ActiveRecord::Migration::Compatibility versioning. Old migrations containing Paperclip schema statements perform the same schema changes both before and after an ActiveRecord version upgrade.

7.0.0 (2021-05-28)
* Replace `mimemagic` gem with `marcel` due to licensing issues. See https://github.com/kreeti/kt-paperclip/pull/54 for details and limitations
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Schema
def self.included(_base)
ActiveRecord::ConnectionAdapters::Table.include TableDefinition
ActiveRecord::ConnectionAdapters::TableDefinition.include TableDefinition
ActiveRecord::ConnectionAdapters::AbstractAdapter.include Statements
ActiveRecord::Migration.include Statements
ActiveRecord::Migration::CommandRecorder.include CommandRecorder
end

Expand Down
12 changes: 6 additions & 6 deletions spec/paperclip/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ def call(_filename)

context "An attachment with only a avatar_file_name column" do
before do
ActiveRecord::Base.connection.create_table :dummies, force: true do |table|
ActiveRecord::Migration.create_table :dummies, force: true do |table|
table.column :avatar_file_name, :string
end
rebuild_class
Expand All @@ -1359,7 +1359,7 @@ def call(_filename)

context "and avatar_created_at column" do
before do
ActiveRecord::Base.connection.add_column :dummies, :avatar_created_at, :timestamp
ActiveRecord::Migration.add_column :dummies, :avatar_created_at, :timestamp
rebuild_class
@dummy = Dummy.new
end
Expand Down Expand Up @@ -1396,7 +1396,7 @@ def call(_filename)

context "and avatar_updated_at column" do
before do
ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp
ActiveRecord::Migration.add_column :dummies, :avatar_updated_at, :timestamp
rebuild_class
@dummy = Dummy.new
end
Expand Down Expand Up @@ -1426,7 +1426,7 @@ def call(_filename)

context "and avatar_content_type column" do
before do
ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string
ActiveRecord::Migration.add_column :dummies, :avatar_content_type, :string
rebuild_class
@dummy = Dummy.new
end
Expand All @@ -1443,7 +1443,7 @@ def call(_filename)

context "and avatar_file_size column" do
before do
ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :bigint
ActiveRecord::Migration.add_column :dummies, :avatar_file_size, :bigint
rebuild_class
@dummy = Dummy.new
end
Expand All @@ -1467,7 +1467,7 @@ def call(_filename)

context "and avatar_fingerprint column" do
before do
ActiveRecord::Base.connection.add_column :dummies, :avatar_fingerprint, :string
ActiveRecord::Migration.add_column :dummies, :avatar_fingerprint, :string
rebuild_class
@dummy = Dummy.new
end
Expand Down
2 changes: 1 addition & 1 deletion spec/paperclip/paperclip_missing_attachment_styles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
expected_hash = { Dummy: { avatar: [:export, :thumb] } }
assert_equal expected_hash, Paperclip.missing_attachments_styles

ActiveRecord::Base.connection.create_table :books, force: true
ActiveRecord::Migration.create_table :books, force: true
class ::Book < ActiveRecord::Base
has_attached_file :cover, styles: { small: "x100", large: "1000x1000>" }
has_attached_file :sample, styles: { thumb: "x100" }
Expand Down
36 changes: 18 additions & 18 deletions spec/paperclip/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

after do
begin
Dummy.connection.drop_table :dummies
ActiveRecord::Migration.drop_table :dummies
rescue StandardError
nil
end
Expand All @@ -23,7 +23,7 @@
ActiveSupport::Deprecation.silenced = false
end
it "creates attachment columns" do
Dummy.connection.create_table :dummies, force: true do |t|
ActiveRecord::Migration.create_table :dummies, force: true do |t|
ActiveSupport::Deprecation.silence do
t.has_attached_file :avatar
end
Expand All @@ -38,7 +38,7 @@
end

it "displays deprecation warning" do
Dummy.connection.create_table :dummies, force: true do |t|
ActiveRecord::Migration.create_table :dummies, force: true do |t|
assert_deprecated do
t.has_attached_file :avatar
end
Expand All @@ -48,7 +48,7 @@

context "using #attachment" do
before do
Dummy.connection.create_table :dummies, force: true do |t|
ActiveRecord::Migration.create_table :dummies, force: true do |t|
t.attachment :avatar
end
end
Expand All @@ -65,7 +65,7 @@

context "using #attachment with options" do
before do
Dummy.connection.create_table :dummies, force: true do |t|
ActiveRecord::Migration.create_table :dummies, force: true do |t|
t.attachment :avatar, default: 1, file_name: { default: "default" }
end
end
Expand All @@ -83,13 +83,13 @@

context "within schema statement" do
before do
Dummy.connection.create_table :dummies, force: true
ActiveRecord::Migration.create_table :dummies, force: true
end

context "migrating up" do
context "with single attachment" do
before do
Dummy.connection.add_attachment :dummies, :avatar
ActiveRecord::Migration.add_attachment :dummies, :avatar
end

it "creates attachment columns" do
Expand All @@ -104,7 +104,7 @@

context "with single attachment and options" do
before do
Dummy.connection.add_attachment :dummies, :avatar, default: "1", file_name: { default: "default" }
ActiveRecord::Migration.add_attachment :dummies, :avatar, default: "1", file_name: { default: "default" }
end

it "sets defaults on columns" do
Expand All @@ -119,7 +119,7 @@

context "with multiple attachments" do
before do
Dummy.connection.add_attachment :dummies, :avatar, :photo
ActiveRecord::Migration.add_attachment :dummies, :avatar, :photo
end

it "creates attachment columns" do
Expand All @@ -138,7 +138,7 @@

context "with multiple attachments and options" do
before do
Dummy.connection.add_attachment :dummies, :avatar, :photo, default: "1", file_name: { default: "default" }
ActiveRecord::Migration.add_attachment :dummies, :avatar, :photo, default: "1", file_name: { default: "default" }
end

it "sets defaults on columns" do
Expand All @@ -157,15 +157,15 @@
context "with no attachment" do
it "raises an error" do
assert_raises ArgumentError do
Dummy.connection.add_attachment :dummies
ActiveRecord::Migration.add_attachment :dummies
end
end
end
end

context "migrating down" do
before do
Dummy.connection.change_table :dummies do |t|
ActiveRecord::Migration.change_table :dummies do |t|
t.column :avatar_file_name, :string
t.column :avatar_content_type, :string
t.column :avatar_file_size, :bigint
Expand All @@ -179,7 +179,7 @@
end
it "removes the attachment columns" do
ActiveSupport::Deprecation.silence do
Dummy.connection.drop_attached_file :dummies, :avatar
ActiveRecord::Migration.drop_attached_file :dummies, :avatar
end

columns = Dummy.columns.map { |column| [column.name, column.sql_type] }
Expand All @@ -192,15 +192,15 @@

it "displays a deprecation warning" do
assert_deprecated do
Dummy.connection.drop_attached_file :dummies, :avatar
ActiveRecord::Migration.drop_attached_file :dummies, :avatar
end
end
end

context "using #remove_attachment" do
context "with single attachment" do
before do
Dummy.connection.remove_attachment :dummies, :avatar
ActiveRecord::Migration.remove_attachment :dummies, :avatar
end

it "removes the attachment columns" do
Expand All @@ -215,14 +215,14 @@

context "with multiple attachments" do
before do
Dummy.connection.change_table :dummies do |t|
ActiveRecord::Migration.change_table :dummies do |t|
t.column :photo_file_name, :string
t.column :photo_content_type, :string
t.column :photo_file_size, :bigint
t.column :photo_updated_at, :datetime
end

Dummy.connection.remove_attachment :dummies, :avatar, :photo
ActiveRecord::Migration.remove_attachment :dummies, :avatar, :photo
end

it "removes the attachment columns" do
Expand All @@ -242,7 +242,7 @@
context "with no attachment" do
it "raises an error" do
assert_raises ArgumentError do
Dummy.connection.remove_attachment :dummies
ActiveRecord::Migration.remove_attachment :dummies
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
config = YAML::safe_load(IO.read(File.dirname(__FILE__) + "/database.yml"))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.establish_connection(config["test"])
ActiveRecord::Migration.verbose = false
if ActiveRecord::VERSION::STRING >= "4.2" &&
ActiveRecord::VERSION::STRING < "5.0"
ActiveRecord::Base.raise_in_transactional_callbacks = true
Expand Down
6 changes: 3 additions & 3 deletions spec/support/model_reconstruction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ def reset_class(class_name)

def reset_table(_table_name, &block)
block ||= lambda { |_table| true }
ActiveRecord::Base.connection.create_table :dummies, **{ force: true }, &block
ActiveRecord::Migration.create_table :dummies, **{ force: true }, &block
end

def modify_table(&block)
ActiveRecord::Base.connection.change_table :dummies, &block
ActiveRecord::Migration.change_table :dummies, &block
end

def rebuild_model(options = {})
ActiveRecord::Base.connection.create_table :dummies, force: true do |table|
ActiveRecord::Migration.create_table :dummies, force: true do |table|
table.column :title, :string
table.column :other, :string
table.column :avatar_file_name, :string
Expand Down

0 comments on commit 1c6dfe6

Please sign in to comment.