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

MAIN-33561: Update goreleaser #972

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 42 additions & 41 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,55 @@
# Check https://circleci.com/docs/2.0/language-go/ for more details
version: 2.1

jobs:
"golang-1_15": &template
machine:
# https://circleci.com/docs/2.0/configuration-reference/#available-machine-images
image: ubuntu-2004:202010-01
# docker_layer_caching: true

# https://circleci.com/docs/2.0/configuration-reference/#resource_class
resource_class: medium
executors:
golang-1_16:
docker:
- image: cimg/go:1.16

# Leave working directory unspecified and use defaults:
# https://circleci.com/blog/go-v1.11-modules-and-circleci/
# working_directory: /go/src/github.com/golang-migrate/migrate
orbs:
artifactory: circleci/[email protected]
gotools: gotest/[email protected]

environment:
GO111MODULE: "on"
GO_VERSION: "1.15.x"
commands:
setup_goenv:
steps:
- run:
name: configure go environment
command: |
go env -w GOPROXY="https://circleci:${ARTIFACTORY_APIKEY}@hqo.jfrog.io/artifactory/api/go/go,direct"
go env -w GOPRIVATE="github.com/HqOapp/*"
go env -w GONOSUMDB="github.com/HqOapp/*"
go env -w GO111MODULE="auto"

jobs:
build_and_push_image:
executor: golang-1_16
steps:
# - setup_remote_docker:
# version: 19.03.13
# docker_layer_caching: true
- run: curl -sL -o ~/bin/gimme https://raw.githubusercontent.com/travis-ci/gimme/master/gimme
- run: curl -sfL -o ~/bin/golangci-lint.sh https://install.goreleaser.com/github.com/golangci/golangci-lint.sh
- run: chmod +x ~/bin/gimme ~/bin/golangci-lint.sh
- run: eval "$(gimme $GO_VERSION)"
- run: golangci-lint.sh -b ~/bin v1.37.0
- setup_remote_docker:
docker_layer_caching: true
- checkout
- restore_cache:
keys:
- go-mod-v1-{{ arch }}-{{ checksum "go.sum" }}
- run: golangci-lint run
- run: make test COVERAGE_DIR=/tmp/coverage
- save_cache:
key: go-mod-v1-{{ arch }}-{{ checksum "go.sum" }}
paths:
- "/go/pkg/mod"
- run: go get github.com/mattn/goveralls
- run: goveralls -service=circle-ci -coverprofile /tmp/coverage/combined.txt

"golang-1_16":
<<: *template
environment:
GO_VERSION: "1.16.x"
- artifactory/install
- artifactory/docker-login:
artifactory-key: ARTIFACTORY_APIKEY
docker-registry: hqo-docker.jfrog.io
- run:
name: Build & Tag Migrate Image
command: docker build . -t hqo-docker.jfrog.io/migrate:$CIRCLE_SHA1 -t hqo-docker.jfrog.io/migrate:latest --progress=plain
- run:
name: Publish migrate image with commit hash tag
command: docker push hqo-docker.jfrog.io/migrate:$CIRCLE_SHA1
- when: # master branch
condition:
equal: [master, << pipeline.git.branch >>]
steps:
- run:
name: Publish latest migrate image
command: docker push hqo-docker.jfrog.io/migrate:latest

workflows:
version: 2
build:
jobs:
- "golang-1_15"
- "golang-1_16"
- build_and_push_image:
name: Build Migrate
context: artifactory
5 changes: 3 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ jobs:
- run: echo "SOURCE=$(make echo-source)" >> $GITHUB_ENV
- run: echo "DATABASE=$(make echo-database)" >> $GITHUB_ENV

- uses: goreleaser/goreleaser-action@v2
- uses: goreleaser/goreleaser-action@v4
with:
distrubution: goreleaser
version: latest
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down
55 changes: 55 additions & 0 deletions .github/workflows/upstream-sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: 'Upstream Sync'

on:
schedule:
# scheduled at 10:00 every Monday and Thursday
- cron: '0 10 * * 1,4'

workflow_dispatch: # click the button on GitHub repo!

jobs:
sync_latest_from_upstream:
runs-on: ubuntu-latest
name: Sync latest commits from upstream repo

