forked from netbirdio/netbird
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement SQLite Store using gorm and relational approach (netbirdio#…
…1065) Restructure data handling for improved performance and flexibility. Introduce 'G'-prefixed fields to represent Gorm relations, simplifying resource management. Eliminate complexity in lookup tables for enhanced query and write speed. Enable independent operations on data structures, requiring adjustments in the Store interface and Account Manager.
- Loading branch information
Showing
44 changed files
with
1,239 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"github.com/netbirdio/netbird/management/server" | ||
"github.com/netbirdio/netbird/util" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var shortDown = "Rollback SQLite store to JSON file store. Please make a backup of the SQLite file before running this command." | ||
|
||
var downCmd = &cobra.Command{ | ||
Use: "downgrade [--datadir directory] [--log-file console]", | ||
Aliases: []string{"down"}, | ||
Short: shortDown, | ||
Long: shortDown + | ||
"\n\n" + | ||
"This command reads the content of {datadir}/store.db and migrates it to {datadir}/store.json that can be used by File store driver.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
flag.Parse() | ||
err := util.InitLog(logLevel, logFile) | ||
if err != nil { | ||
return fmt.Errorf("failed initializing log %v", err) | ||
} | ||
|
||
sqliteStorePath := path.Join(mgmtDataDir, "store.db") | ||
if _, err := os.Stat(sqliteStorePath); errors.Is(err, os.ErrNotExist) { | ||
return fmt.Errorf("%s doesn't exist, couldn't continue the operation", sqliteStorePath) | ||
} | ||
|
||
fileStorePath := path.Join(mgmtDataDir, "store.json") | ||
if _, err := os.Stat(fileStorePath); err == nil { | ||
return fmt.Errorf("%s already exists, couldn't continue the operation", fileStorePath) | ||
} | ||
|
||
sqlstore, err := server.NewSqliteStore(mgmtDataDir, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed creating file store: %s: %v", mgmtDataDir, err) | ||
} | ||
|
||
sqliteStoreAccounts := len(sqlstore.GetAllAccounts()) | ||
log.Infof("%d account will be migrated from sqlite store %s to file store %s", | ||
sqliteStoreAccounts, sqliteStorePath, fileStorePath) | ||
|
||
store, err := server.NewFilestoreFromSqliteStore(sqlstore, mgmtDataDir, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed creating file store: %s: %v", mgmtDataDir, err) | ||
} | ||
|
||
fsStoreAccounts := len(store.GetAllAccounts()) | ||
if fsStoreAccounts != sqliteStoreAccounts { | ||
return fmt.Errorf("failed to migrate accounts from sqlite to file[]. Expected accounts: %d, got: %d", | ||
sqliteStoreAccounts, fsStoreAccounts) | ||
} | ||
|
||
log.Info("Migration finished successfully") | ||
|
||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"github.com/netbirdio/netbird/management/server" | ||
"github.com/netbirdio/netbird/util" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var shortUp = "Migrate JSON file store to SQLite store. Please make a backup of the JSON file before running this command." | ||
|
||
var upCmd = &cobra.Command{ | ||
Use: "upgrade [--datadir directory] [--log-file console]", | ||
Aliases: []string{"up"}, | ||
Short: shortUp, | ||
Long: shortUp + | ||
"\n\n" + | ||
"This command reads the content of {datadir}/store.json and migrates it to {datadir}/store.db that can be used by SQLite store driver.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
flag.Parse() | ||
err := util.InitLog(logLevel, logFile) | ||
if err != nil { | ||
return fmt.Errorf("failed initializing log %v", err) | ||
} | ||
|
||
fileStorePath := path.Join(mgmtDataDir, "store.json") | ||
if _, err := os.Stat(fileStorePath); errors.Is(err, os.ErrNotExist) { | ||
return fmt.Errorf("%s doesn't exist, couldn't continue the operation", fileStorePath) | ||
} | ||
|
||
sqlStorePath := path.Join(mgmtDataDir, "store.db") | ||
if _, err := os.Stat(sqlStorePath); err == nil { | ||
return fmt.Errorf("%s already exists, couldn't continue the operation", sqlStorePath) | ||
} | ||
|
||
fstore, err := server.NewFileStore(mgmtDataDir, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed creating file store: %s: %v", mgmtDataDir, err) | ||
} | ||
|
||
fsStoreAccounts := len(fstore.GetAllAccounts()) | ||
log.Infof("%d account will be migrated from file store %s to sqlite store %s", | ||
fsStoreAccounts, fileStorePath, sqlStorePath) | ||
|
||
store, err := server.NewSqliteStoreFromFileStore(fstore, mgmtDataDir, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed creating file store: %s: %v", mgmtDataDir, err) | ||
} | ||
|
||
sqliteStoreAccounts := len(store.GetAllAccounts()) | ||
if fsStoreAccounts != sqliteStoreAccounts { | ||
return fmt.Errorf("failed to migrate accounts from file to sqlite. Expected accounts: %d, got: %d", | ||
fsStoreAccounts, sqliteStoreAccounts) | ||
} | ||
|
||
log.Info("Migration finished successfully") | ||
|
||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.