2021-07-30 17:46:38 +02:00
|
|
|
package server
|
|
|
|
|
2023-10-12 15:42:36 +02:00
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2024-06-13 12:39:19 +02:00
|
|
|
"errors"
|
2023-10-12 15:42:36 +02:00
|
|
|
"fmt"
|
2024-05-16 18:28:37 +02:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
2023-10-12 15:42:36 +02:00
|
|
|
"os"
|
2024-06-13 12:39:19 +02:00
|
|
|
"path"
|
2024-02-20 15:06:32 +01:00
|
|
|
"path/filepath"
|
2024-10-10 12:35:03 +02:00
|
|
|
"runtime"
|
2023-10-16 11:19:39 +02:00
|
|
|
"strings"
|
2023-10-12 15:42:36 +02:00
|
|
|
"time"
|
|
|
|
|
2023-10-19 21:14:05 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-10-10 12:35:03 +02:00
|
|
|
"gorm.io/driver/sqlite"
|
2024-05-16 18:28:37 +02:00
|
|
|
"gorm.io/gorm"
|
2023-10-19 21:14:05 +02:00
|
|
|
|
2024-10-03 15:50:35 +02:00
|
|
|
"github.com/netbirdio/netbird/dns"
|
|
|
|
|
2024-07-26 07:49:05 +02:00
|
|
|
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
|
|
|
"github.com/netbirdio/netbird/util"
|
|
|
|
|
2024-05-16 18:28:37 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/migration"
|
2023-11-28 13:45:26 +01:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2024-05-30 15:22:42 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/posture"
|
2024-05-27 08:39:18 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/testutil"
|
2024-05-16 18:28:37 +02:00
|
|
|
"github.com/netbirdio/netbird/route"
|
2023-10-12 15:42:36 +02:00
|
|
|
)
|
2023-08-18 19:23:11 +02:00
|
|
|
|
2024-09-16 15:47:03 +02:00
|
|
|
type LockingStrength string
|
|
|
|
|
|
|
|
const (
|
|
|
|
LockingStrengthUpdate LockingStrength = "UPDATE" // Strongest lock, preventing any changes by other transactions until your transaction completes.
|
|
|
|
LockingStrengthShare LockingStrength = "SHARE" // Allows reading but prevents changes by other transactions.
|
|
|
|
LockingStrengthNoKeyUpdate LockingStrength = "NO KEY UPDATE" // Similar to UPDATE but allows changes to related rows.
|
|
|
|
LockingStrengthKeyShare LockingStrength = "KEY SHARE" // Protects against changes to primary/unique keys but allows other updates.
|
|
|
|
)
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
type Store interface {
|
2024-07-03 11:33:02 +02:00
|
|
|
GetAllAccounts(ctx context.Context) []*Account
|
|
|
|
GetAccount(ctx context.Context, accountID string) (*Account, error)
|
2024-09-27 16:10:50 +02:00
|
|
|
AccountExists(ctx context.Context, lockStrength LockingStrength, id string) (bool, error)
|
|
|
|
GetAccountDomainAndCategory(ctx context.Context, lockStrength LockingStrength, accountID string) (string, string, error)
|
2024-07-03 11:33:02 +02:00
|
|
|
GetAccountByUser(ctx context.Context, userID string) (*Account, error)
|
|
|
|
GetAccountByPeerPubKey(ctx context.Context, peerKey string) (*Account, error)
|
|
|
|
GetAccountIDByPeerPubKey(ctx context.Context, peerKey string) (string, error)
|
2024-09-27 16:10:50 +02:00
|
|
|
GetAccountIDByUserID(userID string) (string, error)
|
2024-07-03 11:33:02 +02:00
|
|
|
GetAccountIDBySetupKey(ctx context.Context, peerKey string) (string, error)
|
|
|
|
GetAccountByPeerID(ctx context.Context, peerID string) (*Account, error)
|
|
|
|
GetAccountBySetupKey(ctx context.Context, setupKey string) (*Account, error) // todo use key hash later
|
|
|
|
GetAccountByPrivateDomain(ctx context.Context, domain string) (*Account, error)
|
2024-09-27 16:10:50 +02:00
|
|
|
GetAccountIDByPrivateDomain(ctx context.Context, lockStrength LockingStrength, domain string) (string, error)
|
|
|
|
GetAccountSettings(ctx context.Context, lockStrength LockingStrength, accountID string) (*Settings, error)
|
|
|
|
GetAccountDNSSettings(ctx context.Context, lockStrength LockingStrength, accountID string) (*DNSSettings, error)
|
|
|
|
SaveAccount(ctx context.Context, account *Account) error
|
|
|
|
DeleteAccount(ctx context.Context, account *Account) error
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
GetUserByTokenID(ctx context.Context, tokenID string) (*User, error)
|
2024-09-16 15:47:03 +02:00
|
|
|
GetUserByUserID(ctx context.Context, lockStrength LockingStrength, userID string) (*User, error)
|
2024-07-15 16:04:06 +02:00
|
|
|
SaveUsers(accountID string, users map[string]*User) error
|
2024-10-04 16:17:01 +02:00
|
|
|
SaveUser(ctx context.Context, lockStrength LockingStrength, user *User) error
|
2024-09-27 16:10:50 +02:00
|
|
|
SaveUserLastLogin(ctx context.Context, accountID, userID string, lastLogin time.Time) error
|
|
|
|
GetTokenIDByHashedToken(ctx context.Context, secret string) (string, error)
|
2023-03-20 16:14:55 +01:00
|
|
|
DeleteHashedPAT2TokenIDIndex(hashedToken string) error
|
|
|
|
DeleteTokenID2UserIDIndex(tokenID string) error
|
2024-09-27 16:10:50 +02:00
|
|
|
|
|
|
|
GetAccountGroups(ctx context.Context, accountID string) ([]*nbgroup.Group, error)
|
|
|
|
GetGroupByID(ctx context.Context, lockStrength LockingStrength, groupID, accountID string) (*nbgroup.Group, error)
|
|
|
|
GetGroupByName(ctx context.Context, lockStrength LockingStrength, groupName, accountID string) (*nbgroup.Group, error)
|
2024-10-04 16:17:01 +02:00
|
|
|
SaveGroups(ctx context.Context, lockStrength LockingStrength, groups []*nbgroup.Group) error
|
|
|
|
SaveGroup(ctx context.Context, lockStrength LockingStrength, group *nbgroup.Group) error
|
2024-09-27 16:10:50 +02:00
|
|
|
|
|
|
|
GetAccountPolicies(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*Policy, error)
|
|
|
|
GetPolicyByID(ctx context.Context, lockStrength LockingStrength, policyID string, accountID string) (*Policy, error)
|
|
|
|
|
|
|
|
GetPostureCheckByChecksDefinition(accountID string, checks *posture.ChecksDefinition) (*posture.Checks, error)
|
|
|
|
GetAccountPostureChecks(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*posture.Checks, error)
|
|
|
|
GetPostureChecksByID(ctx context.Context, lockStrength LockingStrength, postureCheckID string, accountID string) (*posture.Checks, error)
|
|
|
|
|
|
|
|
GetPeerLabelsInAccount(ctx context.Context, lockStrength LockingStrength, accountId string) ([]string, error)
|
|
|
|
AddPeerToAllGroup(ctx context.Context, accountID string, peerID string) error
|
|
|
|
AddPeerToGroup(ctx context.Context, accountId string, peerId string, groupID string) error
|
|
|
|
AddPeerToAccount(ctx context.Context, peer *nbpeer.Peer) error
|
|
|
|
GetPeerByPeerPubKey(ctx context.Context, lockStrength LockingStrength, peerKey string) (*nbpeer.Peer, error)
|
2024-10-04 16:17:01 +02:00
|
|
|
GetUserPeers(ctx context.Context, lockStrength LockingStrength, accountID, userID string) ([]*nbpeer.Peer, error)
|
2024-09-27 16:10:50 +02:00
|
|
|
SavePeer(ctx context.Context, accountID string, peer *nbpeer.Peer) error
|
|
|
|
SavePeerStatus(accountID, peerID string, status nbpeer.PeerStatus) error
|
|
|
|
SavePeerLocation(accountID string, peer *nbpeer.Peer) error
|
|
|
|
|
|
|
|
GetSetupKeyBySecret(ctx context.Context, lockStrength LockingStrength, key string) (*SetupKey, error)
|
|
|
|
IncrementSetupKeyUsage(ctx context.Context, setupKeyID string) error
|
|
|
|
GetAccountSetupKeys(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*SetupKey, error)
|
|
|
|
GetSetupKeyByID(ctx context.Context, lockStrength LockingStrength, setupKeyID string, accountID string) (*SetupKey, error)
|
|
|
|
|
|
|
|
GetAccountRoutes(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*route.Route, error)
|
|
|
|
GetRouteByID(ctx context.Context, lockStrength LockingStrength, routeID string, accountID string) (*route.Route, error)
|
|
|
|
|
|
|
|
GetAccountNameServerGroups(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*dns.NameServerGroup, error)
|
|
|
|
GetNameServerGroupByID(ctx context.Context, lockStrength LockingStrength, nameServerGroupID string, accountID string) (*dns.NameServerGroup, error)
|
|
|
|
|
|
|
|
GetTakenIPs(ctx context.Context, lockStrength LockingStrength, accountId string) ([]net.IP, error)
|
|
|
|
IncrementNetworkSerial(ctx context.Context, accountId string) error
|
|
|
|
GetAccountNetwork(ctx context.Context, lockStrength LockingStrength, accountId string) (*Network, error)
|
|
|
|
|
2022-10-16 13:33:46 +02:00
|
|
|
GetInstallationID() string
|
2024-07-03 11:33:02 +02:00
|
|
|
SaveInstallationID(ctx context.Context, ID string) error
|
2024-09-27 16:10:50 +02:00
|
|
|
|
2024-07-31 14:53:32 +02:00
|
|
|
// AcquireWriteLockByUID should attempt to acquire a lock for write purposes and return a function that releases the lock
|
|
|
|
AcquireWriteLockByUID(ctx context.Context, uniqueID string) func()
|
|
|
|
// AcquireReadLockByUID should attempt to acquire lock for read purposes and return a function that releases the lock
|
|
|
|
AcquireReadLockByUID(ctx context.Context, uniqueID string) func()
|
2022-11-07 17:52:23 +01:00
|
|
|
// AcquireGlobalLock should attempt to acquire a global lock and return a function that releases the lock
|
2024-07-03 11:33:02 +02:00
|
|
|
AcquireGlobalLock(ctx context.Context) func()
|
2024-09-27 16:10:50 +02:00
|
|
|
|
2022-11-08 10:46:12 +01:00
|
|
|
// Close should close the store persisting all unsaved data.
|
2024-07-03 11:33:02 +02:00
|
|
|
Close(ctx context.Context) error
|
2023-10-16 11:19:39 +02:00
|
|
|
// GetStoreEngine should return StoreEngine of the current store implementation.
|
2023-10-12 15:42:36 +02:00
|
|
|
// This is also a method of metrics.DataSource interface.
|
2023-10-16 11:19:39 +02:00
|
|
|
GetStoreEngine() StoreEngine
|
2024-09-16 15:47:03 +02:00
|
|
|
ExecuteInTransaction(ctx context.Context, f func(store Store) error) error
|
2023-10-12 15:42:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 11:19:39 +02:00
|
|
|
type StoreEngine string
|
2023-10-12 15:42:36 +02:00
|
|
|
|
|
|
|
const (
|
2024-05-16 18:28:37 +02:00
|
|
|
FileStoreEngine StoreEngine = "jsonfile"
|
|
|
|
SqliteStoreEngine StoreEngine = "sqlite"
|
|
|
|
PostgresStoreEngine StoreEngine = "postgres"
|
|
|
|
|
|
|
|
postgresDsnEnv = "NETBIRD_STORE_ENGINE_POSTGRES_DSN"
|
2023-10-12 15:42:36 +02:00
|
|
|
)
|
|
|
|
|
2023-10-16 11:19:39 +02:00
|
|
|
func getStoreEngineFromEnv() StoreEngine {
|
2024-02-20 15:06:32 +01:00
|
|
|
// NETBIRD_STORE_ENGINE supposed to be used in tests. Otherwise, rely on the config file.
|
2023-10-16 11:19:39 +02:00
|
|
|
kind, ok := os.LookupEnv("NETBIRD_STORE_ENGINE")
|
2023-10-12 15:42:36 +02:00
|
|
|
if !ok {
|
2024-02-20 15:06:32 +01:00
|
|
|
return ""
|
2023-10-12 15:42:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 11:19:39 +02:00
|
|
|
value := StoreEngine(strings.ToLower(kind))
|
2024-06-13 12:39:19 +02:00
|
|
|
if value == SqliteStoreEngine || value == PostgresStoreEngine {
|
2023-10-12 15:42:36 +02:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2024-02-20 15:06:32 +01:00
|
|
|
return SqliteStoreEngine
|
|
|
|
}
|
|
|
|
|
2024-06-20 19:45:57 +02:00
|
|
|
// getStoreEngine determines the store engine to use.
|
|
|
|
// If no engine is specified, it attempts to retrieve it from the environment.
|
|
|
|
// If still not specified, it defaults to using SQLite.
|
|
|
|
// Additionally, it handles the migration from a JSON store file to SQLite if applicable.
|
2024-07-03 11:33:02 +02:00
|
|
|
func getStoreEngine(ctx context.Context, dataDir string, kind StoreEngine) StoreEngine {
|
2023-10-12 15:42:36 +02:00
|
|
|
if kind == "" {
|
2023-10-16 11:19:39 +02:00
|
|
|
kind = getStoreEngineFromEnv()
|
2024-02-20 15:06:32 +01:00
|
|
|
if kind == "" {
|
2024-06-13 12:39:19 +02:00
|
|
|
kind = SqliteStoreEngine
|
2024-06-20 19:45:57 +02:00
|
|
|
|
|
|
|
// Migrate if it is the first run with a JSON file existing and no SQLite file present
|
|
|
|
jsonStoreFile := filepath.Join(dataDir, storeFileName)
|
|
|
|
sqliteStoreFile := filepath.Join(dataDir, storeSqliteFileName)
|
|
|
|
|
|
|
|
if util.FileExists(jsonStoreFile) && !util.FileExists(sqliteStoreFile) {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warnf("unsupported store engine specified, but found %s. Automatically migrating to SQLite.", jsonStoreFile)
|
2024-06-20 19:45:57 +02:00
|
|
|
|
|
|
|
// Attempt to migrate from JSON store to SQLite
|
2024-07-03 11:33:02 +02:00
|
|
|
if err := MigrateFileStoreToSqlite(ctx, dataDir); err != nil {
|
|
|
|
log.WithContext(ctx).Errorf("failed to migrate filestore to SQLite: %v", err)
|
2024-06-20 19:45:57 +02:00
|
|
|
kind = FileStoreEngine
|
|
|
|
}
|
|
|
|
}
|
2024-02-20 15:06:32 +01:00
|
|
|
}
|
2023-10-12 15:42:36 +02:00
|
|
|
}
|
2024-06-20 19:45:57 +02:00
|
|
|
|
2024-06-13 12:39:19 +02:00
|
|
|
return kind
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStore creates a new store based on the provided engine type, data directory, and telemetry metrics
|
2024-07-03 11:33:02 +02:00
|
|
|
func NewStore(ctx context.Context, kind StoreEngine, dataDir string, metrics telemetry.AppMetrics) (Store, error) {
|
|
|
|
kind = getStoreEngine(ctx, dataDir, kind)
|
2024-06-13 12:39:19 +02:00
|
|
|
|
|
|
|
if err := checkFileStoreEngine(kind, dataDir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-12 15:42:36 +02:00
|
|
|
switch kind {
|
2023-10-16 11:19:39 +02:00
|
|
|
case SqliteStoreEngine:
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Info("using SQLite store engine")
|
|
|
|
return NewSqliteStore(ctx, dataDir, metrics)
|
2024-05-16 18:28:37 +02:00
|
|
|
case PostgresStoreEngine:
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Info("using Postgres store engine")
|
|
|
|
return newPostgresStore(ctx, metrics)
|
2023-10-12 15:42:36 +02:00
|
|
|
default:
|
2024-06-20 19:45:57 +02:00
|
|
|
return nil, fmt.Errorf("unsupported kind of store: %s", kind)
|
2023-10-12 15:42:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 12:39:19 +02:00
|
|
|
func checkFileStoreEngine(kind StoreEngine, dataDir string) error {
|
|
|
|
if kind == FileStoreEngine {
|
|
|
|
storeFile := filepath.Join(dataDir, storeFileName)
|
|
|
|
if util.FileExists(storeFile) {
|
|
|
|
return fmt.Errorf("%s is not supported. Please refer to the documentation for migrating to SQLite: "+
|
|
|
|
"https://docs.netbird.io/selfhosted/sqlite-store#migrating-from-json-store-to-sq-lite-store", FileStoreEngine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-16 18:28:37 +02:00
|
|
|
// migrate migrates the SQLite database to the latest schema
|
2024-07-03 11:33:02 +02:00
|
|
|
func migrate(ctx context.Context, db *gorm.DB) error {
|
|
|
|
migrations := getMigrations(ctx)
|
2024-05-16 18:28:37 +02:00
|
|
|
|
|
|
|
for _, m := range migrations {
|
|
|
|
if err := m(db); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func getMigrations(ctx context.Context) []migrationFunc {
|
2024-05-16 18:28:37 +02:00
|
|
|
return []migrationFunc{
|
|
|
|
func(db *gorm.DB) error {
|
2024-07-03 11:33:02 +02:00
|
|
|
return migration.MigrateFieldFromGobToJSON[Account, net.IPNet](ctx, db, "network_net")
|
2024-05-16 18:28:37 +02:00
|
|
|
},
|
|
|
|
func(db *gorm.DB) error {
|
2024-07-03 11:33:02 +02:00
|
|
|
return migration.MigrateFieldFromGobToJSON[route.Route, netip.Prefix](ctx, db, "network")
|
2024-05-16 18:28:37 +02:00
|
|
|
},
|
|
|
|
func(db *gorm.DB) error {
|
2024-07-03 11:33:02 +02:00
|
|
|
return migration.MigrateFieldFromGobToJSON[route.Route, []string](ctx, db, "peer_groups")
|
2024-05-16 18:28:37 +02:00
|
|
|
},
|
|
|
|
func(db *gorm.DB) error {
|
2024-07-03 11:33:02 +02:00
|
|
|
return migration.MigrateNetIPFieldFromBlobToJSON[nbpeer.Peer](ctx, db, "location_connection_ip", "")
|
2024-05-16 18:28:37 +02:00
|
|
|
},
|
|
|
|
func(db *gorm.DB) error {
|
2024-07-03 11:33:02 +02:00
|
|
|
return migration.MigrateNetIPFieldFromBlobToJSON[nbpeer.Peer](ctx, db, "ip", "idx_peers_account_id_ip")
|
2024-05-16 18:28:37 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-10 12:35:03 +02:00
|
|
|
// NewTestStoreFromSQL is only used in tests. It will create a test database base of the store engine set in env.
|
|
|
|
// Optionally it can load a SQL file to the database. If the filename is empty it will return an empty database
|
|
|
|
func NewTestStoreFromSQL(ctx context.Context, filename string, dataDir string) (Store, func(), error) {
|
2024-03-15 10:50:02 +01:00
|
|
|
kind := getStoreEngineFromEnv()
|
|
|
|
if kind == "" {
|
2024-06-13 12:39:19 +02:00
|
|
|
kind = SqliteStoreEngine
|
2024-03-15 10:50:02 +01:00
|
|
|
}
|
|
|
|
|
2024-10-10 12:35:03 +02:00
|
|
|
storeStr := fmt.Sprintf("%s?cache=shared", storeSqliteFileName)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Vo avoid `The process cannot access the file because it is being used by another process` on Windows
|
|
|
|
storeStr = storeSqliteFileName
|
|
|
|
}
|
|
|
|
|
|
|
|
file := filepath.Join(dataDir, storeStr)
|
|
|
|
db, err := gorm.Open(sqlite.Open(file), getGormConfig())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2024-10-03 15:50:35 +02:00
|
|
|
|
2024-10-10 12:35:03 +02:00
|
|
|
if filename != "" {
|
|
|
|
err = loadSQL(db, filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to load SQL file: %v", err)
|
2024-10-03 15:50:35 +02:00
|
|
|
}
|
|
|
|
}
|
2024-10-10 12:35:03 +02:00
|
|
|
|
|
|
|
store, err := NewSqlStore(ctx, db, SqliteStoreEngine, nil)
|
2024-10-03 15:50:35 +02:00
|
|
|
if err != nil {
|
2024-10-10 12:35:03 +02:00
|
|
|
return nil, nil, fmt.Errorf("failed to create test store: %v", err)
|
|
|
|
}
|
|
|
|
cleanUp := func() {
|
|
|
|
store.Close(ctx)
|
2024-10-03 15:50:35 +02:00
|
|
|
}
|
2024-06-13 12:39:19 +02:00
|
|
|
|
|
|
|
if kind == PostgresStoreEngine {
|
2024-05-27 08:39:18 +02:00
|
|
|
cleanUp, err = testutil.CreatePGDB()
|
2024-05-16 18:28:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dsn, ok := os.LookupEnv(postgresDsnEnv)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("%s is not set", postgresDsnEnv)
|
|
|
|
}
|
|
|
|
|
2024-10-03 15:50:35 +02:00
|
|
|
store, err = NewPostgresqlStoreFromSqlStore(ctx, store, dsn, nil)
|
2024-05-16 18:28:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2024-06-13 12:39:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return store, cleanUp, nil
|
|
|
|
}
|
|
|
|
|
2024-10-10 12:35:03 +02:00
|
|
|
func loadSQL(db *gorm.DB, filepath string) error {
|
|
|
|
sqlContent, err := os.ReadFile(filepath)
|
2024-10-03 15:50:35 +02:00
|
|
|
if err != nil {
|
2024-10-10 12:35:03 +02:00
|
|
|
return err
|
2024-10-03 15:50:35 +02:00
|
|
|
}
|
|
|
|
|
2024-10-10 12:35:03 +02:00
|
|
|
queries := strings.Split(string(sqlContent), ";")
|
|
|
|
|
|
|
|
for _, query := range queries {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if query != "" {
|
|
|
|
err := db.Exec(query).Error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-10-03 15:50:35 +02:00
|
|
|
}
|
|
|
|
|
2024-10-10 12:35:03 +02:00
|
|
|
return nil
|
2024-10-03 15:50:35 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 12:39:19 +02:00
|
|
|
// MigrateFileStoreToSqlite migrates the file store to the SQLite store.
|
2024-07-03 11:33:02 +02:00
|
|
|
func MigrateFileStoreToSqlite(ctx context.Context, dataDir string) error {
|
2024-06-13 12:39:19 +02:00
|
|
|
fileStorePath := path.Join(dataDir, storeFileName)
|
|
|
|
if _, err := os.Stat(fileStorePath); errors.Is(err, os.ErrNotExist) {
|
|
|
|
return fmt.Errorf("%s doesn't exist, couldn't continue the operation", fileStorePath)
|
2024-05-16 18:28:37 +02:00
|
|
|
}
|
2024-06-13 12:39:19 +02:00
|
|
|
|
|
|
|
sqlStorePath := path.Join(dataDir, storeSqliteFileName)
|
|
|
|
if _, err := os.Stat(sqlStorePath); err == nil {
|
|
|
|
return fmt.Errorf("%s already exists, couldn't continue the operation", sqlStorePath)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
fstore, err := NewFileStore(ctx, dataDir, nil)
|
2024-06-13 12:39:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed creating file store: %s: %v", dataDir, err)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
fsStoreAccounts := len(fstore.GetAllAccounts(ctx))
|
|
|
|
log.WithContext(ctx).Infof("%d account will be migrated from file store %s to sqlite store %s",
|
2024-06-13 12:39:19 +02:00
|
|
|
fsStoreAccounts, fileStorePath, sqlStorePath)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
store, err := NewSqliteStoreFromFileStore(ctx, fstore, dataDir, nil)
|
2024-06-13 12:39:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed creating file store: %s: %v", dataDir, err)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
sqliteStoreAccounts := len(store.GetAllAccounts(ctx))
|
2024-06-13 12:39:19 +02:00
|
|
|
if fsStoreAccounts != sqliteStoreAccounts {
|
|
|
|
return fmt.Errorf("failed to migrate accounts from file to sqlite. Expected accounts: %d, got: %d",
|
|
|
|
fsStoreAccounts, sqliteStoreAccounts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-05-16 18:28:37 +02:00
|
|
|
}
|