about summary refs log tree commit diff
path: root/db
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2022-07-25 18:53:31 -0500
committerStarfall <us@starfall.systems>2022-07-25 18:53:31 -0500
commit5b9419060d79eda85c40a12c567dd0e1e44a7ecb (patch)
treef5e21930844f7c11ae40b9097a78a32916ba5dba /db
parenta137fecf94d25a03ef7224843c1afd0c30f428e6 (diff)
parent3a7c641dd4db1d67b172f731518b472d58dd2262 (diff)
Merge remote-tracking branch 'glitch/main'
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20220611210335_create_user_roles.rb13
-rw-r--r--db/migrate/20220611212541_add_role_id_to_users.rb8
-rw-r--r--db/migrate/20220613110628_create_custom_filter_keywords.rb13
-rw-r--r--db/migrate/20220613110711_migrate_custom_filters.rb34
-rw-r--r--db/migrate/20220613110834_add_action_to_custom_filters.rb20
-rw-r--r--db/migrate/20220710102457_add_display_name_to_tags.rb5
-rw-r--r--db/migrate/20220714171049_create_tag_follows.rb12
-rw-r--r--db/post_migrate/20220613110802_remove_whole_word_from_custom_filters.rb20
-rw-r--r--db/post_migrate/20220613110903_remove_irreversible_from_custom_filters.rb20
-rw-r--r--db/post_migrate/20220617202502_migrate_roles.rb26
-rw-r--r--db/post_migrate/20220704024901_migrate_settings_to_user_roles.rb41
-rw-r--r--db/schema.rb40
-rw-r--r--db/seeds.rb12
-rw-r--r--db/seeds/01_web_app.rb1
-rw-r--r--db/seeds/02_instance_actor.rb1
-rw-r--r--db/seeds/03_roles.rb9
-rw-r--r--db/seeds/04_admin.rb8
17 files changed, 271 insertions, 12 deletions
diff --git a/db/migrate/20220611210335_create_user_roles.rb b/db/migrate/20220611210335_create_user_roles.rb
new file mode 100644
index 000000000..6b7f2b637
--- /dev/null
+++ b/db/migrate/20220611210335_create_user_roles.rb
@@ -0,0 +1,13 @@
+class CreateUserRoles < ActiveRecord::Migration[6.1]
+  def change
+    create_table :user_roles do |t|
+      t.string :name, null: false, default: ''
+      t.string :color, null: false, default: ''
+      t.integer :position, null: false, default: 0
+      t.bigint :permissions, null: false, default: 0
+      t.boolean :highlighted, null: false, default: false
+
+      t.timestamps
+    end
+  end
+end
diff --git a/db/migrate/20220611212541_add_role_id_to_users.rb b/db/migrate/20220611212541_add_role_id_to_users.rb
new file mode 100644
index 000000000..2fda647d4
--- /dev/null
+++ b/db/migrate/20220611212541_add_role_id_to_users.rb
@@ -0,0 +1,8 @@
+class AddRoleIdToUsers < ActiveRecord::Migration[6.1]
+  disable_ddl_transaction!
+
+  def change
+    safety_assured { add_reference :users, :role, foreign_key: { to_table: 'user_roles', on_delete: :nullify }, index: false }
+    add_index :users, :role_id, algorithm: :concurrently, where: 'role_id IS NOT NULL'
+  end
+end
diff --git a/db/migrate/20220613110628_create_custom_filter_keywords.rb b/db/migrate/20220613110628_create_custom_filter_keywords.rb
new file mode 100644
index 000000000..353fc334f
--- /dev/null
+++ b/db/migrate/20220613110628_create_custom_filter_keywords.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class CreateCustomFilterKeywords < ActiveRecord::Migration[6.1]
+  def change
+    create_table :custom_filter_keywords do |t|
+      t.belongs_to :custom_filter, foreign_key: { on_delete: :cascade }, null: false
+      t.text :keyword, null: false, default: ''
+      t.boolean :whole_word, null: false, default: true
+
+      t.timestamps
+    end
+  end
+end
diff --git a/db/migrate/20220613110711_migrate_custom_filters.rb b/db/migrate/20220613110711_migrate_custom_filters.rb
new file mode 100644
index 000000000..ea6a9b8c6
--- /dev/null
+++ b/db/migrate/20220613110711_migrate_custom_filters.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+class MigrateCustomFilters < ActiveRecord::Migration[6.1]
+  def up
+    # Preserve IDs as much as possible to not confuse existing clients.
+    # As long as this migration is irreversible, we do not have to deal with conflicts.
+    safety_assured do
+      execute <<-SQL.squish
+        INSERT INTO custom_filter_keywords (id, custom_filter_id, keyword, whole_word, created_at, updated_at)
+        SELECT id, id, phrase, whole_word, created_at, updated_at
+        FROM custom_filters
+      SQL
+    end
+  end
+
+  def down
+    # Copy back changes from custom filters guaranteed to be from the old API
+    safety_assured do
+      execute <<-SQL.squish
+        UPDATE custom_filters
+        SET phrase = custom_filter_keywords.keyword, whole_word = custom_filter_keywords.whole_word
+        FROM custom_filter_keywords
+        WHERE custom_filters.id = custom_filter_keywords.id AND custom_filters.id = custom_filter_keywords.custom_filter_id
+      SQL
+    end
+
+    # Drop every keyword as we can't safely provide a 1:1 mapping
+    safety_assured do
+      execute <<-SQL.squish
+        TRUNCATE custom_filter_keywords RESTART IDENTITY
+      SQL
+    end
+  end
+end
diff --git a/db/migrate/20220613110834_add_action_to_custom_filters.rb b/db/migrate/20220613110834_add_action_to_custom_filters.rb
new file mode 100644
index 000000000..9427a66fc
--- /dev/null
+++ b/db/migrate/20220613110834_add_action_to_custom_filters.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+require Rails.root.join('lib', 'mastodon', 'migration_helpers')
+
+class AddActionToCustomFilters < ActiveRecord::Migration[6.1]
+  include Mastodon::MigrationHelpers
+
+  disable_ddl_transaction!
+
+  def up
+    safety_assured do
+      add_column_with_default :custom_filters, :action, :integer, allow_null: false, default: 0
+      execute 'UPDATE custom_filters SET action = 1 WHERE irreversible IS TRUE'
+    end
+  end
+
+  def down
+    execute 'UPDATE custom_filters SET irreversible = (action = 1)'
+    remove_column :custom_filters, :action
+  end
+end
diff --git a/db/migrate/20220710102457_add_display_name_to_tags.rb b/db/migrate/20220710102457_add_display_name_to_tags.rb
new file mode 100644
index 000000000..aa7867645
--- /dev/null
+++ b/db/migrate/20220710102457_add_display_name_to_tags.rb
@@ -0,0 +1,5 @@
+class AddDisplayNameToTags < ActiveRecord::Migration[6.1]
+  def change
+    add_column :tags, :display_name, :string
+  end
+end
diff --git a/db/migrate/20220714171049_create_tag_follows.rb b/db/migrate/20220714171049_create_tag_follows.rb
new file mode 100644
index 000000000..a393e90f5
--- /dev/null
+++ b/db/migrate/20220714171049_create_tag_follows.rb
@@ -0,0 +1,12 @@
+class CreateTagFollows < ActiveRecord::Migration[6.1]
+  def change
+    create_table :tag_follows do |t|
+      t.belongs_to :tag, null: false, foreign_key: { on_delete: :cascade }
+      t.belongs_to :account, null: false, foreign_key: { on_delete: :cascade }, index: false
+
+      t.timestamps
+    end
+
+    add_index :tag_follows, [:account_id, :tag_id], unique: true
+  end
+end
diff --git a/db/post_migrate/20220613110802_remove_whole_word_from_custom_filters.rb b/db/post_migrate/20220613110802_remove_whole_word_from_custom_filters.rb
new file mode 100644
index 000000000..7ef0749e5
--- /dev/null
+++ b/db/post_migrate/20220613110802_remove_whole_word_from_custom_filters.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+require Rails.root.join('lib', 'mastodon', 'migration_helpers')
+
+class RemoveWholeWordFromCustomFilters < ActiveRecord::Migration[6.1]
+  include Mastodon::MigrationHelpers
+
+  disable_ddl_transaction!
+
+  def up
+    safety_assured do
+      remove_column :custom_filters, :whole_word
+    end
+  end
+
+  def down
+    safety_assured do
+      add_column_with_default :custom_filters, :whole_word, :boolean, default: true, allow_null: false
+    end
+  end
+end
diff --git a/db/post_migrate/20220613110903_remove_irreversible_from_custom_filters.rb b/db/post_migrate/20220613110903_remove_irreversible_from_custom_filters.rb
new file mode 100644
index 000000000..6ed8bcfee
--- /dev/null
+++ b/db/post_migrate/20220613110903_remove_irreversible_from_custom_filters.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+require Rails.root.join('lib', 'mastodon', 'migration_helpers')
+
+class RemoveIrreversibleFromCustomFilters < ActiveRecord::Migration[6.1]
+  include Mastodon::MigrationHelpers
+
+  disable_ddl_transaction!
+
+  def up
+    safety_assured do
+      remove_column :custom_filters, :irreversible
+    end
+  end
+
+  def down
+    safety_assured do
+      add_column_with_default :custom_filters, :irreversible, :boolean, allow_null: false, default: false
+    end
+  end
+end
diff --git a/db/post_migrate/20220617202502_migrate_roles.rb b/db/post_migrate/20220617202502_migrate_roles.rb
new file mode 100644
index 000000000..b7a7b2201
--- /dev/null
+++ b/db/post_migrate/20220617202502_migrate_roles.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+class MigrateRoles < ActiveRecord::Migration[5.2]
+  disable_ddl_transaction!
+
+  class UserRole < ApplicationRecord; end
+  class User < ApplicationRecord; end
+
+  def up
+    load Rails.root.join('db', 'seeds', '03_roles.rb')
+
+    admin_role     = UserRole.find_by(name: 'Admin')
+    moderator_role = UserRole.find_by(name: 'Moderator')
+
+    User.where(admin: true).in_batches.update_all(role_id: admin_role.id)
+    User.where(moderator: true).in_batches.update_all(role_id: moderator_role.id)
+  end
+
+  def down
+    admin_role     = UserRole.find_by(name: 'Admin')
+    moderator_role = UserRole.find_by(name: 'Moderator')
+
+    User.where(role_id: admin_role.id).in_batches.update_all(admin: true) if admin_role
+    User.where(role_id: moderator_role.id).in_batches.update_all(moderator: true) if moderator_role
+  end
+end
diff --git a/db/post_migrate/20220704024901_migrate_settings_to_user_roles.rb b/db/post_migrate/20220704024901_migrate_settings_to_user_roles.rb
new file mode 100644
index 000000000..254690cc3
--- /dev/null
+++ b/db/post_migrate/20220704024901_migrate_settings_to_user_roles.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+class MigrateSettingsToUserRoles < ActiveRecord::Migration[6.1]
+  disable_ddl_transaction!
+
+  class UserRole < ApplicationRecord; end
+
+  def up
+    owner_role     = UserRole.find_by(name: 'Owner')
+    admin_role     = UserRole.find_by(name: 'Admin')
+    moderator_role = UserRole.find_by(name: 'Moderator')
+    everyone_role  = UserRole.find_by(id: -99)
+
+    min_invite_role  = Setting.min_invite_role
+    show_staff_badge = Setting.show_staff_badge
+
+    if everyone_role
+      everyone_role.permissions &= ~::UserRole::FLAGS[:invite_users] unless min_invite_role == 'user'
+      everyone_role.save
+    end
+
+    if owner_role
+      owner_role.highlighted = show_staff_badge
+      owner_role.save
+    end
+
+    if admin_role
+      admin_role.permissions |= ::UserRole::FLAGS[:invite_users] if %w(admin moderator).include?(min_invite_role)
+      admin_role.highlighted  = show_staff_badge
+      admin_role.save
+    end
+
+    if moderator_role
+      moderator_role.permissions |= ::UserRole::FLAGS[:invite_users] if %w(moderator).include?(min_invite_role)
+      moderator_role.highlighted  = show_staff_badge
+      moderator_role.save
+    end
+  end
+
+  def down; end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 526480df4..7a8262dfa 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: 2022_06_06_044941) do
+ActiveRecord::Schema.define(version: 2022_07_14_171049) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -339,15 +339,23 @@ ActiveRecord::Schema.define(version: 2022_06_06_044941) do
     t.index ["shortcode", "domain"], name: "index_custom_emojis_on_shortcode_and_domain", unique: true
   end
 
