goose icon indicating copy to clipboard operation
goose copied to clipboard

Double providers with api

Open Satont opened this issue 6 months ago • 2 comments

I'm using own written cli, and i wanna do separate providers. However, right now global golang migrations live together in goose global registered migrations. And when i first running postgres migratios it's fine, but fails on clickhouse because it's trying apply postgres go migrations.

What way i need to use goose api, for fix it?

postgres.go:

package postgres

import (
	"context"
	"database/sql"
	"log/slog"
	"os"

	"github.com/lib/pq"
	"github.com/pressly/goose/v3"
	"github.com/pterm/pterm"
	cfg "github.com/satont/twir/libs/config"
	_ "github.com/satont/twir/libs/migrations/postgres"
	"github.com/satont/twir/libs/migrations/seeds"
)

// migrationsPath: libs/migrations/postgres
func Migrate(ctx context.Context, config *cfg.Config, migrationsPath string) error {
	opts, err := pq.ParseURL(config.DatabaseUrl)
	if err != nil {
		return err
	}

	db, err := sql.Open(string(goose.DialectPostgres), opts)
	if err != nil {
		return err
	}

	if err := goose.SetDialect(string(goose.DialectPostgres)); err != nil {
		return err
	}

	provider, err := goose.NewProvider(
		goose.DialectPostgres,
		db,
		os.DirFS(migrationsPath),
		goose.WithAllowOutofOrder(true),
	)
	if err != nil {
		return err
	}

	if _, err := provider.Up(ctx); err != nil {
		pterm.Error.Println(err)
		return err
	}

	slog.SetDefault(slog.New(pterm.NewSlogHandler(&pterm.DefaultLogger)))

	if err := seeds.CreateDefaultBot(db, config); err != nil {
		return err
	}

	if err := seeds.CreateIntegrations(db, config); err != nil {
		return err
	}

	return nil
}

clichkouse.go:

package clickhouse

import (
	"context"
	"os"

	"github.com/ClickHouse/clickhouse-go/v2"
	"github.com/pressly/goose/v3"
	"github.com/pterm/pterm"
	cfg "github.com/satont/twir/libs/config"
	_ "github.com/satont/twir/libs/migrations/clickhouse"
)

// migrationsPath: libs/migrations/clickhouse
func Migrate(ctx context.Context, config *cfg.Config, migrationsPath string) error {
	dbOptions, err := clickhouse.ParseDSN(config.ClickhouseUrl)
	if err != nil {
		return err
	}

	goose.ResetGlobalMigrations()

	db := clickhouse.OpenDB(dbOptions)
	if err := goose.SetDialect(string(goose.DialectClickHouse)); err != nil {
		return err
	}

	provider, err := goose.NewProvider(
		goose.DialectClickHouse,
		db,
		os.DirFS(migrationsPath),
		goose.WithAllowOutofOrder(true),
	)
	if err != nil {
		return err
	}

	if _, err := provider.Up(ctx); err != nil {
		pterm.Error.Println(err)
		return err
	}

	return nil
}

Satont avatar Jun 22 '25 17:06 Satont

Can you pass the WithDisableGlobalRegistry option when initializing the ClickHouse provider?

mfridman avatar Jun 23 '25 12:06 mfridman

Can you pass the WithDisableGlobalRegistry option when initializing the ClickHouse provider?

Yeah it would work, but i still need to use *.go migrations. Should i write .go files myself, without init?

Satont avatar Jun 27 '25 17:06 Satont