spannerpg

package
v1.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

README

PostgreSQL

This directory contains the Spanner PostgreSQL gorm dialect. This dialect is an extension of the standard gorm PostgreSQL dialect. The dialect uses the Spanner database/sql driver to connect to Spanner. You do not need PGAdapter to use this dialect.

Connecting

Call the New function in the github.com/googleapis/go-gorm-spanner/postgresql package to create a gorm connection to a Spanner PostgreSQL database. Specify a Spanner database name as the connection string, e.g. projects/my-project/instances/my-instance/databases/my-database.

import (
	"fmt"

	spannerpg "github.com/googleapis/go-gorm-spanner/postgresql"
	_ "github.com/googleapis/go-sql-spanner"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

func helloWorld(projectId, instanceId, databaseId string) error {
	db, err := gorm.Open(spannerpg.New(postgres.Config{
		DSN: fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId),
	}), &gorm.Config{})
	if err != nil {
		return fmt.Errorf("failed to open database connection: %v\n", err)
	}
	var msg string
	if err := db.Raw("SELECT $1::varchar as greeting", "Hello World from Spanner PostgreSQL!").Scan(&msg).Error; err != nil {
		return fmt.Errorf("failed to execute query: %v", err)
	}
	fmt.Println(msg)

	return nil
}

Samples

See the samples directory for a list of ready-to-run samples that show how to use Spanner PostgreSQL features with gorm.

Migrations

The Spanner PostgreSQL gorm dialect uses a custom migrator that overrides some of the defaults in the standard PostgreSQL gorm migrator, as Spanner does not support the full DDL dialect of PostgreSQL. This migrator can be used for development purposes.

It is recommended to use a schema management tool for changes to test and production databases. This ensures that you have full control over the schema changes that are made, and makes it easier to use Spanner best-practices for schema management, such as grouping as many DDL statements into one batch as possible.

Supported schema management tools include:

Documentation

Index

Constants

View Source
const (
	// ClauseOnConflict for clause.ClauseBuilder ON CONFLICT key
	ClauseOnConflict = "ON CONFLICT"
	// ClauseLimit for clause.ClauseBuilder LIMIT key
	ClauseLimit = "LIMIT"
)

Variables

This section is empty.

Functions

func AutoOrderBy

func AutoOrderBy(db *gorm.DB)

func BeforeUpdate

func BeforeUpdate(db *gorm.DB)

func New

func New(config postgres.Config) gorm.Dialector

func NewWithSpannerConfig

func NewWithSpannerConfig(config postgres.Config, spannerConfig SpannerConfig) gorm.Dialector

func Open

func Open(dsn string) gorm.Dialector

Types

type Dialector

type Dialector struct {
	postgres.Dialector
	SpannerConfig SpannerConfig
}

func (Dialector) ClauseBuilders

func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder

func (Dialector) DataTypeOf

func (dialector Dialector) DataTypeOf(field *schema.Field) string

func (Dialector) Initialize

func (dialector Dialector) Initialize(db *gorm.DB) (err error)

func (Dialector) Migrator

func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator

func (Dialector) Name

func (dialector Dialector) Name() string

type SpannerConfig

type SpannerConfig struct {
	// DisableAutoMigrateBatching turns off DDL batching for AutoMigrate calls.
	// Spanner PostgreSQL by default uses DDL batching when AutoMigrate is called, as
	// executing multiple DDL statements in a single batch is a lot more efficient
	// than executing each statement separately. You should only use this option
	// if you are experiencing problems with the automatic batching of DDL
	// statements when calling AutoMigrate.
	DisableAutoMigrateBatching bool

	// AutoOrderByPk automatically adds an ORDER BY <pk> to all queries.
	// This flag is primarily intended for testing, as most gorm tests assume that queries will return query results
	// in primary key order, even when there is no ORDER BY clause in the query. Spanner does not guarantee this.
	AutoOrderByPk bool
	// AutoAddPrimaryKey automatically adds a 'generated_id serial' column to tables that are created without a
	// primary key. This flag is primarily intended for testing, as some gorm tests assumes that databases support
	// tables without a primary key. Spanner does not support this.
	AutoAddPrimaryKey bool
}

SpannerConfig contains custom configuration options for Spanner PostgreSQL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL