Skip to content

Commit a93037a

Browse files
Project components table audit (#418)
closes #415 As per issue #415, but also: - Uses db:prepare which will run only run db:seed if the database doesn't exist. - Makes the classroom_management seeds idempotent, by not proceeding if a school exists. --------- Co-authored-by: create-issue-branch[bot] <53036503+create-issue-branch[bot]@users.noreply.github.com> Co-authored-by: Dan Halson <[email protected]>
1 parent c0fd6b3 commit a93037a

16 files changed

+170
-10
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ gem 'omniauth-rpi',
2727
github: 'RaspberryPiFoundation/omniauth-rpi',
2828
tag: 'v1.3.1'
2929
gem 'open-uri'
30+
gem 'paper_trail'
3031
gem 'pg', '~> 1.1'
3132
gem 'postmark-rails'
3233
gem 'puma', '~> 6'

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ GEM
290290
stringio
291291
time
292292
uri
293+
paper_trail (15.1.0)
294+
activerecord (>= 6.1)
295+
request_store (~> 1.4)
293296
parallel (1.22.1)
294297
parser (3.2.1.0)
295298
ast (~> 2.4.1)
@@ -364,6 +367,8 @@ GEM
364367
regexp_parser (2.7.0)
365368
reline (0.5.9)
366369
io-console (~> 0.5)
370+
request_store (1.7.0)
371+
rack (>= 1.4)
367372
rexml (3.3.0)
368373
strscan
369374
roo (2.10.1)
@@ -537,6 +542,7 @@ DEPENDENCIES
537542
omniauth-rails_csrf_protection (~> 1.0.1)
538543
omniauth-rpi!
539544
open-uri
545+
paper_trail
540546
pg (~> 1.1)
541547
postmark-rails
542548
pry-byebug

app/controllers/api_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class ::ParameterError < StandardError; end
1010
rescue_from CanCan::AccessDenied, with: -> { denied }
1111
rescue_from ParameterError, with: -> { unprocessable }
1212

13+
before_action :set_paper_trail_whodunnit
14+
1315
private
1416

1517
def bad_request

app/models/component.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ class Component < ApplicationRecord
66
validates :extension, presence: true
77
validate :default_component_protected_properties, on: :update
88

9+
has_paper_trail(
10+
if: ->(c) { c.project&.school_id },
11+
meta: {
12+
meta_project_id: ->(c) { c.project&.id },
13+
meta_school_id: ->(c) { c.project&.school_id }
14+
}
15+
)
16+
917
private
1018

1119
def default_component_protected_properties

app/models/project.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ class Project < ApplicationRecord
2121

2222
scope :internal_projects, -> { where(user_id: nil) }
2323

24+
has_paper_trail(
25+
if: ->(p) { p&.school_id },
26+
meta: {
27+
meta_remixed_from_id: ->(p) { p&.remixed_from_id },
28+
meta_school_id: ->(p) { p&.school_id }
29+
}
30+
)
31+
2432
def self.users(current_user)
2533
school = School.find_by(id: pluck(:school_id))
2634
SchoolStudent::List.call(school:, token: current_user.token, student_ids: pluck(:user_id))[:school_students] || []

bin/docker-debug-entrypoint.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
rails db:setup --trace
1+
#!/bin/bash
2+
3+
rails db:prepare
24
rdbg -n -o -c -- bin/rails s -p 3009 -b '0.0.0.0'

bin/docker-entrypoint.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
rails db:setup --trace
1+
#!/bin/bash
2+
3+
rails db:prepare
24
rails server --port 3009 --binding 0.0.0.0
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This migration creates the `versions` table, the only schema PT requires.
2+
# All other migrations PT provides are optional.
3+
class CreateVersions < ActiveRecord::Migration[7.1]
4+
5+
# The largest text column available in all supported RDBMS is
6+
# 1024^3 - 1 bytes, roughly one gibibyte. We specify a size
7+
# so that MySQL will use `longtext` instead of `text`. Otherwise,
8+
# when serializing very large objects, `text` might not be big enough.
9+
TEXT_BYTES = 1_073_741_823
10+
11+
def change
12+
create_table :versions, id: :uuid do |t|
13+
t.string :item_type, null: false
14+
t.string :item_id, null: false
15+
t.string :event, null: false
16+
t.string :whodunnit
17+
# We're not using versioning, so exclude the original state by not having an options field (see docs)
18+
# t.json :object
19+
20+
# Known issue in MySQL: fractional second precision
21+
# -------------------------------------------------
22+
#
23+
# MySQL timestamp columns do not support fractional seconds unless
24+
# defined with "fractional seconds precision". MySQL users should manually
25+
# add fractional seconds precision to this migration, specifically, to
26+
# the `created_at` column.
27+
# (https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html)
28+
#
29+
# MySQL users should also upgrade to at least rails 4.2, which is the first
30+
# version of ActiveRecord with support for fractional seconds in MySQL.
31+
# (https://github.com/rails/rails/pull/14359)
32+
#
33+
# MySQL users should use the following line for `created_at`
34+
# t.datetime :created_at, limit: 6
35+
t.datetime :created_at
36+
end
37+
add_index :versions, %i[item_type item_id]
38+
end
39+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This migration adds the optional `object_changes` column, in which PaperTrail
2+
# will store the `changes` diff for each update event. See the readme for
3+
# details.
4+
class AddObjectChangesToVersions < ActiveRecord::Migration[7.1]
5+
# The largest text column available in all supported RDBMS.
6+
# See `create_versions.rb` for details.
7+
TEXT_BYTES = 1_073_741_823
8+
9+
def change
10+
add_column :versions, :object_changes, :json
11+
end
12+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This migration adds the optional `object_changes` column, in which PaperTrail
2+
# will store the `changes` diff for each update event. See the readme for
3+
# details.
4+
class AddMetaColumnsToVersions < ActiveRecord::Migration[7.1]
5+
def change
6+
add_column :versions, :meta_project_id, :uuid
7+
add_column :versions, :meta_school_id, :uuid
8+
add_column :versions, :meta_remixed_from_id, :uuid
9+
end
10+
end

0 commit comments

Comments
 (0)