-
-
Notifications
You must be signed in to change notification settings - Fork 59
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
fix: prevent channel deletion when unsetting device channel #1012
Open
devin-ai-integration
wants to merge
8
commits into
main
Choose a base branch
from
devin/1738885380-fix-channel-deletion
base: main
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
8 commits
Select commit
Hold shift + click to select a range
af8540b
fix: prevent channel deletion when unsetting device channel
devin-ai-integration[bot] 2b51efc
fix: add owner_org to channel seed function
devin-ai-integration[bot] 0093e0b
fix: update channel_self types and add debug logging
devin-ai-integration[bot] da2b54e
fix: add owner_org check to channel tests
devin-ai-integration[bot] 3e83c57
fix: add owner_org check to remaining channel tests
devin-ai-integration[bot] 7a080fa
fix: add owner_org check to setupChannel function
devin-ai-integration[bot] a56e854
fix: update Context type to use unknown instead of empty object
devin-ai-integration[bot] bf8c756
[autofix.ci] apply automated fixes
autofix-ci[bot] 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
Binary file not shown.
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
File renamed without changes.
37 changes: 37 additions & 0 deletions
37
supabase/migrations/20240101000002_fix_channel_deletion.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,37 @@ | ||
-- Drop existing constraint | ||
ALTER TABLE "public"."channel_devices" | ||
DROP CONSTRAINT IF EXISTS "channel_devices_channel_id_fkey"; | ||
|
||
-- Re-add constraint with ON DELETE SET NULL | ||
ALTER TABLE "public"."channel_devices" | ||
ADD CONSTRAINT "channel_devices_channel_id_fkey" | ||
FOREIGN KEY ("channel_id") | ||
REFERENCES "public"."channels"("id") | ||
ON DELETE SET NULL; | ||
|
||
-- Add NOT NULL constraint to prevent accidental deletions | ||
ALTER TABLE "public"."channels" | ||
ALTER COLUMN "name" SET NOT NULL, | ||
ALTER COLUMN "app_id" SET NOT NULL; | ||
|
||
-- Add explicit deletion protection | ||
CREATE OR REPLACE FUNCTION prevent_channel_deletion() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
IF EXISTS ( | ||
SELECT 1 | ||
FROM public.channel_devices | ||
WHERE channel_id = OLD.id | ||
LIMIT 1 | ||
) THEN | ||
RAISE EXCEPTION 'Cannot delete channel while devices are associated with it'; | ||
END IF; | ||
RETURN OLD; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
DROP TRIGGER IF EXISTS prevent_channel_deletion_trigger ON public.channels; | ||
CREATE TRIGGER prevent_channel_deletion_trigger | ||
BEFORE DELETE ON public.channels | ||
FOR EACH ROW | ||
EXECUTE FUNCTION prevent_channel_deletion(); |
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,72 @@ | ||
CREATE OR REPLACE FUNCTION public.reset_and_seed_app_data(p_app_id character varying) RETURNS void | ||
LANGUAGE plpgsql SECURITY DEFINER | ||
AS $$ | ||
DECLARE | ||
org_id uuid := '046a36ac-e03c-4590-9257-bd6c9dba9ee8'; | ||
user_id uuid := '6aa76066-55ef-4238-ade6-0b32334a4097'; | ||
max_version_id bigint; | ||
max_channel_id bigint; | ||
BEGIN | ||
-- Lock the tables to prevent concurrent inserts | ||
LOCK TABLE app_versions, channels IN EXCLUSIVE MODE; | ||
|
||
-- Delete existing data for the specified app_id | ||
DELETE FROM channels WHERE app_id = p_app_id; | ||
DELETE FROM app_versions WHERE app_id = p_app_id; | ||
DELETE FROM apps WHERE app_id = p_app_id; | ||
|
||
-- Get the current max ids and reset the sequences | ||
SELECT COALESCE(MAX(id), 0) + 1 INTO max_version_id FROM app_versions; | ||
SELECT COALESCE(MAX(id), 0) + 1 INTO max_channel_id FROM channels; | ||
|
||
-- Reset both sequences | ||
PERFORM setval('app_versions_id_seq', max_version_id, false); | ||
PERFORM setval('channel_id_seq', max_channel_id, false); | ||
|
||
-- Insert new app data | ||
INSERT INTO apps (created_at, app_id, icon_url, name, last_version, updated_at, owner_org, user_id) | ||
VALUES (now(), p_app_id, '', 'Seeded App', '1.0.0', now(), org_id, user_id); | ||
|
||
-- Insert app versions in a single statement | ||
WITH inserted_versions AS ( | ||
INSERT INTO app_versions (created_at, app_id, name, r2_path, updated_at, deleted, external_url, checksum, storage_provider, owner_org) | ||
VALUES | ||
(now(), p_app_id, 'builtin', NULL, now(), 't', NULL, NULL, 'supabase', org_id), | ||
(now(), p_app_id, 'unknown', NULL, now(), 't', NULL, NULL, 'supabase', org_id), | ||
(now(), p_app_id, '1.0.1', 'orgs/'||org_id||'/apps/'||p_app_id||'/1.0.1.zip', now(), 'f', NULL, '', 'r2-direct', org_id), | ||
(now(), p_app_id, '1.0.0', 'orgs/'||org_id||'/apps/'||p_app_id||'/1.0.0.zip', now(), 'f', NULL, '3885ee49', 'r2', org_id), | ||
(now(), p_app_id, '1.361.0', 'orgs/'||org_id||'/apps/'||p_app_id||'/1.361.0.zip', now(), 'f', NULL, '9d4f798a', 'r2', org_id), | ||
(now(), p_app_id, '1.360.0', 'orgs/'||org_id||'/apps/'||p_app_id||'/1.360.0.zip', now(), 'f', NULL, '44913a9f', 'r2', org_id), | ||
(now(), p_app_id, '1.359.0', 'orgs/'||org_id||'/apps/'||p_app_id||'/1.359.0.zip', now(), 'f', NULL, '9f74e70a', 'r2', org_id) | ||
RETURNING id, name | ||
) | ||
-- Insert channels using the version IDs from the CTE | ||
INSERT INTO channels (created_at, name, app_id, version, updated_at, public, disable_auto_update_under_native, disable_auto_update, ios, android, allow_device_self_set, allow_emulator, allow_dev, owner_org) | ||
SELECT | ||
now(), | ||
c.name, | ||
p_app_id, | ||
v.id, | ||
now(), | ||
c.is_public, | ||
't', | ||
'major', | ||
c.ios, | ||
c.android, | ||
't', | ||
't', | ||
't', | ||
org_id | ||
FROM ( | ||
VALUES | ||
('production', '1.0.0', true, false, true), | ||
('no_access', '1.361.0', false, true, true), | ||
('two_default', '1.0.0', true, true, false) | ||
) as c(name, version_name, is_public, ios, android) | ||
JOIN inserted_versions v ON v.name = c.version_name; | ||
|
||
END; | ||
$$; | ||
|
||
REVOKE ALL ON FUNCTION public.reset_and_seed_app_data(p_app_id character varying) FROM PUBLIC; | ||
GRANT ALL ON FUNCTION public.reset_and_seed_app_data(p_app_id character varying) TO service_role; |
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.
do no add this