From df5f33768ab8bc63a3b146ff4b295b2d6542daec Mon Sep 17 00:00:00 2001 From: leio10 Date: Wed, 30 Jun 2021 08:57:00 +0000 Subject: [PATCH 1/4] Update decidim to active_storage_migration branch --- Gemfile | 2 +- Gemfile.lock | 4 +-- ...15_create_active_storage_tables.decidim.rb | 30 ++++++++++++++++ ...umn_in_oauth_applications_table.decidim.rb | 8 +++++ ...cidim_content_block_attachments.decidim.rb | 11 ++++++ ...s_table.decidim_participatory_processes.rb | 8 +++++ db/schema.rb | 34 +++++++++++++++++-- 7 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20210630085615_create_active_storage_tables.decidim.rb create mode 100644 db/migrate/20210630085616_allow_null_organization_logo_column_in_oauth_applications_table.decidim.rb create mode 100644 db/migrate/20210630085617_create_decidim_content_block_attachments.decidim.rb create mode 100644 db/migrate/20210630085618_allow_null_file_column_in_decidim_attachments_table.decidim_participatory_processes.rb diff --git a/Gemfile b/Gemfile index b9da4d96..71f5ab24 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,7 @@ source "https://rubygems.org" ruby RUBY_VERSION -DECIDIM_VERSION = { git: "https://github.com/decidim/decidim", branch: "develop" } +DECIDIM_VERSION = { git: "https://github.com/decidim/decidim", branch: "active_storage_migration" } gem "decidim", DECIDIM_VERSION #gem "decidim-conferences", DECIDIM_VERSION diff --git a/Gemfile.lock b/Gemfile.lock index 0f4cad0e..7f51759b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ GIT remote: https://github.com/decidim/decidim - revision: 39de071f4f6c16fad8115101882b4c568dfe86f0 - branch: develop + revision: c1f8db7ed0b6638a6eb59568925ec7a19d61e2a9 + branch: active_storage_migration specs: decidim (0.25.0.dev) decidim-accountability (= 0.25.0.dev) diff --git a/db/migrate/20210630085615_create_active_storage_tables.decidim.rb b/db/migrate/20210630085615_create_active_storage_tables.decidim.rb new file mode 100644 index 00000000..0afc5f4c --- /dev/null +++ b/db/migrate/20210630085615_create_active_storage_tables.decidim.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true +# This migration comes from decidim (originally 20210309190009) + +# This migration comes from active_storage (originally 20170806125915) +class CreateActiveStorageTables < ActiveRecord::Migration[5.2] + def change + create_table :active_storage_blobs do |t| + t.string :key, null: false + t.string :filename, null: false + t.string :content_type + t.text :metadata + t.bigint :byte_size, null: false + t.string :checksum, null: false + t.datetime :created_at, null: false + + t.index [:key], unique: true + end + + create_table :active_storage_attachments do |t| + t.string :name, null: false + t.references :record, null: false, polymorphic: true, index: false + t.references :blob, null: false + + t.datetime :created_at, null: false + + 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 + end +end diff --git a/db/migrate/20210630085616_allow_null_organization_logo_column_in_oauth_applications_table.decidim.rb b/db/migrate/20210630085616_allow_null_organization_logo_column_in_oauth_applications_table.decidim.rb new file mode 100644 index 00000000..cd239430 --- /dev/null +++ b/db/migrate/20210630085616_allow_null_organization_logo_column_in_oauth_applications_table.decidim.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +# This migration comes from decidim (originally 20210407190753) + +class AllowNullOrganizationLogoColumnInOAuthApplicationsTable < ActiveRecord::Migration[6.0] + def change + change_column_null :oauth_applications, :organization_logo, true + end +end diff --git a/db/migrate/20210630085617_create_decidim_content_block_attachments.decidim.rb b/db/migrate/20210630085617_create_decidim_content_block_attachments.decidim.rb new file mode 100644 index 00000000..cbcad613 --- /dev/null +++ b/db/migrate/20210630085617_create_decidim_content_block_attachments.decidim.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true +# This migration comes from decidim (originally 20210419165805) + +class CreateDecidimContentBlockAttachments < ActiveRecord::Migration[6.0] + def change + create_table :decidim_content_block_attachments do |t| + t.string :name + t.references :decidim_content_block, null: false, index: { name: "decidim_content_block_attachments_on_content_block" } + end + end +end diff --git a/db/migrate/20210630085618_allow_null_file_column_in_decidim_attachments_table.decidim_participatory_processes.rb b/db/migrate/20210630085618_allow_null_file_column_in_decidim_attachments_table.decidim_participatory_processes.rb new file mode 100644 index 00000000..ca6a8fb3 --- /dev/null +++ b/db/migrate/20210630085618_allow_null_file_column_in_decidim_attachments_table.decidim_participatory_processes.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +# This migration comes from decidim_participatory_processes (originally 20210415163339) + +class AllowNullFileColumnInDecidimAttachmentsTable < ActiveRecord::Migration[6.0] + def change + change_column_null :decidim_attachments, :file, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 20867f8e..6fb96e52 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,13 +10,34 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_06_28_083105) do +ActiveRecord::Schema.define(version: 2021_06_30_085618) do # These are extensions that must be enabled in order to support this database enable_extension "ltree" enable_extension "pg_trgm" 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.bigint "byte_size", null: false + t.string "checksum", null: false + t.datetime "created_at", null: false + t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true + end + create_table "decidim_accountability_results", id: :serial, force: :cascade do |t| t.jsonb "title" t.jsonb "description" @@ -237,7 +258,7 @@ create_table "decidim_attachments", id: :serial, force: :cascade do |t| t.jsonb "title", null: false t.jsonb "description" - t.string "file", null: false + t.string "file" t.string "content_type", null: false t.string "file_size", null: false t.integer "attached_to_id", null: false @@ -648,6 +669,12 @@ t.index ["decidim_user_group_id"], name: "index_decidim_consultations_votes_on_decidim_user_group_id" end + create_table "decidim_content_block_attachments", force: :cascade do |t| + t.string "name" + t.bigint "decidim_content_block_id", null: false + t.index ["decidim_content_block_id"], name: "decidim_content_block_attachments_on_content_block" + end + create_table "decidim_content_blocks", force: :cascade do |t| t.integer "decidim_organization_id", null: false t.string "manifest_name", null: false @@ -2084,7 +2111,7 @@ t.string "name", null: false t.string "organization_name", null: false t.string "organization_url", null: false - t.string "organization_logo", null: false + t.string "organization_logo" t.string "uid", null: false t.string "secret", null: false t.text "redirect_uri", null: false @@ -2110,6 +2137,7 @@ t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id" end + add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "decidim_area_types", "decidim_organizations" add_foreign_key "decidim_areas", "decidim_area_types", column: "area_type_id" add_foreign_key "decidim_areas", "decidim_organizations" From 6c2ed772e6469eb1e39d9421c3b597714be51373 Mon Sep 17 00:00:00 2001 From: Leonardo Diez Date: Wed, 30 Jun 2021 11:28:58 +0200 Subject: [PATCH 2/4] fix: apply migration fix from decidim --- ...13_move_organization_fields_to_hero_content_block.decidim.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/migrate/20201009122313_move_organization_fields_to_hero_content_block.decidim.rb b/db/migrate/20201009122313_move_organization_fields_to_hero_content_block.decidim.rb index 095150f7..fad69403 100644 --- a/db/migrate/20201009122313_move_organization_fields_to_hero_content_block.decidim.rb +++ b/db/migrate/20201009122313_move_organization_fields_to_hero_content_block.decidim.rb @@ -5,7 +5,7 @@ class MoveOrganizationFieldsToHeroContentBlock < ActiveRecord::Migration[5.2] class Organization < ApplicationRecord self.table_name = :decidim_organizations - mount_uploader :homepage_image, ::Decidim::HomepageImageUploader + mount_uploader :homepage_image, CarrierWave::Uploader::Base end def change From 8518667e977d887c39a6084f632024ebe017926c Mon Sep 17 00:00:00 2001 From: Leonardo Diez Date: Wed, 30 Jun 2021 11:58:51 +0200 Subject: [PATCH 3/4] fix: config active_storage for the app --- Gemfile | 1 + Gemfile.lock | 18 ++++++++++++++++ config/environments/development.rb | 3 +++ config/environments/production.rb | 3 +++ config/environments/test.rb | 3 +++ config/initializers/carrierwave.rb | 33 ------------------------------ config/secrets.yml | 5 ----- config/storage.yml | 14 +++++++++++++ 8 files changed, 42 insertions(+), 38 deletions(-) delete mode 100644 config/initializers/carrierwave.rb create mode 100644 config/storage.yml diff --git a/Gemfile b/Gemfile index 71f5ab24..f7e29269 100644 --- a/Gemfile +++ b/Gemfile @@ -36,6 +36,7 @@ group :development do end group :production do + gem "aws-sdk-s3", require: false gem "fog-aws" gem "lograge" gem "sendgrid-ruby" diff --git a/Gemfile.lock b/Gemfile.lock index 7f51759b..b351b6d6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -257,6 +257,22 @@ GEM ast (2.4.2) autoprefixer-rails (8.6.5) execjs + aws-eventstream (1.1.1) + aws-partitions (1.472.0) + aws-sdk-core (3.115.0) + aws-eventstream (~> 1, >= 1.0.2) + aws-partitions (~> 1, >= 1.239.0) + aws-sigv4 (~> 1.1) + jmespath (~> 1.0) + aws-sdk-kms (1.44.0) + aws-sdk-core (~> 3, >= 3.112.0) + aws-sigv4 (~> 1.1) + aws-sdk-s3 (1.96.1) + aws-sdk-core (~> 3, >= 3.112.0) + aws-sdk-kms (~> 1) + aws-sigv4 (~> 1.1) + aws-sigv4 (1.2.3) + aws-eventstream (~> 1, >= 1.0.2) axe-core-api (4.2.1) capybara dumb_delegator @@ -478,6 +494,7 @@ GEM invisible_captcha (0.13.0) rails (>= 3.2.0) ipaddress (0.8.3) + jmespath (1.4.0) jquery-rails (4.4.0) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) @@ -847,6 +864,7 @@ PLATFORMS ruby DEPENDENCIES + aws-sdk-s3 byebug decidim! decidim-dev! diff --git a/config/environments/development.rb b/config/environments/development.rb index 033cf750..aae60ccd 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -46,4 +46,7 @@ # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. config.file_watcher = ActiveSupport::EventedFileUpdateChecker + + # Store files locally. + config.active_storage.service = :local end diff --git a/config/environments/production.rb b/config/environments/production.rb index a697bddb..ac1dab20 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -119,4 +119,7 @@ referer: event.payload[:referer], } end + + # Store files on Amazon S3. + config.active_storage.service = :amazon end diff --git a/config/environments/test.rb b/config/environments/test.rb index 8e5cbde5..2102217f 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -39,4 +39,7 @@ # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + # Store files locally. + config.active_storage.service = :test end diff --git a/config/initializers/carrierwave.rb b/config/initializers/carrierwave.rb deleted file mode 100644 index 75916c05..00000000 --- a/config/initializers/carrierwave.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true - -# Default CarrierWave setup. -# -CarrierWave.configure do |config| - config.permissions = 0o666 - config.directory_permissions = 0o777 - config.storage = :file - config.enable_processing = !Rails.env.test? -end - -# Setup CarrierWave to use Amazon S3. Add `gem "fog-aws" to your Gemfile. -# -if Rails.application.secrets.aws_access_key_id.present? - require "carrierwave/storage/fog" - CarrierWave.configure do |config| - config.storage = :fog - config.fog_provider = 'fog/aws' # required - config.fog_credentials = { - provider: 'AWS', # required - aws_access_key_id: Rails.application.secrets.aws_access_key_id, # required - aws_secret_access_key: Rails.application.secrets.aws_secret_access_key, # required - region: Rails.application.secrets.aws_region, - host: Rails.application.secrets.aws_host, - } - config.fog_directory = Rails.application.secrets.aws_bucket - config.fog_public = true - config.fog_attributes = { - 'Cache-Control' => "max-age=#{365.day.to_i}", - 'X-Content-Type-Options' => "nosniff" - } - end -end diff --git a/config/secrets.yml b/config/secrets.yml index 3e1724af..353e2a38 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -12,11 +12,6 @@ default: &default sentry_enabled: false - aws_access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %> - aws_secret_access_key: <%= ENV["AWS_SECRET_ACCESS_KEY"] %> - aws_region: <%= ENV["AWS_REGION"] %> - aws_bucket: <%= ENV["AWS_BUCKET_NAME"] %> - aws_host: <%= ENV["AWS_HOST"] %> omniauth: facebook: # It must be a boolean. Remember ENV variables doesn't support booleans. diff --git a/config/storage.yml b/config/storage.yml new file mode 100644 index 00000000..e353d5c8 --- /dev/null +++ b/config/storage.yml @@ -0,0 +1,14 @@ +local: + service: Disk + root: <%= Rails.root.join("storage") %> + +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> + +amazon: + service: S3 + access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %> + secret_access_key: <%= ENV["AWS_SECRET_ACCESS_KEY"] %> + region: <%= ENV["AWS_REGION"] %> + bucket: <%= ENV["AWS_BUCKET_NAME"] %> \ No newline at end of file From f4757f5444611431ecfb2a43329237b4766cb25e Mon Sep 17 00:00:00 2001 From: Leonardo Diez Date: Wed, 7 Jul 2021 18:18:08 +0200 Subject: [PATCH 4/4] fix: add folder to active_storage files --- config/initializers/active_storage.rb | 9 +++++++++ config/initializers/decidim.rb | 1 - 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 config/initializers/active_storage.rb diff --git a/config/initializers/active_storage.rb b/config/initializers/active_storage.rb new file mode 100644 index 00000000..909cf5e3 --- /dev/null +++ b/config/initializers/active_storage.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +if ENV["HEROKU_APP_NAME"].present? + ActiveSupport.on_load(:active_storage_blob) do + def key + self[:key] ||= "#{Decidim.base_uploads_path}#{self.class.generate_unique_secure_token}" + end + end +end \ No newline at end of file diff --git a/config/initializers/decidim.rb b/config/initializers/decidim.rb index c6f1957e..f0358508 100644 --- a/config/initializers/decidim.rb +++ b/config/initializers/decidim.rb @@ -134,4 +134,3 @@ Rails.application.config.i18n.available_locales = Decidim.available_locales Rails.application.config.i18n.default_locale = Decidim.default_locale -