Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Project < ApplicationRecord
self.ignored_columns += [:instructions]

module Types
PYTHON = 'python'
HTML = 'html'
Expand Down Expand Up @@ -83,12 +85,11 @@ def scratch_component=(value)
end

def instructions
self[:instruction_steps].nil? ? self[:instructions] : self[:instruction_steps]
instruction_steps
end

def instructions=(value)
self[:instructions] = value unless value.is_a?(Array)
self[:instruction_steps] = value
self.instruction_steps = value
end

def last_edited_at
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class BackfillInstructionStepsFromInstructions < ActiveRecord::Migration[8.1]
def up
execute <<~SQL.squish
UPDATE projects
SET instruction_steps = to_jsonb(instructions)
WHERE instruction_steps IS NULL AND instructions IS NOT NULL
SQL
Comment on lines +5 to +9

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to be bold and try to run this as I expect the migration to run within a few seconds.

end

def down
end
end
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 3 additions & 22 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,31 +437,12 @@
end

describe '#instructions' do
let(:project) { create(:project, :with_instructions, school:, user_id: create(:teacher, school:).id) }
let(:project) { create(:project, school:, user_id: create(:teacher, school:).id) }

it 'falls back to the legacy text column for rows that predate instruction_steps' do
project.instruction_steps = nil
project.save!
expect(project.instructions).to eq(project[:instructions])
end

it 'returns instruction_steps once set, without touching the legacy column for arrays' do
legacy_value = project[:instructions]
it 'delegates to instruction_steps' do
project.update!(instructions: [{ markdown_content: 'step 1' }])
expect(project.instructions).to eq([{ 'markdown_content' => 'step 1' }])
expect(project[:instructions]).to eq(legacy_value)
end

it 'does not fall back to stale legacy text once instruction_steps is set to an empty array' do
project.update!(instructions: [])
expect(project.instructions).to eq([])
end

it 'also clears the legacy column when instructions are set to nil' do
project.update!(instructions: [{ markdown_content: 'step 1' }])
project.update!(instructions: nil)
expect(project.instructions).to be_nil
expect(project[:instructions]).to be_nil
expect(project.instruction_steps).to eq([{ 'markdown_content' => 'step 1' }])
end
end

Expand Down
Loading