-
-
Notifications
You must be signed in to change notification settings - Fork 305
feat: lifecycle configuration control plane #1338
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
ferhatelmas
wants to merge
3
commits into
master
Choose a base branch
from
ferhat/lifecycle-control-plane
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.
+3,653
−9
Open
Changes from all commits
Commits
Show all changes
3 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
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
148 changes: 148 additions & 0 deletions
148
migrations/tenant/0065-bucket-lifecycle-configuration.sql
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,148 @@ | ||
| -- Add lifecycle configuration. | ||
|
|
||
| ALTER TABLE storage.buckets | ||
| ADD COLUMN IF NOT EXISTS lifecycle_configuration jsonb, | ||
| ADD COLUMN IF NOT EXISTS lifecycle_configuration_generation uuid; | ||
|
|
||
| DO $$ | ||
| BEGIN | ||
| IF NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM pg_catalog.pg_constraint | ||
| WHERE conrelid = 'storage.buckets'::regclass | ||
| AND conname = 'buckets_lifecycle_configuration_pair_check' | ||
| ) THEN | ||
| ALTER TABLE storage.buckets | ||
| ADD CONSTRAINT buckets_lifecycle_configuration_pair_check CHECK ( | ||
| (lifecycle_configuration IS NULL) = | ||
| (lifecycle_configuration_generation IS NULL) | ||
| ); | ||
| END IF; | ||
|
|
||
| IF NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM pg_catalog.pg_constraint | ||
| WHERE conrelid = 'storage.buckets'::regclass | ||
| AND conname = 'buckets_lifecycle_configuration_shape_check' | ||
| ) THEN | ||
| ALTER TABLE storage.buckets | ||
| ADD CONSTRAINT buckets_lifecycle_configuration_shape_check CHECK ( | ||
| lifecycle_configuration IS NULL | ||
| OR ( | ||
| jsonb_typeof(lifecycle_configuration) = 'object' | ||
| AND lifecycle_configuration ? 'rules' | ||
| AND CASE | ||
| WHEN jsonb_typeof(lifecycle_configuration -> 'rules') = 'array' | ||
| THEN jsonb_array_length(lifecycle_configuration -> 'rules') BETWEEN 1 AND 1000 | ||
| ELSE false | ||
| END | ||
| ) | ||
| ); | ||
| END IF; | ||
|
|
||
| IF NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM pg_catalog.pg_constraint | ||
| WHERE conrelid = 'storage.buckets'::regclass | ||
| AND conname = 'buckets_lifecycle_configuration_standard_only_check' | ||
| ) THEN | ||
| ALTER TABLE storage.buckets | ||
| ADD CONSTRAINT buckets_lifecycle_configuration_standard_only_check CHECK ( | ||
| type = 'STANDARD' | ||
| OR ( | ||
| lifecycle_configuration IS NULL | ||
| AND lifecycle_configuration_generation IS NULL | ||
| ) | ||
| ); | ||
| END IF; | ||
| END; | ||
| $$; | ||
|
|
||
| CREATE OR REPLACE FUNCTION storage.protect_bucket_control_columns() | ||
| RETURNS trigger | ||
| LANGUAGE plpgsql | ||
| SET search_path = pg_catalog | ||
| AS $$ | ||
| DECLARE | ||
| service_role text = TG_ARGV[0]; | ||
| current_operation text = COALESCE(current_setting('storage.operation', true), ''); | ||
| configuration_changed boolean; | ||
| BEGIN | ||
| IF TG_OP = 'INSERT' THEN | ||
| IF NEW.lifecycle_configuration IS NOT NULL | ||
| OR NEW.lifecycle_configuration_generation IS NOT NULL THEN | ||
| RAISE EXCEPTION 'bucket control columns must use their protected defaults on insert' | ||
| USING ERRCODE = '42501'; | ||
| END IF; | ||
|
|
||
| RETURN NEW; | ||
| END IF; | ||
|
|
||
| configuration_changed = | ||
| OLD.lifecycle_configuration IS DISTINCT FROM NEW.lifecycle_configuration | ||
| OR OLD.lifecycle_configuration_generation IS DISTINCT FROM NEW.lifecycle_configuration_generation; | ||
|
|
||
| IF NOT configuration_changed THEN | ||
| RETURN NEW; | ||
| END IF; | ||
|
|
||
| IF NEW.type IS DISTINCT FROM 'STANDARD' THEN | ||
| RAISE EXCEPTION 'bucket versioning and lifecycle controls require a Standard bucket' | ||
| USING ERRCODE = '0A000'; | ||
| END IF; | ||
|
|
||
| IF current_user::text IS DISTINCT FROM service_role THEN | ||
| RAISE EXCEPTION 'bucket control columns may only be changed by the configured storage service role' | ||
| USING ERRCODE = '42501'; | ||
| END IF; | ||
|
|
||
| IF NEW.lifecycle_configuration IS NULL | ||
| AND NEW.lifecycle_configuration_generation IS NULL THEN | ||
| IF current_operation NOT IN ( | ||
| 'storage.s3.bucket.delete_lifecycle', | ||
| 'storage.bucket.delete_lifecycle' | ||
| ) THEN | ||
| RAISE EXCEPTION 'invalid operation for lifecycle configuration deletion' | ||
| USING ERRCODE = '42501'; | ||
| END IF; | ||
|
|
||
| RETURN NEW; | ||
| END IF; | ||
|
|
||
| IF current_operation NOT IN ( | ||
| 'storage.s3.bucket.put_lifecycle', | ||
| 'storage.bucket.put_lifecycle' | ||
| ) THEN | ||
| RAISE EXCEPTION 'invalid operation for lifecycle configuration update' | ||
| USING ERRCODE = '42501'; | ||
| END IF; | ||
|
|
||
| IF NEW.lifecycle_configuration IS NULL | ||
| OR NEW.lifecycle_configuration_generation IS NULL | ||
| OR OLD.lifecycle_configuration IS NOT DISTINCT FROM NEW.lifecycle_configuration | ||
| OR OLD.lifecycle_configuration_generation IS NOT DISTINCT FROM NEW.lifecycle_configuration_generation THEN | ||
| RAISE EXCEPTION 'a changed lifecycle policy requires a new non-null generation' | ||
| USING ERRCODE = '22023'; | ||
| END IF; | ||
|
|
||
| RETURN NEW; | ||
| END; | ||
| $$; | ||
|
|
||
| DO $$ | ||
| DECLARE | ||
| service_role text = COALESCE(current_setting('storage.service_role', true), 'service_role'); | ||
| BEGIN | ||
| DROP TRIGGER IF EXISTS protect_bucket_control_insert ON storage.buckets; | ||
| EXECUTE format( | ||
| 'CREATE TRIGGER protect_bucket_control_insert BEFORE INSERT ON storage.buckets FOR EACH ROW EXECUTE FUNCTION storage.protect_bucket_control_columns(%L)', | ||
| service_role | ||
| ); | ||
|
|
||
| DROP TRIGGER IF EXISTS protect_bucket_control_update ON storage.buckets; | ||
| EXECUTE format( | ||
| 'CREATE TRIGGER protect_bucket_control_update BEFORE UPDATE OF lifecycle_configuration, lifecycle_configuration_generation ON storage.buckets FOR EACH ROW EXECUTE FUNCTION storage.protect_bucket_control_columns(%L)', | ||
| service_role | ||
| ); | ||
| 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
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
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
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.
@GuptaManan100 this was working a week ago but now it's failing with
is it expected?
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.
MT_UNSAFE_POOLER_MODE: "true"seems silencing it but does it have any drawbacks to be aware of?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.
For completeness, it fails its first example in https://github.com/supabase/storage/blob/master/migrations/tenant/0002-storage-schema.sql but we can't change old migrations.
This commit seems working, 64dd33d