jackc / tern

The SQL Fan's Migrator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inject custom data when creating a `NewMigrator`

kamikazechaser opened this issue · comments

Right now, Data available to use in migrations is only picked up from the migration's files path (when executing the template). The Data field in the Migrator struct is initialized empty (https://github.com/jackc/tern/blob/master/migrate/migrate.go#L102).

It would be nice to have a way to populate this data from external data sources first and let the other populators (template.Execute) override this data later on.

The initial Data could either be passed with MigratorOptions or the library could support (and load) functional options when calling NewMigrator.

This is for embedded usage of tern, not the CLI right?

What's wrong with setting data after calling the constructor function? That's what the tern CLI does. e.g.

	migrator, err := migrate.NewMigrator(ctx, conn, config.VersionTable)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error initializing migrator:\n  %v\n", err)
		os.Exit(1)
	}
	migrator.Data = config.Data

Yeah the embedded migrator. Ah right, yeah it is exported. So that will work as well.