+  create_table "custom_filter_keywords", force: :cascade do |t|
+    t.bigint "custom_filter_id", null: false
+    t.text "keyword", default: "", null: false
+    t.boolean "whole_word", default: true, null: false
+    t.datetime "created_at", precision: 6, null: false
+    t.datetime "updated_at", precision: 6, null: false
+    t.index ["custom_filter_id"], name: "index_custom_filter_keywords_on_custom_filter_id"
+  end
+
   create_table "custom_filters", force: :cascade do |t|
     t.bigint "account_id"
     t.datetime "expires_at"
     t.text "phrase", default: "", null: false
     t.string "context", default: [], null: false, array: true
-    t.boolean "irreversible", default: false, null: false
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
-    t.boolean "whole_word", default: true, null: false
+    t.integer "action", default: 0, null: false
     t.index ["account_id"], name: "index_custom_filters_on_account_id"
   end
 
@@ -923,6 +931,15 @@ ActiveRecord::Schema.define(version: 2022_06_06_044941) do
     t.datetime "updated_at", null: false
   end
 
+  create_table "tag_follows", force: :cascade do |t|
+    t.bigint "tag_id", null: false
+    t.bigint "account_id", null: false
+    t.datetime "created_at", precision: 6, null: false
+    t.datetime "updated_at", precision: 6, null: false
+    t.index ["account_id", "tag_id"], name: "index_tag_follows_on_account_id_and_tag_id", unique: true
+    t.index ["tag_id"], name: "index_tag_follows_on_tag_id"
+  end
+
   create_table "tags", force: :cascade do |t|
     t.string "name", default: "", null: false
     t.datetime "created_at", null: false
