noti
This commit is contained in:
73
web-server/database/04_notifications.sql
Normal file
73
web-server/database/04_notifications.sql
Normal file
@@ -0,0 +1,73 @@
|
||||
USE [RobotInstaller];
|
||||
GO
|
||||
|
||||
SET ANSI_NULLS ON;
|
||||
SET QUOTED_IDENTIFIER ON;
|
||||
GO
|
||||
|
||||
-- Additive/idempotent migration. Safe to run more than once.
|
||||
IF OBJECT_ID(N'dbo.Notifications', N'U') IS NULL
|
||||
BEGIN
|
||||
CREATE TABLE dbo.Notifications
|
||||
(
|
||||
Id UNIQUEIDENTIFIER NOT NULL
|
||||
CONSTRAINT PK_Notifications PRIMARY KEY CLUSTERED
|
||||
CONSTRAINT DF_Notifications_Id DEFAULT NEWSEQUENTIALID(),
|
||||
RecipientUserId UNIQUEIDENTIFIER NOT NULL,
|
||||
ActorUserId UNIQUEIDENTIFIER NULL,
|
||||
ActorName NVARCHAR(200) NULL,
|
||||
BroadcastId UNIQUEIDENTIFIER NULL,
|
||||
EventType NVARCHAR(100) NOT NULL,
|
||||
Severity NVARCHAR(20) NOT NULL
|
||||
CONSTRAINT DF_Notifications_Severity DEFAULT N'info',
|
||||
Title NVARCHAR(200) NOT NULL,
|
||||
Message NVARCHAR(1000) NOT NULL,
|
||||
EntityType NVARCHAR(50) NULL,
|
||||
EntityId NVARCHAR(100) NULL,
|
||||
ActionUrl NVARCHAR(1000) NULL,
|
||||
ReadAt DATETIME2(3) NULL,
|
||||
CreatedAt DATETIME2(3) NOT NULL
|
||||
CONSTRAINT DF_Notifications_CreatedAt DEFAULT SYSUTCDATETIME(),
|
||||
ExpiresAt DATETIME2(3) NULL,
|
||||
CONSTRAINT FK_Notifications_RecipientUser
|
||||
FOREIGN KEY (RecipientUserId) REFERENCES dbo.Users(Id) ON DELETE CASCADE,
|
||||
CONSTRAINT CK_Notifications_Severity
|
||||
CHECK (Severity IN (N'info', N'success', N'warning', N'error')),
|
||||
CONSTRAINT CK_Notifications_EventType_NotBlank
|
||||
CHECK (LEN(LTRIM(RTRIM(EventType))) > 0),
|
||||
CONSTRAINT CK_Notifications_Title_NotBlank
|
||||
CHECK (LEN(LTRIM(RTRIM(Title))) > 0),
|
||||
CONSTRAINT CK_Notifications_Message_NotBlank
|
||||
CHECK (LEN(LTRIM(RTRIM(Message))) > 0)
|
||||
);
|
||||
END;
|
||||
GO
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.indexes
|
||||
WHERE object_id = OBJECT_ID(N'dbo.Notifications')
|
||||
AND name = N'IX_Notifications_Recipient_Read_Created'
|
||||
)
|
||||
BEGIN
|
||||
CREATE INDEX IX_Notifications_Recipient_Read_Created
|
||||
ON dbo.Notifications(RecipientUserId, ReadAt, CreatedAt DESC)
|
||||
INCLUDE (Severity, Title, ActionUrl, ExpiresAt);
|
||||
END;
|
||||
GO
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.indexes
|
||||
WHERE object_id = OBJECT_ID(N'dbo.Notifications')
|
||||
AND name = N'IX_Notifications_BroadcastId'
|
||||
)
|
||||
BEGIN
|
||||
CREATE INDEX IX_Notifications_BroadcastId
|
||||
ON dbo.Notifications(BroadcastId)
|
||||
WHERE BroadcastId IS NOT NULL;
|
||||
END;
|
||||
GO
|
||||
|
||||
PRINT N'RobotInstaller notification schema is ready.';
|
||||
GO
|
||||
@@ -28,6 +28,7 @@ Không lưu mật khẩu thật vào file cấu hình. Khi chạy local, tạo f
|
||||
| `dbo.PackageVersions` | Các version của từng package |
|
||||
| `dbo.Applications` | App được đóng gói từ nhiều package |
|
||||
| `dbo.ApplicationPackages` | Liên kết app-package, có thể chọn version cụ thể |
|
||||
| `dbo.Notifications` | Thông báo riêng cho từng user và thông báo hệ thống do Admin đăng |
|
||||
|
||||
## Ràng buộc quan trọng
|
||||
|
||||
@@ -63,12 +64,15 @@ $env:SQLCMDPASSWORD = '<mat-khau-sa>'
|
||||
sqlcmd -S 172.20.235.176 -U sa -b -i .\database\01_create_database.sql
|
||||
sqlcmd -S 172.20.235.176 -U sa -d RobotInstaller -b -i .\database\02_schema.sql
|
||||
sqlcmd -S 172.20.235.176 -U sa -d RobotInstaller -b -i .\database\03_views.sql
|
||||
sqlcmd -S 172.20.235.176 -U sa -d RobotInstaller -b -i .\database\04_notifications.sql
|
||||
```
|
||||
|
||||
Chạy các lệnh trên từ thư mục `web-server`.
|
||||
|
||||
Khi dùng `sqlcmd` để seed/test dữ liệu, thêm `-I` hoặc bật `SET QUOTED_IDENTIFIER ON` vì schema có filtered index cho ràng buộc một latest version trên mỗi package.
|
||||
|
||||
`04_notifications.sql` là migration chỉ bổ sung bảng/index và có thể chạy lặp lại. Với database đang hoạt động, chỉ chạy file này; không chạy lại `02_schema.sql` vì script schema gốc chủ động dừng khi phát hiện bảng đã tồn tại.
|
||||
|
||||
## Luồng dữ liệu đề xuất
|
||||
|
||||
1. Upload package mới:
|
||||
|
||||
Reference in New Issue
Block a user