row:=db.QueryRowxContext(ctx,`SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'aggregate_name'`)
varfoundint
row:=db.QueryRowxContext(ctx,`SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'name'`)
err=row.Scan(&found)
iferr!=nil{
returnerr
}
iffound==0{
iffound==1{
// Re-sequence events
_,err=db.ExecContext(ctx,`with tmp as (
select id as eid,
row_number() over (partition by aggregate_id, aggregate_name order by tstamp) rn
from events
where (aggregate_id, aggregate_name) in (select aggregate_id, aggregate_name
from events
group by aggregate_id, aggregate_name, sequence_no
having count(*) > 1)
)
update events
set sequence_no = tmp.rn
from tmp
where id = eid`)
iferr!=nil{
returnerr
}
_,err=db.ExecContext(ctx,`drop index if exists event_ix`)
iferr!=nil{
returnerr
}
}
_,err=db.ExecContext(ctx,`create unique index if not exists event_ix on events (aggregate_id, aggregate_name, sequence_no, id, name, tstamp)`)
_,err=db.ExecContext(ctx,`create unique index if not exists event_ix on events (aggregate_id, aggregate_name, sequence_no)`)
mock.ExpectExec("alter table events alter column aggregate_name set not null").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectQuery("SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'aggregate_name'").
mock.ExpectQuery("SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'name'").
mock.ExpectExec("create table if not exists events ( id bigserial primary key, name text not null, aggregate_id text not null, sequence_no bigint not null, payload text not null, tstamp timestamptz not null )").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("create unique index if not exists event_ix on events (aggregate_id, sequence_no, id, name, tstamp)").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("create table if not exists snapshots ( id bigserial primary key, name text not null, aggregate_id text not null, sequence_no bigint not null, payload text not null, tstamp timestamptz not null )").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("create unique index if not exists snapshot_ix on snapshots (aggregate_id, sequence_no, id, name, tstamp)").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("create table if not exists aggregates (id text, name text, primary key (id, name))").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("create unique index if not exists aggregate_ix on aggregates (id, name)").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("alter table events add column if not exists aggregate_name text").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("update aggregates set name = TRIM(LEADING '*' FROM name) where name like '*%'").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("with cte as ( select a.name, e.id, e.aggregate_name from aggregates a join events e on a.id = e.aggregate_id ) update events set aggregate_name = cte.name from cte where events.id = cte.id and events.aggregate_name is null").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("alter table events alter column aggregate_name set not null").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectQuery("SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'name'").
mock.ExpectExec("with tmp as (\nselect id as eid,\n row_number() over (partition by aggregate_id, aggregate_name order by tstamp) rn\nfrom events\nwhere (aggregate_id, aggregate_name) in (select aggregate_id, aggregate_name\n from events\n group by aggregate_id, aggregate_name, sequence_no\n having count(*) > 1)\n)\nupdate events\nset sequence_no = tmp.rn\nfrom tmp\nwhere id = eid").
mock.ExpectExec("alter table events alter column aggregate_name set not null").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectQuery("SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'aggregate_name'").
mock.ExpectQuery("SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'name'").
mock.ExpectExec("with tmp as (\nselect id as eid,\n row_number() over (partition by aggregate_id, aggregate_name order by tstamp) rn\nfrom events\nwhere (aggregate_id, aggregate_name) in (select aggregate_id, aggregate_name\n from events\n group by aggregate_id, aggregate_name, sequence_no\n having count(*) > 1)\n)\nupdate events\nset sequence_no = tmp.rn\nfrom tmp\nwhere id = eid").
mock.ExpectExec("alter table events alter column aggregate_name set not null").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectQuery("SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'aggregate_name'").
mock.ExpectQuery("SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'name'").
mock.ExpectExec("with tmp as (\nselect id as eid,\n row_number() over (partition by aggregate_id, aggregate_name order by tstamp) rn\nfrom events\nwhere (aggregate_id, aggregate_name) in (select aggregate_id, aggregate_name\n from events\n group by aggregate_id, aggregate_name, sequence_no\n having count(*) > 1)\n)\nupdate events\nset sequence_no = tmp.rn\nfrom tmp\nwhere id = eid").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("drop index if exists event_ix").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("create unique index if not exists event_ix on events (aggregate_id, aggregate_name, sequence_no, id, name, tstamp)").
mock.ExpectExec("create unique index if not exists event_ix on events (aggregate_id, aggregate_name, sequence_no)").
mock.ExpectExec("alter table events alter column aggregate_name set not null").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectQuery("SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'aggregate_name'").
mock.ExpectQuery("SELECT coalesce(max(1), 0) as found FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid WHERE a.attnum = ANY (ix.indkey) AND t.relkind = 'r' AND t.relname = 'events' AND a.attname = 'name'").
mock.ExpectExec("create unique index if not exists event_ix on events (aggregate_id, aggregate_name, sequence_no, id, name, tstamp)").
mock.ExpectExec("with tmp as (\nselect id as eid,\n row_number() over (partition by aggregate_id, aggregate_name order by tstamp) rn\nfrom events\nwhere (aggregate_id, aggregate_name) in (select aggregate_id, aggregate_name\n from events\n group by aggregate_id, aggregate_name, sequence_no\n having count(*) > 1)\n)\nupdate events\nset sequence_no = tmp.rn\nfrom tmp\nwhere id = eid").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("drop index if exists event_ix").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectExec("create unique index if not exists event_ix on events (aggregate_id, aggregate_name, sequence_no)").