From db0d98f8d45dd47cbd06119ea9fb99ccc50e7931 Mon Sep 17 00:00:00 2001 From: Evangelos Giataganas Date: Tue, 22 Aug 2023 18:23:30 +0300 Subject: [PATCH] Add rails active_storage and setup with amazon S3 --- app/javascript/application.js | 3 + config/environments/development.rb | 6 +- config/environments/production.rb | 4 +- config/storage.yml | 30 ++-------- ...te_active_storage_tables.active_storage.rb | 57 +++++++++++++++++++ db/schema.rb | 32 ++++++++++- package.json | 1 + yarn.lock | 12 ++++ 8 files changed, 115 insertions(+), 30 deletions(-) create mode 100644 db/migrate/20230207120738_create_active_storage_tables.active_storage.rb diff --git a/app/javascript/application.js b/app/javascript/application.js index d933293..f956c1c 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -1,3 +1,6 @@ // Entry point for the build script in your package.json import "@hotwired/turbo-rails" +import * as ActiveStorage from "@rails/activestorage" +ActiveStorage.start() + import "./controllers" diff --git a/config/environments/development.rb b/config/environments/development.rb index d2dd0be..fa8995e 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -36,7 +36,11 @@ end # Store uploaded files on the local file system (see config/storage.yml for options). - config.active_storage.service = :local + config.active_storage.service = if ENV["AWS_PROFILE"].present? + :amazon + else + :local + end # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false diff --git a/config/environments/production.rb b/config/environments/production.rb index 345b2b4..c1f73d0 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -39,8 +39,8 @@ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache # 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 + # Store files on Amazon S3. + config.active_storage.service = :amazon # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. # config.force_ssl = true diff --git a/config/storage.yml b/config/storage.yml index 4942ab6..2990fd2 100644 --- a/config/storage.yml +++ b/config/storage.yml @@ -6,29 +6,7 @@ local: service: Disk root: <%= Rails.root.join("storage") %> -# Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) -# amazon: -# service: S3 -# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> -# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> -# region: us-east-1 -# bucket: your_own_bucket-<%= Rails.env %> - -# Remember not to checkin your GCS keyfile to a repository -# google: -# service: GCS -# project: your_project -# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> -# bucket: your_own_bucket-<%= Rails.env %> - -# Use bin/rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) -# microsoft: -# service: AzureStorage -# storage_account_name: your_account_name -# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> -# container: your_container_name-<%= Rails.env %> - -# mirror: -# service: Mirror -# primary: local -# mirrors: [ amazon, google, microsoft ] +amazon: + service: S3 + region: <%= (ENV["AWS_REGION"] || "eu-west-2").inspect %> + bucket: <%= ENV["S3_BUCKET"].inspect %> diff --git a/db/migrate/20230207120738_create_active_storage_tables.active_storage.rb b/db/migrate/20230207120738_create_active_storage_tables.active_storage.rb new file mode 100644 index 0000000..1797c4d --- /dev/null +++ b/db/migrate/20230207120738_create_active_storage_tables.active_storage.rb @@ -0,0 +1,57 @@ +# This migration comes from active_storage (originally 20170806125915) +class CreateActiveStorageTables < ActiveRecord::Migration[5.2] + 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 \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 2588bfa..0684cb7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,10 +10,38 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_01_30_150645) do +ActiveRecord::Schema[7.0].define(version: 2023_02_07_120738) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "active_storage_attachments", force: :cascade do |t| + t.string "name", null: false + t.string "record_type", null: false + t.bigint "record_id", null: false + t.bigint "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", 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", force: :cascade do |t| + t.bigint "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 "addresses", force: :cascade do |t| t.string "full", null: false t.string "town" @@ -96,6 +124,8 @@ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + 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 "addresses", "properties" add_foreign_key "planning_applications_properties", "planning_applications" add_foreign_key "planning_applications_properties", "properties" diff --git a/package.json b/package.json index d587fb1..be0e621 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "dependencies": { "@hotwired/stimulus": "^3.2.1", "@hotwired/turbo-rails": "^7.2.4", + "@rails/activestorage": "^7.0.4-2", "esbuild": "^0.16.6", "govuk-frontend": "^4.4.1", "sass": "^1.56.2" diff --git a/yarn.lock b/yarn.lock index 310c0df..35434a2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -135,6 +135,13 @@ resolved "https://registry.yarnpkg.com/@rails/actioncable/-/actioncable-7.0.4.tgz#70a3ca56809f7aaabb80af2f9c01ae51e1a8ed41" integrity sha512-tz4oM+Zn9CYsvtyicsa/AwzKZKL+ITHWkhiu7x+xF77clh2b4Rm+s6xnOgY/sGDWoFWZmtKsE95hxBPkgQQNnQ== +"@rails/activestorage@^7.0.4-2": + version "7.0.4-2" + resolved "https://registry.yarnpkg.com/@rails/activestorage/-/activestorage-7.0.4-2.tgz#b535b1ff3087a929de4cdf7f686500d16e1cfc06" + integrity sha512-TVNuL13CmXlUb41+fgRzTmOp9Xf3LQtw1A3ntrOtQvDPipy4NOkZ9Emyr2m9Gq/5gPfWon6x8GWXBsMQw8a5mg== + dependencies: + spark-md5 "^3.0.1" + anymatch@~3.1.2: version "3.1.3" resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.3.tgz#790c58b19ba1720a84205b57c618d5ad8524973e" @@ -282,6 +289,11 @@ sass@^1.56.2: resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c" integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw== +spark-md5@^3.0.1: + version "3.0.2" + resolved "https://registry.yarnpkg.com/spark-md5/-/spark-md5-3.0.2.tgz#7952c4a30784347abcee73268e473b9c0167e3fc" + integrity sha512-wcFzz9cDfbuqe0FZzfi2or1sgyIrsDwmPwfZC4hiNidPdPINjeUwNfv5kldczoEAcjl9Y1L3SM7Uz2PUEQzxQw== + to-regex-range@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4"