feat: add helm to go mod (#497)

This commit is contained in:
naison
2025-03-30 11:52:21 +08:00
committed by GitHub
parent a030dc582b
commit d191c927f4
2687 changed files with 190853 additions and 78174 deletions

6
vendor/github.com/rubenv/sql-migrate/.dockerignore generated vendored Normal file
View File

@@ -0,0 +1,6 @@
*
!go.mod
!go.sum
!*.go
!sql-migrate/*.go
!sqlparse/*.go

9
vendor/github.com/rubenv/sql-migrate/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,9 @@
.*.swp
*.test
.idea
/vendor/
/sql-migrate/test.db
/test.db
.vscode/
bin/

107
vendor/github.com/rubenv/sql-migrate/.golangci.yaml generated vendored Normal file
View File

@@ -0,0 +1,107 @@
linters-settings:
gocritic:
disabled-checks:
- ifElseChain
goimports:
local-prefixes: github.com/rubenv/sql-migrate
govet:
enable-all: true
disable:
- fieldalignment
depguard:
rules:
main:
allow:
- $gostd
- github.com/denisenkom/go-mssqldb
- github.com/go-sql-driver/mysql
- github.com/go-gorp/gorp/v3
- github.com/lib/pq
- github.com/mattn/go-sqlite3
- github.com/mitchellh/cli
- github.com/olekukonko/tablewriter
- github.com/rubenv/sql-migrate
exhaustive:
default-signifies-exhaustive: true
nolintlint:
allow-unused: false
allow-leading-space: false
allow-no-explanation:
- depguard
require-explanation: true
require-specific: true
revive:
enable-all-rules: false
rules:
- name: atomic
- name: blank-imports
- name: bool-literal-in-expr
- name: call-to-gc
- name: constant-logical-expr
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: duplicated-imports
- name: empty-block
- name: empty-lines
- name: error-naming
- name: error-return
- name: error-strings
- name: errorf
- name: exported
- name: identical-branches
- name: imports-blacklist
- name: increment-decrement
- name: indent-error-flow
- name: modifies-parameter
- name: modifies-value-receiver
- name: package-comments
- name: range
- name: range-val-address
- name: range-val-in-closure
- name: receiver-naming
- name: string-format
- name: string-of-int
- name: struct-tag
- name: time-naming
- name: unconditional-recursion
- name: unexported-naming
- name: unexported-return
- name: superfluous-else
- name: unreachable-code
- name: var-declaration
- name: waitgroup-by-value
- name: unused-receiver
- name: unnecessary-stmt
- name: unused-parameter
run:
tests: true
timeout: 1m
linters:
disable-all: true
enable:
- asciicheck
- depguard
- errcheck
- exhaustive
- gocritic
- gofmt
- gofumpt
- goimports
- govet
- ineffassign
- nolintlint
- revive
- staticcheck
- typecheck
- unused
- whitespace
- errorlint
- gosimple
- unparam
issues:
exclude:
- 'declaration of "err" shadows declaration at' # Allow shadowing of `err` because it's so common
- 'error-strings: error strings should not be capitalized or end with punctuation or a newline'
max-same-issues: 10000
max-issues-per-linter: 10000

25
vendor/github.com/rubenv/sql-migrate/Dockerfile generated vendored Normal file
View File

@@ -0,0 +1,25 @@
ARG GO_VERSION=1.20.6
ARG ALPINE_VERSION=3.12
### Vendor
FROM golang:${GO_VERSION} as vendor
COPY . /project
WORKDIR /project
RUN go mod tidy && go mod vendor
### Build binary
FROM golang:${GO_VERSION} as build-binary
COPY . /project
COPY --from=vendor /project/vendor /project/vendor
WORKDIR /project
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 GO111MODULE=on go build \
-v \
-mod vendor \
-o /project/bin/sql-migrate \
/project/sql-migrate
### Image
FROM alpine:${ALPINE_VERSION} as image
COPY --from=build-binary /project/bin/sql-migrate /usr/local/bin/sql-migrate
RUN chmod +x /usr/local/bin/sql-migrate
ENTRYPOINT ["sql-migrate"]

21
vendor/github.com/rubenv/sql-migrate/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (C) 2014-2021 by Ruben Vermeersch <ruben@rocketeer.be>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

11
vendor/github.com/rubenv/sql-migrate/Makefile generated vendored Normal file
View File

@@ -0,0 +1,11 @@
.PHONY: test lint build
test:
go test ./...
lint:
golangci-lint run --fix --config .golangci.yaml
build:
mkdir -p bin
go build -o ./bin/sql-migrate ./sql-migrate

386
vendor/github.com/rubenv/sql-migrate/README.md generated vendored Normal file
View File

@@ -0,0 +1,386 @@
# sql-migrate
> SQL Schema migration tool for [Go](https://golang.org/). Based on [gorp](https://github.com/go-gorp/gorp) and [goose](https://bitbucket.org/liamstask/goose).
[![Test](https://github.com/rubenv/sql-migrate/actions/workflows/test.yml/badge.svg)](https://github.com/rubenv/sql-migrate/actions/workflows/test.yml) [![Go Reference](https://pkg.go.dev/badge/github.com/rubenv/sql-migrate.svg)](https://pkg.go.dev/github.com/rubenv/sql-migrate)
## Features
- Usable as a CLI tool or as a library
- Supports SQLite, PostgreSQL, MySQL, MSSQL and Oracle databases (through [gorp](https://github.com/go-gorp/gorp))
- Can embed migrations into your application
- Migrations are defined with SQL for full flexibility
- Atomic migrations
- Up/down migrations to allow rollback
- Supports multiple database types in one project
- Works great with other libraries such as [sqlx](https://jmoiron.github.io/sqlx/)
- Supported on go1.13+
## Installation
To install the library and command line program, use the following:
```bash
go get -v github.com/rubenv/sql-migrate/...
```
For Go version from 1.18, use:
```bash
go install github.com/rubenv/sql-migrate/...@latest
```
## Usage
### As a standalone tool
```
$ sql-migrate --help
usage: sql-migrate [--version] [--help] <command> [<args>]
Available commands are:
down Undo a database migration
new Create a new migration
redo Reapply the last migration
status Show migration status
up Migrates the database to the most recent version available
```
Each command requires a configuration file (which defaults to `dbconfig.yml`, but can be specified with the `-config` flag). This config file should specify one or more environments:
```yml
development:
dialect: sqlite3
datasource: test.db
dir: migrations/sqlite3
production:
dialect: postgres
datasource: dbname=myapp sslmode=disable
dir: migrations/postgres
table: migrations
```
(See more examples for different set ups [here](test-integration/dbconfig.yml))
Also one can obtain env variables in datasource field via `os.ExpandEnv` embedded call for the field.
This may be useful if one doesn't want to store credentials in file:
```yml
production:
dialect: postgres
datasource: host=prodhost dbname=proddb user=${DB_USER} password=${DB_PASSWORD} sslmode=require
dir: migrations
table: migrations
```
The `table` setting is optional and will default to `gorp_migrations`.
The environment that will be used can be specified with the `-env` flag (defaults to `development`).
Use the `--help` flag in combination with any of the commands to get an overview of its usage:
```
$ sql-migrate up --help
Usage: sql-migrate up [options] ...
Migrates the database to the most recent version available.
Options:
-config=dbconfig.yml Configuration file to use.
-env="development" Environment.
-limit=0 Limit the number of migrations (0 = unlimited).
-version Run migrate up to a specific version, eg: the version number of migration 1_initial.sql is 1.
-dryrun Don't apply migrations, just print them.
```
The `new` command creates a new empty migration template using the following pattern `<current time>-<name>.sql`.
The `up` command applies all available migrations. By contrast, `down` will only apply one migration by default. This behavior can be changed for both by using the `-limit` parameter, and the `-version` parameter. Note `-version` has higher priority than `-limit` if you try to use them both.
The `redo` command will unapply the last migration and reapply it. This is useful during development, when you're writing migrations.
Use the `status` command to see the state of the applied migrations:
```bash
$ sql-migrate status
+---------------+-----------------------------------------+
| MIGRATION | APPLIED |
+---------------+-----------------------------------------+
| 1_initial.sql | 2014-09-13 08:19:06.788354925 +0000 UTC |
| 2_record.sql | no |
+---------------+-----------------------------------------+
```
#### Running Test Integrations
You can see how to run setups for different setups by executing the `.sh` files in [test-integration](test-integration/)
```bash
# Run mysql-env.sh example (you need to be in the project root directory)
./test-integration/mysql-env.sh
```
### MySQL Caveat
If you are using MySQL, you must append `?parseTime=true` to the `datasource` configuration. For example:
```yml
production:
dialect: mysql
datasource: root@/dbname?parseTime=true
dir: migrations/mysql
table: migrations
```
See [here](https://github.com/go-sql-driver/mysql#parsetime) for more information.
### Oracle (oci8)
Oracle Driver is [oci8](https://github.com/mattn/go-oci8), it is not pure Go code and relies on Oracle Office Client ([Instant Client](https://www.oracle.com/database/technologies/instant-client/downloads.html)), more detailed information is in the [oci8 repo](https://github.com/mattn/go-oci8).
#### Install with Oracle support
To install the library and command line program, use the following:
```bash
go get -tags oracle -v github.com/rubenv/sql-migrate/...
```
```yml
development:
dialect: oci8
datasource: user/password@localhost:1521/sid
dir: migrations/oracle
table: migrations
```
### Oracle (godror)
Oracle Driver is [godror](https://github.com/godror/godror), it is not pure Go code and relies on Oracle Office Client ([Instant Client](https://www.oracle.com/database/technologies/instant-client/downloads.html)), more detailed information is in the [godror repository](https://github.com/godror/godror).
#### Install with Oracle support
To install the library and command line program, use the following:
1. Install sql-migrate
```bash
go get -tags godror -v github.com/rubenv/sql-migrate/...
```
2. Download Oracle Office Client(e.g. macos, click [Instant Client](https://www.oracle.com/database/technologies/instant-client/downloads.html) if you are other system)
```bash
wget https://download.oracle.com/otn_software/mac/instantclient/193000/instantclient-basic-macos.x64-19.3.0.0.0dbru.zip
```
3. Configure environment variables `LD_LIBRARY_PATH`
```
export LD_LIBRARY_PATH=your_oracle_office_path/instantclient_19_3
```
```yml
development:
dialect: godror
datasource: user/password@localhost:1521/sid
dir: migrations/oracle
table: migrations
```
### As a library
Import sql-migrate into your application:
```go
import "github.com/rubenv/sql-migrate"
```
Set up a source of migrations, this can be from memory, from a set of files, from bindata (more on that later), or from any library that implements [`http.FileSystem`](https://godoc.org/net/http#FileSystem):
```go
// Hardcoded strings in memory:
migrations := &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
&migrate.Migration{
Id: "123",
Up: []string{"CREATE TABLE people (id int)"},
Down: []string{"DROP TABLE people"},
},
},
}
// OR: Read migrations from a folder:
migrations := &migrate.FileMigrationSource{
Dir: "db/migrations",
}
// OR: Use migrations from a packr box
// Note: Packr is no longer supported, your best option these days is [embed](https://pkg.go.dev/embed)
migrations := &migrate.PackrMigrationSource{
Box: packr.New("migrations", "./migrations"),
}
// OR: Use pkger which implements `http.FileSystem`
migrationSource := &migrate.HttpFileSystemMigrationSource{
FileSystem: pkger.Dir("/db/migrations"),
}
// OR: Use migrations from bindata:
migrations := &migrate.AssetMigrationSource{
Asset: Asset,
AssetDir: AssetDir,
Dir: "migrations",
}
// OR: Read migrations from a `http.FileSystem`
migrationSource := &migrate.HttpFileSystemMigrationSource{
FileSystem: httpFS,
}
```
Then use the `Exec` function to upgrade your database:
```go
db, err := sql.Open("sqlite3", filename)
if err != nil {
// Handle errors!
}
n, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up)
if err != nil {
// Handle errors!
}
fmt.Printf("Applied %d migrations!\n", n)
```
Note that `n` can be greater than `0` even if there is an error: any migration that succeeded will remain applied even if a later one fails.
Check [the GoDoc reference](https://godoc.org/github.com/rubenv/sql-migrate) for the full documentation.
## Writing migrations
Migrations are defined in SQL files, which contain a set of SQL statements. Special comments are used to distinguish up and down migrations.
```sql
-- +migrate Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE people (id int);
-- +migrate Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE people;
```
You can put multiple statements in each block, as long as you end them with a semicolon (`;`).
You can alternatively set up a separator string that matches an entire line by setting `sqlparse.LineSeparator`. This
can be used to imitate, for example, MS SQL Query Analyzer functionality where commands can be separated by a line with
contents of `GO`. If `sqlparse.LineSeparator` is matched, it will not be included in the resulting migration scripts.
If you have complex statements which contain semicolons, use `StatementBegin` and `StatementEnd` to indicate boundaries:
```sql
-- +migrate Up
CREATE TABLE people (id int);
-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION do_something()
returns void AS $$
DECLARE
create_query text;
BEGIN
-- Do something here
END;
$$
language plpgsql;
-- +migrate StatementEnd
-- +migrate Down
DROP FUNCTION do_something();
DROP TABLE people;
```
The order in which migrations are applied is defined through the filename: sql-migrate will sort migrations based on their name. It's recommended to use an increasing version number or a timestamp as the first part of the filename.
Normally each migration is run within a transaction in order to guarantee that it is fully atomic. However some SQL commands (for example creating an index concurrently in PostgreSQL) cannot be executed inside a transaction. In order to execute such a command in a migration, the migration can be run using the `notransaction` option:
```sql
-- +migrate Up notransaction
CREATE UNIQUE INDEX CONCURRENTLY people_unique_id_idx ON people (id);
-- +migrate Down
DROP INDEX people_unique_id_idx;
```
## Embedding migrations with [embed](https://pkg.go.dev/embed)
If you like your Go applications self-contained (that is: a single binary): use [embed](https://pkg.go.dev/embed) to embed the migration files.
Just write your migration files as usual, as a set of SQL files in a folder.
Import the embed package into your application and point it to your migrations:
```go
import "embed"
//go:embed migrations/*
var dbMigrations embed.FS
```
Use the `EmbedFileSystemMigrationSource` in your application to find the migrations:
```go
migrations := migrate.EmbedFileSystemMigrationSource{
FileSystem: dbMigrations,
Root: "migrations",
}
```
Other options such as [packr](https://github.com/gobuffalo/packr) or [go-bindata](https://github.com/shuLhan/go-bindata) are no longer recommended.
## Embedding migrations with libraries that implement `http.FileSystem`
You can also embed migrations with any library that implements `http.FileSystem`, like [`vfsgen`](https://github.com/shurcooL/vfsgen), [`parcello`](https://github.com/phogolabs/parcello), or [`go-resources`](https://github.com/omeid/go-resources).
```go
migrationSource := &migrate.HttpFileSystemMigrationSource{
FileSystem: httpFS,
}
```
## Extending
Adding a new migration source means implementing `MigrationSource`.
```go
type MigrationSource interface {
FindMigrations() ([]*Migration, error)
}
```
The resulting slice of migrations will be executed in the given order, so it should usually be sorted by the `Id` field.
## Usage with [sqlx](https://jmoiron.github.io/sqlx/)
This library is compatible with sqlx. When calling migrate just dereference the DB from your `*sqlx.DB`:
```
n, err := migrate.Exec(db.DB, "sqlite3", migrations, migrate.Up)
// ^^^ <-- Here db is a *sqlx.DB, the db.DB field is the plain sql.DB
if err != nil {
// Handle errors!
}
```
## Questions or Feedback?
You can use Github Issues for feedback or questions.
## License
This library is distributed under the [MIT](LICENSE) license.

238
vendor/github.com/rubenv/sql-migrate/doc.go generated vendored Normal file
View File

@@ -0,0 +1,238 @@
/*
SQL Schema migration tool for Go.
Key features:
- Usable as a CLI tool or as a library
- Supports SQLite, PostgreSQL, MySQL, MSSQL and Oracle databases (through gorp)
- Can embed migrations into your application
- Migrations are defined with SQL for full flexibility
- Atomic migrations
- Up/down migrations to allow rollback
- Supports multiple database types in one project
# Installation
To install the library and command line program, use the following:
go get -v github.com/rubenv/sql-migrate/...
# Command-line tool
The main command is called sql-migrate.
$ sql-migrate --help
usage: sql-migrate [--version] [--help] <command> [<args>]
Available commands are:
down Undo a database migration
new Create a new migration
redo Reapply the last migration
status Show migration status
up Migrates the database to the most recent version available
Each command requires a configuration file (which defaults to dbconfig.yml, but can be specified with the -config flag). This config file should specify one or more environments:
development:
dialect: sqlite3
datasource: test.db
dir: migrations/sqlite3
production:
dialect: postgres
datasource: dbname=myapp sslmode=disable
dir: migrations/postgres
table: migrations
The `table` setting is optional and will default to `gorp_migrations`.
The environment that will be used can be specified with the -env flag (defaults to development).
Use the --help flag in combination with any of the commands to get an overview of its usage:
$ sql-migrate up --help
Usage: sql-migrate up [options] ...
Migrates the database to the most recent version available.
Options:
-config=config.yml Configuration file to use.
-env="development" Environment.
-limit=0 Limit the number of migrations (0 = unlimited).
-dryrun Don't apply migrations, just print them.
The up command applies all available migrations. By contrast, down will only apply one migration by default. This behavior can be changed for both by using the -limit parameter.
The redo command will unapply the last migration and reapply it. This is useful during development, when you're writing migrations.
Use the status command to see the state of the applied migrations:
$ sql-migrate status
+---------------+-----------------------------------------+
| MIGRATION | APPLIED |
+---------------+-----------------------------------------+
| 1_initial.sql | 2014-09-13 08:19:06.788354925 +0000 UTC |
| 2_record.sql | no |
+---------------+-----------------------------------------+
# MySQL Caveat
If you are using MySQL, you must append ?parseTime=true to the datasource configuration. For example:
production:
dialect: mysql
datasource: root@/dbname?parseTime=true
dir: migrations/mysql
table: migrations
See https://github.com/go-sql-driver/mysql#parsetime for more information.
# Library
Import sql-migrate into your application:
import "github.com/rubenv/sql-migrate"
Set up a source of migrations, this can be from memory, from a set of files or from bindata (more on that later):
// Hardcoded strings in memory:
migrations := &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
&migrate.Migration{
Id: "123",
Up: []string{"CREATE TABLE people (id int)"},
Down: []string{"DROP TABLE people"},
},
},
}
// OR: Read migrations from a folder:
migrations := &migrate.FileMigrationSource{
Dir: "db/migrations",
}
// OR: Use migrations from bindata:
migrations := &migrate.AssetMigrationSource{
Asset: Asset,
AssetDir: AssetDir,
Dir: "migrations",
}
Then use the Exec function to upgrade your database:
db, err := sql.Open("sqlite3", filename)
if err != nil {
// Handle errors!
}
n, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up)
if err != nil {
// Handle errors!
}
fmt.Printf("Applied %d migrations!\n", n)
Note that n can be greater than 0 even if there is an error: any migration that succeeded will remain applied even if a later one fails.
The full set of capabilities can be found in the API docs below.
# Writing migrations
Migrations are defined in SQL files, which contain a set of SQL statements. Special comments are used to distinguish up and down migrations.
-- +migrate Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE people (id int);
-- +migrate Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE people;
You can put multiple statements in each block, as long as you end them with a semicolon (;).
If you have complex statements which contain semicolons, use StatementBegin and StatementEnd to indicate boundaries:
-- +migrate Up
CREATE TABLE people (id int);
-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION do_something()
returns void AS $$
DECLARE
create_query text;
BEGIN
-- Do something here
END;
$$
language plpgsql;
-- +migrate StatementEnd
-- +migrate Down
DROP FUNCTION do_something();
DROP TABLE people;
The order in which migrations are applied is defined through the filename: sql-migrate will sort migrations based on their name. It's recommended to use an increasing version number or a timestamp as the first part of the filename.
Normally each migration is run within a transaction in order to guarantee that it is fully atomic. However some SQL commands (for example creating an index concurrently in PostgreSQL) cannot be executed inside a transaction. In order to execute such a command in a migration, the migration can be run using the notransaction option:
-- +migrate Up notransaction
CREATE UNIQUE INDEX people_unique_id_idx CONCURRENTLY ON people (id);
-- +migrate Down
DROP INDEX people_unique_id_idx;
# Embedding migrations with packr
If you like your Go applications self-contained (that is: a single binary): use packr (https://github.com/gobuffalo/packr) to embed the migration files.
Just write your migration files as usual, as a set of SQL files in a folder.
Use the PackrMigrationSource in your application to find the migrations:
migrations := &migrate.PackrMigrationSource{
Box: packr.NewBox("./migrations"),
}
If you already have a box and would like to use a subdirectory:
migrations := &migrate.PackrMigrationSource{
Box: myBox,
Dir: "./migrations",
}
# Embedding migrations with bindata
As an alternative, but slightly less maintained, you can use bindata (https://github.com/shuLhan/go-bindata) to embed the migration files.
Just write your migration files as usual, as a set of SQL files in a folder.
Then use bindata to generate a .go file with the migrations embedded:
go-bindata -pkg myapp -o bindata.go db/migrations/
The resulting bindata.go file will contain your migrations. Remember to regenerate your bindata.go file whenever you add/modify a migration (go generate will help here, once it arrives).
Use the AssetMigrationSource in your application to find the migrations:
migrations := &migrate.AssetMigrationSource{
Asset: Asset,
AssetDir: AssetDir,
Dir: "db/migrations",
}
Both Asset and AssetDir are functions provided by bindata.
Then proceed as usual.
# Extending
Adding a new migration source means implementing MigrationSource.
type MigrationSource interface {
FindMigrations() ([]*Migration, error)
}
The resulting slice of migrations will be executed in the given order, so it should usually be sorted by the Id field.
*/
package migrate

891
vendor/github.com/rubenv/sql-migrate/migrate.go generated vendored Normal file
View File

@@ -0,0 +1,891 @@
package migrate
import (
"bytes"
"context"
"database/sql"
"embed"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/go-gorp/gorp/v3"
"github.com/rubenv/sql-migrate/sqlparse"
)
type MigrationDirection int
const (
Up MigrationDirection = iota
Down
)
// MigrationSet provides database parameters for a migration execution
type MigrationSet struct {
// TableName name of the table used to store migration info.
TableName string
// SchemaName schema that the migration table be referenced.
SchemaName string
// IgnoreUnknown skips the check to see if there is a migration
// ran in the database that is not in MigrationSource.
//
// This should be used sparingly as it is removing a safety check.
IgnoreUnknown bool
// DisableCreateTable disable the creation of the migration table
DisableCreateTable bool
}
var migSet = MigrationSet{}
// NewMigrationSet returns a parametrized Migration object
func (ms MigrationSet) getTableName() string {
if ms.TableName == "" {
return "gorp_migrations"
}
return ms.TableName
}
var numberPrefixRegex = regexp.MustCompile(`^(\d+).*$`)
// PlanError happens where no migration plan could be created between the sets
// of already applied migrations and the currently found. For example, when the database
// contains a migration which is not among the migrations list found for an operation.
type PlanError struct {
Migration *Migration
ErrorMessage string
}
func newPlanError(migration *Migration, errorMessage string) error {
return &PlanError{
Migration: migration,
ErrorMessage: errorMessage,
}
}
func (p *PlanError) Error() string {
return fmt.Sprintf("Unable to create migration plan because of %s: %s",
p.Migration.Id, p.ErrorMessage)
}
// TxError is returned when any error is encountered during a database
// transaction. It contains the relevant *Migration and notes it's Id in the
// Error function output.
type TxError struct {
Migration *Migration
Err error
}
func newTxError(migration *PlannedMigration, err error) error {
return &TxError{
Migration: migration.Migration,
Err: err,
}
}
func (e *TxError) Error() string {
return e.Err.Error() + " handling " + e.Migration.Id
}
// Set the name of the table used to store migration info.
//
// Should be called before any other call such as (Exec, ExecMax, ...).
func SetTable(name string) {
if name != "" {
migSet.TableName = name
}
}
// SetSchema sets the name of a schema that the migration table be referenced.
func SetSchema(name string) {
if name != "" {
migSet.SchemaName = name
}
}
// SetDisableCreateTable sets the boolean to disable the creation of the migration table
func SetDisableCreateTable(disable bool) {
migSet.DisableCreateTable = disable
}
// SetIgnoreUnknown sets the flag that skips database check to see if there is a
// migration in the database that is not in migration source.
//
// This should be used sparingly as it is removing a safety check.
func SetIgnoreUnknown(v bool) {
migSet.IgnoreUnknown = v
}
type Migration struct {
Id string
Up []string
Down []string
DisableTransactionUp bool
DisableTransactionDown bool
}
func (m Migration) Less(other *Migration) bool {
switch {
case m.isNumeric() && other.isNumeric() && m.VersionInt() != other.VersionInt():
return m.VersionInt() < other.VersionInt()
case m.isNumeric() && !other.isNumeric():
return true
case !m.isNumeric() && other.isNumeric():
return false
default:
return m.Id < other.Id
}
}
func (m Migration) isNumeric() bool {
return len(m.NumberPrefixMatches()) > 0
}
func (m Migration) NumberPrefixMatches() []string {
return numberPrefixRegex.FindStringSubmatch(m.Id)
}
func (m Migration) VersionInt() int64 {
v := m.NumberPrefixMatches()[1]
value, err := strconv.ParseInt(v, 10, 64)
if err != nil {
panic(fmt.Sprintf("Could not parse %q into int64: %s", v, err))
}
return value
}
type PlannedMigration struct {
*Migration
DisableTransaction bool
Queries []string
}
type byId []*Migration
func (b byId) Len() int { return len(b) }
func (b byId) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byId) Less(i, j int) bool { return b[i].Less(b[j]) }
type MigrationRecord struct {
Id string `db:"id"`
AppliedAt time.Time `db:"applied_at"`
}
type OracleDialect struct {
gorp.OracleDialect
}
func (OracleDialect) IfTableNotExists(command, _, _ string) string {
return command
}
func (OracleDialect) IfSchemaNotExists(command, _ string) string {
return command
}
func (OracleDialect) IfTableExists(command, _, _ string) string {
return command
}
var MigrationDialects = map[string]gorp.Dialect{
"sqlite3": gorp.SqliteDialect{},
"postgres": gorp.PostgresDialect{},
"mysql": gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8"},
"mssql": gorp.SqlServerDialect{},
"oci8": OracleDialect{},
"godror": OracleDialect{},
"snowflake": gorp.SnowflakeDialect{},
}
type MigrationSource interface {
// Finds the migrations.
//
// The resulting slice of migrations should be sorted by Id.
FindMigrations() ([]*Migration, error)
}
// A hardcoded set of migrations, in-memory.
type MemoryMigrationSource struct {
Migrations []*Migration
}
var _ MigrationSource = (*MemoryMigrationSource)(nil)
func (m MemoryMigrationSource) FindMigrations() ([]*Migration, error) {
// Make sure migrations are sorted. In order to make the MemoryMigrationSource safe for
// concurrent use we should not mutate it in place. So `FindMigrations` would sort a copy
// of the m.Migrations.
migrations := make([]*Migration, len(m.Migrations))
copy(migrations, m.Migrations)
sort.Sort(byId(migrations))
return migrations, nil
}
// A set of migrations loaded from an http.FileServer
type HttpFileSystemMigrationSource struct {
FileSystem http.FileSystem
}
var _ MigrationSource = (*HttpFileSystemMigrationSource)(nil)
func (f HttpFileSystemMigrationSource) FindMigrations() ([]*Migration, error) {
return findMigrations(f.FileSystem, "/")
}
// A set of migrations loaded from a directory.
type FileMigrationSource struct {
Dir string
}
var _ MigrationSource = (*FileMigrationSource)(nil)
func (f FileMigrationSource) FindMigrations() ([]*Migration, error) {
filesystem := http.Dir(f.Dir)
return findMigrations(filesystem, "/")
}
func findMigrations(dir http.FileSystem, root string) ([]*Migration, error) {
migrations := make([]*Migration, 0)
file, err := dir.Open(root)
if err != nil {
return nil, err
}
files, err := file.Readdir(0)
if err != nil {
return nil, err
}
for _, info := range files {
if strings.HasSuffix(info.Name(), ".sql") {
migration, err := migrationFromFile(dir, root, info)
if err != nil {
return nil, err
}
migrations = append(migrations, migration)
}
}
// Make sure migrations are sorted
sort.Sort(byId(migrations))
return migrations, nil
}
func migrationFromFile(dir http.FileSystem, root string, info os.FileInfo) (*Migration, error) {
path := path.Join(root, info.Name())
file, err := dir.Open(path)
if err != nil {
return nil, fmt.Errorf("Error while opening %s: %w", info.Name(), err)
}
defer func() { _ = file.Close() }()
migration, err := ParseMigration(info.Name(), file)
if err != nil {
return nil, fmt.Errorf("Error while parsing %s: %w", info.Name(), err)
}
return migration, nil
}
// Migrations from a bindata asset set.
type AssetMigrationSource struct {
// Asset should return content of file in path if exists
Asset func(path string) ([]byte, error)
// AssetDir should return list of files in the path
AssetDir func(path string) ([]string, error)
// Path in the bindata to use.
Dir string
}
var _ MigrationSource = (*AssetMigrationSource)(nil)
func (a AssetMigrationSource) FindMigrations() ([]*Migration, error) {
migrations := make([]*Migration, 0)
files, err := a.AssetDir(a.Dir)
if err != nil {
return nil, err
}
for _, name := range files {
if strings.HasSuffix(name, ".sql") {
file, err := a.Asset(path.Join(a.Dir, name))
if err != nil {
return nil, err
}
migration, err := ParseMigration(name, bytes.NewReader(file))
if err != nil {
return nil, err
}
migrations = append(migrations, migration)
}
}
// Make sure migrations are sorted
sort.Sort(byId(migrations))
return migrations, nil
}
// A set of migrations loaded from an go1.16 embed.FS
type EmbedFileSystemMigrationSource struct {
FileSystem embed.FS
Root string
}
var _ MigrationSource = (*EmbedFileSystemMigrationSource)(nil)
func (f EmbedFileSystemMigrationSource) FindMigrations() ([]*Migration, error) {
return findMigrations(http.FS(f.FileSystem), f.Root)
}
// Avoids pulling in the packr library for everyone, mimicks the bits of
// packr.Box that we need.
type PackrBox interface {
List() []string
Find(name string) ([]byte, error)
}
// Migrations from a packr box.
type PackrMigrationSource struct {
Box PackrBox
// Path in the box to use.
Dir string
}
var _ MigrationSource = (*PackrMigrationSource)(nil)
func (p PackrMigrationSource) FindMigrations() ([]*Migration, error) {
migrations := make([]*Migration, 0)
items := p.Box.List()
prefix := ""
dir := path.Clean(p.Dir)
if dir != "." {
prefix = fmt.Sprintf("%s/", dir)
}
for _, item := range items {
if !strings.HasPrefix(item, prefix) {
continue
}
name := strings.TrimPrefix(item, prefix)
if strings.Contains(name, "/") {
continue
}
if strings.HasSuffix(name, ".sql") {
file, err := p.Box.Find(item)
if err != nil {
return nil, err
}
migration, err := ParseMigration(name, bytes.NewReader(file))
if err != nil {
return nil, err
}
migrations = append(migrations, migration)
}
}
// Make sure migrations are sorted
sort.Sort(byId(migrations))
return migrations, nil
}
// Migration parsing
func ParseMigration(id string, r io.ReadSeeker) (*Migration, error) {
m := &Migration{
Id: id,
}
parsed, err := sqlparse.ParseMigration(r)
if err != nil {
return nil, fmt.Errorf("Error parsing migration (%s): %w", id, err)
}
m.Up = parsed.UpStatements
m.Down = parsed.DownStatements
m.DisableTransactionUp = parsed.DisableTransactionUp
m.DisableTransactionDown = parsed.DisableTransactionDown
return m, nil
}
type SqlExecutor interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Insert(list ...interface{}) error
Delete(list ...interface{}) (int64, error)
}
// Execute a set of migrations
//
// Returns the number of applied migrations.
func Exec(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection) (int, error) {
return ExecMaxContext(context.Background(), db, dialect, m, dir, 0)
}
// Returns the number of applied migrations.
func (ms MigrationSet) Exec(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection) (int, error) {
return ms.ExecMaxContext(context.Background(), db, dialect, m, dir, 0)
}
// Execute a set of migrations with an input context.
//
// Returns the number of applied migrations.
func ExecContext(ctx context.Context, db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection) (int, error) {
return ExecMaxContext(ctx, db, dialect, m, dir, 0)
}
// Returns the number of applied migrations.
func (ms MigrationSet) ExecContext(ctx context.Context, db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection) (int, error) {
return ms.ExecMaxContext(ctx, db, dialect, m, dir, 0)
}
// Execute a set of migrations
//
// Will apply at most `max` migrations. Pass 0 for no limit (or use Exec).
//
// Returns the number of applied migrations.
func ExecMax(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) (int, error) {
return migSet.ExecMax(db, dialect, m, dir, max)
}
// Execute a set of migrations with an input context.
//
// Will apply at most `max` migrations. Pass 0 for no limit (or use Exec).
//
// Returns the number of applied migrations.
func ExecMaxContext(ctx context.Context, db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) (int, error) {
return migSet.ExecMaxContext(ctx, db, dialect, m, dir, max)
}
// Execute a set of migrations
//
// Will apply at the target `version` of migration. Cannot be a negative value.
//
// Returns the number of applied migrations.
func ExecVersion(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, version int64) (int, error) {
return ExecVersionContext(context.Background(), db, dialect, m, dir, version)
}
// Execute a set of migrations with an input context.
//
// Will apply at the target `version` of migration. Cannot be a negative value.
//
// Returns the number of applied migrations.
func ExecVersionContext(ctx context.Context, db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, version int64) (int, error) {
if version < 0 {
return 0, fmt.Errorf("target version %d should not be negative", version)
}
return migSet.ExecVersionContext(ctx, db, dialect, m, dir, version)
}
// Returns the number of applied migrations.
func (ms MigrationSet) ExecMax(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) (int, error) {
return ms.ExecMaxContext(context.Background(), db, dialect, m, dir, max)
}
// Returns the number of applied migrations, but applies with an input context.
func (ms MigrationSet) ExecMaxContext(ctx context.Context, db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) (int, error) {
migrations, dbMap, err := ms.PlanMigration(db, dialect, m, dir, max)
if err != nil {
return 0, err
}
return ms.applyMigrations(ctx, dir, migrations, dbMap)
}
// Returns the number of applied migrations.
func (ms MigrationSet) ExecVersion(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, version int64) (int, error) {
return ms.ExecVersionContext(context.Background(), db, dialect, m, dir, version)
}
func (ms MigrationSet) ExecVersionContext(ctx context.Context, db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, version int64) (int, error) {
migrations, dbMap, err := ms.PlanMigrationToVersion(db, dialect, m, dir, version)
if err != nil {
return 0, err
}
return ms.applyMigrations(ctx, dir, migrations, dbMap)
}
// Applies the planned migrations and returns the number of applied migrations.
func (MigrationSet) applyMigrations(ctx context.Context, dir MigrationDirection, migrations []*PlannedMigration, dbMap *gorp.DbMap) (int, error) {
applied := 0
for _, migration := range migrations {
var executor SqlExecutor
var err error
if migration.DisableTransaction {
executor = dbMap.WithContext(ctx)
} else {
e, err := dbMap.Begin()
if err != nil {
return applied, newTxError(migration, err)
}
executor = e.WithContext(ctx)
}
for _, stmt := range migration.Queries {
// remove the semicolon from stmt, fix ORA-00922 issue in database oracle
stmt = strings.TrimSuffix(stmt, "\n")
stmt = strings.TrimSuffix(stmt, " ")
stmt = strings.TrimSuffix(stmt, ";")
if _, err := executor.Exec(stmt); err != nil {
if trans, ok := executor.(*gorp.Transaction); ok {
_ = trans.Rollback()
}
return applied, newTxError(migration, err)
}
}
switch dir {
case Up:
err = executor.Insert(&MigrationRecord{
Id: migration.Id,
AppliedAt: time.Now(),
})
if err != nil {
if trans, ok := executor.(*gorp.Transaction); ok {
_ = trans.Rollback()
}
return applied, newTxError(migration, err)
}
case Down:
_, err := executor.Delete(&MigrationRecord{
Id: migration.Id,
})
if err != nil {
if trans, ok := executor.(*gorp.Transaction); ok {
_ = trans.Rollback()
}
return applied, newTxError(migration, err)
}
default:
panic("Not possible")
}
if trans, ok := executor.(*gorp.Transaction); ok {
if err := trans.Commit(); err != nil {
return applied, newTxError(migration, err)
}
}
applied++
}
return applied, nil
}
// Plan a migration.
func PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) ([]*PlannedMigration, *gorp.DbMap, error) {
return migSet.PlanMigration(db, dialect, m, dir, max)
}
// Plan a migration to version.
func PlanMigrationToVersion(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, version int64) ([]*PlannedMigration, *gorp.DbMap, error) {
return migSet.PlanMigrationToVersion(db, dialect, m, dir, version)
}
// Plan a migration.
func (ms MigrationSet) PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) ([]*PlannedMigration, *gorp.DbMap, error) {
return ms.planMigrationCommon(db, dialect, m, dir, max, -1)
}
// Plan a migration to version.
func (ms MigrationSet) PlanMigrationToVersion(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, version int64) ([]*PlannedMigration, *gorp.DbMap, error) {
return ms.planMigrationCommon(db, dialect, m, dir, 0, version)
}
// A common method to plan a migration.
func (ms MigrationSet) planMigrationCommon(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int, version int64) ([]*PlannedMigration, *gorp.DbMap, error) {
dbMap, err := ms.getMigrationDbMap(db, dialect)
if err != nil {
return nil, nil, err
}
migrations, err := m.FindMigrations()
if err != nil {
return nil, nil, err
}
var migrationRecords []MigrationRecord
_, err = dbMap.Select(&migrationRecords, fmt.Sprintf("SELECT * FROM %s", dbMap.Dialect.QuotedTableForQuery(ms.SchemaName, ms.getTableName())))
if err != nil {
return nil, nil, err
}
// Sort migrations that have been run by Id.
var existingMigrations []*Migration
for _, migrationRecord := range migrationRecords {
existingMigrations = append(existingMigrations, &Migration{
Id: migrationRecord.Id,
})
}
sort.Sort(byId(existingMigrations))
// Make sure all migrations in the database are among the found migrations which
// are to be applied.
if !ms.IgnoreUnknown {
migrationsSearch := make(map[string]struct{})
for _, migration := range migrations {
migrationsSearch[migration.Id] = struct{}{}
}
for _, existingMigration := range existingMigrations {
if _, ok := migrationsSearch[existingMigration.Id]; !ok {
return nil, nil, newPlanError(existingMigration, "unknown migration in database")
}
}
}
// Get last migration that was run
record := &Migration{}
if len(existingMigrations) > 0 {
record = existingMigrations[len(existingMigrations)-1]
}
result := make([]*PlannedMigration, 0)
// Add missing migrations up to the last run migration.
// This can happen for example when merges happened.
if len(existingMigrations) > 0 {
result = append(result, ToCatchup(migrations, existingMigrations, record)...)
}
// Figure out which migrations to apply
toApply := ToApply(migrations, record.Id, dir)
toApplyCount := len(toApply)
if version >= 0 {
targetIndex := 0
for targetIndex < len(toApply) {
tempVersion := toApply[targetIndex].VersionInt()
if dir == Up && tempVersion > version || dir == Down && tempVersion < version {
return nil, nil, newPlanError(&Migration{}, fmt.Errorf("unknown migration with version id %d in database", version).Error())
}
if tempVersion == version {
toApplyCount = targetIndex + 1
break
}
targetIndex++
}
if targetIndex == len(toApply) {
return nil, nil, newPlanError(&Migration{}, fmt.Errorf("unknown migration with version id %d in database", version).Error())
}
} else if max > 0 && max < toApplyCount {
toApplyCount = max
}
for _, v := range toApply[0:toApplyCount] {
if dir == Up {
result = append(result, &PlannedMigration{
Migration: v,
Queries: v.Up,
DisableTransaction: v.DisableTransactionUp,
})
} else if dir == Down {
result = append(result, &PlannedMigration{
Migration: v,
Queries: v.Down,
DisableTransaction: v.DisableTransactionDown,
})
}
}
return result, dbMap, nil
}
// Skip a set of migrations
//
// Will skip at most `max` migrations. Pass 0 for no limit.
//
// Returns the number of skipped migrations.
func SkipMax(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) (int, error) {
migrations, dbMap, err := PlanMigration(db, dialect, m, dir, max)
if err != nil {
return 0, err
}
// Skip migrations
applied := 0
for _, migration := range migrations {
var executor SqlExecutor
if migration.DisableTransaction {
executor = dbMap
} else {
executor, err = dbMap.Begin()
if err != nil {
return applied, newTxError(migration, err)
}
}
err = executor.Insert(&MigrationRecord{
Id: migration.Id,
AppliedAt: time.Now(),
})
if err != nil {
if trans, ok := executor.(*gorp.Transaction); ok {
_ = trans.Rollback()
}
return applied, newTxError(migration, err)
}
if trans, ok := executor.(*gorp.Transaction); ok {
if err := trans.Commit(); err != nil {
return applied, newTxError(migration, err)
}
}
applied++
}
return applied, nil
}
// Filter a slice of migrations into ones that should be applied.
func ToApply(migrations []*Migration, current string, direction MigrationDirection) []*Migration {
index := -1
if current != "" {
for index < len(migrations)-1 {
index++
if migrations[index].Id == current {
break
}
}
}
if direction == Up {
return migrations[index+1:]
} else if direction == Down {
if index == -1 {
return []*Migration{}
}
// Add in reverse order
toApply := make([]*Migration, index+1)
for i := 0; i < index+1; i++ {
toApply[index-i] = migrations[i]
}
return toApply
}
panic("Not possible")
}
func ToCatchup(migrations, existingMigrations []*Migration, lastRun *Migration) []*PlannedMigration {
missing := make([]*PlannedMigration, 0)
for _, migration := range migrations {
found := false
for _, existing := range existingMigrations {
if existing.Id == migration.Id {
found = true
break
}
}
if !found && migration.Less(lastRun) {
missing = append(missing, &PlannedMigration{
Migration: migration,
Queries: migration.Up,
DisableTransaction: migration.DisableTransactionUp,
})
}
}
return missing
}
func GetMigrationRecords(db *sql.DB, dialect string) ([]*MigrationRecord, error) {
return migSet.GetMigrationRecords(db, dialect)
}
func (ms MigrationSet) GetMigrationRecords(db *sql.DB, dialect string) ([]*MigrationRecord, error) {
dbMap, err := ms.getMigrationDbMap(db, dialect)
if err != nil {
return nil, err
}
var records []*MigrationRecord
query := fmt.Sprintf("SELECT * FROM %s ORDER BY %s ASC", dbMap.Dialect.QuotedTableForQuery(ms.SchemaName, ms.getTableName()), dbMap.Dialect.QuoteField("id"))
_, err = dbMap.Select(&records, query)
if err != nil {
return nil, err
}
return records, nil
}
func (ms MigrationSet) getMigrationDbMap(db *sql.DB, dialect string) (*gorp.DbMap, error) {
d, ok := MigrationDialects[dialect]
if !ok {
return nil, fmt.Errorf("Unknown dialect: %s", dialect)
}
// When using the mysql driver, make sure that the parseTime option is
// configured, otherwise it won't map time columns to time.Time. See
// https://github.com/rubenv/sql-migrate/issues/2
if dialect == "mysql" {
var out *time.Time
err := db.QueryRow("SELECT NOW()").Scan(&out)
if err != nil {
if err.Error() == "sql: Scan error on column index 0: unsupported driver -> Scan pair: []uint8 -> *time.Time" ||
err.Error() == "sql: Scan error on column index 0: unsupported Scan, storing driver.Value type []uint8 into type *time.Time" ||
err.Error() == "sql: Scan error on column index 0, name \"NOW()\": unsupported Scan, storing driver.Value type []uint8 into type *time.Time" {
return nil, errors.New(`Cannot parse dates.
Make sure that the parseTime option is supplied to your database connection.
Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)
}
return nil, err
}
}
// Create migration database map
dbMap := &gorp.DbMap{Db: db, Dialect: d}
table := dbMap.AddTableWithNameAndSchema(MigrationRecord{}, ms.SchemaName, ms.getTableName()).SetKeys(false, "Id")
if dialect == "oci8" || dialect == "godror" {
table.ColMap("Id").SetMaxSize(4000)
}
if ms.DisableCreateTable {
return dbMap, nil
}
err := dbMap.CreateTablesIfNotExists()
if err != nil {
// Oracle database does not support `if not exists`, so use `ORA-00955:` error code
// to check if the table exists.
if (dialect == "oci8" || dialect == "godror") && strings.Contains(err.Error(), "ORA-00955:") {
return dbMap, nil
}
return nil, err
}
return dbMap, nil
}
// TODO: Run migration + record insert in transaction.

22
vendor/github.com/rubenv/sql-migrate/sqlparse/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,22 @@
MIT License
Copyright (C) 2014-2017 by Ruben Vermeersch <ruben@rocketeer.be>
Copyright (C) 2012-2014 by Liam Staskawicz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,7 @@
# SQL migration parser
Based on the [goose](https://bitbucket.org/liamstask/goose) migration parser.
## License
This library is distributed under the [MIT](LICENSE) license.

View File

@@ -0,0 +1,226 @@
package sqlparse
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
)
const (
sqlCmdPrefix = "-- +migrate "
optionNoTransaction = "notransaction"
)
type ParsedMigration struct {
UpStatements []string
DownStatements []string
DisableTransactionUp bool
DisableTransactionDown bool
}
// LineSeparator can be used to split migrations by an exact line match. This line
// will be removed from the output. If left blank, it is not considered. It is defaulted
// to blank so you will have to set it manually.
// Use case: in MSSQL, it is convenient to separate commands by GO statements like in
// SQL Query Analyzer.
var LineSeparator = ""
func errNoTerminator() error {
if len(LineSeparator) == 0 {
return fmt.Errorf(`ERROR: The last statement must be ended by a semicolon or '-- +migrate StatementEnd' marker.
See https://github.com/rubenv/sql-migrate for details.`)
}
return fmt.Errorf(`ERROR: The last statement must be ended by a semicolon, a line whose contents are %q, or '-- +migrate StatementEnd' marker.
See https://github.com/rubenv/sql-migrate for details.`, LineSeparator)
}
// Checks the line to see if the line has a statement-ending semicolon
// or if the line contains a double-dash comment.
func endsWithSemicolon(line string) bool {
prev := ""
scanner := bufio.NewScanner(strings.NewReader(line))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
word := scanner.Text()
if strings.HasPrefix(word, "--") {
break
}
prev = word
}
return strings.HasSuffix(prev, ";")
}
type migrationDirection int
const (
directionNone migrationDirection = iota
directionUp
directionDown
)
type migrateCommand struct {
Command string
Options []string
}
func (c *migrateCommand) HasOption(opt string) bool {
for _, specifiedOption := range c.Options {
if specifiedOption == opt {
return true
}
}
return false
}
func parseCommand(line string) (*migrateCommand, error) {
cmd := &migrateCommand{}
if !strings.HasPrefix(line, sqlCmdPrefix) {
return nil, fmt.Errorf("ERROR: not a sql-migrate command")
}
fields := strings.Fields(line[len(sqlCmdPrefix):])
if len(fields) == 0 {
return nil, fmt.Errorf(`ERROR: incomplete migration command`)
}
cmd.Command = fields[0]
cmd.Options = fields[1:]
return cmd, nil
}
// Split the given sql script into individual statements.
//
// The base case is to simply split on semicolons, as these
// naturally terminate a statement.
//
// However, more complex cases like pl/pgsql can have semicolons
// within a statement. For these cases, we provide the explicit annotations
// 'StatementBegin' and 'StatementEnd' to allow the script to
// tell us to ignore semicolons.
func ParseMigration(r io.ReadSeeker) (*ParsedMigration, error) {
p := &ParsedMigration{}
_, err := r.Seek(0, 0)
if err != nil {
return nil, err
}
var buf bytes.Buffer
scanner := bufio.NewScanner(r)
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
statementEnded := false
ignoreSemicolons := false
currentDirection := directionNone
for scanner.Scan() {
line := scanner.Text()
// ignore comment except beginning with '-- +'
if strings.HasPrefix(line, "-- ") && !strings.HasPrefix(line, "-- +") {
continue
}
// handle any migrate-specific commands
if strings.HasPrefix(line, sqlCmdPrefix) {
cmd, err := parseCommand(line)
if err != nil {
return nil, err
}
switch cmd.Command {
case "Up":
if len(strings.TrimSpace(buf.String())) > 0 {
return nil, errNoTerminator()
}
currentDirection = directionUp
if cmd.HasOption(optionNoTransaction) {
p.DisableTransactionUp = true
}
case "Down":
if len(strings.TrimSpace(buf.String())) > 0 {
return nil, errNoTerminator()
}
currentDirection = directionDown
if cmd.HasOption(optionNoTransaction) {
p.DisableTransactionDown = true
}
case "StatementBegin":
if currentDirection != directionNone {
ignoreSemicolons = true
}
case "StatementEnd":
if currentDirection != directionNone {
statementEnded = ignoreSemicolons
ignoreSemicolons = false
}
}
}
if currentDirection == directionNone {
continue
}
isLineSeparator := !ignoreSemicolons && len(LineSeparator) > 0 && line == LineSeparator
if !isLineSeparator && !strings.HasPrefix(line, "-- +") {
if _, err := buf.WriteString(line + "\n"); err != nil {
return nil, err
}
}
// Wrap up the two supported cases: 1) basic with semicolon; 2) psql statement
// Lines that end with semicolon that are in a statement block
// do not conclude statement.
if (!ignoreSemicolons && (endsWithSemicolon(line) || isLineSeparator)) || statementEnded {
statementEnded = false
switch currentDirection {
case directionUp:
p.UpStatements = append(p.UpStatements, buf.String())
case directionDown:
p.DownStatements = append(p.DownStatements, buf.String())
default:
panic("impossible state")
}
buf.Reset()
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
// diagnose likely migration script errors
if ignoreSemicolons {
return nil, fmt.Errorf("ERROR: saw '-- +migrate StatementBegin' with no matching '-- +migrate StatementEnd'")
}
if currentDirection == directionNone {
return nil, fmt.Errorf(`ERROR: no Up/Down annotations found, so no statements were executed.
See https://github.com/rubenv/sql-migrate for details.`)
}
// allow comment without sql instruction. Example:
// -- +migrate Down
// -- nothing to downgrade!
if len(strings.TrimSpace(buf.String())) > 0 && !strings.HasPrefix(buf.String(), "-- +") {
return nil, errNoTerminator()
}
return p, nil
}