Skip to content

Commit a03bc1e

Browse files
authored
Merge pull request #19 from upfluence/jl/source-io-fs
x/migration: add NewFSSource
2 parents babac1c + 715dca6 commit a03bc1e

File tree

8 files changed

+189
-17
lines changed

8 files changed

+189
-17
lines changed

.circleci/config.yml

-16
This file was deleted.

.github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @AlexisMontagne @upfluence/golang

.github/pull_request_template.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
### What does this PR do?
2+
3+
<!-- A brief description of the context of this pull request and its purpose. -->
4+
5+
Fixes #<!-- enter issue number here -->
6+
7+
### What are the observable changes?
8+
<!-- This question could be adequate with multiple use cases, for example: -->
9+
10+
<!-- Frontend: explain the feature created / updated, give instructions telling how to see the change in staging -->
11+
<!-- Performance: what metric should be impacted, link to the right graphana dashboard for exemple -->
12+
<!-- Bug: a given issue trail on sentry should stop happening -->
13+
<!-- Feature: Implements X thrift service / Z HTTP REST API added, provide instructions on how leverage your feature from staging or your workstation -->
14+
15+
### Good PR checklist
16+
17+
- [ ] Title makes sense
18+
- [ ] Is against the correct branch
19+
- [ ] Only addresses one issue
20+
- [ ] Properly assigned
21+
- [ ] Added/updated tests
22+
- [ ] Added/updated documentation
23+
- [ ] Properly labeled
24+
25+
### Additional Notes
26+
27+
<!--
28+
You can add anything you want here, an explanation on the way you built your implementation,
29+
precisions on the origin of the bug, gotchas you need to mention.
30+
-->

.github/workflows/ci.yml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
tags-ignore:
8+
- '**'
9+
10+
jobs:
11+
test:
12+
name: Run Tests
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
go: [ '1.16.x', '1.15.x', '1.14.x' ]
17+
services:
18+
postgres:
19+
image: postgres
20+
env:
21+
POSTGRES_USER: postgres
22+
POSTGRES_PASSWORD: postgres
23+
POSTGRES_DB: tata_test
24+
options: >-
25+
--health-cmd pg_isready
26+
--health-interval 10s
27+
--health-timeout 5s
28+
--health-retries 5
29+
ports:
30+
- 5432:5432
31+
32+
steps:
33+
- name: Install Go ${{ matrix.go }}
34+
uses: actions/setup-go@v2
35+
with:
36+
go-version: ${{ matrix.go }}
37+
38+
- name: Checkout
39+
uses: actions/checkout@v2
40+
- name: Cache Modules
41+
uses: actions/cache@v2
42+
with:
43+
path: ~/go/pkg/mod
44+
key: ${{ runner.os }}-go-v1-${{ hashFiles('**/go.sum') }}
45+
restore-keys: |
46+
${{ runner.os }}-go-
47+
- name: Run tests
48+
run: go test -p 1 -v ./...
49+
env:
50+
POSTGRES_URL: postgres://postgres:postgres@localhost:5432/tata_test?sslmode=disable

.github/workflows/lint.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: reviewdog
2+
on: [pull_request]
3+
4+
jobs:
5+
lint:
6+
name: runner / golangci-lint
7+
runs-on: ubuntu-latest
8+
timeout-minutes: 30
9+
steps:
10+
- name: Install Go
11+
uses: actions/setup-go@v2
12+
with:
13+
go-version: 1.16.x
14+
- name: Check out code
15+
uses: actions/checkout@v1
16+
- name: golanci-lint
17+
uses: upfluence/action-golangci-lint@master
18+
with:
19+
github_token: ${{ secrets.github_token }}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/upfluence/sql
22

3-
go 1.14
3+
go 1.16
44

55
require (
66
github.com/lib/pq v1.4.0

x/migration/fs_source.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// +build go1.16
2+
3+
package migration
4+
5+
import (
6+
"io/fs"
7+
8+
"github.com/upfluence/log"
9+
)
10+
11+
func MustFSSource(fs fs.FS, logger log.Logger) Source {
12+
s, err := NewFSSource(fs, logger)
13+
14+
if err != nil {
15+
panic(err)
16+
}
17+
18+
return s
19+
}
20+
21+
func NewFSSource(src fs.FS, logger log.Logger) (Source, error) {
22+
files, err := fs.ReadDir(src, ".")
23+
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
vs := make([]string, len(files))
29+
30+
for i, f := range files {
31+
vs[i] = f.Name()
32+
}
33+
34+
return NewStaticSource(
35+
vs,
36+
func(file string) ([]byte, error) {
37+
return fs.ReadFile(src, file)
38+
},
39+
logger,
40+
), nil
41+
}

x/migration/fs_source_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// +build go1.16
2+
3+
package migration
4+
5+
import (
6+
"context"
7+
"testing"
8+
"testing/fstest"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
"github.com/upfluence/log"
13+
)
14+
15+
func TestFSSource(t *testing.T) {
16+
var (
17+
ctx = context.Background()
18+
fs = fstest.MapFS{
19+
"3_final.down.sql": &fstest.MapFile{Data: []byte("bar")},
20+
"2_initial.up.postgres": &fstest.MapFile{Data: []byte("foo")},
21+
"3_final.up.sql": &fstest.MapFile{Data: []byte("bar")},
22+
}
23+
)
24+
25+
s, err := NewFSSource(fs, log.NewLogger(log.WithSink(sink{})))
26+
require.NoError(t, err)
27+
28+
m, err := s.First(ctx)
29+
require.NoError(t, err)
30+
31+
assertMigration(t, m, fetchDriver("postgres"), 2, "foo", "")
32+
assertMigration(t, m, fetchDriver("sqlite3"), 2, "", "")
33+
34+
_, id, _ := s.Next(ctx, 2)
35+
36+
assert.Equal(t, uint(3), id)
37+
38+
m, _ = s.Get(ctx, id)
39+
assertMigration(t, m, fetchDriver("postgres"), 3, "bar", "bar")
40+
41+
ok, _, _ := s.Prev(ctx, 2)
42+
assert.False(t, ok)
43+
44+
ok, _, err = s.Next(ctx, 3)
45+
assert.NoError(t, err)
46+
assert.False(t, ok)
47+
}

0 commit comments

Comments
 (0)