snowflake

package module
v0.0.0-...-5fe6fa5 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: MIT Imports: 13 Imported by: 0

README

gorm-snowflake

Snowflake driver for gorm

Snowflake Features

Notable Snowflake (SF) features that affect decisions in this driver

  • Use of quotes in SF enforces case-sensitivity which requires string conditions to match. Right now, we are removing all quotes in the internals to make the driver case-insensitive and only uppercase when working with internal tables (INFORMATION_SCHEMA)
  • SF does not support INDEX, it does micro-partitioning automatically in all tables for optimizations. Therefore all Index related functions are nil-returned.
  • Transactions in SF do not support SAVEPOINT (https://docs.snowflake.com/en/sql-reference/transactions.html)
  • GORM rely on being able to query back inserted rows in every transaction in order to get default values back. There is no easy way to do this ala SQL Server (OUTPUT INSERTED) or Postgres (RETURNING). Instead, we automatically turn on SF CHANGE_TRACKING feature on for all tables. This allows us to run CHANGES query on the table after running any DML. However due to non-deterministic nature of return from MERGE, it doesn't support updates.
  • The SELECT...CHANGES feature of SF does not return unchanged rows from MERGE statement, therefore we can only rely on the APPEND_ONLY option and only support returning fields from inserted rows in the same order.
  • SF does not enforce any constraint other than NOT NULL. This driver expect all tests and features related to enforcing constraint to be disabled. (https://docs.snowflake.com/en/user-guide/table-considerations.html#referential-integrity-constraints)

How To

How to use this project

package main

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/snowflakedb/gosnowflake"
	snowflake "github.com/omixen/gorm-snowflake"
	"github.com/tillfinancial/goutil/errorss"
	"gorm.io/gorm"
)

type User struct {
	ID        int64
	FirstName string
	LastName  string
	Status    string
	CreatedAt time.Time
	UpdatedAt time.Time
}

// Optional if you specify the Schema in the Config
func (r *User) TableName() string {
	return "MY_SCHEMA.USERS"
}

func main() {

	config := gosnowflake.Config{
		Account:   "12345678.us-east-1",
		User:      "<SNOWFLAKE USER>",
		Password:  "<PASSWORD>",
		Database:  "MY_DATABASE",
		Schema:    "MY_SCHEMA",    // Optional if your models have a TableName method
		Warehouse: "MY_WAREHOUSE", // Optional
	}

	connStr, err := gosnowflake.DSN(&config)
	if err != nil {
		panic(err)
	}

	db, err := gorm.Open(snowflake.Open(connStr), &gorm.Config{
		NamingStrategy: snowflake.NewNamingStrategy(),
	})
	if err != nil {
		panic(err)
	}

	as := []User{}
	tx := db.Where("status = ?", "active").Find(&as)
	if tx.Error != nil {
		fmt.Println(tx.Error)
	}

	bytes, err := json.MarshalIndent(as, "", "\t")
	if err !nil {
		panic(err)
	}
	fmt.Print(string(bytes))
}

Documentation

Index

Constants

View Source
const (
	SnowflakeDriverName = "snowflake"
)

Variables

This section is empty.

Functions

func Create

func Create(db *gorm.DB)

func MergeCreate

func MergeCreate(db *gorm.DB, onConflict clause.OnConflict, values clause.Values)

func New

func New(config Config) gorm.Dialector

func NewNamingStrategy

func NewNamingStrategy() schema.Namer

NewNamingStrategy create new instance of snowflake naming strat

func Open

func Open(dsn string) gorm.Dialector

Types

type Config

type Config struct {
	DriverName string
	DSN        string
	Conn       gorm.ConnPool
}

type Dialector

type Dialector struct {
	*Config
}

func (Dialector) BindVarTo

func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{})

func (Dialector) ClauseBuilders

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

func (Dialector) DataTypeOf

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

func (Dialector) DefaultValueOf

func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression

func (Dialector) Explain

func (dialector Dialector) Explain(sql string, vars ...interface{}) 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

func (Dialector) QuoteTo

func (dialector Dialector) QuoteTo(writer clause.Writer, str string)

no quotes, quotes cause everything needing quotes

