Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Snowflake support #1044

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions database/snowflake/README.md
Expand Up @@ -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.
25 changes: 19 additions & 6 deletions database/snowflake/snowflake.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down