@@ -935,6 +952,7 @@ ActiveRecord::Schema.define(version: 2022_06_06_044941) do
     t.datetime "last_status_at"
     t.float "max_score"
     t.datetime "max_score_at"
+    t.string "display_name"
     t.index "lower((name)::text) text_pattern_ops", name: "index_tags_on_name_lower_btree", unique: true
   end
 
@@ -963,6 +981,16 @@ ActiveRecord::Schema.define(version: 2022_06_06_044941) do
     t.index ["user_id"], name: "index_user_invite_requests_on_user_id"
   end
 
+  create_table "user_roles", force: :cascade do |t|
+    t.string "name", default: "", null: false
+    t.string "color", default: "", null: false
+    t.integer "position", default: 0, null: false
+    t.bigint "permissions", default: 0, null: false
+    t.boolean "highlighted", default: false, null: false
+    t.datetime "created_at", precision: 6, null: false
+    t.datetime "updated_at", precision: 6, null: false
+  end
+
   create_table "users", force: :cascade do |t|
     t.string "email", default: "", null: false
     t.datetime "created_at", null: false
@@ -998,11 +1026,13 @@ ActiveRecord::Schema.define(version: 2022_06_06_044941) do
     t.string "webauthn_id"
     t.inet "sign_up_ip"
     t.boolean "skip_sign_in_token"