func (Dialector) RollbackTo

func (dialectopr Dialector) RollbackTo(tx *gorm.DB, name string) error

func (Dialector) SavePoint

func (dialectopr Dialector) SavePoint(tx *gorm.DB, name string) error

no support for savepoint

type Migrator

type Migrator struct {
	migrator.Migrator
}

func (Migrator) AlterColumn

func (m Migrator) AlterColumn(value interface{}, field string) error

AlterColumn no change

func (Migrator) AutoMigrate

func (m Migrator) AutoMigrate(values ...interface{}) error

AutoMigrate remove index

func (Migrator) CreateConstraint

func (m Migrator) CreateConstraint(value interface{}, name string) error

CreateConstraint no change

func (Migrator) CreateIndex

func (m Migrator) CreateIndex(value interface{}, name string) error

CreateIndex return nil, SF does not support Index

func (Migrator) CreateTable

func (m Migrator) CreateTable(values ...interface{}) error

CreateTable modified - include CHANGE_TRACKING=true, for getting output back, may be removed once it can globally supported with table options - remove index (unsupported)

func (Migrator) CurrentDatabase

func (m Migrator) CurrentDatabase() (name string)

CurrentDatabase SF flavor

func (Migrator) DropConstraint

func (m Migrator) DropConstraint(value interface{}, name string) error

DropConstraint no change

func (Migrator) DropIndex

func (m Migrator) DropIndex(value interface{}, name string) error

DropIndex return nil, SF does not support Index

func (Migrator) DropTable

func (m Migrator) DropTable(values ...interface{}) error

DropTable no change

func (Migrator) FullDataTypeOf

func (m Migrator) FullDataTypeOf(field *schema.Field) (expr clause.Expr)

FullDataTypeOf no change

func (Migrator) GuessConstraintAndTable

func (m Migrator) GuessConstraintAndTable(stmt *gorm.Statement, name string) (_ *schema.Constraint, _ *schema.CheckConstraint, table string)

GuessConstraintAndTable no change

func (Migrator) HasColumn

func (m Migrator) HasColumn(value interface{}, field string) bool

HasColumn modified for SF information schema structure

func (Migrator) HasConstraint

func (m Migrator) HasConstraint(value interface{}, name string) bool

HasConstraint SF flavor

func (Migrator) HasIndex

func (m Migrator) HasIndex(value interface{}, name string) bool

HasIndex return true to satisfy unit tests

func (Migrator) HasTable

func (m Migrator) HasTable(value interface{}) bool

HasTable modified for snowflake information_schema structure and convention (uppercased)

func (Migrator) RenameColumn

func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error

RenameColumn not supported

func (Migrator) RenameIndex

func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error

RenameIndex return nil, SF does not support Index

func (Migrator) RenameTable

func (m Migrator) RenameTable(oldName, newName interface{}) error

RenameTable no change

type NamingStrategy

type NamingStrategy struct {
	// contains filtered or unexported fields
}

NamingStrategy for snowflake (always uppercase)

func (NamingStrategy) CheckerName

func (sns NamingStrategy) CheckerName(table, column string) string

CheckerName snowflake edition

func (NamingStrategy) ColumnName

func (sns NamingStrategy) ColumnName(table, column string) string

ColumnName snowflake edition

func (NamingStrategy) IndexName

func (sns NamingStrategy) IndexName(table, column string) string

IndexName snowflake edition

func (NamingStrategy) JoinTableName

func (sns NamingStrategy) JoinTableName(joinTable string) string

JoinTableName snowflake edition

func (NamingStrategy) RelationshipFKName

func (sns NamingStrategy) RelationshipFKName(rel schema.Relationship) string

RelationshipFKName snowflake edition

func (NamingStrategy) SchemaName

func (sns NamingStrategy) SchemaName(table string) string

SchemaName snowflake edition

func (NamingStrategy) TableName

func (sns NamingStrategy) TableName(table string) string

TableName snowflake edition

func (NamingStrategy) UniqueName

func (sns NamingStrategy) UniqueName(table, column string) string

UniqueName snowflake edition

Jump to

Keyboard shortcuts

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