-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SQL scripts for connecting new music services and migrating user …
…data (#1384) * Add new external_auth table for music services * Add migration script for moving spotify users data to new tables * Rename external_auth to external_service_oauth and external_auth_service to external_service_oauth_type. * Add indices for external_service_oauth table * Rename files to make order of application clear * Fix indent
- Loading branch information
Showing
8 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
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
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
29 changes: 29 additions & 0 deletions
29
admin/sql/updates/2021-04-12-1-new-music-services-table.sql
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,29 @@ | ||
BEGIN; | ||
|
||
CREATE TYPE external_service_oauth_type AS ENUM ('spotify', 'youtube'); | ||
|
||
CREATE TABLE external_service_oauth ( | ||
id SERIAL, | ||
user_id INTEGER NOT NULL, | ||
service external_service_oauth_type NOT NULL, | ||
access_token TEXT NOT NULL, | ||
refresh_token TEXT, | ||
token_expires TIMESTAMP WITH TIME ZONE, | ||
last_updated TIMESTAMP WITH TIME ZONE DEFAULT NOW(), | ||
record_listens BOOLEAN NOT NULL, | ||
service_details JSONB | ||
); | ||
|
||
ALTER TABLE external_service_oauth ADD CONSTRAINT external_service_oauth_pkey PRIMARY KEY (id); | ||
|
||
ALTER TABLE external_service_oauth | ||
ADD CONSTRAINT external_service_oauth_user_id_foreign_key | ||
FOREIGN KEY (user_id) | ||
REFERENCES "user" (id) | ||
ON DELETE CASCADE; | ||
|
||
CREATE INDEX user_id_ndx_external_service_oauth ON external_service_oauth (user_id); | ||
CREATE INDEX service_ndx_external_service_oauth ON external_service_oauth (service); | ||
CREATE UNIQUE INDEX user_id_service_ndx_external_service_oauth ON external_service_oauth (user_id, service); | ||
|
||
COMMIT; |
26 changes: 26 additions & 0 deletions
26
admin/sql/updates/2021-04-12-2-migrate-existing-spotify-users-to-new-schema.sql
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,26 @@ | ||
BEGIN; | ||
INSERT INTO external_service_oauth | ||
( | ||
user_id, | ||
service, | ||
access_token, | ||
refresh_token, | ||
token_expires, | ||
last_updated, | ||
record_listens, | ||
service_details | ||
) | ||
SELECT user_id, | ||
'spotify' as service, | ||
user_token as access_token, | ||
refresh_token, | ||
token_expires, | ||
last_updated, | ||
record_listens, | ||
jsonb_build_object( | ||
'latest_listened_at', latest_listened_at, | ||
'error_message', error_message, | ||
'permission', permission | ||
) | ||
FROM spotify_auth; | ||
COMMIT; |