jackc / tern

The SQL Fan's Migrator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expose CLI as an importable package

codeclown opened this issue · comments

I want to embed migration logic into my application so I don't need to install tern into the container separately.

I can do that by importing github.com/jackc/tern/migrate and creating a new migrator like is done in the tern CLI:

tern/main.go

Line 411 in 7208d82

migrator, err := migrate.NewMigrator(ctx, conn, config.VersionTable)

However, I don't really need to customize that process in any way. For my purposes I'd be just copying most of func Migrate from that file.

It would be a nicer alternative if I could do this:

args := [0]string{}
tern.Migrate(cobra.Command{}, args)

In fact, my use case would be to just pass the args from my own cobra command which is powering my CLI:

func RunMigrate(cmd *cobra.Command, args []string) {
	tern.Migrate(cmd, args)
}

So I could run my-command migrate ... which would just run tern.

Would you consider supporting this and/or approving a PR?

Hmm... I see how it could be useful in some cases... But this would essentially make the entire CLI interface and all the code part of the public interface. I'd rather not commit to making that stable. It's also only useful to applications using cobra.

When I've embedded tern in an application I've usually been able to simplify the interface a bit. Also, the application probably has its own config for the database connection. So I would recommend using github.com/jackc/tern/migrate and copying whatever you need / want from func Migrate.

I understand that turning it into a stable API might not be worth it, although it is unfortunate because I don't really want to replicate the console output logic that is already present there.

It's also only useful to applications using cobra.

Well, it could be useful to more applications than just those that use cobra, with a small refactoring of moving the meaningful logic outside to functions that don't require a Cobra command as argument.

But again, I fully understand! Closing for now.