From a036e8561344866b072c2874906c9c9533cd27ea Mon Sep 17 00:00:00 2001 From: Fire Demon Date: Fri, 31 Jul 2020 01:52:08 -0500 Subject: [Feature] Add general-purpose account metadata to backend --- app/models/account.rb | 6 +++++ app/models/account_metadata.rb | 30 ++++++++++++++++++++++ app/models/concerns/account_associations.rb | 3 +++ .../20200731064236_create_account_metadata.rb | 10 ++++++++ .../20200731135033_backfill_account_metadata.rb | 11 ++++++++ db/schema.rb | 9 ++++++- 6 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 app/models/account_metadata.rb create mode 100644 db/migrate/20200731064236_create_account_metadata.rb create mode 100644 db/migrate/20200731135033_backfill_account_metadata.rb diff --git a/app/models/account.rb b/app/models/account.rb index 7d19aac08..dab091bae 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -562,6 +562,8 @@ class Account < ApplicationRecord before_validation :prepare_username, on: :create before_destroy :clean_feed_manager + after_create_commit :set_metadata, if: :local? + private def prepare_contents @@ -605,4 +607,8 @@ class Account < ApplicationRecord end end end + + def set_metadata + self.metadata = AccountMetadata.new(account_id: id, fields: {}) if metadata.nil? + end end diff --git a/app/models/account_metadata.rb b/app/models/account_metadata.rb new file mode 100644 index 000000000..1085d7f14 --- /dev/null +++ b/app/models/account_metadata.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true +# == Schema Information +# +# Table name: account_metadata +# +# id :bigint(8) not null, primary key +# account_id :bigint(8) not null +# fields :jsonb not null +# + +class AccountMetadata < ApplicationRecord + include Cacheable + + belongs_to :account, inverse_of: :metadata + cache_associated :account + + def fields + self[:fields].presence || {} + end + + class << self + def create_or_update(fields) + create(fields).presence || update(fields) + end + + def create_or_update!(fields) + create(fields).presence || update!(fields) + end + end +end diff --git a/app/models/concerns/account_associations.rb b/app/models/concerns/account_associations.rb index 10eaa4874..5a54f2208 100644 --- a/app/models/concerns/account_associations.rb +++ b/app/models/concerns/account_associations.rb @@ -66,5 +66,8 @@ module AccountAssociations # Domain permissions has_many :domain_permissions, class_name: 'AccountDomainPermission', inverse_of: :account, dependent: :destroy + + # Custom metadata + has_one :metadata, class_name: 'AccountMetadata', inverse_of: :account, dependent: :destroy end end diff --git a/db/migrate/20200731064236_create_account_metadata.rb b/db/migrate/20200731064236_create_account_metadata.rb new file mode 100644 index 000000000..c2eb32b79 --- /dev/null +++ b/db/migrate/20200731064236_create_account_metadata.rb @@ -0,0 +1,10 @@ +class CreateAccountMetadata < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + create_table :account_metadata do |t| + t.references :account, null: false, unique: true, foreign_key: { on_delete: :cascade } + t.jsonb :fields, null: false, default: {} + end + end +end diff --git a/db/migrate/20200731135033_backfill_account_metadata.rb b/db/migrate/20200731135033_backfill_account_metadata.rb new file mode 100644 index 000000000..2ddfa6081 --- /dev/null +++ b/db/migrate/20200731135033_backfill_account_metadata.rb @@ -0,0 +1,11 @@ +class BackfillAccountMetadata < ActiveRecord::Migration[5.2] + def up + safety_assured do + execute("INSERT INTO account_metadata (account_id) SELECT id FROM accounts WHERE domain IS NULL OR domain = ''") + end + end + + def down + true + end +end diff --git a/db/schema.rb b/db/schema.rb index 4fcdd7e2f..06d012600 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_07_28_173757) do +ActiveRecord::Schema.define(version: 2020_07_31_135033) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -66,6 +66,12 @@ ActiveRecord::Schema.define(version: 2020_07_28_173757) do t.index ["account_id", "provider", "provider_username"], name: "index_account_proofs_on_account_and_provider_and_username", unique: true end + create_table "account_metadata", force: :cascade do |t| + t.bigint "account_id", null: false + t.jsonb "fields", default: {}, null: false + t.index ["account_id"], name: "index_account_metadata_on_account_id" + end + create_table "account_migrations", force: :cascade do |t| t.bigint "account_id" t.string "acct", default: "", null: false @@ -976,6 +982,7 @@ ActiveRecord::Schema.define(version: 2020_07_28_173757) do add_foreign_key "account_domain_blocks", "accounts", name: "fk_206c6029bd", on_delete: :cascade add_foreign_key "account_domain_permissions", "accounts", on_delete: :cascade add_foreign_key "account_identity_proofs", "accounts", on_delete: :cascade + add_foreign_key "account_metadata", "accounts", on_delete: :cascade add_foreign_key "account_migrations", "accounts", column: "target_account_id", on_delete: :nullify add_foreign_key "account_migrations", "accounts", on_delete: :cascade add_foreign_key "account_moderation_notes", "accounts" -- cgit