mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-12 08:04:25 +08:00
feat(go): store new tables in the same place.
1. Add _v1 suffix to new tables. 2. Remove archival code.
This commit is contained in:
parent
bb62e99027
commit
5645f550a9
8 changed files with 28 additions and 115 deletions
|
@ -31,7 +31,7 @@ func (s *sqliteConnector) connect() (*gorm.DB, error) {
|
|||
|
||||
dbFilePath := filepath.Join("data", "netmaker.db")
|
||||
|
||||
// ensure netmaker_v1.db exists.
|
||||
// ensure netmaker.db exists.
|
||||
_, err = os.Stat(dbFilePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
|
|
|
@ -3,14 +3,10 @@ package migrate
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gravitl/netmaker/database"
|
||||
"github.com/gravitl/netmaker/db"
|
||||
"github.com/gravitl/netmaker/schema"
|
||||
"github.com/gravitl/netmaker/servercfg"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// ToSQLSchema migrates the data from key-value
|
||||
|
@ -18,15 +14,6 @@ import (
|
|||
//
|
||||
// This function archives the old data and does not
|
||||
// delete it.
|
||||
//
|
||||
// Based on the db server, the archival is done in the
|
||||
// following way:
|
||||
//
|
||||
// 1. Sqlite: Moves the old data to a
|
||||
// netmaker_archive.db file.
|
||||
//
|
||||
// 2. Postgres: Moves the data to a netmaker_archive
|
||||
// schema within the same database.
|
||||
func ToSQLSchema() error {
|
||||
// initialize sql schema db.
|
||||
err := db.InitializeDB(schema.ListModels()...)
|
||||
|
@ -34,17 +21,10 @@ func ToSQLSchema() error {
|
|||
return err
|
||||
}
|
||||
|
||||
defer db.CloseDB()
|
||||
|
||||
// migrate, if not done already.
|
||||
err = migrate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// archive key-value schema db, if not done already.
|
||||
// ignore errors.
|
||||
_ = archive()
|
||||
|
||||
return nil
|
||||
return migrate()
|
||||
}
|
||||
|
||||
func migrate() error {
|
||||
|
@ -90,94 +70,3 @@ func migrate() error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func archive() error {
|
||||
dbServer := servercfg.GetDB()
|
||||
if dbServer != "sqlite" && dbServer != "postgres" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// begin a new transaction.
|
||||
dbctx := db.BeginTx(context.TODO())
|
||||
commit := false
|
||||
defer func() {
|
||||
if commit {
|
||||
db.FromContext(dbctx).Commit()
|
||||
} else {
|
||||
db.FromContext(dbctx).Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// check if key-value schema db archived already.
|
||||
archivalJob := &schema.Job{
|
||||
ID: "archival-v1.0.0",
|
||||
}
|
||||
err := archivalJob.Get(dbctx)
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
// archive.
|
||||
switch dbServer {
|
||||
case "sqlite":
|
||||
err = sqliteArchiveOldData()
|
||||
default:
|
||||
err = pgArchiveOldData()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// mark archival job completed.
|
||||
err = archivalJob.Create(dbctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
commit = true
|
||||
} else {
|
||||
// remove the residual
|
||||
if dbServer == "sqlite" {
|
||||
_ = os.Remove(filepath.Join("data", "netmaker.db"))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func sqliteArchiveOldData() error {
|
||||
oldDBFilePath := filepath.Join("data", "netmaker.db")
|
||||
archiveDBFilePath := filepath.Join("data", "netmaker_archive.db")
|
||||
|
||||
// check if netmaker_archive.db exist.
|
||||
_, err := os.Stat(archiveDBFilePath)
|
||||
if err == nil {
|
||||
return nil
|
||||
} else if !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
// rename old db file to netmaker_archive.db.
|
||||
return os.Rename(oldDBFilePath, archiveDBFilePath)
|
||||
}
|
||||
|
||||
func pgArchiveOldData() error {
|
||||
_, err := database.PGDB.Exec("CREATE SCHEMA IF NOT EXISTS netmaker_archive")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, table := range database.Tables {
|
||||
_, err := database.PGDB.Exec(
|
||||
fmt.Sprintf(
|
||||
"ALTER TABLE public.%s SET SCHEMA netmaker_archive",
|
||||
table,
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -30,6 +30,10 @@ type PolicyGroupTag struct {
|
|||
Tag string
|
||||
}
|
||||
|
||||
func (a *ACL) TableName() string {
|
||||
return "acls_v1"
|
||||
}
|
||||
|
||||
func (a *ACL) Create(ctx context.Context) error {
|
||||
return db.FromContext(ctx).Model(&ACL{}).Create(a).Error
|
||||
}
|
||||
|
|
|
@ -48,6 +48,10 @@ type Interface struct {
|
|||
Address string
|
||||
}
|
||||
|
||||
func (h *Host) TableName() string {
|
||||
return "hosts_v1"
|
||||
}
|
||||
|
||||
func (h *Host) Create(ctx context.Context) error {
|
||||
return db.FromContext(ctx).Model(&Host{}).Create(h).Error
|
||||
}
|
||||
|
|
|
@ -20,6 +20,10 @@ type Job struct {
|
|||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (j *Job) TableName() string {
|
||||
return "jobs_v1"
|
||||
}
|
||||
|
||||
// Create creates a job record in the jobs table.
|
||||
func (j *Job) Create(ctx context.Context) error {
|
||||
return db.FromContext(ctx).Model(&Job{}).Create(j).Error
|
||||
|
|
|
@ -37,6 +37,10 @@ type Network struct {
|
|||
FailOverNode *Node `gorm:"foreignKey:FailOverNodeID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
|
||||
}
|
||||
|
||||
func (n *Network) TableName() string {
|
||||
return "networks_v1"
|
||||
}
|
||||
|
||||
func (n *Network) Create(ctx context.Context) error {
|
||||
return db.FromContext(ctx).Model(&Network{}).Create(n).Error
|
||||
}
|
||||
|
|
|
@ -11,6 +11,10 @@ type NetworkACL struct {
|
|||
Access datatypes.JSONType[map[string]map[string]byte]
|
||||
}
|
||||
|
||||
func (n *NetworkACL) TableName() string {
|
||||
return "network_acls_v1"
|
||||
}
|
||||
|
||||
func (n *NetworkACL) Create(ctx context.Context) error {
|
||||
if n.Access.Data() == nil {
|
||||
n.Access = datatypes.NewJSONType(map[string]map[string]byte{})
|
||||
|
|
|
@ -107,6 +107,10 @@ type RangeWithMetric struct {
|
|||
Metric uint32 `json:"metric"`
|
||||
}
|
||||
|
||||
func (n *Node) TableName() string {
|
||||
return "nodes_v1"
|
||||
}
|
||||
|
||||
func (n *Node) Create(ctx context.Context) error {
|
||||
return db.FromContext(ctx).Model(&Node{}).Create(n).Error
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue