-
-
Notifications
You must be signed in to change notification settings - Fork 76
feat: prepare external API persistence foundation #750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bballdavis
wants to merge
4
commits into
DialmasterOrg:dev
Choose a base branch
from
bballdavis:feature/external-api-foundation
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
59cafaf
feat: add external API persistence foundation
bballdavis e2d0a7f
test: keep channel model mocks association-compatible
bballdavis 18a5c52
fix: use portable JSON literal in API key migration
bballdavis 6fa6aae
Merge upstream dev into external API foundation
bballdavis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
| 'use strict'; | ||
| const { addColumnIfMissing, removeColumnIfExists } = require('./helpers'); | ||
| module.exports = { | ||
| async up(q, S) { | ||
| const cols = [ | ||
| ['role', { type: S.STRING(32), allowNull: false, defaultValue: 'legacy_download' }], | ||
|
bballdavis marked this conversation as resolved.
|
||
| ['auto_approve_video_requests', { type: S.BOOLEAN, allowNull: false, defaultValue: false }], | ||
| ['auto_approve_channel_requests', { type: S.BOOLEAN, allowNull: false, defaultValue: false }], | ||
| ['auto_approve_delete_requests', { type: S.BOOLEAN, allowNull: false, defaultValue: false }], | ||
| ['max_rating_level', { type: S.INTEGER, allowNull: false, defaultValue: 4 }], | ||
| ['allow_unrated', { type: S.BOOLEAN, allowNull: false, defaultValue: false }], | ||
| ['allowed_media_types', { type: S.JSON, allowNull: true, defaultValue: null }], | ||
| ['revoked_at', { type: S.DATE, allowNull: true, defaultValue: null }], | ||
| ['allow_video_requests', { type: S.BOOLEAN, allowNull: true, defaultValue: null }], | ||
| ['allow_channel_requests', { type: S.BOOLEAN, allowNull: true, defaultValue: null }], | ||
| ['allow_delete_video_requests', { type: S.BOOLEAN, allowNull: true, defaultValue: null }], | ||
| ['max_active_jobs', { type: S.INTEGER, allowNull: false, defaultValue: 5 }], | ||
| ['hourly_write_limit', { type: S.INTEGER, allowNull: false, defaultValue: 30 }], | ||
| ['daily_write_limit', { type: S.INTEGER, allowNull: false, defaultValue: 200 }], | ||
| ]; | ||
| for (const [name, definition] of cols) await addColumnIfMissing(q, 'apikeys', name, definition); | ||
| await q.sequelize.query("UPDATE apikeys SET role = 'legacy_download' WHERE role IS NULL OR role = ''"); | ||
| await q.sequelize.query("UPDATE apikeys SET allowed_media_types = '[\"video\"]' WHERE allowed_media_types IS NULL"); | ||
| await q.changeColumn('apikeys', 'allowed_media_types', { type: S.JSON, allowNull: false }); | ||
| for (const [column, roles] of [['allow_video_requests', "'request', 'delete', 'admin'"], ['allow_channel_requests', "'request', 'delete', 'admin'"], ['allow_delete_video_requests', "'delete', 'admin'"]]) { | ||
| await q.sequelize.query(`UPDATE apikeys SET ${column} = CASE WHEN role IN (${roles}) THEN true ELSE false END WHERE ${column} IS NULL`); | ||
| await q.changeColumn('apikeys', column, { type: S.BOOLEAN, allowNull: false, defaultValue: false }); | ||
| } | ||
| }, | ||
| async down(q) { | ||
| const columns = await q.describeTable('apikeys'); | ||
| if (columns.role && columns.is_active) { | ||
| const revokedAt = columns.revoked_at | ||
| ? ', revoked_at = COALESCE(revoked_at, CURRENT_TIMESTAMP)' | ||
| : ''; | ||
| await q.sequelize.query(`UPDATE apikeys SET is_active = false${revokedAt} WHERE role IS NOT NULL AND role <> 'legacy_download'`); | ||
| } | ||
| for (const name of ['daily_write_limit', 'hourly_write_limit', 'max_active_jobs', 'allow_delete_video_requests', 'allow_channel_requests', 'allow_video_requests', 'revoked_at', 'allowed_media_types', 'allow_unrated', 'max_rating_level', 'auto_approve_delete_requests', 'auto_approve_channel_requests', 'auto_approve_video_requests', 'role']) await removeColumnIfExists(q, 'apikeys', name); | ||
| }, | ||
| }; | ||
15 changes: 15 additions & 0 deletions
15
migrations/20260908101000-create-api-key-channel-grants.js
This file contains hidden or 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,15 @@ | ||
| 'use strict'; | ||
| const { createTableIfNotExists, dropTableIfExists, addIndexIfMissing } = require('./helpers'); | ||
| module.exports = { | ||
| async up(q, S) { | ||
| await createTableIfNotExists(q, 'api_key_channel_grants', { | ||
| id: { type: S.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }, | ||
| api_key_id: { type: S.INTEGER, allowNull: false, references: { model: 'apikeys', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'CASCADE' }, | ||
| channel_id: { type: S.INTEGER, allowNull: false, references: { model: 'channels', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'CASCADE' }, | ||
| created_at: { type: S.DATE, allowNull: false, defaultValue: S.NOW }, | ||
| }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci' }); | ||
| await addIndexIfMissing(q, 'api_key_channel_grants', ['api_key_id', 'channel_id'], { unique: true, name: 'api_key_channel_grants_key_channel_uq' }); | ||
| await addIndexIfMissing(q, 'api_key_channel_grants', ['channel_id'], { name: 'api_key_channel_grants_channel_idx' }); | ||
| }, | ||
| async down(q) { await dropTableIfExists(q, 'api_key_channel_grants'); }, | ||
| }; |
This file contains hidden or 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,47 @@ | ||
| 'use strict'; | ||
| const { createTableIfNotExists, dropTableIfExists, addIndexIfMissing } = require('./helpers'); | ||
| module.exports = { | ||
| async up(q, S) { | ||
| await createTableIfNotExists(q, 'external_requests', { | ||
| id: { type: S.UUID, primaryKey: true, allowNull: false }, | ||
| api_key_id: { | ||
| type: S.INTEGER, | ||
| allowNull: false, | ||
| references: { model: 'apikeys', key: 'id' }, | ||
| onUpdate: 'CASCADE', | ||
| onDelete: 'CASCADE', | ||
| }, | ||
| channel_id: { | ||
| type: S.INTEGER, | ||
| allowNull: true, | ||
| references: { model: 'channels', key: 'id' }, | ||
| onUpdate: 'CASCADE', | ||
| onDelete: 'SET NULL', | ||
| }, | ||
| youtube_id: { type: S.STRING(32), allowNull: true }, | ||
| channel_url: { type: S.STRING(500), allowNull: true }, | ||
| grant_to_requesting_key: { type: S.BOOLEAN, allowNull: true }, | ||
| request_type: { type: S.STRING(20), allowNull: false, defaultValue: 'video' }, | ||
| status: { type: S.STRING(20), allowNull: false, defaultValue: 'pending' }, | ||
| active_dedupe_key: { type: S.STRING(191), allowNull: true }, | ||
| idempotency_hash: { type: S.STRING(64), allowNull: true }, | ||
| job_id: { | ||
| type: S.UUID, | ||
| allowNull: true, | ||
| references: { model: 'jobs', key: 'id' }, | ||
| onUpdate: 'CASCADE', | ||
| onDelete: 'SET NULL', | ||
| }, | ||
| message: { type: S.STRING(500), allowNull: true }, | ||
| created_at: { type: S.DATE, allowNull: false, defaultValue: S.NOW }, | ||
| updated_at: { type: S.DATE, allowNull: false, defaultValue: S.NOW }, | ||
| decided_at: { type: S.DATE, allowNull: true }, | ||
| completed_at: { type: S.DATE, allowNull: true }, | ||
| }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci' }); | ||
| await addIndexIfMissing(q, 'external_requests', ['active_dedupe_key'], { unique: true, name: 'external_requests_active_dedupe_uq' }); | ||
| await addIndexIfMissing(q, 'external_requests', ['api_key_id', 'idempotency_hash'], { unique: true, name: 'external_requests_key_idempotency_uq' }); | ||
| await addIndexIfMissing(q, 'external_requests', ['api_key_id', 'created_at'], { name: 'external_requests_key_created_idx' }); | ||
| await addIndexIfMissing(q, 'external_requests', ['api_key_id', 'status'], { name: 'external_requests_key_status_idx' }); | ||
| }, | ||
| async down(q) { await dropTableIfExists(q, 'external_requests'); }, | ||
| }; |
24 changes: 24 additions & 0 deletions
24
migrations/20260908106000-create-external-api-usage-buckets.js
This file contains hidden or 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,24 @@ | ||
| 'use strict'; | ||
| const { createTableIfNotExists, dropTableIfExists, addIndexIfMissing } = require('./helpers'); | ||
| module.exports = { | ||
| async up(q, S) { | ||
| await createTableIfNotExists(q, 'external_api_usage_buckets', { | ||
| id: { type: S.BIGINT, primaryKey: true, autoIncrement: true, allowNull: false }, | ||
| api_key_id: { | ||
| type: S.INTEGER, | ||
| allowNull: false, | ||
| references: { model: 'apikeys', key: 'id' }, | ||
| onUpdate: 'CASCADE', | ||
| onDelete: 'CASCADE', | ||
| }, | ||
| window_type: { type: S.STRING(8), allowNull: false }, | ||
| window_start: { type: S.DATE, allowNull: false }, | ||
| accepted_writes: { type: S.INTEGER, allowNull: false, defaultValue: 0 }, | ||
| created_at: { type: S.DATE, allowNull: false, defaultValue: S.NOW }, | ||
| updated_at: { type: S.DATE, allowNull: false, defaultValue: S.NOW }, | ||
| }); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other two new tables pass { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci' } to createTable, but this one doesn't. It works today, because an earlier migration already sets the database default, but can you add it here too so all the new tables match? |
||
| await addIndexIfMissing(q, 'external_api_usage_buckets', ['api_key_id', 'window_type', 'window_start'], { unique: true, name: 'external_api_usage_key_window_uq' }); | ||
| await addIndexIfMissing(q, 'external_api_usage_buckets', ['window_start'], { name: 'external_api_usage_window_idx' }); | ||
| }, | ||
| async down(q) { await dropTableIfExists(q, 'external_api_usage_buckets'); }, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section describes fields that nothing uses yet, so users reading the auth docs can't do anything with it. Can you move it to docs/DATABASE.md instead? The table list there (around line 32) should also get the 3 new tables, since that's where people look to see what's in the database.