Skip to content

Commit

Permalink
Fix create clients migration
Browse files Browse the repository at this point in the history
  • Loading branch information
angusmcleod committed Dec 5, 2024
1 parent d2956ac commit 7622b67
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions db/migrate/20241111133654_create_discourse_activity_pub_clients.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true
class CreateDiscourseActivityPubClients < ActiveRecord::Migration[7.1]
def change
def up
create_table :discourse_activity_pub_clients do |t|
t.integer :auth_type, null: false
t.string :domain, null: false
Expand All @@ -9,16 +9,24 @@ def change
t.timestamps
end

execute <<~SQL
INSERT INTO discourse_activity_pub_clients(auth_type, domain, credentials)
SELECT #{DiscourseActivityPubClient.auth_types[:mastodon]}, key, value::json
auth_type = DiscourseActivityPubClient.auth_types[:mastodon]
allowed_keys = DiscourseActivityPubClient::ALLOWED_CREDENTIAL_KEYS[:mastodon]
plugin_name = "#{DiscourseActivityPub::PLUGIN_NAME}-oauth-app"

DB.exec(<<~SQL, auth_type:, allowed_keys:, plugin_name:)
INSERT INTO discourse_activity_pub_clients(auth_type, domain, credentials, created_at, updated_at)
SELECT :auth_type, key, (SELECT json_object_agg(key, value) FROM json_each(value::json) WHERE key IN (:allowed_keys)), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
FROM plugin_store_rows
WHERE plugin_name = '#{DiscourseActivityPub::PLUGIN_NAME}-oauth-app' AND value IS NOT NULL
WHERE plugin_name = :plugin_name AND value IS NOT NULL
SQL

add_index :discourse_activity_pub_clients,
%i[auth_type domain],
unique: true,
name: "unique_activity_pub_client_auth_domains"
end

def down
drop_table :discourse_activity_pub_clients
end
end

0 comments on commit 7622b67

Please sign in to comment.