-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP controller automations migrations
- Loading branch information
Showing
7 changed files
with
149 additions
and
71 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
47 changes: 47 additions & 0 deletions
47
supabase/migrations/20241219171749_controller_automations-1.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,47 @@ | ||
begin; | ||
|
||
-- Add the `controller_task_id` column to the `live_specs` table, which will | ||
-- allow new agent versions to publish specs and use automations for controllers | ||
-- of the new specs, while still allowing legacy agents to run using the old | ||
-- `controller_next_run` column and handler. | ||
alter table public.live_specs | ||
add column controller_task_id public.flowid; | ||
|
||
comment on column public.live_specs.controller_task_id is 'The task id of the controller task that is responsible for this spec'; | ||
|
||
create unique index live_specs_controller_task_id_uindex on public.live_specs (controller_task_id); | ||
|
||
|
||
-- Update the inferred schema trigger function to support both the new and | ||
-- legacy controller notification mechanisms. This allows both the new and | ||
-- legacy agents to coexist and run controllers. | ||
CREATE or replace FUNCTION internal.on_inferred_schema_update() RETURNS trigger | ||
LANGUAGE plpgsql SECURITY DEFINER | ||
AS $$ | ||
declare | ||
controller_task_id flowid; | ||
begin | ||
|
||
select ls.controller_task_id into controller_task_id | ||
from public.live_specs ls | ||
where ls.catalog_name = new.collection_name and ls.spec_type = 'collection'; | ||
if controller_task_id is not null then | ||
perform internal.send_to_task( | ||
controller_task_id, | ||
'00:00:00:00:00:00:00:00'::flowid, | ||
'{"type":"inferred_schema_updated"}' | ||
); | ||
else | ||
-- Legacy controller notification code, to be removed once the rollout is complete. | ||
-- The least function is necessary in order to avoid delaying a controller job in scenarios | ||
-- where there is a backlog of controller runs that are due. | ||
update live_specs set controller_next_run = least(controller_next_run, now()) | ||
where catalog_name = new.collection_name and spec_type = 'collection'; | ||
end if; | ||
|
||
return null; | ||
end; | ||
$$; | ||
|
||
|
||
commit; |
33 changes: 0 additions & 33 deletions
33
supabase/migrations/20241219171749_controller_automations.sql
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
supabase/migrations/20250106141749_controller-automations-2.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,25 @@ | ||
-- Incrementally enable automation-based controllers for existing live specs, while | ||
-- preserving the ability to run legacy controllers for specs that haven't been upgraded yet. | ||
-- This is important because a controller that has been upgraded could subsequently be downgraded | ||
-- by a legacy controller (due to legacy agents running `notify_dependents`, which sets `controller_next_run`). | ||
begin; | ||
|
||
with new_tasks as ( | ||
update public.live_specs | ||
set | ||
controller_task_id = gen.tid, | ||
controller_next_run = null | ||
from ( | ||
select internal.id_generator () as tid, id as live_spec_id | ||
from public.live_specs | ||
where controller_task_id is null | ||
limit 100 | ||
) gen | ||
where live_specs.id = gen.live_spec_id | ||
returning controller_task_id | ||
) | ||
insert into internal.tasks (task_id, task_type, wake_at) | ||
select controller_task_id, 2, now () | ||
from new_tasks; | ||
|
||
commit; |
63 changes: 63 additions & 0 deletions
63
supabase/migrations/20250106141751_controller-automations-3.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,63 @@ | ||
-- To be run after no more legacy agents are running, to complete the transition to automation-based controllers | ||
-- and remove the old `live_specs.controller_next_run` column. | ||
begin; | ||
|
||
-- Generate controller task ids for any live specs that still don't have them. | ||
update public.live_specs | ||
set | ||
controller_task_id = gen.tid | ||
from | ||
( | ||
select | ||
internal.id_generator () as tid, | ||
id as live_spec_id | ||
from | ||
public.live_specs | ||
where | ||
controller_task_id is null | ||
) gen | ||
where | ||
live_specs.id = gen.live_spec_id; | ||
|
||
alter table public.live_specs | ||
alter column controller_task_id | ||
set | ||
not null; | ||
|
||
alter table public.live_specs | ||
drop column controller_next_run; | ||
|
||
-- Create automation jobs for any live specs that don't yet have them. | ||
insert into | ||
internal.tasks (task_id, task_type, wake_at) | ||
select | ||
controller_task_id, | ||
2, | ||
controller_next_run | ||
from | ||
public.live_specs; | ||
|
||
-- Update the inferred schema trigger function to drop support for legacy controller notifications. | ||
CREATE or replace FUNCTION internal.on_inferred_schema_update() RETURNS trigger | ||
LANGUAGE plpgsql SECURITY DEFINER | ||
AS $$ | ||
declare | ||
controller_task_id flowid; | ||
begin | ||
|
||
select ls.controller_task_id into controller_task_id | ||
from public.live_specs ls | ||
where ls.catalog_name = new.collection_name and ls.spec_type = 'collection'; | ||
if controller_task_id is not null then | ||
perform internal.send_to_task( | ||
controller_task_id, | ||
'00:00:00:00:00:00:00:00'::flowid, | ||
'{"type":"inferred_schema_updated"}'::json | ||
); | ||
end if; | ||
|
||
return null; | ||
end; | ||
$$; | ||
|
||
commit; |