-
Notifications
You must be signed in to change notification settings - Fork 1
STINGS MIGRATIONS SASHI and also add some to scope one #2
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
rhit-sashiks
wants to merge
2
commits into
master
Choose a base branch
from
stings
base: master
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
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| local datetime = require"@antiraid/datetime" | ||
| local typesext = require"@antiraid/typesext" | ||
| local db = require"../common/db" | ||
|
|
||
| local OLD_SCOPE = "builtins.stings" | ||
| local KEX_SCOPE = "__kexsbuiltins.stings" | ||
| local FAR_FUTURE_SECS = 100 * 365 * 24 * 60 * 60 | ||
|
|
||
| return { | ||
| up = function(db: db.Db) | ||
| print("[stings] merging old sting data into kex records (pre-kv_scope_unnest)") | ||
|
|
||
| local tx = db:begin() | ||
|
|
||
| local ok, err = pcall(function() | ||
| local sting_rows = tx:fetchall( | ||
| "SELECT owner_id, owner_type, key, scopes, value FROM tenant_kv WHERE $1 = ANY(scopes)", | ||
| { db:cast(OLD_SCOPE, "string") } | ||
| ) | ||
|
|
||
| type StingFields = { owner_id: string, owner_type: string, key: string, userid: string, modid: string?, reason: string, stings: number } | ||
| local old_stings: {[string]: StingFields} = {} | ||
| local missing_userid = 0 | ||
| for _, row in sting_rows do | ||
| local owner_id = row:get(0, "string"):take() :: string | ||
| local owner_type = row:get(1, "string"):take() :: string | ||
| local key = row:get(2, "string"):take() :: string | ||
| local scopes = row:get(3, "{string}"):take() :: {string} | ||
| local value = row:get(4, "custom@khronosvalue"):take() :: any | ||
|
|
||
| local userid: string? = nil | ||
| for _, s in scopes do | ||
| if s ~= OLD_SCOPE then | ||
| userid = s | ||
| break | ||
| end | ||
| end | ||
|
|
||
| if not userid then | ||
| print(string.format("[stings] sting key=%s owner=%s/%s has no userid in scopes %s; skipping", key, owner_type, owner_id, table.concat(scopes, ","))) | ||
| missing_userid += 1 | ||
| continue | ||
| end | ||
|
Comment on lines
+39
to
+43
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. Skipped rows are still deleted, causing data loss. Line 42 skips rows with missing userid, but Line 138 deletes all 🔧 Proposed fix- local deleted = tx:execute(
- "DELETE FROM tenant_kv WHERE $1 = ANY(scopes)",
- { db:cast(OLD_SCOPE, "string") }
- )
+ local deleted = tx:execute(
+ [[DELETE FROM tenant_kv
+ WHERE $1 = ANY(scopes)
+ AND EXISTS (
+ SELECT 1 FROM unnest(scopes) AS s WHERE s <> $1
+ )]],
+ { db:cast(OLD_SCOPE, "string") }
+ )Also applies to: 138-141 🤖 Prompt for AI Agents |
||
|
|
||
| local composite = owner_id .. "|" .. owner_type .. "|" .. key | ||
| old_stings[composite] = { | ||
| owner_id = owner_id, | ||
| owner_type = owner_type, | ||
| key = key, | ||
| userid = userid, | ||
| modid = value.modId or value.modid, | ||
| reason = value.reason or "", | ||
| stings = value.stings or 1, | ||
| } | ||
| end | ||
| print(string.format("[stings] loaded %d old sting rows (%d skipped)", #sting_rows - missing_userid, missing_userid)) | ||
|
|
||
| local kex_rows = tx:fetchall( | ||
| "SELECT id, owner_id, owner_type, key, value FROM tenant_kv WHERE $1 = ANY(scopes)", | ||
| { db:cast(KEX_SCOPE, "string") } | ||
| ) | ||
| print(string.format("[stings] found %d existing kex rows", #kex_rows)) | ||
|
|
||
| local merged = 0 | ||
| local matched: {[string]: boolean} = {} | ||
|
|
||
| for _, row in kex_rows do | ||
| local id = row:get(0, "string"):take() :: string | ||
| local owner_id = row:get(1, "string"):take() :: string | ||
| local owner_type = row:get(2, "string"):take() :: string | ||
| local key = row:get(3, "string"):take() :: string | ||
| local value = row:get(4, "custom@khronosvalue"):take() :: any | ||
|
|
||
| local composite = owner_id .. "|" .. owner_type .. "|" .. key | ||
| local sting = old_stings[composite] | ||
| if not sting then | ||
| print(string.format("[stings] kex key=%s owner=%s/%s has no matching sting data; leaving as-is", key, owner_type, owner_id)) | ||
| continue | ||
| end | ||
| matched[composite] = true | ||
|
|
||
| local new_value = { | ||
| expiresat = value.expiresat, | ||
| data = { | ||
| userid = sting.userid, | ||
| modid = sting.modid, | ||
| reason = sting.reason, | ||
| stings = sting.stings, | ||
| }, | ||
| } | ||
|
|
||
| tx:execute( | ||
| "UPDATE tenant_kv SET value = $1, last_updated_at = NOW() WHERE id = $2", | ||
| { | ||
| db:cast(new_value, "custom@khronosvalue"), | ||
| db:cast(id, "string"), | ||
| } | ||
| ) | ||
| merged += 1 | ||
| end | ||
| print(string.format("[stings] merged %d kex rows with matching sting data", merged)) | ||
|
|
||
| local inserted = 0 | ||
| local far_future = datetime.UTC:now() + datetime.timedelta_seconds(FAR_FUTURE_SECS) | ||
| for composite, sting in old_stings do | ||
| if matched[composite] then continue end | ||
|
|
||
| local new_id = typesext.randstring(64) | ||
| local new_value = { | ||
| expiresat = far_future, | ||
| data = { | ||
| userid = sting.userid, | ||
| modid = sting.modid, | ||
| reason = sting.reason, | ||
| stings = sting.stings, | ||
| }, | ||
| } | ||
|
|
||
| tx:execute( | ||
| [[INSERT INTO tenant_kv (id, owner_id, owner_type, key, value, scopes) | ||
| VALUES ($1, $2, $3, $4, $5, $6) | ||
| ON CONFLICT (owner_id, owner_type, key, scopes) DO UPDATE SET value = EXCLUDED.value, last_updated_at = NOW()]], | ||
| { | ||
| db:cast(new_id, "string"), | ||
| db:cast(sting.owner_id, "string"), | ||
| db:cast(sting.owner_type, "string"), | ||
| db:cast(sting.key, "string"), | ||
| db:cast(new_value, "custom@khronosvalue"), | ||
| db:cast({KEX_SCOPE}, "{string}"), | ||
| } | ||
| ) | ||
| inserted += 1 | ||
| end | ||
| if inserted > 0 then | ||
| print(string.format("[stings] inserted %d new kex rows (no prior expiry, assigned far-future)", inserted)) | ||
| end | ||
|
|
||
| local deleted = tx:execute( | ||
| "DELETE FROM tenant_kv WHERE $1 = ANY(scopes)", | ||
| { db:cast(OLD_SCOPE, "string") } | ||
| ) | ||
| print(string.format("[stings] deleted %d rows from scope='%s'", deleted, OLD_SCOPE)) | ||
| end) | ||
|
|
||
| if not ok then | ||
| print("[stings] migration failed: " .. tostring(err)) | ||
| tx:rollback() | ||
| error(err) | ||
| end | ||
|
|
||
| tx:commit() | ||
| print("[stings] committed") | ||
| end, | ||
| } | ||
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
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.
🧩 Analysis chain
🏁 Script executed:
Repository: Anti-Raid/template-worker
Length of output: 880
🏁 Script executed:
Repository: Anti-Raid/template-worker
Length of output: 852
🏁 Script executed:
Repository: Anti-Raid/template-worker
Length of output: 51
PgRow.getindexes are off by one (0-based used, API is 1-based).Lines 25-29 and 68-72 use
row:get(0, ...)throughrow:get(4, ...), butPgRow.getis documented as 1-based. This will read from wrong columns during migration.🔧 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents