Files
2026-11-29 19:35:13 +01:00

68 lines
3.2 KiB
Go

package testdb
import "gorm.io/gorm"
// RowOrderColumnDDL is the seq column DDL the application migration adds on
// postgres, see database.ROW_ORDER_COLUMN. A test in the database package
// checks the two stay the same.
var RowOrderColumnDDL = map[string]string{
"campaign_events": `ALTER TABLE campaign_events ADD COLUMN IF NOT EXISTS seq BIGINT GENERATED BY DEFAULT AS IDENTITY`,
"recipients": `ALTER TABLE recipients ADD COLUMN IF NOT EXISTS seq BIGINT GENERATED BY DEFAULT AS IDENTITY`,
}
// RowOrderIndexDDL is the DDL of the seq indexes the application migration
// makes on postgres, see database.rowOrderIndexes, in the same order. A test
// in the database package checks the two stay the same.
var RowOrderIndexDDL = map[string][]string{
"campaign_events": {
`CREATE UNIQUE INDEX idx_campaign_events_seq ON campaign_events (seq)`,
`CREATE INDEX idx_campaign_events_campaign_event_seq ON campaign_events (campaign_id, event_id, seq)`,
},
"recipients": {
`CREATE UNIQUE INDEX idx_recipients_seq ON recipients (seq)`,
`CREATE INDEX idx_recipients_company_seq ON recipients (company_id, seq)`,
},
}
// Test only. Many tests make their tables with gorm AutoMigrate alone and not
// the migration of the application, which on postgres adds the seq column
// the repositories read in place of the sqlite rowid. This event trigger adds
// the column and its indexes right when gorm creates one of those tables, so
// such a test gets the schema and the query plans the application has. Whole
// constant statements. CREATE EVENT TRIGGER needs a superuser, so the tests
// connect to postgres as one.
const rowOrderTriggerFunctionSQL = `CREATE FUNCTION pctest_row_order() RETURNS event_trigger LANGUAGE plpgsql AS $fn$
DECLARE
r record;
BEGIN
FOR r IN SELECT * FROM pg_event_trigger_ddl_commands() WHERE command_tag = 'CREATE TABLE' LOOP
IF r.objid = to_regclass('campaign_events') THEN
EXECUTE 'ALTER TABLE campaign_events ADD COLUMN IF NOT EXISTS seq BIGINT GENERATED BY DEFAULT AS IDENTITY';
EXECUTE 'CREATE UNIQUE INDEX idx_campaign_events_seq ON campaign_events (seq)';
EXECUTE 'CREATE INDEX idx_campaign_events_campaign_event_seq ON campaign_events (campaign_id, event_id, seq)';
ELSIF r.objid = to_regclass('recipients') THEN
EXECUTE 'ALTER TABLE recipients ADD COLUMN IF NOT EXISTS seq BIGINT GENERATED BY DEFAULT AS IDENTITY';
EXECUTE 'CREATE UNIQUE INDEX idx_recipients_seq ON recipients (seq)';
EXECUTE 'CREATE INDEX idx_recipients_company_seq ON recipients (company_id, seq)';
END IF;
END LOOP;
END
$fn$`
const rowOrderTriggerSQL = `CREATE EVENT TRIGGER pctest_row_order ON ddl_command_end
WHEN TAG IN ('CREATE TABLE') EXECUTE FUNCTION pctest_row_order()`
// rowOrderTriggerAlwaysSQL makes the trigger fire also with foreign keys off,
// which sets session_replication_role to replica, see Open
const rowOrderTriggerAlwaysSQL = `ALTER EVENT TRIGGER pctest_row_order ENABLE ALWAYS`
// addRowOrderTrigger installs the event trigger in a new test database
func addRowOrderTrigger(db *gorm.DB) error {
for _, stmt := range []string{rowOrderTriggerFunctionSQL, rowOrderTriggerSQL, rowOrderTriggerAlwaysSQL} {
if err := db.Exec(stmt).Error; err != nil {
return err
}
}
return nil
}