From ee573fe5e9a50db053d950a06246170c0f575b4a Mon Sep 17 00:00:00 2001 From: Jacob Michalskie Date: Thu, 18 Apr 2024 08:59:06 +0200 Subject: [PATCH] Install activestorage for storing user uploaded files --- .gitignore | 1 + .../app/controllers/webui/webui_controller.rb | 1 + src/api/config/application.rb | 1 + src/api/config/environments/development.rb | 2 +- src/api/config/environments/production.rb | 2 +- src/api/config/environments/test.rb | 2 +- src/api/config/storage.yml | 7 +++ ...te_active_storage_tables.active_storage.rb | 57 +++++++++++++++++++ src/api/db/schema.rb | 38 +++++++++++-- 9 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 src/api/config/storage.yml create mode 100644 src/api/db/migrate/20240418065204_create_active_storage_tables.active_storage.rb diff --git a/.gitignore b/.gitignore index 5504f6752153..17a0328d8d83 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /src/api/config/options.yml /src/api/config/secret.key /src/api/config/thinking_sphinx.yml +/src/api/storage /src/api/db/sphinx /src/api/config/*.sphinx.conf /src/api/coverage diff --git a/src/api/app/controllers/webui/webui_controller.rb b/src/api/app/controllers/webui/webui_controller.rb index 0a137b7889e7..de059c95f795 100644 --- a/src/api/app/controllers/webui/webui_controller.rb +++ b/src/api/app/controllers/webui/webui_controller.rb @@ -12,6 +12,7 @@ class Webui::WebuiController < ActionController::Base include RescueAuthorizationHandler include SetCurrentRequestDetails include Webui::ElisionsHelper + include ActiveStorage::SetCurrent protect_from_forgery before_action :setup_view_path diff --git a/src/api/config/application.rb b/src/api/config/application.rb index fc0af305144a..44873c761e92 100644 --- a/src/api/config/application.rb +++ b/src/api/config/application.rb @@ -7,6 +7,7 @@ require 'action_mailer/railtie' require 'action_controller/railtie' require 'action_view/railtie' +require 'active_storage/engine' require 'sprockets/railtie' require 'rails/test_unit/railtie' diff --git a/src/api/config/environments/development.rb b/src/api/config/environments/development.rb index cd6978b423cc..dc8f34238f94 100644 --- a/src/api/config/environments/development.rb +++ b/src/api/config/environments/development.rb @@ -45,7 +45,7 @@ end # Store uploaded files on the local file system (see config/storage.yml for options). - # config.active_storage.service = :local + config.active_storage.service = :local # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log diff --git a/src/api/config/environments/production.rb b/src/api/config/environments/production.rb index f797884d5129..fc5ac7084862 100644 --- a/src/api/config/environments/production.rb +++ b/src/api/config/environments/production.rb @@ -43,7 +43,7 @@ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX # Store uploaded files on the local file system (see config/storage.yml for options). - # config.active_storage.service = :local + config.active_storage.service = :local # Mount Action Cable outside main process or domain. # config.action_cable.mount_path = nil diff --git a/src/api/config/environments/test.rb b/src/api/config/environments/test.rb index fee2c2be992f..253801b58320 100644 --- a/src/api/config/environments/test.rb +++ b/src/api/config/environments/test.rb @@ -36,7 +36,7 @@ # config.action_dispatch.show_exceptions = false # Store uploaded files on the local file system in a temporary directory. - # config.active_storage.service = :test + config.active_storage.service = :test # Tell Action Mailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the diff --git a/src/api/config/storage.yml b/src/api/config/storage.yml new file mode 100644 index 000000000000..1f93f732379a --- /dev/null +++ b/src/api/config/storage.yml @@ -0,0 +1,7 @@ +local: + service: Disk + root: <%= Rails.root.join("storage") %> + +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> diff --git a/src/api/db/migrate/20240418065204_create_active_storage_tables.active_storage.rb b/src/api/db/migrate/20240418065204_create_active_storage_tables.active_storage.rb new file mode 100644 index 000000000000..6bd8bd082afd --- /dev/null +++ b/src/api/db/migrate/20240418065204_create_active_storage_tables.active_storage.rb @@ -0,0 +1,57 @@ +# This migration comes from active_storage (originally 20170806125915) +class CreateActiveStorageTables < ActiveRecord::Migration[7.0] + def change + # Use Active Record's configured type for primary and foreign keys + primary_key_type, foreign_key_type = primary_and_foreign_key_types + + create_table :active_storage_blobs, id: primary_key_type do |t| + t.string :key, null: false + t.string :filename, null: false + t.string :content_type + t.text :metadata + t.string :service_name, null: false + t.bigint :byte_size, null: false + t.string :checksum + + if connection.supports_datetime_with_precision? + t.datetime :created_at, precision: 6, null: false + else + t.datetime :created_at, null: false + end + + t.index [ :key ], unique: true + end + + create_table :active_storage_attachments, id: primary_key_type do |t| + t.string :name, null: false + t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type + t.references :blob, null: false, type: foreign_key_type + + if connection.supports_datetime_with_precision? + t.datetime :created_at, precision: 6, null: false + else + t.datetime :created_at, null: false + end + + t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true + t.foreign_key :active_storage_blobs, column: :blob_id + end + + create_table :active_storage_variant_records, id: primary_key_type do |t| + t.belongs_to :blob, null: false, index: false, type: foreign_key_type + t.string :variation_digest, null: false + + t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true + t.foreign_key :active_storage_blobs, column: :blob_id + end + end + + private + def primary_and_foreign_key_types + config = Rails.configuration.generators + setting = config.options[config.orm][:primary_key_type] + primary_key_type = setting || :primary_key + foreign_key_type = setting || :bigint + [ primary_key_type, foreign_key_type ] + end +end diff --git a/src/api/db/schema.rb b/src/api/db/schema.rb index 40339780b1db..6b270b989cb7 100644 --- a/src/api/db/schema.rb +++ b/src/api/db/schema.rb @@ -10,7 +10,35 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_04_11_150057) do +ActiveRecord::Schema[7.0].define(version: 2024_04_18_065204) do + create_table "active_storage_attachments", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| + t.string "name", null: false + t.string "record_type", null: false + t.integer "record_id", null: false + t.integer "blob_id", null: false + t.datetime "created_at", null: false + t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" + t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true + end + + create_table "active_storage_blobs", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| + t.string "key", null: false + t.string "filename", null: false + t.string "content_type" + t.text "metadata" + t.string "service_name", null: false + t.bigint "byte_size", null: false + t.string "checksum" + t.datetime "created_at", null: false + t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true + end + + create_table "active_storage_variant_records", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| + t.integer "blob_id", null: false + t.string "variation_digest", null: false + t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true + end + create_table "appeals", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| t.text "reason", null: false t.integer "appellant_id", null: false @@ -388,6 +416,7 @@ t.text "code_of_conduct" t.string "contact_name" t.string "contact_url" + t.text "logo" end create_table "data_migrations", id: false, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| @@ -400,6 +429,7 @@ t.integer "kind", default: 0 t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "type", default: "DecisionCleared" t.index ["moderator_id"], name: "index_decisions_on_moderator_id" end @@ -1140,7 +1170,7 @@ t.index ["state"], name: "index_users_on_state" end - create_table "versions", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + create_table "versions", charset: "utf8mb4", force: :cascade do |t| t.string "item_type", limit: 191, null: false t.bigint "item_id", null: false t.string "event", null: false @@ -1213,8 +1243,8 @@ t.index ["user_id"], name: "index_workflow_token_users_on_user_id" end - add_foreign_key "appeals", "decisions" - add_foreign_key "appeals", "users", column: "appellant_id" + add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" + add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "attrib_allowed_values", "attrib_types", name: "attrib_allowed_values_ibfk_1" add_foreign_key "attrib_default_values", "attrib_types", name: "attrib_default_values_ibfk_1" add_foreign_key "attrib_issues", "attribs", name: "attrib_issues_ibfk_1"