...

Source file src/github.com/cybertec-postgresql/pgwatch/v3/internal/metrics/postgres_schema.go

Documentation: github.com/cybertec-postgresql/pgwatch/v3/internal/metrics

     1  package metrics
     2  
     3  import (
     4  	"context"
     5  	_ "embed"
     6  	"fmt"
     7  
     8  	"github.com/cybertec-postgresql/pgwatch/v3/internal/db"
     9  	"github.com/cybertec-postgresql/pgwatch/v3/internal/log"
    10  	migrator "github.com/cybertec-postgresql/pgx-migrator"
    11  	"github.com/jackc/pgx/v5"
    12  )
    13  
    14  //go:embed postgres_schema.sql
    15  var sqlConfigSchema string
    16  
    17  var initSchema = func(ctx context.Context, conn db.PgxIface) (err error) {
    18  	var exists bool
    19  	if exists, err = db.DoesSchemaExist(ctx, conn, "pgwatch"); err != nil || exists {
    20  		return err
    21  	}
    22  	tx, err := conn.Begin(ctx)
    23  	if err != nil {
    24  		return err
    25  	}
    26  	defer func() { _ = tx.Rollback(ctx) }()
    27  	if _, err := tx.Exec(ctx, sqlConfigSchema); err != nil {
    28  		return err
    29  	}
    30  	if err := writeMetricsToPostgres(ctx, tx, GetDefaultMetrics()); err != nil {
    31  		return err
    32  	}
    33  	if err := tx.Commit(ctx); err != nil {
    34  		return err
    35  	}
    36  	return nil
    37  }
    38  
    39  var initMigrator = func(dmrw *dbMetricReaderWriter) (*migrator.Migrator, error) {
    40  	return migrator.New(
    41  		migrator.TableName("pgwatch.migration"),
    42  		migrator.SetNotice(func(s string) {
    43  			log.GetLogger(dmrw.ctx).Info(s)
    44  		}),
    45  		migrations(),
    46  	)
    47  }
    48  
    49  // MigrateDb upgrades database with all migrations
    50  func (dmrw *dbMetricReaderWriter) Migrate() error {
    51  	m, err := initMigrator(dmrw)
    52  	if err != nil {
    53  		return fmt.Errorf("cannot initialize migration: %w", err)
    54  	}
    55  	return m.Migrate(dmrw.ctx, dmrw.configDb)
    56  }
    57  
    58  // NeedsMigration checks if database needs migration
    59  func (dmrw *dbMetricReaderWriter) NeedsMigration() (bool, error) {
    60  	m, err := initMigrator(dmrw)
    61  	if err != nil {
    62  		return false, err
    63  	}
    64  	return m.NeedUpgrade(dmrw.ctx, dmrw.configDb)
    65  }
    66  
    67  // migrations holds function returning all updgrade migrations needed
    68  var migrations func() migrator.Option = func() migrator.Option {
    69  	return migrator.Migrations(
    70  		&migrator.Migration{
    71  			Name: "00179 Apply metrics migrations for v3",
    72  			Func: func(context.Context, pgx.Tx) error {
    73  				// "migrations" table will be created automatically
    74  				return nil
    75  			},
    76  		},
    77  
    78  		// adding new migration here, update "pgwatch"."migration" in "postgres_schema.sql"
    79  		// and "dbapi" variable in main.go!
    80  
    81  		// &migrator.Migration{
    82  		// 	Name: "000XX Short description of a migration",
    83  		// 	Func: func(ctx context.Context, tx pgx.Tx) error {
    84  		// 		return executeMigrationScript(ctx, tx, "000XX.sql")
    85  		// 	},
    86  		// },
    87  	)
    88  }
    89