dendrite/syncapi/storage/postgres/deltas/20201017140347_create_receipt_table.go
2020-10-17 15:15:35 +02:00

47 lines
1.1 KiB
Go

package deltas
import (
"database/sql"
"fmt"
"github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/pressly/goose"
)
func LoadFromGoose() {
goose.AddMigration(UpCreateReceiptTable, DownCreateReceiptTable)
}
func LoadCreateReceiptTable(m *sqlutil.Migrations) {
m.AddMigration(UpCreateReceiptTable, DownCreateReceiptTable)
}
func UpCreateReceiptTable(tx *sql.Tx) error {
_, err := tx.Exec(`
-- Stores data about receipts
CREATE TABLE IF NOT EXISTS syncapi_receipts (
-- The ID
id BIGINT PRIMARY KEY DEFAULT nextval('syncapi_stream_id'),
room_id TEXT NOT NULL,
receipt_type TEXT NOT NULL,
user_id TEXT NOT NULL,
event_id TEXT NOT NULL,
receipt_ts BIGINT NOT NULL,
CONSTRAINT syncapi_receipts_unique UNIQUE (room_id, receipt_type, user_id)
);
CREATE INDEX IF NOT EXISTS syncapi_receipts_room_id ON syncapi_receipts(room_id);
`)
if err != nil {
return fmt.Errorf("failed to execute upgrade: %w", err)
}
return nil
}
func DownCreateReceiptTable(tx *sql.Tx) error {
_, err := tx.Exec("DROP TABLE IF EXISTS syncapi_receipts;")
if err != nil {
return fmt.Errorf("failed to execute downgrade: %w", err)
}
return nil
}