github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/migrations/migrations.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package migrations
    20  
    21  import (
    22  	"fmt"
    23  	"net/http"
    24  	"path"
    25  
    26  	migrate "github.com/rubenv/sql-migrate"
    27  	"gorm.io/gorm"
    28  
    29  	"github.com/e154/smart-home/common/logger"
    30  	"github.com/e154/smart-home/migrations"
    31  	"github.com/e154/smart-home/system/orm"
    32  )
    33  
    34  var (
    35  	log = logger.MustGetLogger("migrations")
    36  )
    37  
    38  // Migrations ...
    39  type Migrations struct {
    40  	cfg    *orm.Config
    41  	source migrate.MigrationSource
    42  	orm    *orm.Orm
    43  	db     *gorm.DB
    44  }
    45  
    46  // NewMigrations ...
    47  func NewMigrations(cfg *orm.Config,
    48  	db *gorm.DB,
    49  	orm *orm.Orm,
    50  	mConf *Config) *Migrations {
    51  
    52  	var source migrate.MigrationSource
    53  
    54  	switch mConf.Source {
    55  	case "embed":
    56  		source = &migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(migrations.F)}
    57  	case "dir":
    58  		source = &migrate.FileMigrationSource{
    59  			Dir: path.Join(mConf.Dir),
    60  		}
    61  	default:
    62  		panic(fmt.Sprintf("unknown source %s", mConf.Source))
    63  	}
    64  
    65  	return &Migrations{
    66  		cfg:    cfg,
    67  		source: source,
    68  		orm:    orm,
    69  		db:     db,
    70  	}
    71  }
    72  
    73  // Up ...
    74  func (m Migrations) Up() (err error) {
    75  
    76  	var n int
    77  	if n, err = migrate.Exec(m.orm.DB(), "postgres", m.source, migrate.Up); err != nil {
    78  		log.Error(err.Error())
    79  	}
    80  
    81  	log.Infof("Applied %d migrations!", n)
    82  
    83  	return
    84  }
    85  
    86  // Down ...
    87  func (m Migrations) Down() (err error) {
    88  
    89  	var n int
    90  	if n, err = migrate.Exec(m.orm.DB(), "postgres", m.source, migrate.Down); err != nil {
    91  		log.Error(err.Error())
    92  	}
    93  
    94  	fmt.Printf("Down %d migrations!\n", n)
    95  
    96  	return
    97  }
    98  
    99  // Purge ...
   100  func (m Migrations) Purge() (err error) {
   101  
   102  	fmt.Printf("Drop database: %s\n", m.cfg.Name)
   103  
   104  	if err = m.db.Exec(`DROP SCHEMA IF EXISTS "public" CASCADE;`).Error; err != nil {
   105  		log.Error(err.Error())
   106  		return
   107  	}
   108  	if err = m.db.Exec(`CREATE SCHEMA "public";`).Error; err != nil {
   109  		log.Error(err.Error())
   110  		return
   111  	}
   112  
   113  	_ = m.orm.Check()
   114  
   115  	err = m.Up()
   116  
   117  	return
   118  }