Using tern migrations in go test container with postgres

WBOY
Release: 2024-02-06 11:42:04
forward
651 people have browsed it

在带有 postgres 的 go test 容器中使用 tern 迁移

Question content

I'm trying to write an integration test for my Postgres database. To do this, I use a test container in Go. I've used tern migrations in the actual database and placed the migrations in the migrations folder. The problem is that some of my migrations use properties in the .tern.conf file because the values ​​may vary depending on the environment. I created a similar configuration file local.tern.conf for local and tried setting global variables. But for some reason when I run the migration the values ​​are not selected. Here is an example-

func SetupTestDatabase() *TestDatabase {
    // Set the TERN_CONFIG environment variable to the path of your local.tern.conf file
    if err := os.Setenv("TERN_CONFIG", "../local.tern.conf"); err != nil {
        // Handle error if setting the environment variable fails
        zap.S().Fatal("failed to set TERN_CONFIG", err)
    }

    // setup db container
    ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
    defer cancel()

    container, dbInstance, dbAddr, err := createContainer(ctx)
    if err != nil {
        zap.S().Fatal("failed to setup test", err)
    }

    conn, _ := dbInstance.Acquire(ctx)
    m, err := migrate.NewMigrator(ctx, conn.Conn(), "schema_version_table")
    if err != nil {
        zap.S().Fatal("failed to setup migrator", err)
    }

    if err := m.LoadMigrations("../migrations"); err != nil {
        zap.S().Fatal("failed to load migrations", err)
        return nil
    }

    if len(m.Migrations) == 0 {
        zap.S().Fatal("no migrations found")
        return nil
    }

    if err := m.Migrate(ctx); err != nil {
        zap.S().Fatal("failed to migrate", err)
        return nil
    }

    return &TestDatabase{
        container:  container,
        DbInstance: dbInstance,
        DbAddress:  dbAddr,
    }
}
Copy after login

This is part of my migration -

....
CREATE TABLE {{.version_table}} (
    version integer NOT NULL
);

alter table {{.version_table}}
    owner to {{.master_user}};
.....
Copy after login

This is local.tern.conf-

[database]
 host = <host>
 port = 5432
 database = <db name>
 version_table = schema_version_table

[data]
 master_user = <user>
Copy after login


Correct Answer


I guess the config file must target the random port provided by testcontainers-go, rather than Postgres' default 5432 port. You may need to dynamically generate this file using the container's Host and MappedPort methods, getting the host and random port from testcontainers-go. Please refer to https://golang.testcontainers.org/features/networking /#Get the container host

ip, _ := nginxC.Host(ctx)
port, _ := nginxC.MappedPort(ctx, "80")
_, _ = http.Get(fmt.Sprintf("http://%s:%s", ip, port.Port()))
Copy after login

Can you try it?

Another option is to use "Testcontainers Desktop" which allows defining a port for a given service (postgres) to transparently proxy any requests to that port to a random port. Please refer to https://www.php.cn/link/9a4d6e8685bd057e4f68930bd7c8ecc0

The above is the detailed content of Using tern migrations in go test container with postgres. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!