code.vegaprotocol.io/vega@v0.79.0/blockexplorer/store/migration.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package store
    17  
    18  import (
    19  	"embed"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/logging"
    23  
    24  	"github.com/jackc/pgx/v4/stdlib"
    25  	"github.com/pkg/errors"
    26  	"github.com/pressly/goose/v3"
    27  )
    28  
    29  //go:embed migrations/*.sql
    30  var EmbedMigrations embed.FS
    31  
    32  const SQLMigrationsDir = "migrations"
    33  
    34  func MigrateToLatestSchema(log *logging.Logger, config Config) error {
    35  	goose.SetBaseFS(EmbedMigrations)
    36  	goose.SetLogger(log.Named("db migration").GooseLogger())
    37  
    38  	poolConfig, err := config.Postgres.ToPgxPoolConfig()
    39  	if err != nil {
    40  		return errors.Wrap(err, "migrating schema")
    41  	}
    42  
    43  	db := stdlib.OpenDB(*poolConfig.ConnConfig)
    44  	defer db.Close()
    45  
    46  	if err := goose.Up(db, SQLMigrationsDir); err != nil {
    47  		return fmt.Errorf("error migrating sql schema: %w", err)
    48  	}
    49  	return nil
    50  }