Compare commits

...

2 Commits

Author SHA1 Message Date
Efril
9f653eef37 fix migration number 2026-05-10 13:30:40 +07:00
Efril
ddaf6df436 migration notification 2026-05-10 12:35:44 +07:00
9 changed files with 76 additions and 0 deletions

View File

@ -37,6 +37,10 @@ func GetAllEntities() []interface{} {
&OtpSession{}, &OtpSession{},
// Analytics entities are not database tables, they are query results // Analytics entities are not database tables, they are query results
&UserDevice{}, &UserDevice{},
// Notification entities
&Notification{},
&NotificationReceiver{},
&NotificationDelivery{},
} }
} }

View File

@ -0,0 +1 @@
DROP TABLE IF EXISTS notifications;

View File

@ -0,0 +1,28 @@
-- Notifications table (master notification record)
CREATE TABLE notifications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title VARCHAR(255) NOT NULL,
body TEXT,
type VARCHAR(100),
category VARCHAR(100),
priority VARCHAR(50) NOT NULL DEFAULT 'normal' CHECK (priority IN ('low', 'normal', 'high')),
image_url VARCHAR(512),
action_url VARCHAR(512),
notifiable_type VARCHAR(100),
notifiable_id UUID,
data JSONB,
scheduled_at TIMESTAMP WITH TIME ZONE,
sent_at TIMESTAMP WITH TIME ZONE,
expired_at TIMESTAMP WITH TIME ZONE,
created_by UUID REFERENCES users(id) ON DELETE SET NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Indexes
CREATE INDEX idx_notifications_created_by ON notifications(created_by);
CREATE INDEX idx_notifications_type ON notifications(type);
CREATE INDEX idx_notifications_category ON notifications(category);
CREATE INDEX idx_notifications_notifiable ON notifications(notifiable_type, notifiable_id);
CREATE INDEX idx_notifications_scheduled_at ON notifications(scheduled_at);
CREATE INDEX idx_notifications_sent_at ON notifications(sent_at);

View File

@ -0,0 +1 @@
DROP TABLE IF EXISTS notification_receivers;

View File

@ -0,0 +1,18 @@
-- Notification receivers table (links a notification to a specific user)
CREATE TABLE notification_receivers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
notification_id UUID NOT NULL REFERENCES notifications(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
is_read BOOLEAN NOT NULL DEFAULT FALSE,
read_at TIMESTAMP WITH TIME ZONE,
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
deleted_at TIMESTAMP WITH TIME ZONE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Indexes
CREATE INDEX idx_notification_receivers_notification_id ON notification_receivers(notification_id);
CREATE INDEX idx_notification_receivers_user_id ON notification_receivers(user_id);
CREATE INDEX idx_notification_receivers_user_unread ON notification_receivers(user_id, is_read) WHERE is_deleted = FALSE;
CREATE UNIQUE INDEX idx_notification_receivers_unique ON notification_receivers(notification_id, user_id);

View File

@ -0,0 +1 @@
DROP TABLE IF EXISTS notification_deliveries;

View File

@ -0,0 +1,23 @@
-- Notification deliveries table (tracks per-device delivery attempts)
CREATE TABLE notification_deliveries (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
notification_receiver_id UUID NOT NULL REFERENCES notification_receivers(id) ON DELETE CASCADE,
user_device_id UUID NOT NULL REFERENCES user_devices(id) ON DELETE CASCADE,
channel VARCHAR(50) NOT NULL DEFAULT 'push' CHECK (channel IN ('push', 'websocket', 'email')),
delivery_status VARCHAR(50) NOT NULL DEFAULT 'pending' CHECK (delivery_status IN ('pending', 'sent', 'delivered', 'failed')),
provider VARCHAR(50) CHECK (provider IN ('firebase', 'onesignal')),
provider_message_id VARCHAR(255),
sent_at TIMESTAMP WITH TIME ZONE,
delivered_at TIMESTAMP WITH TIME ZONE,
failed_at TIMESTAMP WITH TIME ZONE,
failure_reason TEXT,
retry_count INT NOT NULL DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Indexes
CREATE INDEX idx_notification_deliveries_receiver_id ON notification_deliveries(notification_receiver_id);
CREATE INDEX idx_notification_deliveries_device_id ON notification_deliveries(user_device_id);
CREATE INDEX idx_notification_deliveries_status ON notification_deliveries(delivery_status);
CREATE INDEX idx_notification_deliveries_provider ON notification_deliveries(provider);