Skip to content

Commit

Permalink
fix: Fix priority trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
leo91000 committed Sep 17, 2022
1 parent e95de06 commit d609088
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 167 deletions.
165 changes: 0 additions & 165 deletions clients/ts/src/class.ts

This file was deleted.

2 changes: 0 additions & 2 deletions clients/ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ export interface QuoteOutput {
updatedAt: string
}

export * from './class'

interface LyonkitClientOptions { endpoint?: string; apiKey: string }

export function createLyonkitReadonlyApiClient({ endpoint = 'https://lyonkit.leo-coletta.fr', apiKey }: LyonkitClientOptions) {
Expand Down
2 changes: 2 additions & 0 deletions crates/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod m20220716_000007_fix_blok_priority_trigger;
mod m20220831_000008_create_blok_table;
mod m20220901_000009_add_slug_column_to_posts;
mod m20220901_000010_create_git_auths_table;
mod m20220917_000011_fix_blok_priority_trigger;
pub(crate) mod utils;

pub struct Migrator;
Expand All @@ -29,6 +30,7 @@ impl MigratorTrait for Migrator {
Box::new(m20220831_000008_create_blok_table::Migration),
Box::new(m20220901_000009_add_slug_column_to_posts::Migration),
Box::new(m20220901_000010_create_git_auths_table::Migration),
Box::new(m20220917_000011_fix_blok_priority_trigger::Migration),
]
}
}
112 changes: 112 additions & 0 deletions crates/migration/src/m20220917_000011_fix_blok_priority_trigger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use crate::utils::macros::exec_stmt;
use sea_orm_migration::{prelude::*, MigrationName};

pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220917_000011_fix_blok_priority_trigger"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
exec_stmt!(
manager,
r#"
create or replace function tg_bloks__set_priority() returns trigger as $$
declare
max_priority integer;
updated_ids integer[];
begin
set constraints all deferred;
-- Set default priority to maximum
if new.priority is null or new.priority = 0 then
select max(b1.priority) into max_priority from bloks b1 where b1.page_id = new.page_id;
new.priority := coalesce(max_priority + 1, 0);
end if;
if exists(select 1 from bloks where page_id = new.page_id and priority = new.priority) then
with t as (update bloks b2 set priority = b2.priority + 1 where b2.page_id = new.page_id and b2.priority >= new.priority and b2.id is distinct from new.id returning id)
select array_agg(id) into updated_ids from t;
raise notice 'Offset priority for rows IDS : %', updated_ids;
end if;
return new;
end;
$$ language plpgsql volatile;
"#
)?;

// Only execute trigger with one recursion limit
exec_stmt!(
manager,
"drop trigger if exists _500_set_display_priority on bloks;"
)?;
exec_stmt!(
manager,
r#"create trigger _500_set_display_priority before insert or update on bloks for each row when (pg_trigger_depth() = 0) execute procedure public.tg_bloks__set_priority();"#
)?;

// Use constraint based uniqueness instead of index
exec_stmt!(
manager,
r#"drop index if exists bloks__page_id_priority__uniq_idx;"#
)?;
exec_stmt!(
manager,
r#"alter table bloks drop constraint if exists page_id_priority__uniq, add constraint page_id_priority__uniq unique (page_id, priority) deferrable initially immediate;"#
)?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
exec_stmt!(
manager,
r#"
create or replace function tg_bloks__set_priority() returns trigger as $$
declare
max_priority integer;
begin
set constraints all deferred;
-- Set default priority to maximum
if new.priority is null or new.priority = 0 then
select max(b1.priority) into max_priority from bloks b1 where b1.page_id = new.page_id;
new.priority := coalesce(max_priority + 1, 0);
end if;
-- Offset other priorities if conflict
update bloks b2 set priority = b2.priority + 1 where b2.page_id = new.page_id and b2.priority = new.priority and b2.id != new.id;
return new;
end;
$$ language plpgsql volatile;
"#
)?;

exec_stmt!(
manager,
"drop trigger if exists _500_set_display_priority on bloks;"
)?;
exec_stmt!(
manager,
r#"create trigger _500_set_display_priority before insert or update on bloks for each row execute procedure public.tg_bloks__set_priority();"#
)?;

exec_stmt!(
manager,
"alter table bloks drop constraint if exists page_id_priority__uniq;"
)?;
exec_stmt!(
manager,
r#"create unique index if not exists bloks__page_id_priority__uniq_idx on bloks(page_id, priority);"#
)?;

Ok(())
}
}

0 comments on commit d609088

Please sign in to comment.