第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Golang 使用 migrate with dockertest

Golang 使用 migrate with dockertest

Go
暮色呼如 2023-02-06 10:27:04
目標(biāo)是測(cè)試數(shù)據(jù)庫上的邏輯??磥韉ockertest可能對(duì)它有用,他們提供了如何設(shè)置測(cè)試的示例。但是,這些臨時(shí)數(shù)據(jù)庫是空的(沒有完成遷移)。使用dockertest時(shí)如何遷移數(shù)據(jù)庫?
查看完整描述

1 回答

?
溫溫醬

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊

不確定這是最好的方法,但我已經(jīng)連接了幾個(gè)示例,并在測(cè)試設(shè)置上通過 [golang-migrate] lib 提出了完整遷移。


下面TestMain是設(shè)置 Dockertest 與 postgres 數(shù)據(jù)庫連接的設(shè)置函數(shù)。就在m.Run()運(yùn)行測(cè)試之前,有runMigrations一個(gè)使用 golang-migrate 將所有腳本拉到本地目錄并將它們應(yīng)用到數(shù)據(jù)庫。它有效,但速度很慢。


func TestMain(m *testing.M) {

    // uses a sensible default on windows (tcp/http) and linux/osx (socket)

    pool, err := dockertest.NewPool("")

    if err != nil {

        log.Fatalf("Could not connect to docker: %s", err)

    }


    // pulls an image, creates a container based on it and runs it

    resource, err := pool.RunWithOptions(&dockertest.RunOptions{

        Repository: "postgres",

        Tag:        "11",

        Env: []string{

            "POSTGRES_PASSWORD=secret",

            "POSTGRES_USER=user_name",

            "POSTGRES_DB=dbname",

            "listen_addresses = '*'",

        },

    }, func(config *docker.HostConfig) {

        // set AutoRemove to true so that stopped container goes away by itself

        config.AutoRemove = true

        config.RestartPolicy = docker.RestartPolicy{Name: "no"}

    })

    if err != nil {

        log.Fatalf("Could not start resource: %s", err)

    }


    hostAndPort := resource.GetHostPort("5432/tcp")

    databaseUrl := fmt.Sprintf("postgres://user_name:secret@%s/dbname?sslmode=disable", hostAndPort)


    log.Println("Connecting to database on url: ", databaseUrl)


    resource.Expire(120) // Tell docker to hard kill the container in 120 seconds


    // exponential backoff-retry, because the application in the container might not be ready to accept connections yet

    pool.MaxWait = 120 * time.Second

    if err = pool.Retry(func() error {

        db, err = sql.Open("postgres", databaseUrl)

        if err != nil {

            return err

        }

        return db.Ping()

    }); err != nil {

        log.Fatalf("Could not connect to docker: %s", err)

    }


    // Migrating DB

    if err := runMigrations("../migrations", db); err != nil {

        log.Fatalf("Could not migrate db: %s", err)

    }


    //Run tests

    code := m.Run()


    // You can't defer this because os.Exit doesn't care for defer

    if err := pool.Purge(resource); err != nil {

        log.Fatalf("Could not purge resource: %s", err)

    }


    os.Exit(code)

}


func runMigrations(migrationsPath string, db *sql.DB) error {

    if migrationsPath == "" {

        return errors.New("missing migrations path")

    }

    driver, err := postgres.WithInstance(db, &postgres.Config{})

    if err != nil {

        return err

    }

    m, err := migrate.NewWithDatabaseInstance("file://"+migrationsPath, "postgres", driver)

    if err != nil {

        return err

    }

    err = m.Up()

    if err != nil && err != migrate.ErrNoChange {

        return err

    }

    return nil

}



查看完整回答
反對(duì) 回復(fù) 2023-02-06
  • 1 回答
  • 0 關(guān)注
  • 164 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)