-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate OAuth credentials to OAuth tokens
- Loading branch information
Showing
2 changed files
with
68 additions
and
128 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
priv/repo/migrations/20250307045854_migrate_oauth_credentials_to_tokens.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
defmodule Lightning.Repo.Migrations.MigrateOauthCredentialsToTokens do | ||
use Ecto.Migration | ||
|
||
def change do | ||
execute """ | ||
DO $$ | ||
DECLARE | ||
cred_record RECORD; | ||
token_id UUID; | ||
tokens_created INTEGER := 0; | ||
credentials_updated INTEGER := 0; | ||
BEGIN | ||
-- Process each oauth credential | ||
FOR cred_record IN | ||
SELECT | ||
id, | ||
user_id, | ||
oauth_client_id, | ||
body | ||
FROM credentials | ||
WHERE schema = 'oauth' | ||
AND body IS NOT NULL | ||
ORDER BY updated_at DESC | ||
LOOP | ||
-- Print debug info about the client_id | ||
RAISE NOTICE 'Processing credential % with client_id %', | ||
cred_record.id, cred_record.oauth_client_id; | ||
-- Check if we already created a token for this user/client combination | ||
SELECT id INTO token_id | ||
FROM oauth_tokens | ||
WHERE user_id = cred_record.user_id | ||
AND ((oauth_client_id = cred_record.oauth_client_id) OR | ||
(oauth_client_id IS NULL AND cred_record.oauth_client_id IS NULL)) | ||
LIMIT 1; | ||
-- If no token exists yet, create one | ||
IF token_id IS NULL THEN | ||
-- Create the token with explicit oauth_client_id | ||
INSERT INTO oauth_tokens | ||
(id, user_id, oauth_client_id, body, scopes, inserted_at, updated_at) | ||
VALUES | ||
(gen_random_uuid(), cred_record.user_id, cred_record.oauth_client_id, | ||
cred_record.body, '{}', NOW(), NOW()) | ||
RETURNING id INTO token_id; | ||
tokens_created := tokens_created + 1; | ||
RAISE NOTICE 'Created token % for user % and client %', | ||
token_id, cred_record.user_id, cred_record.oauth_client_id; | ||
END IF; | ||
-- Update the credential to reference the token | ||
UPDATE credentials | ||
SET | ||
oauth_token_id = token_id, | ||
body = NULL, | ||
updated_at = NOW() | ||
WHERE id = cred_record.id; | ||
credentials_updated := credentials_updated + 1; | ||
END LOOP; | ||
RAISE NOTICE 'Migration completed: Created % tokens, updated % credentials', | ||
tokens_created, credentials_updated; | ||
END $$; | ||
""" | ||
end | ||
end |