lxd/daemon.go
func initializeDbObject(d *Daemon) (*db.Dump, error) {
	logger.Info("Initializing local database")
	// Rename the old database name if needed.
	if shared.PathExists(d.os.LegacyLocalDatabasePath()) {
		if shared.PathExists(d.os.LocalDatabasePath()) {
			return nil, fmt.Errorf("Both legacy and new local database files exists")
		}
		logger.Info("Renaming local database file from lxd.db to database/local.db")
		err := os.Rename(d.os.LegacyLocalDatabasePath(), d.os.LocalDatabasePath())
		if err != nil {
			return nil, errors.Wrap(err, "Failed to rename legacy local database file")
		}
	}

	// NOTE: we use the legacyPatches parameter to run a few
	// legacy non-db updates that were in place before the
	// patches mechanism was introduced in lxd/patches.go. The
	// rest of non-db patches will be applied separately via
	// patchesApplyAll. See PR #3322 for more details.
	legacy := map[int]*db.LegacyPatch{}
	for i, patch := range legacyPatches {
		legacy[i] = &db.LegacyPatch{
			Hook: func(node *sql.DB) error {
				// FIXME: Use the low-level *node* SQL db as backend for both the
				//        db.Node and db.Cluster objects, since at this point we
				//        haven't migrated the data to the cluster database yet.
				cluster := d.cluster
				defer func() {
					d.cluster = cluster
				}()
				d.db = db.ForLegacyPatches(node)
				d.cluster = db.ForLocalInspection(node)
				return patch(d)
			},
		}
	}
	for _, i := range legacyPatchesNeedingDB {
		legacy[i].NeedsDB = true
	}

	// Hook to run when the local database is created from scratch. It will
	// create the default profile and mark all patches as applied.
	freshHook := func(db *db.Node) error {
		for _, patchName := range patchesGetNames() {
			err := db.PatchesMarkApplied(patchName)
			if err != nil {
				return err
			}
		}
		return nil
	}
	var err error
	var dump *db.Dump
	d.db, dump, err = db.OpenNode(filepath.Join(d.os.VarDir, "database"), freshHook, legacy)
	if err != nil {
		return nil, fmt.Errorf("Error creating database: %s", err)
	}

	return dump, nil
}
func initializeDbObject(d *Daemon) (*db.Dump, error) {
	logger.Info("Initializing local database")
	// Rename the old database name if needed.
	if shared.PathExists(d.os.LegacyLocalDatabasePath()) {
		if shared.PathExists(d.os.LocalDatabasePath()) {
			return nil, fmt.Errorf("Both legacy and new local database files exists")
		}
		logger.Info("Renaming local database file from lxd.db to database/local.db")
		err := os.Rename(d.os.LegacyLocalDatabasePath(), d.os.LocalDatabasePath())
		if err != nil {
			return nil, errors.Wrap(err, "Failed to rename legacy local database file")
		}
	}

	// NOTE: we use the legacyPatches parameter to run a few
	// legacy non-db updates that were in place before the
	// patches mechanism was introduced in lxd/patches.go. The
	// rest of non-db patches will be applied separately via
	// patchesApplyAll. See PR #3322 for more details.
	legacy := map[int]*db.LegacyPatch{}
	for i, patch := range legacyPatches {
		legacy[i] = &db.LegacyPatch{
			Hook: func(node *sql.DB) error {
				// FIXME: Use the low-level *node* SQL db as backend for both the
				//        db.Node and db.Cluster objects, since at this point we
				//        haven't migrated the data to the cluster database yet.
				cluster := d.cluster
				defer func() {
					d.cluster = cluster
				}()
				d.db = db.ForLegacyPatches(node)
				d.cluster = db.ForLocalInspection(node)
				return patch(d)
			},
		}
	}
	for _, i := range legacyPatchesNeedingDB {
		legacy[i].NeedsDB = true
	}

	// Hook to run when the local database is created from scratch. It will
	// create the default profile and mark all patches as applied.
	freshHook := func(db *db.Node) error {
		for _, patchName := range patchesGetNames() {
			err := db.PatchesMarkApplied(patchName)
			if err != nil {
				return err
			}
		}
		return nil
	}
	var err error
	var dump *db.Dump
	d.db, dump, err = db.OpenNode(filepath.Join(d.os.VarDir, "database"), freshHook, legacy)
	if err != nil {
		return nil, fmt.Errorf("Error creating database: %s", err)
	}

	return dump, nil
}
func initializeDbObject ( d * Daemon ) ( * db . Dump , error ) { logger . Info ( " " ) // Rename the old database name if needed. if shared . PathExists ( d . os . LegacyLocalDatabasePath ( ) ) { if shared . PathExists ( d . os . LocalDatabasePath ( ) ) { return nil , fmt . Errorf ( " " ) } logger . Info ( " " ) err := os . Rename ( d . os . LegacyLocalDatabasePath ( ) , d . os . LocalDatabasePath ( ) ) if err != nil { return nil , errors . Wrap ( err , " " ) } } // NOTE: we use the legacyPatches parameter to run a few // legacy non-db updates that were in place before the // patches mechanism was introduced in lxd/patches.go. The // rest of non-db patches will be applied separately via // patchesApplyAll. See PR #3322 for more details. legacy := map [ int ] * db . LegacyPatch { } for i , patch := range legacyPatches { legacy [ i ] = & db . LegacyPatch { Hook : func ( node * sql . DB ) error { // FIXME: Use the low-level *node* SQL db as backend for both the //        db.Node and db.Cluster objects, since at this point we //        haven't migrated the data to the cluster database yet. cluster := d . cluster defer func ( ) { d . cluster = cluster } ( ) d . db = db . ForLegacyPatches ( node ) d . cluster = db . ForLocalInspection ( node ) return patch ( d ) } , } } for _ , i := range legacyPatchesNeedingDB { legacy [ i ] . NeedsDB = true } // Hook to run when the local database is created from scratch. It will // create the default profile and mark all patches as applied. freshHook := func ( db * db . Node ) error { for _ , patchName := range patchesGetNames ( ) { err := db . PatchesMarkApplied ( patchName ) if err != nil { return err } } return nil } var err error var dump * db . Dump d . db , dump , err = db . OpenNode ( filepath . Join ( d . os . VarDir , " " ) , freshHook , legacy ) if err != nil { return nil , fmt . Errorf ( " " , err ) } return dump , nil }
// Create a database connection and perform any updates needed.
Create a database connection and perform any updates needed .