+    t.bigint "role_id"
     t.index ["account_id"], name: "index_users_on_account_id"
     t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
     t.index ["created_by_application_id"], name: "index_users_on_created_by_application_id", where: "(created_by_application_id IS NOT NULL)"
     t.index ["email"], name: "index_users_on_email", unique: true
     t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, opclass: :text_pattern_ops, where: "(reset_password_token IS NOT NULL)"
+    t.index ["role_id"], name: "index_users_on_role_id", where: "(role_id IS NOT NULL)"
   end
 
   create_table "web_push_subscriptions", force: :cascade do |t|
@@ -1085,6 +1115,7 @@ ActiveRecord::Schema.define(version: 2022_06_06_044941) do
   add_foreign_key "canonical_email_blocks", "accounts", column: "reference_account_id", on_delete: :cascade
   add_foreign_key "conversation_mutes", "accounts", name: "fk_225b4212bb", on_delete: :cascade
   add_foreign_key "conversation_mutes", "conversations", on_delete: :cascade
+  add_foreign_key "custom_filter_keywords", "custom_filters", on_delete: :cascade
   add_foreign_key "custom_filters", "accounts", on_delete: :cascade
   add_foreign_key "devices", "accounts", on_delete: :cascade
   add_foreign_key "devices", "oauth_access_tokens", column: "access_token_id", on_delete: :cascade
@@ -1148,11 +1179,14 @@ ActiveRecord::Schema.define(version: 2022_06_06_044941) do
   add_foreign_key "statuses", "statuses", column: "reblog_of_id", on_delete: :cascade
   add_foreign_key "statuses_tags", "statuses", on_delete: :cascade
   add_foreign_key "statuses_tags", "tags", name: "fk_3081861e21", on_delete: :cascade
+  add_foreign_key "tag_follows", "accounts", on_delete: :cascade
+  add_foreign_key "tag_follows", "tags", on_delete: :cascade
   add_foreign_key "tombstones", "accounts", on_delete: :cascade
   add_foreign_key "user_invite_requests", "users", on_delete: :cascade
   add_foreign_key "users", "accounts", name: "fk_50500f500d", on_delete: :cascade
   add_foreign_key "users", "invites", on_delete: :nullify
   add_foreign_key "users", "oauth_applications", column: "created_by_application_id", on_delete: :nullify
+  add_foreign_key "users", "user_roles", column: "role_id", on_delete: :nullify
   add_foreign_key "web_push_subscriptions", "oauth_access_tokens", column: "access_token_id", on_delete: :cascade
   add_foreign_key "web_push_subscriptions", "users", on_delete: :cascade
   add_foreign_key "web_settings", "users", name: "fk_11910667b2", on_delete: :cascade
diff --git a/db/seeds.rb b/db/seeds.rb
index 0bfb5d0db..1ca300de7 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -1,11 +1,5 @@
-Doorkeeper::Application.create!(name: 'Web', superapp: true, redirect_uri: Doorkeeper.configuration.native_redirect_uri, scopes: 'read write follow push')
+# frozen_string_literal: true
 
-domain = ENV['LOCAL_DOMAIN'] || Rails.configuration.x.local_domain
-account = Account.find_or_initialize_by(id: -99, actor_type: 'Application', locked: true, username: domain)
-account.save!
-
-if Rails.env.development?
-  admin  = Account.where(username: 'admin').first_or_initialize(username: 'admin')
-  admin.save(validate: false)
-  User.where(email: "admin@#{domain}").first_or_initialize(email: "admin@#{domain}", password: 'mastodonadmin', password_confirmation: 'mastodonadmin', confirmed_at: Time.now.utc, admin: true, account: admin, agreement: true, approved: true).save!
+Dir[Rails.root.join('db', 'seeds', '*.rb')].sort.each do |seed|
+  load seed
 end
diff --git a/db/seeds/01_web_app.rb b/db/seeds/01_web_app.rb
new file mode 100644
index 000000000..a457a883b
--- /dev/null
+++ b/db/seeds/01_web_app.rb
@@ -0,0 +1 @@
+Doorkeeper::Application.create_with(name: 'Web', redirect_uri: Doorkeeper.configuration.native_redirect_uri, scopes: 'read write follow push').find_or_create_by(superapp: true)
diff --git a/db/seeds/02_instance_actor.rb b/db/seeds/02_instance_actor.rb
new file mode 100644
index 000000000..39186b273
--- /dev/null
+++ b/db/seeds/02_instance_actor.rb
@@ -0,0 +1 @@
+Account.create_with(actor_type: 'Application', locked: true, username: ENV['LOCAL_DOMAIN'] || Rails.configuration.x.local_domain).find_or_create_by(id: -99)
diff --git a/db/seeds/03_roles.rb b/db/seeds/03_roles.rb
new file mode 100644
index 000000000..7fedf0f71
--- /dev/null
+++ b/db/seeds/03_roles.rb
@@ -0,0 +1,9 @@
+# Pre-create base role
+UserRole.everyone
+
+# Create default roles defined in config file
+default_roles = YAML.load_file(Rails.root.join('config', 'roles.yml'))
+
+default_roles.each do |_, config|
+  UserRole.create_with(position: config['position'], permissions_as_keys: config['permissions'], highlighted: true).find_or_create_by(name: config['name'])
+end
diff --git a/db/seeds/04_admin.rb b/db/seeds/04_admin.rb
new file mode 100644
index 000000000..a67040e4e
--- /dev/null
+++ b/db/seeds/04_admin.rb
@@ -0,0 +1,8 @@
+if Rails.env.development?
+  domain = ENV['LOCAL_DOMAIN'] || Rails.configuration.x.local_domain
+
+  admin = Account.where(username: 'admin').first_or_initialize(username: 'admin')
+  admin.save(validate: false)
+
+  User.where(email: "admin@#{domain}").first_or_initialize(email: "admin@#{domain}", password: 'mastodonadmin', password_confirmation: 'mastodonadmin', confirmed_at: Time.now.utc, role: UserRole.find_by(name: 'Owner'), account: admin, agreement: true, approved: true).save!
+end