Commit Graph

12 Commits

Author SHA1 Message Date
Javi Martín
7212657c02 Remove Paperclip and use just Active Storage 2022-02-23 18:43:48 +01:00
Javi Martín
5519518cfb Parse cached attachment URLs with remote storages
In commit 5a4921a1a we replaced `URI.parse` with `URI.open` due to some
issues during our tests with S3.

However, there are some security issues with `URI.open` [1], since it
might allow some users to execute code on the server.

So we're using `URI.parse#open` instead.

[1] https://docs.rubocop.org/rubocop/cops_security.html#securityopen
2021-11-16 12:37:32 +01:00
Senén Rodero Rodríguez
87e427f433 Allow remote storages in Active Storage migration
So forks that connected Paperclip with S3 buckets can migrate files too.
2021-09-24 13:39:15 +02:00
Javi Martín
b5026e12a7 Ignore missing records in Active Storage migration
There could be inconsistencies in the database and an attachment might
have a `record_id` pointing to a record which no longer exist. We were
getting an exception in this case.
2021-09-24 13:39:15 +02:00
Javi Martín
fd67477281 Don't migrate files already in Active Storage
This way we reduce the hypothetical problems we could find if executing
the task several times.
2021-09-24 13:39:15 +02:00
Javi Martín
7330bfb6ae Ignore deleted files in Active Storage migration
Files might be missing for whatever reason or records might not point to
any files; in these edge cases, we were getting an exception.
2021-09-24 13:39:15 +02:00
Javi Martín
038fb76896 Allow executing the migration task twice in tests
We were getting a duplicate prepared statement error when trying to
execute more than one test using this task. So we're checking whether
the prepared statement exists before defining it.
2021-09-24 13:39:15 +02:00
Javi Martín
9900a21fd5 Use the storage_ prefix for migrated attachments
Just like we add the `storage_` prefix for new records so we can use
both Active Storage and Paperclip at the same time.

Now the migration actually works, at least for basic cases.
2021-09-24 13:39:15 +02:00
Javi Martín
b4b42c7cc9 Fix duplicate entries in Active Storage migation
Using `LASTVAL()` wasn't working 100% of the time for some reason after
inserting and deleting some rows. So we're using `RETURNING` instead.
2021-09-24 13:39:15 +02:00
Javi Martín
a92e82326b Ignore OldPassword in Active Storage migration
This model is added by the devise-security gem, but we don't use it. We
were getting an error while migrating:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "old_passwords" does not exist
LINE 8:                WHERE a.attrelid = '"old_passwords"'::regclas...
                                          ^
SELECT a.attname, format_type(a.atttypid, a.atttypmod),
     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
     c.collname, col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
WHERE a.attrelid = '"old_passwords"'::regclass
  AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
lib/tasks/active_storage.rake:27:in `block (4 levels) in <top (required)>'
lib/tasks/active_storage.rake:26:in `each'
lib/tasks/active_storage.rake:26:in `block (3 levels) in <top (required)>'
lib/tasks/active_storage.rake:25:in `block (2 levels) in <top (required)>'

Caused by:
PG::UndefinedTable: ERROR: relation "old_passwords" does not exist
2021-09-24 13:39:15 +02:00
Javi Martín
a6025d3a12 Simplify copying files in Active Storage migration
We can use the `ActiveStorage::Blob` class to find where the file is
supposed to be stored instead of manually setting the path. This is also
more robust because it works with Active Storage configurations which
don't store files in the default folder.
2021-09-24 13:39:15 +02:00
Javi Martín
619d402f92 Add task to migrate files to Active Storage
This task was copied from Thoughtbot's migration guide [1]. They use a
migration file, but we usually use rake tasks to migrate data.

[1] https://github.com/thoughtbot/paperclip/blob/master/MIGRATING.md
2021-09-24 13:39:15 +02:00