steps:
# REQUIRED
# Step 1: run a standard checkout action, provided by GitHub
- name: Checkout target repo
uses: actions/checkout@v2
with:
# optional: set the branch to checkout,
# sync action checks out your 'target_sync_branch' anyway
ref: master
# REQUIRED if your upstream repo is private (see wiki)
persist-credentials: false
fetch-depth: 0

# REQUIRED
# Step 2: run the sync action
- name: Sync upstream changes
id: sync
uses: aormsby/[email protected]
with:
target_sync_branch: master
# REQUIRED 'target_repo_token' exactly like this!
target_repo_token: ${{ secrets.GITHUB_TOKEN }}
upstream_sync_branch: master
upstream_sync_repo: golang-migrate/migrate
upstream_pull_args: '--allow-unrelated-histories'
# upstream_repo_access_token: ${{ secrets.UPSTREAM_REPO_SECRET }}

# Set test_mode true to run tests instead of the true action!!
test_mode: false

# Step 3: Display a sample message based on the sync output var 'has_new_commits'
- name: New commits found
if: steps.sync.outputs.has_new_commits == 'true'
run: echo "New commits were found to sync."

- name: No new commits
if: steps.sync.outputs.has_new_commits == 'false'
run: echo "There were no new commits."

- name: Show value of 'has_new_commits'
run: echo ${{ steps.sync.outputs.has_new_commits }}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG VERSION

RUN apk add --no-cache git gcc musl-dev make

WORKDIR /go/src/github.com/golang-migrate/migrate
WORKDIR /go/src/github.com/HqOapp/migrate

ENV GO111MODULE=on

Expand All @@ -19,7 +19,7 @@ FROM alpine:3.18

RUN apk add --no-cache ca-certificates

COPY --from=builder /go/src/github.com/golang-migrate/migrate/build/migrate.linux-386 /usr/local/bin/migrate
COPY --from=builder /go/src/github.com/HqOapp/migrate/build/migrate.linux-386 /usr/local/bin/migrate
RUN ln -s /usr/local/bin/migrate /migrate

ENTRYPOINT ["migrate"]
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.circleci
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM $DOCKER_IMAGE

RUN apk add --no-cache git gcc musl-dev make

WORKDIR /go/src/github.com/golang-migrate/migrate
WORKDIR /go/src/github.com/HqOapp/migrate

ENV GO111MODULE=on
ENV COVERAGE_DIR=/tmp/coverage
Expand Down
5 changes: 4 additions & 1 deletion MIGRATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ used for them. `migrate` expects the filenames of migrations to have the format

{version}_{title}.up.{extension}
{version}_{title}.down.{extension}
{version}_{title}.alwaysup.{extension}
{version}_{title}.alwaysdown.{extension}

The `title` of each migration is unused, and is only for readability. Similarly,
the `extension` of the migration files is not checked by the library, and should
be an appropriate format for the database in use (`.sql` for SQL variants, for
instance).
instance). The `always` designation will run the migration file each time. This
is useful for stored procedure and custom type definitions.

