-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #13 setup Makefile to generate proto * #13 rename file to image * #13 implement GetBoolEnv * #13 create pkg and move env loader * #13 create request validations * #13 implement error convertors * #13 change service name * #13 add uuid * #13 fix test rule * #13 remove .gitkeep * #13 separate required value getter * #13 ignore env files * #13 implement repository and setup migration * #13 implement upload image function * #13 setup local and test database * #13 refresh mod file * #13 import driver library * #13 add device id to request * #13 add device id to request * #13 fix failed to use transaction * #13 change pb package name * #13 use yaml for config * #13 use yaml for config * #13 setup ci * #13 fix ci config * #13 down go version on ci * #13 upgrade actions/setup-go * #13 down go version on ci * #13 change ci config * #13 change ci config * #13 change ci config * #13 change ci config * #13 change ci config * #13 change ci config * #13 change ci config * #13 change ci config
- Loading branch information
Showing
70 changed files
with
7,297 additions
and
886 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,12 @@ | |
go.work | ||
|
||
# ide | ||
.idea | ||
.idea | ||
|
||
# dotenv | ||
.env | ||
.envrc | ||
|
||
docker/minio/data/** | ||
docker/mysql/db/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
MIGRATION = "" | ||
|
||
gen-proto: | ||
protoc --proto_path=protoc --go_out=./pb --go_opt=paths=source_relative --go-grpc_out=./pb --go-grpc_opt=paths=source_relative protoc/**/v1/*.proto | ||
|
||
test: | ||
go test -v ./... | ||
|
||
gen-schema: | ||
sqlboiler mysql -c database.toml -o ./adaptor/repository/mysql/shcema -p schema --no-tests --wipe | ||
|
||
gen-migration: | ||
migrate create -ext sql -dir ./db/migration -seq ${MIGRATION} | ||
|
||
migrate-up: | ||
migrate --path db/migration --database "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/${MYSQL_DATABASE}" --verbose up | ||
|
||
migrate-down: | ||
migrate --path db/migration --database "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/${MYSQL_DATABASE}" --verbose down | ||
|
||
migrate-drop: | ||
migrate --path db/migration --database "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/${MYSQL_DATABASE}" --verbose drop |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package mysql | ||
|
||
import ( | ||
"context" | ||
"github.com/CityBear3/satellite/adaptor/repository/mysql/shcema" | ||
"github.com/CityBear3/satellite/domain/model" | ||
"github.com/oklog/ulid/v2" | ||
"github.com/volatiletech/sqlboiler/v4/boil" | ||
) | ||
|
||
type ArchiveRepository struct{} | ||
|
||
func NewArchiveRepository() *ArchiveRepository { | ||
return &ArchiveRepository{} | ||
} | ||
|
||
func (i *ArchiveRepository) Save(ctx context.Context, archive model.Archive) error { | ||
tx, err := GetTransaction(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
archiveSchema := schema.Archive{ | ||
ID: archive.Id.String(), | ||
Size: archive.Size, | ||
Ext: archive.Ext, | ||
DeviceID: archive.DeviceId.String(), | ||
} | ||
if err := archiveSchema.Upsert(ctx, tx, boil.Infer(), boil.Infer()); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (i *ArchiveRepository) GetArchive(ctx context.Context, imageId ulid.ULID) (*model.Archive, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package mysql | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"github.com/CityBear3/satellite/domain/model" | ||
"github.com/CityBear3/satellite/tests/helper" | ||
"github.com/CityBear3/satellite/tests/table" | ||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/oklog/ulid/v2" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/volatiletech/sqlboiler/v4/boil" | ||
"testing" | ||
) | ||
|
||
func setTx(ctx context.Context, tx *sql.Tx) context.Context { | ||
return context.WithValue(ctx, "tx", tx) | ||
} | ||
|
||
func TestArchiveRepository_Save(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
archive model.Archive | ||
} | ||
type test struct { | ||
name string | ||
args args | ||
wantErr bool | ||
expectedErr string | ||
tables []helper.TableOperator | ||
queries []string | ||
} | ||
|
||
db, err := helper.GetTestDB() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func(db *sql.DB) { | ||
err := db.Close() | ||
if err != nil { | ||
panic(err) | ||
} | ||
}(db) | ||
boil.SetDB(db) | ||
|
||
sut := NewArchiveRepository() | ||
|
||
clientId := ulid.Make() | ||
deviceId := ulid.Make() | ||
archiveId := ulid.Make() | ||
|
||
tests := []test{ | ||
{ | ||
name: "insert image", | ||
args: args{ | ||
ctx: context.Background(), | ||
archive: model.Archive{ | ||
Id: archiveId, | ||
Size: 1024, | ||
Ext: "jpg", | ||
DeviceId: deviceId, | ||
}, | ||
}, | ||
wantErr: false, | ||
tables: []helper.TableOperator{ | ||
table.ClientTable{ | ||
Id: clientId.String(), | ||
Name: "test", | ||
Description: "test", | ||
Secret: "", | ||
}, | ||
table.DeviceTable{ | ||
Id: deviceId.String(), | ||
Name: "test", | ||
Description: "test", | ||
Secret: "", | ||
ClientId: clientId.String(), | ||
}, | ||
}, | ||
queries: []string{ | ||
"SELECT * FROM `archive` WHERE `id`=?", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tx, err := boil.BeginTx(tt.args.ctx, nil) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
ctx := setTx(tt.args.ctx, tx) | ||
for _, operator := range tt.tables { | ||
if err := operator.Insert(ctx, tx); err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
} | ||
|
||
savedResult := table.ArchiveTable{} | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err = sut.Save(ctx, tt.args.archive); err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
err := tx.QueryRowContext(ctx, tt.queries[0], tt.args.archive.Id.String()).Scan(&savedResult.Id, &savedResult.DeviceId, &savedResult.Size, &savedResult.Ext, &savedResult.CreatedAt, &savedResult.UpdatedAt) | ||
if err != nil { | ||
t.Error(err) | ||
} else { | ||
assert.Equal(t, tt.args.archive.Id.String(), savedResult.Id) | ||
assert.Equal(t, tt.args.archive.Size, savedResult.Size) | ||
assert.Equal(t, tt.args.archive.Ext, savedResult.Ext) | ||
} | ||
|
||
t.Cleanup(func() { | ||
if err = tx.Rollback(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.