Skip to content

Commit

Permalink
Enum backfill data migration (#3936)
Browse files Browse the repository at this point in the history
Follows #3925

> This PR is the first of few to migrate the few enums (backed by
integers) to ones backed by strings
> 
> 1. add new columns, with temp_ in the name and prefix: true to avoid
method namespace collision with existing enums
> 2. backfill data migration to populate new column with the string
equivalent of the integer column (0 => draft, etc)

This PR does 1 and 2 from #3925
  • Loading branch information
veganstraightedge authored Sep 10, 2024
1 parent 63fc5d4 commit db23795
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 57 deletions.
9 changes: 5 additions & 4 deletions app/models/concerns/featureable.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Featureable
extend ActiveSupport::Concern

included do
scope :featured, -> { where.not(featured_at: nil) }
before_save :update_featured_at
end
# TEMP: re-enable and expand coverage to include Journal, Issue, et al
# included do
# scope :featured, -> { where.not(featured_at: nil) }
# before_save :update_featured_at
# end

module ClassMethods
def for_index fallback_sort: { title: :asc }, fallback_locale: 'en'
Expand Down
6 changes: 3 additions & 3 deletions app/models/concerns/publishable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module Publishable
included do
enum :publication_status, PUBLICATION_STATUSES
enum :temp_publication_status,
{ draft: :draft, published: :published },
default: :draft,
{ draft: 'draft', published: 'published' },
# default: :draft,
prefix: true
# validate: true,
# validate: true

default_scope { order(published_at: :desc) }

Expand Down
6 changes: 3 additions & 3 deletions app/models/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class Locale < ApplicationRecord

enum :language_direction, { ltr: 0, rtl: 1 }
enum :temp_language_direction,
{ ltr: :ltr, rtl: :rtl },
default: :ltr,
{ ltr: 'ltr', rtl: 'rtl' },
# default: :ltr,
prefix: true
# validate: true,
# validate: true

default_scope { order abbreviation: :asc }

Expand Down
10 changes: 5 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class User < ApplicationRecord
ROLES = %i[author editor publisher].freeze

enum :role, ROLES
enum :temp_roles,
{ author: :author, editor: :editor, publisher: :publisher },
default: :author,
prefix: true
# validate: true,
enum :temp_role,
{ author: 'author', editor: 'editor', publisher: 'publisher' },
# default: :author,
prefix: true
# validate: true

validates :username, presence: true, uniqueness: true, on: %i[create update]

Expand Down
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
require_relative '../app/middlewares/rack/teapot'
require 'rack/contrib'

# TEMP: enum backfill data migration class
require_relative '../db/data_migrate'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
Expand Down
66 changes: 66 additions & 0 deletions db/data_migrate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# rubocop:disable Metrics/MethodLength
class DataMigrate
class << self
def run
# locales
Locale.find_each do |locale|
locale.update! temp_language_direction: locale.language_direction
end

# users
User.find_each do |user|
user.update! temp_role: user.role
end

# "publishable" tables
Article.find_each do |article|
article.update! temp_publication_status: article.publication_status
end

Book.find_each do |book|
book.update! temp_publication_status: book.publication_status
end

Definition.find_each do |definition|
definition.update! temp_publication_status: definition.publication_status
end

Episode.find_each do |episode|
episode.update! temp_publication_status: episode.publication_status
end

Issue.find_each do |issue|
issue.update! temp_publication_status: issue.publication_status
end

Journal.find_each do |journal|
journal.update! temp_publication_status: journal.publication_status
end

Logo.find_each do |logo|
logo.update! temp_publication_status: logo.publication_status
end

Page.find_each do |page|
page.update! temp_publication_status: page.publication_status
end

Poster.find_each do |poster|
poster.update! temp_publication_status: poster.publication_status
end

Sticker.find_each do |sticker|
sticker.update! temp_publication_status: sticker.publication_status
end

Video.find_each do |video|
video.update! temp_publication_status: video.publication_status
end

Zine.find_each do |zine|
zine.update! temp_publication_status: zine.publication_status
end
end
end
end
# rubocop:enable Metrics/MethodLength
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameColumnTempRolesOnUsers < ActiveRecord::Migration[7.2]
def change
rename_column :users, :temp_roles, :temp_role
end
end
Loading

0 comments on commit db23795

Please sign in to comment.