Skip to content

Latest commit

 

History

History
52 lines (44 loc) · 1.8 KB

gorm.md

File metadata and controls

52 lines (44 loc) · 1.8 KB

PGAdapter - gorm Connection Options

PGAdapter has Pilot Support for gorm version v1.23.8 and higher.

Limitations

Pilot Support means that it is possible to use gorm with Cloud Spanner PostgreSQL databases, but with limitations. This means that porting an existing application from PostgreSQL to Cloud Spanner will probably require code changes. See Limitations in the gorm sample directory for a full list of limitations.

Usage

First start PGAdapter:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
docker pull gcr.io/cloud-spanner-pg-adapter/pgadapter
docker run \
  -d -p 5432:5432 \
  -v ${GOOGLE_APPLICATION_CREDENTIALS}:${GOOGLE_APPLICATION_CREDENTIALS}:ro \
  -e GOOGLE_APPLICATION_CREDENTIALS \
  gcr.io/cloud-spanner-pg-adapter/pgadapter \
  -p my-project -i my-instance \
  -x

Then connect to PGAdapter and use gorm like this:

db, err := gorm.Open(postgres.Open("host=localhost port=5432 database=gorm-sample"), &gorm.Config{
    // DisableNestedTransaction will turn off the use of Savepoints if gorm
    // detects a nested transaction. Cloud Spanner does not support Savepoints,
    // so it is recommended to set this configuration option to true.
    DisableNestedTransaction: true,
    Logger: logger.Default.LogMode(logger.Error),
})
if err != nil {
  fmt.Printf("Failed to open gorm connection: %v\n", err)
}
tx := db.Begin()
user := User{
    ID:        1,
    Name:      "User Name",
    Age:       20,
}
res := tx.Create(&user)

Full Sample and Limitations

This directory contains a full sample of how to work with gorm with Cloud Spanner and PGAdapter. The sample readme file also lists the current limitations when working with gorm.