Versions of migrations may be represented as any 64 bit unsigned integer.
All migrations are applied upward in order of increasing version number, and
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ Each migration has an up and down migration. [Why?](FAQ.md#why-two-separate-file
```bash
1481574547_create_users_table.up.sql
1481574547_create_users_table.down.sql
1481574547_stored_procs.alwaysup.sql
1481574547_stored_procs.alwaysdown.sql
```

[Best practices: How to write migrations.](MIGRATIONS.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere.
13 changes: 9 additions & 4 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,13 @@ func (m *Migrate) runMigrations(ret <-chan interface{}) error {

case *Migration:
migr := r
once := r.Direction == source.Up || r.Direction == source.Down

// set version with dirty state
if err := m.databaseDrv.SetVersion(migr.TargetVersion, true); err != nil {
return err
if once {
if err := m.databaseDrv.SetVersion(migr.TargetVersion, true); err != nil {
return err
}
}

if migr.Body != nil {
Expand All @@ -749,8 +752,10 @@ func (m *Migrate) runMigrations(ret <-chan interface{}) error {
}

// set clean state
if err := m.databaseDrv.SetVersion(migr.TargetVersion, false); err != nil {
return err
if once {
if err := m.databaseDrv.SetVersion(migr.TargetVersion, false); err != nil {
return err
}
}

endTime := time.Now()
Expand Down
21 changes: 21 additions & 0 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"time"

"github.com/golang-migrate/migrate/v4/source"
)

// DefaultBufferSize sets the in memory buffer size (in Bytes) for every
Expand All @@ -27,6 +31,9 @@ type Migration struct {
// Can be -1, implying that this is a NilVersion.
TargetVersion int

// Direction is either Up, Down, AlwaysUp, or AlwaysDown.
Direction source.Direction

// Body holds an io.ReadCloser to the source.
Body io.ReadCloser

Expand Down Expand Up @@ -94,6 +101,20 @@ func NewMigration(body io.ReadCloser, identifier string,
return m, nil
}

m.Direction = source.Up
if version > uint(targetVersion) {
m.Direction = source.Down
}

if ft, ok := body.(*os.File); ok {
_, f := filepath.Split(ft.Name())
mp, mpErr := source.Parse(f)
if mpErr != nil {
return m, mpErr
}
m.Direction = mp.Direction
}

br, bw := io.Pipe()
m.Body = body // want to simulate low latency? newSlowReader(body)
m.BufferSize = DefaultBufferSize
Expand Down
16 changes: 13 additions & 3 deletions source/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
type Direction string

const (
Down Direction = "down"
Up Direction = "up"
Down Direction = "down"
Up Direction = "up"
AlwaysUp Direction = "alwaysup"
AlwaysDown Direction = "alwaysdown"
)

// Migration is a helper struct for source drivers that need to
Expand All @@ -23,7 +25,7 @@ type Migration struct {
// this migration in the source.
Identifier string

// Direction is either Up or Down.
// Direction is either Up, Down, AlwaysUp, or AlwaysDown.
Direction Direction

// Raw holds the raw location path to this migration in source.
Expand Down Expand Up @@ -103,6 +105,9 @@ func (i *Migrations) Up(version uint) (m *Migration, ok bool) {
if mx, ok := i.migrations[version][Up]; ok {
return mx, true
}
if mx, ok := i.migrations[version][AlwaysUp]; ok {
return mx, true
}
}
return nil, false
}
Expand All @@ -113,6 +118,11 @@ func (i *Migrations) Down(version uint) (m *Migration, ok bool) {
return mx, true
}
}
if _, ok := i.migrations[version]; ok {
if mx, ok := i.migrations[version][AlwaysDown]; ok {
return mx, true
}
}
return nil, false
}

Expand Down
8 changes: 5 additions & 3 deletions source/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

// Regex matches the following pattern:
//
// 123_name.up.ext
// 123_name.down.ext
var Regex = regexp.MustCompile(`^([0-9]+)_(.*)\.(` + string(Down) + `|` + string(Up) + `)\.(.*)$`)
// 123_name.up.ext

Check failure on line 20 in source/parse.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
// 123_name.down.ext
// 123_name.alwaysup.ext
// 123_name.alwaysdown.ext
var Regex = regexp.MustCompile(`^([0-9]+)_(.*)\.(` + string(Down) + `|` + string(Up) + `|` + string(AlwaysDown) + `|` + string(AlwaysUp) + `)\.(.*)$`)

// Parse returns Migration for matching Regex pattern.
func Parse(raw string) (*Migration, error) {
Expand Down
20 changes: 20 additions & 0 deletions source/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ func TestParse(t *testing.T) {
Raw: "1_foobar.down.sql",
},
},
{
name: "1_foobar.alwaysup.sql",
expectErr: nil,
expectMigration: &Migration{
Version: 1,
Identifier: "foobar",
Direction: AlwaysUp,
Raw: "1_foobar.alwaysup.sql",
},
},
{
name: "1_foobar.alwaysdown.sql",
expectErr: nil,
expectMigration: &Migration{
Version: 1,
Identifier: "foobar",
Direction: AlwaysDown,
Raw: "1_foobar.alwaysdown.sql",
},
},
{
name: "1_f-o_ob+ar.up.sql",
expectErr: nil,
Expand Down
Loading