From 19078ceb6d9cdcf3e4a67fa7ddb8ff83ebe26a36 Mon Sep 17 00:00:00 2001 From: Fawad Halim Date: Tue, 5 Mar 2024 15:40:34 -0600 Subject: [PATCH 1/3] Snowflake migrations working with script batches --- Makefile | 2 +- database/snowflake/snowflake.go | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 8e23a43c7..0fbc814b3 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ SOURCE ?= file go_bindata github github_ee bitbucket aws_s3 google_cloud_storage godoc_vfs gitlab -DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb yugabytedb clickhouse mongodb sqlserver firebird neo4j pgx pgx5 rqlite +DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb yugabytedb clickhouse mongodb sqlserver firebird neo4j pgx pgx5 rqlite snowflake DATABASE_TEST ?= $(DATABASE) sqlite sqlite3 sqlcipher VERSION ?= $(shell git describe --tags 2>/dev/null | cut -c 2-) TEST_FLAGS ?= diff --git a/database/snowflake/snowflake.go b/database/snowflake/snowflake.go index 46ce30200..32714508f 100644 --- a/database/snowflake/snowflake.go +++ b/database/snowflake/snowflake.go @@ -118,12 +118,15 @@ func (p *Snowflake) Open(url string) (database.Driver, error) { return nil, ErrNoSchema } + warehouse := purl.Query().Get("warehouse") + cfg := &sf.Config{ - Account: purl.Host, - User: purl.User.Username(), - Password: password, - Database: database, - Schema: schema, + Account: purl.Host, + User: purl.User.Username(), + Password: password, + Database: database, + Schema: schema, + Warehouse: warehouse, } dsn, err := sf.DSN(cfg) @@ -180,7 +183,12 @@ func (p *Snowflake) Run(migration io.Reader) error { // run migration query := string(migr[:]) - if _, err := p.conn.ExecContext(context.Background(), query); err != nil { + stmtCount := countStatements(query) + context, err := sf.WithMultiStatement(context.Background(), stmtCount) + if err != nil { + return err + } + if _, err := p.conn.ExecContext(context, query); err != nil { if pgErr, ok := err.(*pq.Error); ok { var line uint var col uint @@ -205,6 +213,11 @@ func (p *Snowflake) Run(migration io.Reader) error { return nil } +func countStatements(query string) int { + semicolonCount := strings.Count(query, ";") + return semicolonCount +} + func computeLineFromPos(s string, pos int) (line uint, col uint, ok bool) { // replace crlf with lf s = strings.Replace(s, "\r\n", "\n", -1) From 187bf0d9eeee3fa7acee43d43f4f2852594c2bdf Mon Sep 17 00:00:00 2001 From: Fawad Halim Date: Thu, 7 Mar 2024 09:36:07 -0600 Subject: [PATCH 2/3] Do not build snowflake by default --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0fbc814b3..8e23a43c7 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ SOURCE ?= file go_bindata github github_ee bitbucket aws_s3 google_cloud_storage godoc_vfs gitlab -DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb yugabytedb clickhouse mongodb sqlserver firebird neo4j pgx pgx5 rqlite snowflake +DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb yugabytedb clickhouse mongodb sqlserver firebird neo4j pgx pgx5 rqlite DATABASE_TEST ?= $(DATABASE) sqlite sqlite3 sqlcipher VERSION ?= $(shell git describe --tags 2>/dev/null | cut -c 2-) TEST_FLAGS ?= From 5521968ed77706a56dd766328e9b8ba1d14ef8f8 Mon Sep 17 00:00:00 2001 From: Fawad Halim Date: Thu, 7 Mar 2024 09:41:22 -0600 Subject: [PATCH 3/3] Document warehouse parameter --- database/snowflake/README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/database/snowflake/README.md b/database/snowflake/README.md index 90a28d177..358abed6a 100644 --- a/database/snowflake/README.md +++ b/database/snowflake/README.md @@ -2,11 +2,14 @@ `snowflake://user:password@accountname/schema/dbname?query` -| URL Query | WithInstance Config | Description | -|------------|---------------------|-------------| -| `x-migrations-table` | `MigrationsTable` | Name of the migrations table | +| URL Query | WithInstance Config | Description | +| -------------------- | ------------------- | ---------------------------- | +| `x-migrations-table` | `MigrationsTable` | Name of the migrations table | +| `warehouse` | | Snowflake warehouse to use | -Snowflake is PostgreSQL compatible but has some specific features (or lack thereof) that require slightly different behavior. +Snowflake is PostgreSQL compatible but has some specific features (or lack +thereof) that require slightly different behavior. ## Status + This driver is not officially supported as there are no tests for it.