Skip to content

Commit

Permalink
test for github actions, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
o.i.musin committed Jun 28, 2023
1 parent 4cb4024 commit 66daab3
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 109 deletions.
28 changes: 0 additions & 28 deletions .github/workflows/go.yml

This file was deleted.

47 changes: 47 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Test

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build&test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.20'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
lint:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
- name: setup-go
uses: actions/setup-go@v4
with:
go-version: "1.20"
cache: true
cache-dependency-path: go.sum
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.52.2
- name: go mod tidy
run: go mod tidy
- name: check for any changes
run: |
[[ $(git status --porcelain) == "" ]] || (echo "changes detected" && exit 1)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (C) 2023-2025 by Oleg Musin <[email protected]>

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 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Migradaptor
# migradaptor
> Tool for adapting migration files for different library formats. Current version allows to adapt [rubenv/sql-migrate](https://github.com/rubenv/sql-migrate) files to [golang-migrate](https://github.com/golang-migrate).
[![Test](https://github.com/musinit/migradaptor/actions/workflows/test.yml/badge.svg)](https://github.com/musinit/migradaptor/actions/workflows/test.yml)

About
---------
Expand Down Expand Up @@ -26,5 +29,9 @@ Use
migradaptor -src={source_folder} -dst={destination_folder}
```

TODO
## Questions or Feedback?

You can use GitHub Issues for feedback or questions.

## TODO
- processing multiple concurrent indexes in single file, splitting the file by the number of such CREATE INDEX CONCURRENTLY commands.
10 changes: 3 additions & 7 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

var (
sqlExt = regexp.MustCompile("\\.sql$")
concurrentIndexesGroupReg = regexp.MustCompile("(?:(CREATE INDEX CONCURRENTLY)\\s+(?P<indexName>\\w+).+?;)")
sqlExt = regexp.MustCompile(`\\.sql$`)
concurrentIndexesGroupReg = regexp.MustCompile(`(?:(CREATE INDEX CONCURRENTLY)\\s+(?P<indexName>\\w+).+?;)`)
specialCharsMap = map[rune]struct{}{
rune('\t'): {},
rune('\n'): {},
Expand Down Expand Up @@ -41,13 +41,9 @@ func isKeyExists(reg *regexp.Regexp, source string) bool {
return len(fileparts) != 0
}

func IsSubstringExists(source, substr string) bool {
return strings.Index(source, substr) >= 0
}

func RemoveSpecialCharacters(s string) string {
result := s
for k, _ := range specialCharsMap {
for k := range specialCharsMap {
result = strings.ReplaceAll(result, string(k), "")
}
result = strings.TrimSpace(result)
Expand Down
55 changes: 3 additions & 52 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package builder_test

import (
"errors"
"reflect"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -174,7 +174,6 @@ func TestValidateInput(t *testing.T) {
sourceType *string
srcPath *string
dstPath *string
err error
}
tests := []struct {
name string
Expand Down Expand Up @@ -260,54 +259,6 @@ func TestValidateInput(t *testing.T) {
}
}

func TestFindUniqueConcurrentIdxStatements(t *testing.T) {
type args struct {
lineJoin string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "several concurrent index with drop line",
args: args{
lineJoin: `
DROP INDEX companies_id_idx;
CREATE INDEX CONCURRENTLY companies_id_idx ON companies (id);
DROP INDEX companies_title_idx;
CREATE INDEX CONCURRENTLY companies_title_idx ON companies (title);
DROP INDEX clients_id_idx;
CREATE INDEX CONCURRENTLY clients_id_idx ON clients;`,
},
want: []string{
"CREATE INDEX CONCURRENTLY companies_id_idx ON companies (id);",
"CREATE INDEX CONCURRENTLY companies_title_idx ON companies (title);",
"CREATE INDEX CONCURRENTLY clients_id_idx ON clients;",
},
},
{
name: "one concurrent without drop line",
args: args{
lineJoin: `
CREATE INDEX CONCURRENTLY companies_id_idx ON companies (id);`,
},
want: []string{
"CREATE INDEX CONCURRENTLY companies_id_idx ON companies (id);",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := builder.FindUniqueConcurrentIdxStatements(tt.args.lineJoin); !reflect.DeepEqual(got, tt.want) {
t.Errorf("FindUniqueConcurrentIdxStatements() = %v, want %v", got, tt.want)
}
})
}
}

func TestIsSubstringExists(t *testing.T) {
type args struct {
source string
Expand Down Expand Up @@ -357,8 +308,8 @@ func TestIsSubstringExists(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := builder.IsSubstringExists(tt.args.source, tt.args.substr); got != tt.want {
t.Errorf("IsSubstringExists() = %v, want %v", got, tt.want)
if got := strings.Contains(tt.args.source, tt.args.substr); got != tt.want {
t.Errorf("strings.Contains() = %v, want %v", got, tt.want)
}
})
}
Expand Down
25 changes: 12 additions & 13 deletions builder/rubenv-sql-migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/pkg/errors"
)
Expand All @@ -17,35 +18,33 @@ var (
)

var (
filenameReg = regexp.MustCompile("(\\d{0,15})-(.*)(.sql)")
filenameReg = regexp.MustCompile(`(\\d{0,15})-(.*)(.sql)`)
)

func BuildMigrationData(lines []string) ([]string, []string) {
upLines, downLines := make([]string, 0, len(lines)/2), make([]string, 0, len(lines)/2)
isUpTx := true
upTransactionMode, downTransactionMode := false, false
for _, line := range lines {
upMigrationLine := IsSubstringExists(line, MigrationUpCmd)
downMigrationLine := IsSubstringExists(line, MigrationDownCmd)
upMigrationLine := strings.Contains(line, MigrationUpCmd)
downMigrationLine := strings.Contains(line, MigrationDownCmd)
switch {
case upMigrationLine:
if !IsSubstringExists(line, NoTransactionCmd) {
if !strings.Contains(line, NoTransactionCmd) {
upTransactionMode = true
upLines = append(upLines, "BEGIN;\n")
}
isUpTx = true
break
case downMigrationLine:
if upTransactionMode {
upLines = append(upLines, "COMMIT;\n")
}
if !IsSubstringExists(line, NoTransactionCmd) {
if !strings.Contains(line, NoTransactionCmd) {
downTransactionMode = true
downLines = append(downLines, "BEGIN;\n")
}
isUpTx = false
break
case !(IsSubstringExists(line, StatementBeginCmd) || IsSubstringExists(line, StatementEndCmd)):
case !(strings.Contains(line, StatementBeginCmd) || strings.Contains(line, StatementEndCmd)):
if isUpTx {
upLines = append(upLines, line)
} else {
Expand All @@ -62,22 +61,22 @@ func BuildMigrationData(lines []string) ([]string, []string) {
return upLines, downLines
}

func ParseFilename(filename string) (int64, string) {
func ParseFilename(filename string) (int64, string, error) {
fileparts := filenameReg.FindAllStringSubmatch(filename, 10)
if !isKeyExists(filenameReg, filename) {
panic("no parts")
return 0, "", errors.New("parse fileparts: filename not match")
}
fel := fileparts[0]
// {timestamp}-{name}.sql format is expected
// 1 - timestamp, 2 - name
if len(fel) < 2 {
panic(errors.New("can't parse file " + filename))
return 0, "", errors.New("parse file")
}
ts := fel[1]
name := fel[2]
tsInt, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
panic(err)
return 0, "", errors.Wrap(err, "parse timestamp")
}
return tsInt, name
return tsInt, name, nil
}
7 changes: 6 additions & 1 deletion builder/rubenv-sql-migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,37 @@ func Test_ParseFilename(t *testing.T) {
input string
expectedTimestamp int64
extectedName string
expectedErr error
}{
{
"1-test.sql",
"1-test.sql",
1,
"test",
nil,
},
{
"1-1_initial.sql",
"1-1_initial.sql",
1,
"1_initial",
nil,
},
{
"1-_.sql",
"1-_.sql",
1,
"_",
nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ts, name := builder.ParseFilename(tc.input)
ts, name, err := builder.ParseFilename(tc.input)
require.True(t, ts == tc.expectedTimestamp)
require.True(t, name == tc.extectedName)
require.ErrorIs(t, err, tc.expectedErr)
})
}
}
Loading

0 comments on commit 66daab3

Please sign in to comment.