This commit is contained in:
2026-03-27 09:56:26 +07:00
parent 56ab9f931e
commit 251b4ee673
26 changed files with 3076 additions and 2364 deletions

63
database/run-setup.ps1 Normal file
View File

@@ -0,0 +1,63 @@
# PowerShell Script to Execute SQL Server Setup
# Database: AccManager
# Server: 172.20.235.176
# SQL Server Connection Info
$ServerName = "172.20.235.176"
$Username = "sa"
$Password = "robotics@2020"
$SqlScriptPath = ".\setup.sql"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "AccManager Database Setup" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# Check if sqlcmd is available
if (-not (Get-Command sqlcmd -ErrorAction SilentlyContinue)) {
Write-Host "ERROR: sqlcmd is not available on this system." -ForegroundColor Red
Write-Host "Please install SQL Server Command Line Tools (sqlcmd)" -ForegroundColor Yellow
Write-Host "Download from: https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility" -ForegroundColor Yellow
exit
}
# Check if SQL script file exists
if (-not (Test-Path $SqlScriptPath)) {
Write-Host "ERROR: SQL script file not found: $SqlScriptPath" -ForegroundColor Red
exit
}
Write-Host "Server: $ServerName" -ForegroundColor Green
Write-Host "User: $Username" -ForegroundColor Green
Write-Host "Script: $SqlScriptPath" -ForegroundColor Green
Write-Host ""
# Execute SQL Script
Write-Host "Executing SQL script..." -ForegroundColor Yellow
Write-Host ""
try {
sqlcmd -S $ServerName -U $Username -P $Password -i $SqlScriptPath -o "setup_output.log"
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "SETUP COMPLETED SUCCESSFULLY!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "Output saved to: setup_output.log" -ForegroundColor Cyan
Write-Host ""
# Display output
Write-Host "Setup Output:" -ForegroundColor Cyan
Get-Content "setup_output.log"
}
catch {
Write-Host "ERROR executing SQL script:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
exit
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "Database is ready to use!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green

168
database/setup.sql Normal file
View File

@@ -0,0 +1,168 @@
-- ===========================================
-- SQL Server Setup Script for AccManager
-- Database: AccManager
-- Server: 172.20.235.176
-- ===========================================
-- Create Database
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = 'AccManager')
BEGIN
CREATE DATABASE AccManager;
PRINT 'Database AccManager created successfully.';
END
ELSE
BEGIN
PRINT 'Database AccManager already exists.';
END
USE AccManager;
-- ===========================================
-- 1. CREATE USERS TABLE
-- ===========================================
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Users')
BEGIN
CREATE TABLE Users (
UserId INT PRIMARY KEY IDENTITY(1,1),
Username NVARCHAR(50) UNIQUE NOT NULL,
Password NVARCHAR(255) NOT NULL,
Email NVARCHAR(100),
FullName NVARCHAR(100),
Role NVARCHAR(50) NOT NULL,
Status NVARCHAR(20) DEFAULT 'Active',
CreatedDate DATETIME DEFAULT GETDATE(),
LastLogin DATETIME,
IsActive BIT DEFAULT 1
);
PRINT 'Table Users created successfully.';
END
-- ===========================================
-- 2. CREATE APPLICATIONS TABLE
-- ===========================================
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Applications')
BEGIN
CREATE TABLE Applications (
AppId INT PRIMARY KEY IDENTITY(1,1),
Name NVARCHAR(100) NOT NULL,
Type NVARCHAR(50),
Status NVARCHAR(20) DEFAULT 'online',
Icon NVARCHAR(50),
Description NVARCHAR(500),
CreatedDate DATETIME DEFAULT GETDATE(),
UpdatedDate DATETIME DEFAULT GETDATE()
);
PRINT 'Table Applications created successfully.';
END
-- ===========================================
-- 3. CREATE ACCOUNTS TABLE
-- ===========================================
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Accounts')
BEGIN
CREATE TABLE Accounts (
AccountId INT PRIMARY KEY IDENTITY(1,1),
UserId INT NOT NULL,
AppId INT NOT NULL,
AccountUsername NVARCHAR(100),
AccountPassword NVARCHAR(255),
Email NVARCHAR(100),
AccessLevel NVARCHAR(50),
Status NVARCHAR(20) DEFAULT 'Active',
Notes NVARCHAR(MAX),
CreatedDate DATETIME DEFAULT GETDATE(),
UpdatedDate DATETIME DEFAULT GETDATE(),
FOREIGN KEY (UserId) REFERENCES Users(UserId) ON DELETE CASCADE,
FOREIGN KEY (AppId) REFERENCES Applications(AppId) ON DELETE CASCADE
);
PRINT 'Table Accounts created successfully.';
END
-- ===========================================
-- 4. CREATE AUDIT LOG TABLE
-- ===========================================
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'AuditLog')
BEGIN
CREATE TABLE AuditLog (
LogId INT PRIMARY KEY IDENTITY(1,1),
UserId INT,
Action NVARCHAR(50),
TableName NVARCHAR(50),
RecordId INT,
OldValue NVARCHAR(MAX),
NewValue NVARCHAR(MAX),
Timestamp DATETIME DEFAULT GETDATE(),
FOREIGN KEY (UserId) REFERENCES Users(UserId)
);
PRINT 'Table AuditLog created successfully.';
END
-- ===========================================
-- 5. CREATE INDEXES
-- ===========================================
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Users_Username')
BEGIN
CREATE INDEX IX_Users_Username ON Users(Username);
END
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Accounts_UserId')
BEGIN
CREATE INDEX IX_Accounts_UserId ON Accounts(UserId);
END
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Accounts_AppId')
BEGIN
CREATE INDEX IX_Accounts_AppId ON Accounts(AppId);
END
PRINT 'Indexes created successfully.';
-- ===========================================
-- 6. INSERT INITIAL DATA
-- ===========================================
-- Check if admin user exists
IF NOT EXISTS (SELECT * FROM Users WHERE Username = 'admin')
BEGIN
INSERT INTO Users (Username, Password, Email, FullName, Role, Status, IsActive)
VALUES ('admin', 'admin', 'admin@accmanager.local', 'Administrator', 'admin', 'Active', 1);
PRINT 'Admin user created: Username=admin, Password=admin';
END
-- Insert sample applications
IF (SELECT COUNT(*) FROM Applications) = 0
BEGIN
INSERT INTO Applications (Name, Type, Status, Icon, Description)
VALUES
('AWS', 'Cloud', 'online', 'cloud', 'Amazon Web Services - Cloud Computing'),
('GitHub', 'VCS', 'online', 'code', 'GitHub - Version Control System'),
('Google Workspace', 'Collaboration', 'online', 'mail', 'Google Workspace - Email and Collaboration'),
('Nginx Proxy', 'Infra', 'offline', 'dns', 'Nginx - Web Server and Reverse Proxy');
PRINT 'Sample applications inserted successfully.';
END
-- ===========================================
-- 7. DISPLAY DATABASE INFORMATION
-- ===========================================
PRINT '';
PRINT '========================================';
PRINT 'DATABASE SETUP COMPLETED SUCCESSFULLY';
PRINT '========================================';
PRINT '';
PRINT 'Database Name: AccManager';
PRINT '';
PRINT 'Tables created:';
SELECT ' - ' + name AS TableName FROM sys.tables ORDER BY name;
PRINT '';
PRINT 'Users in system:';
SELECT ' Username: ' + Username + ' | Role: ' + Role + ' | Status: ' + Status AS UserInfo FROM Users;
PRINT '';
PRINT 'Applications available:';
SELECT ' - ' + Name + ' (' + Type + ') - ' + Status AS AppInfo FROM Applications ORDER BY Name;
PRINT '';
PRINT 'Login Credentials:';
PRINT ' Username: admin';
PRINT ' Password: admin';
PRINT ' Role: admin';
PRINT '';
PRINT '========================================';