jackc / tern

The SQL Fan's Migrator
MIT License
850 stars 66 forks source link

Inject custom data when creating a `NewMigrator` #98

Closed kamikazechaser closed 1 month ago

kamikazechaser commented 1 month ago

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.

jackc commented 1 month ago

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
kamikazechaser commented 1 month ago

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