1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00
documize/core/database/scripts.go

92 lines
2.2 KiB
Go
Raw Normal View History

// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package database
import (
2021-08-18 19:39:51 -04:00
"embed"
"fmt"
"sort"
2021-08-18 19:39:51 -04:00
"github.com/documize/community/core/asset"
"github.com/documize/community/core/env"
)
// Scripts holds all .SQL files for all supported database providers.
type Scripts struct {
MySQL []Script
PostgreSQL []Script
SQLServer []Script
}
// Script holds SQL script and it's associated version number.
type Script struct {
Version int
Script []byte
}
// LoadScripts returns .SQL scripts for supported database providers.
2021-08-18 19:39:51 -04:00
func LoadScripts(runtime *env.Runtime) (s Scripts, err error) {
// MySQL
2021-08-18 19:39:51 -04:00
s.MySQL, err = loadFiles(runtime.Assets, "scripts/mysql")
if err != nil {
return
}
// PostgreSQL
2021-08-18 19:39:51 -04:00
s.PostgreSQL, err = loadFiles(runtime.Assets, "scripts/postgresql")
if err != nil {
return
}
2019-03-26 08:51:02 -04:00
// PostgreSQL
2021-08-18 19:39:51 -04:00
s.SQLServer, err = loadFiles(runtime.Assets, "scripts/sqlserver")
2019-03-26 08:51:02 -04:00
if err != nil {
return
}
return s, nil
}
// SpecificScripts returns SQL scripts for current databasse provider.
func SpecificScripts(runtime *env.Runtime, all Scripts) (s []Script) {
switch runtime.StoreProvider.Type() {
case env.StoreTypeMySQL, env.StoreTypeMariaDB, env.StoreTypePercona:
return all.MySQL
case env.StoreTypePostgreSQL:
return all.PostgreSQL
2019-03-26 08:51:02 -04:00
case env.StoreTypeSQLServer:
return all.SQLServer
}
return
}
// loadFiles returns all SQL scripts in specified folder as [][]byte.
2021-08-18 19:39:51 -04:00
func loadFiles(fs embed.FS, path string) (b []Script, err error) {
scripts, err := asset.FetchStaticDir(fs, path)
if err != nil {
return
}
2021-08-18 19:39:51 -04:00
sort.Strings(scripts)
2021-08-18 19:39:51 -04:00
for i := range scripts {
filename := scripts[i]
sqlfile, _, err := asset.FetchStatic(fs, fmt.Sprintf("%s/%s", path, filename))
if err != nil {
2021-08-18 19:39:51 -04:00
return b, err
}
2021-08-18 19:39:51 -04:00
b = append(b, Script{Version: extractVersionNumber(filename), Script: []byte(sqlfile)})
}
return b, nil
}