feat(MSSQL): Fully implemented MS-SQL
This commit is contained in:
parent
8d1e992631
commit
3a27a6a3e8
1 changed files with 81 additions and 5 deletions
|
|
@ -3,9 +3,10 @@ package SQL
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "github.com/denisenkom/go-mssqldb"
|
mssqlpkg "github.com/denisenkom/go-mssqldb"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"scabiosa/Logging"
|
"scabiosa/Logging"
|
||||||
"scabiosa/Tools"
|
"scabiosa/Tools"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -31,7 +32,7 @@ func GetMSSQLInstance(sqlConfig Tools.SQLConfig) MSSQLConnector {
|
||||||
return mssql
|
return mssql
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckIfEventLogTableExist(db *sql.DB, mssql MSSQLConnector) bool {
|
func (mssql MSSQLConnector) checkIfEventLogTableExist(db *sql.DB) bool {
|
||||||
rows, _ := db.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'EventLog';")
|
rows, _ := db.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'EventLog';")
|
||||||
if !rows.Next() {
|
if !rows.Next() {
|
||||||
return false
|
return false
|
||||||
|
|
@ -39,7 +40,24 @@ func CheckIfEventLogTableExist(db *sql.DB, mssql MSSQLConnector) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateMSSQLConnection(mssql MSSQLConnector) *sql.DB {
|
func (mssql MSSQLConnector) checkIfBackupTableExist(db *sql.DB) bool {
|
||||||
|
rows, _ := db.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'Backups';")
|
||||||
|
if !rows.Next() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mssql MSSQLConnector) checkIfBackupEntryExist(db *sql.DB, backupName string, hostname string) bool {
|
||||||
|
query := fmt.Sprintf("SELECT * FROM dbo.Backups WHERE Hostname = '%s' AND BackupName = '%s'", hostname, backupName)
|
||||||
|
rows, _ := db.Query(query)
|
||||||
|
if !rows.Next() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func createMSSQLConnection(mssql MSSQLConnector) *sql.DB {
|
||||||
logger := Logging.DetailedLogger("MS-SQL", "createConnection")
|
logger := Logging.DetailedLogger("MS-SQL", "createConnection")
|
||||||
|
|
||||||
query := url.Values{}
|
query := url.Values{}
|
||||||
|
|
@ -53,16 +71,74 @@ func CreateMSSQLConnection(mssql MSSQLConnector) *sql.DB {
|
||||||
RawQuery: query.Encode(),
|
RawQuery: query.Encode(),
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("sqlserver", sqlSettings.String())
|
connector, err := mssqlpkg.NewConnector(sqlSettings.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connector.SessionInitSQL = "SET ANSI_NULLS ON"
|
||||||
|
|
||||||
|
db := sql.OpenDB(connector)
|
||||||
|
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mssql MSSQLConnector) createDefaultTables() {}
|
func (mssql MSSQLConnector) createDefaultTables() {
|
||||||
|
logger := Logging.DetailedLogger("MS-SQL", "createDefaultTables")
|
||||||
|
|
||||||
|
eventLogSQL := "create table dbo.EventLog(UUID text null, LogType VARCHAR(20) NOT NULL CHECK (LogType IN('INFO', 'WARNING', 'ERROR', 'FATAL')), Hostname varchar(256) null, BackupName varchar(256) null, Stage VARCHAR(20) NOT NULL CHECK (Stage IN('COMPRESS', 'UPLOAD', 'DELETE TMP')), RemoteStorage VARCHAR(20) NOT NULL CHECK (RemoteStorage IN('AZURE-FILE', 'AZURE-BLOB', 'NONE')), Description text null, Timestamp datetime null);"
|
||||||
|
backupSQL := "create table dbo.Backups(UUID text null, Hostname varchar(256) null, BackupName varchar(256) null, LastBackup datetime null, LocalBackup tinyint null, FilePath varchar(256) null, RemoteStorage VARCHAR(20) NOT NULL CHECK (RemoteStorage IN('AZURE-FILE', 'AZURE-BLOB', 'NONE')), RemotePath varchar(256) null, LocalPath varchar(256) null);"
|
||||||
|
|
||||||
|
db := createMSSQLConnection(mssql)
|
||||||
|
|
||||||
|
if !mssql.checkIfBackupTableExist(db) {
|
||||||
|
_, err := db.Exec(backupSQL)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !mssql.checkIfEventLogTableExist(db) {
|
||||||
|
_, err := db.Exec(eventLogSQL)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = db.Close()
|
||||||
|
}
|
||||||
func (mssql MSSQLConnector) newLogEntry(uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time) {
|
func (mssql MSSQLConnector) newLogEntry(uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time) {
|
||||||
|
logger := Logging.DetailedLogger("MS-SQL", "newLogEntry")
|
||||||
|
db := createMSSQLConnection(mssql)
|
||||||
|
|
||||||
|
hostname, _ := os.Hostname()
|
||||||
|
query := fmt.Sprintf("INSERT INTO dbo.EventLog VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", uuid.String(), logType.String(), hostname, backupName, stage.String(), storageType.String(), description, timestamp.Format("2006-01-02 15:04:05.999"))
|
||||||
|
_, err := db.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func (mssql MSSQLConnector) newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) {
|
func (mssql MSSQLConnector) newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) {
|
||||||
|
logger := Logging.DetailedLogger("MS-SQL", "newBackupEntry")
|
||||||
|
db := createMSSQLConnection(mssql)
|
||||||
|
|
||||||
|
hostname, _ := os.Hostname()
|
||||||
|
var localBackupInt uint8
|
||||||
|
if localBackup {
|
||||||
|
localBackupInt = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if mssql.checkIfBackupEntryExist(db, backupName, hostname) {
|
||||||
|
queryUpdate := fmt.Sprintf("UPDATE dbo.Backups SET Lastbackup = '%s', LocalBackup = %d, RemoteStorage = '%s', RemotePath = '%s', LocalPath = '%s' WHERE Hostname = '%s' AND BackupName = '%s'", lastBackup.Format("2006-01-02 15:04:05.999"), localBackupInt, storageType.String(), remotePath, localPath, hostname, backupName)
|
||||||
|
_, err := db.Query(queryUpdate)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
queryInsert := fmt.Sprintf("INSERT INTO dbo.Backups VALUES ('%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s')", uuid.New(), hostname, backupName, lastBackup.Format("2006-01-02 15:04:05.999"), localBackupInt, filePath, storageType.String(), remotePath, localPath)
|
||||||
|
_, err := db.Query(queryInsert)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue