Permalink
Browse files

Add 'cleanup' migrations

Optional cleanup migrations for 3.5.0 can be used by developers after they have tested the data migration. Rather than just blowing away old columns and files, they can be retained for testing

These migrations will:

* Remove the old attachment file storage directories
* Update the attachments and models tables to move the columns to the attachments table.
  • Loading branch information...
1 parent 464f034 commit 21bd20f6e85643fc007240cfe8fe6ac439a20caa @peakpg peakpg committed May 8, 2012
View
15 db/migrate/20120329144406_browsercms350.rb
@@ -1,24 +1,27 @@
+require 'cms/upgrades/v3_5_0'
+
class Browsercms350 < ActiveRecord::Migration
def change
- update_attachment_schema
-
+ migrate_attachments_table
+ migrate_attachment_for(Cms::ImageBlock)
+ migrate_attachment_for(Cms::FileBlock)
+ migrate_attachment_files_to_new_location
end
-
-
private
- def update_attachment_schema
+
+ def migrate_attachments_table
rename_table :attachments, :cms_attachments if table_exists?(:attachments)
rename_table :attachment_versions, :cms_attachment_versions if table_exists?(:attachment_versions)
rename_content_column :cms_attachments, :file_path, :data_file_path
rename_content_column :cms_attachments, :file_size, :data_file_size
rename_content_column :cms_attachments, :file_type, :data_content_type
rename_content_column :cms_attachments, :name, :data_file_name
- # No longer used, calculated dynamically from data_file_name
remove_content_column :cms_attachments, :file_extension
+
add_content_column :cms_attachments, :data_fingerprint, :string
add_content_column :cms_attachments, :attachable_type, :string
add_content_column :cms_attachments, :attachment_name, :string
View
11 db/migrate/20120427181438_update_attachment_data.rb
@@ -1,11 +0,0 @@
-require 'cms/upgrades/v3_5_0'
-
-class UpdateAttachmentData < ActiveRecord::Migration
-
- def change
- migrate_attachment_for(Cms::ImageBlock)
- migrate_attachment_for(Cms::FileBlock)
- move_attachments_to_new_location
- end
-
-end
View
37 lib/cms/upgrades/v3_5_0.rb
@@ -11,6 +11,7 @@ def upgrade_to_3_5_0
# Add additional methods to ActiveRecord::Migration when this file is required.
module FileStorageUpdates
+
# Old paths are:
# uploads/:year/:month/:day/:fingerprint
# i.e. uploads/2012/04/27/fb598......
@@ -65,19 +66,47 @@ def migrated_attachment_for_versioned_table(model_class, attachable_type)
end
# Move the attachment files from the old file path path to new one. Updates the Attachment record to match.
- def move_attachments_to_new_location
+ def migrate_attachment_files_to_new_location
migrate_attachments_file_location(Cms::Attachment)
migrate_attachments_file_location(Cms::Attachment::Version)
end
+ # Remove the attachment_version and attachment_id columns for all core CMS blocks.
+ def cleanup_attachment_columns_for_core_cms
+ cleanup_attachment_columns(Cms::FileBlock)
+ end
+
+ # The 'attachment_id and attachment_version are no longer stored in the model, but in the attachments table'
+ # @param [Class] model_class The model to have its columns cleaned up.
+ def cleanup_attachment_columns(model_class)
+ remove_content_column model_class.table_name, :attachment_id if column_exists?(model_class.table_name, :attachment_id)
+ remove_content_column model_class.table_name, :attachment_version if column_exists?(model_class.table_name, :attachment_id)
+ end
+
+ # Removes the cms_attachments.file_location column
+ #
+ # data_fingerprint is used to store the file name instead now.
+ def cleanup_attachments_file_location
+ remove_content_column :cms_attachments, :file_location if column_exists?(:cms_attachments, :file_location)
+ end
+
+ # Deletes the old file storage folders from the uploads directory.
+ #
+ def cleanup_attachment_file_storage
+ ["2009", "2010", "2011", "2012"].each do |year|
+ folder_to_remove = File.join(attachments_dir, year)
+ FileUtils.rm_rf(folder_to_remove, :verbose => true)
+ end
+ end
+
private
# Multiple version of attachments may point to the same file, so we need to test
# a file exists before moving. Failure to move isn't necessary an error, since it may have been moved in
# an earlier version
def migrate_attachments_file_location(model_class)
model_class.unscoped.each do |attachment|
- old_location = File.join(Cms::Attachment.configuration.attachments_root, attachment.file_location)
+ old_location = File.join(attachments_dir, attachment.file_location)
new_file_location, new_dir = new_file_location(attachment)
new_location = File.join(Cms::Attachment.configuration.attachments_root, new_file_location)
new_dir_path = File.join(Cms::Attachment.configuration.attachments_root, new_dir)
@@ -94,6 +123,10 @@ def migrate_attachments_file_location(model_class)
end
end
+ def attachments_dir
+ Cms::Attachment.configuration.attachments_root
+ end
+
# Allows us to use the Paperclip interpolations logic directly
class AttachmentWrapper
def initialize(attachment)
View
9 todo_list.markdown
@@ -12,19 +12,16 @@ rake db:drop db:create && mysql -u root -p browsercms_development < test/dummy/d
rake db:migrate
#### Writing migrations
-- Merge migrations for 3.5 into a single one
-- Review all rows to ensure the correct version is getting copied.
+
- Write upgrade docs for models that use Attachments
-- Will need to change model and add has_attachment :file
-- Will need to add a migration with:
standardize_version_id_column "block_table_name"
migrate_attachment_for(BlockClassName)
+
- After everything is done, users should be able to create a 'cleanup' migration that will tidy things up.
-- Write instructions for defining a 'cleanup' migration
--- Add methods to v3_5_0 for upgrades.
--- Remove attachment_id and attachment_version from any models (file_blocks and file_block_versions)
--- Remove attachments.file_location
--- Remove old folders in uploads directory
+
- Figure out plan for Migrating from Asset module attachments. The file structure may be the same, but we need to make sure it works.

0 comments on commit 21bd20f

Please sign in to comment.