diff --git a/.gitignore b/.gitignore index 8cdcb6ce..de8ae463 100644 --- a/.gitignore +++ b/.gitignore @@ -101,8 +101,8 @@ mysql-persistence/ # Dependency directories (remove the comment below to include it) # vendor/ -# built binary -pipeline-backend +# local protogen-go +protogen-go # air related files and folders .air.toml diff --git a/configs/config.yaml b/configs/config.yaml index fbd030a7..02c03c0b 100644 --- a/configs/config.yaml +++ b/configs/config.yaml @@ -15,7 +15,7 @@ database: host: localhost port: 5432 name: pipeline - version: 4 + version: 6 timezone: Etc/UTC pool: idleconnections: 5 diff --git a/go.mod b/go.mod index c76e8c7f..9f2a6cff 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/golang/mock v1.6.0 github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.3 - github.com/instill-ai/protogen-go v0.1.5-alpha + github.com/instill-ai/protogen-go v0.1.5-alpha.0.20220406002712-68dad8357630 github.com/instill-ai/vdp v0.1.3-alpha.0.20220320033117-c3e0db375b45 github.com/instill-ai/x v0.1.0-alpha github.com/knadh/koanf v1.4.0 diff --git a/go.sum b/go.sum index 661417fc..ec77b2c4 100644 --- a/go.sum +++ b/go.sum @@ -596,8 +596,8 @@ github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/instill-ai/protogen-go v0.1.3-alpha/go.mod h1:q2Pq4P0AY/59RGibT4nSDnOsA4wD4XhLueFRoGYNBjk= -github.com/instill-ai/protogen-go v0.1.5-alpha h1:rmovaQhNaTFGuZxXPWLpYX5E6jqS/akWWaJ+3yAx0Xo= -github.com/instill-ai/protogen-go v0.1.5-alpha/go.mod h1:q2Pq4P0AY/59RGibT4nSDnOsA4wD4XhLueFRoGYNBjk= +github.com/instill-ai/protogen-go v0.1.5-alpha.0.20220406002712-68dad8357630 h1:MrBHKXx1h26Doikv6FaRqpQYnsVSN8Qm4L3W7PMetBM= +github.com/instill-ai/protogen-go v0.1.5-alpha.0.20220406002712-68dad8357630/go.mod h1:q2Pq4P0AY/59RGibT4nSDnOsA4wD4XhLueFRoGYNBjk= github.com/instill-ai/vdp v0.1.3-alpha.0.20220320033117-c3e0db375b45 h1:MureMU/JCh3C0QwBVVgv1HsIPRcCN+wxqpg9vnvfutU= github.com/instill-ai/vdp v0.1.3-alpha.0.20220320033117-c3e0db375b45/go.mod h1:xcFgWKOHkqqkKMUkc5ftoD/5/hMSVR3qNhnnHxWzxYQ= github.com/instill-ai/x v0.1.0-alpha h1:cXzOGkHr+u1XdB8Pn6VHYb+hZX621mvlz7qBxZ29S5Y= diff --git a/integration-test/rest.js b/integration-test/rest.js index 92d5d060..211db760 100644 --- a/integration-test/rest.js +++ b/integration-test/rest.js @@ -17,6 +17,7 @@ const model_name = pipelineConstants.detectionModel.name; const det_model = open(`${__ENV.TEST_FOLDER_ABS_PATH}/integration-test/data/dummy-det-model.zip`, "b"); export let options = { + setupTimeout: '300s', insecureSkipTLSVerify: true, thresholds: { checks: ["rate == 1.0"], @@ -94,7 +95,7 @@ export default function (data) { { name: randomString(100), description: randomString(512), - active: true, + status: "STATUS_ACTIVE", }, pipelineConstants.detectionRecipe ); @@ -189,7 +190,7 @@ export default function (data) { let updatePipelineEntity = Object.assign( { description: randomString(512), - active: true, + status: "STATUS_ACTIVE", }, ); group("Pipelines API: Update a pipeline", () => { diff --git a/internal/db/db.go b/internal/db/db.go index c5cb2157..dc9f301b 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -7,6 +7,7 @@ import ( "gorm.io/driver/postgres" "gorm.io/gorm" + "gorm.io/gorm/schema" configs "github.com/instill-ai/pipeline-backend/configs" ) @@ -31,6 +32,9 @@ func GetConnection() *gorm.DB { PreferSimpleProtocol: true, // disables implicit prepared statement usage }), &gorm.Config{ QueryFields: true, // QueryFields mode will select by all fields’ name for current model + NamingStrategy: schema.NamingStrategy{ + SingularTable: true, + }, }) if err != nil { diff --git a/internal/db/migration/000001_init.down.sql b/internal/db/migration/000001_init.down.sql index 45765700..82767669 100644 --- a/internal/db/migration/000001_init.down.sql +++ b/internal/db/migration/000001_init.down.sql @@ -1,9 +1,5 @@ BEGIN; -ALTER TABLE "pipeline_history" DROP CONSTRAINT IF EXISTS "pipeline_history_fk_pipeline_id"; - -DROP TABLE IF EXISTS "pipeline_history"; - -DROP TABLE IF EXISTS "pipelines"; +DROP TABLE IF EXISTS pipelines; COMMIT; diff --git a/internal/db/migration/000001_init.up.sql b/internal/db/migration/000001_init.up.sql index d8974929..534a551a 100644 --- a/internal/db/migration/000001_init.up.sql +++ b/internal/db/migration/000001_init.up.sql @@ -1,35 +1,23 @@ BEGIN; -CREATE TABLE IF NOT EXISTS "pipelines" ( - "id" SERIAL PRIMARY KEY, - "ext_id" varchar(20) NOT NULL, - "name" varchar(256) NOT NULL, - "description" text, - "enabled" boolean NOT NULL DEFAULT (false), - "created_at" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - "updated_at" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - "deleted_at" timestamp, - "recipe" JSONB, - "crontab" varchar(13) +CREATE TABLE IF NOT EXISTS pipelines ( + id SERIAL PRIMARY KEY, + ext_id varchar(20) NOT NULL, + name varchar(256) NOT NULL, + description text, + enabled boolean NOT NULL DEFAULT (false), + created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, + deleted_at timestamp, + recipe JSONB, + crontab varchar(13) ); -CREATE TABLE IF NOT EXISTS "pipeline_history" ( - "pipeline_id" int NOT NULL, - "recipe" JSONB NOT NULL, - "version" int NOT NULL, - "created_at" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, - PRIMARY KEY ("pipeline_id", "version") -); - -COMMENT ON COLUMN "pipelines"."ext_id" IS 'the hash of the id'; -COMMENT ON COLUMN "pipelines"."name" IS 'name of this pipeline'; -COMMENT ON COLUMN "pipelines"."description" IS 'description of this pipeline'; -COMMENT ON COLUMN "pipelines"."enabled" IS 'activate/deactivate pipeline'; -COMMENT ON COLUMN "pipelines"."recipe" IS 'describe what the pipeline looks like'; -COMMENT ON COLUMN "pipelines"."crontab" IS 'the 6 * crontab format'; - -COMMENT ON COLUMN "pipeline_history"."recipe" IS 'describe what the pipeline looks like'; - -ALTER TABLE "pipeline_history" ADD CONSTRAINT "pipeline_history_fk_pipeline_id" FOREIGN KEY ("pipeline_id") REFERENCES "pipelines"("id"); +COMMENT ON COLUMN pipelines.ext_id IS 'the hash of the id'; +COMMENT ON COLUMN pipelines.name IS 'name of this pipeline'; +COMMENT ON COLUMN pipelines.description IS 'description of this pipeline'; +COMMENT ON COLUMN pipelines.enabled IS 'activate/deactivate pipeline'; +COMMENT ON COLUMN pipelines.recipe IS 'pipeline configration'; +COMMENT ON COLUMN pipelines.crontab IS 'the 6 * crontab format'; COMMIT; diff --git a/internal/db/migration/000002_create_column_creator_id.down.sql b/internal/db/migration/000002_create_column_creator_id.down.sql index 59370476..ff9550ac 100644 --- a/internal/db/migration/000002_create_column_creator_id.down.sql +++ b/internal/db/migration/000002_create_column_creator_id.down.sql @@ -1,5 +1,5 @@ BEGIN; -ALTER TABLE "pipelines" DROP "creator_id"; +ALTER TABLE pipelines DROP creator_id; COMMIT; diff --git a/internal/db/migration/000002_create_column_creator_id.up.sql b/internal/db/migration/000002_create_column_creator_id.up.sql index e41cfb25..0b41d091 100644 --- a/internal/db/migration/000002_create_column_creator_id.up.sql +++ b/internal/db/migration/000002_create_column_creator_id.up.sql @@ -1,5 +1,5 @@ BEGIN; -ALTER TABLE "pipelines" ADD "creator_id" varchar(36) NOT NULL; +ALTER TABLE pipelines ADD creator_id varchar(36) NOT NULL; COMMIT; diff --git a/internal/db/migration/000003_change_enabled_column_name.down.sql b/internal/db/migration/000003_change_enabled_column_name.down.sql index ce66f026..28d48e2f 100644 --- a/internal/db/migration/000003_change_enabled_column_name.down.sql +++ b/internal/db/migration/000003_change_enabled_column_name.down.sql @@ -1,5 +1,5 @@ BEGIN; -ALTER TABLE "pipelines" RENAME COLUMN "active" TO "enabled"; +ALTER TABLE pipelines RENAME COLUMN active TO enabled; COMMIT; diff --git a/internal/db/migration/000003_change_enabled_column_name.up.sql b/internal/db/migration/000003_change_enabled_column_name.up.sql index 8e4c9c27..906382e6 100644 --- a/internal/db/migration/000003_change_enabled_column_name.up.sql +++ b/internal/db/migration/000003_change_enabled_column_name.up.sql @@ -1,5 +1,5 @@ BEGIN; -ALTER TABLE "pipelines" RENAME COLUMN "enabled" TO "active"; +ALTER TABLE pipelines RENAME COLUMN enabled TO active; COMMIT; diff --git a/internal/db/migration/000004_human_readable_name.down.sql b/internal/db/migration/000004_human_readable_name.down.sql index 8fb0e6c9..fc3e5665 100644 --- a/internal/db/migration/000004_human_readable_name.down.sql +++ b/internal/db/migration/000004_human_readable_name.down.sql @@ -1,21 +1,19 @@ BEGIN; -ALTER TABLE "pipelines" ALTER COLUMN "id" SET DATA TYPE INT; +ALTER TABLE pipelines DROP CONSTRAINT unique_name_namespace; -ALTER TABLE "pipelines" DROP CONSTRAINT "unique_name_namespace"; +ALTER TABLE pipelines DROP COLUMN IF EXISTS namespace; -ALTER TABLE "pipelines" DROP COLUMN IF EXISTS "namespace"; +ALTER TABLE pipelines ADD ext_id varchar(20) NOT NULL DEFAULT '000004'; +COMMENT ON COLUMN pipelines.ext_id IS 'the hash of the id'; -ALTER TABLE "pipelines" ADD "ext_id" varchar(20) NOT NULL DEFAULT '000004'; -COMMENT ON COLUMN "pipelines"."ext_id" IS 'the hash of the id'; +ALTER TABLE pipelines ADD creator_id varchar(36) NOT NULL DEFAULT '000004'; -ALTER TABLE "pipelines" ADD "creator_id" varchar(36) NOT NULL DEFAULT '000004'; +UPDATE pipelines +SET ext_id = migration.ext_id, creator_id = migration.creator_id +FROM pipelines_migration AS migration +WHERE pipelines.id = migration.id; -UPDATE "pipelines" -SET "ext_id" = bak."ext_id", "creator_id" = bak."creator_id" -FROM "pipelines_bak" AS bak -WHERE "pipelines"."id" = bak."id"; - -DROP TABLE IF EXISTS "pipelines_bak"; +DROP TABLE IF EXISTS pipelines_migration; COMMIT; diff --git a/internal/db/migration/000004_human_readable_name.up.sql b/internal/db/migration/000004_human_readable_name.up.sql index 71a93521..174920e2 100644 --- a/internal/db/migration/000004_human_readable_name.up.sql +++ b/internal/db/migration/000004_human_readable_name.up.sql @@ -1,15 +1,13 @@ BEGIN; -CREATE TABLE IF NOT EXISTS "pipelines_bak" AS TABLE "pipelines"; +CREATE TABLE IF NOT EXISTS pipelines_migration AS TABLE pipelines; -ALTER TABLE "pipelines" DROP COLUMN IF EXISTS "ext_id"; -ALTER TABLE "pipelines" DROP COLUMN IF EXISTS "creator_id"; +ALTER TABLE pipelines DROP COLUMN IF EXISTS ext_id; +ALTER TABLE pipelines DROP COLUMN IF EXISTS creator_id; -ALTER TABLE "pipelines" ADD "namespace" varchar(39) NOT NULL DEFAULT 'undefined'; -COMMENT ON COLUMN "pipelines"."namespace" IS 'a set of pipelines'; +ALTER TABLE pipelines ADD namespace varchar(39) NOT NULL DEFAULT 'undefined'; +COMMENT ON COLUMN pipelines.namespace IS 'namespace in which the pipeline belongs to'; -ALTER TABLE "pipelines" ADD CONSTRAINT "unique_name_namespace" UNIQUE ("name", "deleted_at", "namespace"); - -ALTER TABLE "pipelines" ALTER COLUMN "id" SET DATA TYPE BIGINT; +ALTER TABLE pipelines ADD CONSTRAINT unique_name_namespace UNIQUE (name, deleted_at, namespace); COMMIT; diff --git a/internal/db/migration/000005_rename_pipeline_table_to_singular.down.sql b/internal/db/migration/000005_rename_pipeline_table_to_singular.down.sql new file mode 100644 index 00000000..5bd20802 --- /dev/null +++ b/internal/db/migration/000005_rename_pipeline_table_to_singular.down.sql @@ -0,0 +1,7 @@ +BEGIN; + +ALTER TABLE pipeline RENAME TO pipelines; + +ALTER TABLE pipelines RENAME CONSTRAINT pipeline_pkey TO pipelines_pkey; + +COMMIT; diff --git a/internal/db/migration/000005_rename_pipeline_table_to_singular.up.sql b/internal/db/migration/000005_rename_pipeline_table_to_singular.up.sql new file mode 100644 index 00000000..06c11c95 --- /dev/null +++ b/internal/db/migration/000005_rename_pipeline_table_to_singular.up.sql @@ -0,0 +1,7 @@ +BEGIN; + +ALTER TABLE pipelines RENAME TO pipeline; + +ALTER TABLE pipeline RENAME CONSTRAINT pipelines_pkey TO pipeline_pkey; + +COMMIT; diff --git a/internal/db/migration/000006_add_pipeline_status_column.down.sql b/internal/db/migration/000006_add_pipeline_status_column.down.sql new file mode 100644 index 00000000..815f394c --- /dev/null +++ b/internal/db/migration/000006_add_pipeline_status_column.down.sql @@ -0,0 +1,14 @@ +BEGIN; + +ALTER TABLE pipeline ALTER COLUMN status DROP DEFAULT; + +ALTER TABLE pipeline ALTER COLUMN status TYPE boolean +USING CASE WHEN status='STATUS_INACTIVE'::valid_status THEN FALSE ELSE TRUE END; + +ALTER TABLE pipeline ALTER COLUMN status SET DEFAULT FALSE; + +ALTER TABLE pipeline RENAME COLUMN status TO active; + +DROP TYPE valid_status; + +COMMIT; diff --git a/internal/db/migration/000006_add_pipeline_status_column.up.sql b/internal/db/migration/000006_add_pipeline_status_column.up.sql new file mode 100644 index 00000000..514a7710 --- /dev/null +++ b/internal/db/migration/000006_add_pipeline_status_column.up.sql @@ -0,0 +1,16 @@ +BEGIN; + +CREATE TYPE valid_status AS ENUM ('STATUS_INACTIVE', 'STATUS_ACTIVE', 'STATUS_ERROR'); + +ALTER TABLE pipeline RENAME COLUMN active TO status; + +ALTER TABLE pipeline ALTER COLUMN status DROP DEFAULT; + +ALTER TABLE pipeline ALTER COLUMN status TYPE valid_status +USING CASE WHEN status=FALSE THEN 'STATUS_INACTIVE'::valid_status ELSE 'STATUS_ACTIVE'::valid_status END; + +ALTER TABLE pipeline ALTER COLUMN status SET DEFAULT 'STATUS_INACTIVE'::valid_status; + +COMMENT ON COLUMN pipeline.status IS 'pipeline status'; + +COMMIT; diff --git a/pkg/datamodel/datamodel.go b/pkg/datamodel/datamodel.go index c2639358..93a3b88c 100644 --- a/pkg/datamodel/datamodel.go +++ b/pkg/datamodel/datamodel.go @@ -1,13 +1,14 @@ package datamodel import ( - "time" + "database/sql/driver" + "errors" "gorm.io/gorm" ) type Pipeline struct { - Id uint64 + gorm.Model // the name of this Instill Pipeline Name string @@ -15,16 +16,7 @@ type Pipeline struct { // the more detail of this Instill Pipeline Description string - Active bool `gorm:"type:tinyint"` - - // the time when entity created - CreatedAt time.Time `gorm:"type:timestamp"` - - // the time when entity has been updated - UpdatedAt time.Time `gorm:"type:timestamp"` - - // the time when entity has been deleted - DeletedAt gorm.DeletedAt + Status ValidStatus `sql:"type:valid_status"` Recipe *Recipe `gorm:"type:json"` @@ -50,3 +42,27 @@ type TriggerPipelineContent struct { Base64 string `json:"base64,omitempty"` Chunk []byte `json:"chunk,omitempty"` } + +type ValidStatus string + +const ( + StatusInactive ValidStatus = "STATUS_INACTIVE" + StatusActive ValidStatus = "STATUS_ACTIVE" + StatusError ValidStatus = "STATUS_ERROR" +) + +func (p *ValidStatus) Scan(value interface{}) error { + switch v := value.(type) { + case string: + *p = ValidStatus(v) + case []byte: + *p = ValidStatus(v) + default: + return errors.New("Incompatible type for ValidStatus") + } + return nil +} + +func (p ValidStatus) Value() (driver.Value, error) { + return string(p), nil +} diff --git a/pkg/datamodel/recipe.go b/pkg/datamodel/recipe.go index 5c27dd10..b34d26bd 100644 --- a/pkg/datamodel/recipe.go +++ b/pkg/datamodel/recipe.go @@ -23,7 +23,7 @@ type Destination struct { } type Model struct { - Name string `json:"model_name,omitempty"` + Name string `json:"model,omitempty"` Version uint64 `json:"version,omitempty"` } diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go index 854f8112..0be98408 100644 --- a/pkg/handler/handler.go +++ b/pkg/handler/handler.go @@ -75,7 +75,7 @@ func (s *handler) CreatePipeline(ctx context.Context, in *pipelinePB.CreatePipel entity := datamodel.Pipeline{ Name: in.Name, Description: in.Description, - Active: in.Active, + Status: datamodel.ValidStatus(in.Status.String()), Namespace: username, } if in.Recipe != nil { @@ -124,10 +124,10 @@ func (s *handler) ListPipeline(ctx context.Context, in *pipelinePB.ListPipelineR var nextCursor uint64 for _, pipeline := range pipelines { resp.Pipelines = append(resp.Pipelines, marshalPipeline(&pipeline)) - nextCursor = pipeline.Id + nextCursor = uint64(pipeline.ID) } - if min != nextCursor { + if uint64(min) != nextCursor { resp.NextPageToken = s.paginateTocken.Encode(nextCursor) } @@ -168,8 +168,8 @@ func (s *handler) UpdatePipeline(ctx context.Context, in *pipelinePB.UpdatePipel switch field { case "description": entity.Description = in.PipelinePatch.Description - case "active": - entity.Active = in.PipelinePatch.Active + case "status": + entity.Status = datamodel.ValidStatus(in.PipelinePatch.Status.String()) } if strings.Contains(field, "recipe") { entity.Recipe = unmarshalRecipe(in.PipelinePatch.Recipe) diff --git a/pkg/handler/marshal.go b/pkg/handler/marshal.go index f7d9452e..a0b63ac2 100644 --- a/pkg/handler/marshal.go +++ b/pkg/handler/marshal.go @@ -41,10 +41,10 @@ func marshalRecipe(recipe *datamodel.Recipe) *pipelinePB.Recipe { func marshalPipeline(pipeline *datamodel.Pipeline) *pipelinePB.Pipeline { ret := &pipelinePB.Pipeline{ - Id: pipeline.Id, + Id: uint64(pipeline.ID), Name: pipeline.Name, Description: pipeline.Description, - Active: pipeline.Active, + Status: pipelinePB.Pipeline_Status(pipelinePB.Pipeline_Status_value[string(pipeline.Status)]), CreatedAt: timestamppb.New(pipeline.CreatedAt), UpdatedAt: timestamppb.New(pipeline.UpdatedAt), FullName: pipeline.FullName, diff --git a/pkg/repository/repository.go b/pkg/repository/repository.go index 92d35278..dd7aa9aa 100644 --- a/pkg/repository/repository.go +++ b/pkg/repository/repository.go @@ -14,7 +14,7 @@ import ( type Repository interface { CreatePipeline(pipeline datamodel.Pipeline) error - ListPipelines(query datamodel.ListPipelineQuery) ([]datamodel.Pipeline, uint64, uint64, error) + ListPipelines(query datamodel.ListPipelineQuery) ([]datamodel.Pipeline, uint, uint, error) GetPipelineByName(namespace string, pipelineName string) (datamodel.Pipeline, error) UpdatePipeline(pipeline datamodel.Pipeline) error DeletePipeline(namespace string, pipelineName string) error @@ -30,27 +30,27 @@ func NewRepository(db *gorm.DB) Repository { } } -var GetPipelineSelectField = []string{ - `"pipelines"."id" as id`, - `"pipelines"."name"`, - `"pipelines"."description"`, - `"pipelines"."active"`, - `"pipelines"."created_at"`, - `"pipelines"."updated_at"`, - `'Pipeline' as kind`, - `CONCAT(namespace, '/', name) as full_name`, +var GetpipelineelectField = []string{ + "pipeline.id as id", + "pipeline.name", + "pipeline.description", + "pipeline.status", + "pipeline.created_at", + "pipeline.updated_at", + "Pipeline as kind", + "CONCAT(namespace, '/', name) as full_name", } var GetPipelineWithRecipeSelectField = []string{ - `"pipelines"."id" as id`, - `"pipelines"."name"`, - `"pipelines"."description"`, - `"pipelines"."active"`, - `"pipelines"."created_at"`, - `"pipelines"."updated_at"`, - `"pipelines"."recipe"`, - `'Pipeline' as kind`, - `CONCAT(namespace, '/', name) as full_name`, + "pipeline.id as id", + "pipeline.name", + "pipeline.description", + "pipeline.status", + "pipeline.created_at", + "pipeline.updated_at", + "pipeline.recipe", + "Pipeline as kind", + "CONCAT(namespace, '/', name) as full_name", } func (r *repository) CreatePipeline(pipeline datamodel.Pipeline) error { @@ -58,7 +58,7 @@ func (r *repository) CreatePipeline(pipeline datamodel.Pipeline) error { // We ignore the full_name column since it's a virtual column if result := r.db.Model(&datamodel.Pipeline{}). - Omit(`"pipelines"."full_name"`). + Omit("pipeline.full_name"). Create(&pipeline); result.Error != nil { l.Error(fmt.Sprintf("Error occur: %v", result.Error)) return status.Errorf(codes.Internal, "Error %v", result.Error) @@ -67,14 +67,14 @@ func (r *repository) CreatePipeline(pipeline datamodel.Pipeline) error { return nil } -func (r *repository) ListPipelines(query datamodel.ListPipelineQuery) ([]datamodel.Pipeline, uint64, uint64, error) { - var pipelines []datamodel.Pipeline +func (r *repository) ListPipelines(query datamodel.ListPipelineQuery) ([]datamodel.Pipeline, uint, uint, error) { + var pipeline []datamodel.Pipeline var count int64 r.db.Model(&datamodel.Pipeline{}).Where("namespace = ?", query.Namespace).Count(&count) - var min uint64 - var max uint64 + var min uint + var max uint if count > 0 { rows, err := r.db.Model(&datamodel.Pipeline{}). Select("MIN(id) AS min, MAX(id) as max"). @@ -104,17 +104,17 @@ func (r *repository) ListPipelines(query datamodel.ListPipelineQuery) ([]datamod Where("namespace = ? AND id < ?", query.Namespace, cursor). Limit(int(query.PageSize)). Order("id desc"). - Find(&pipelines) + Find(&pipeline) } else { r.db.Model(&datamodel.Pipeline{}). - Select(GetPipelineSelectField). + Select(GetpipelineelectField). Where("namespace = ? AND id < ?", query.Namespace, cursor). Limit(int(query.PageSize)). Order("id desc"). - Find(&pipelines) + Find(&pipeline) } - return pipelines, max, min, nil + return pipeline, max, min, nil } func (r *repository) GetPipelineByName(namespace string, pipelineName string) (datamodel.Pipeline, error) { @@ -125,7 +125,6 @@ func (r *repository) GetPipelineByName(namespace string, pipelineName string) (d First(&pipeline); result.Error != nil { return datamodel.Pipeline{}, status.Errorf(codes.NotFound, "The pipeline name %s you specified is not found", pipelineName) } - return pipeline, nil } @@ -134,7 +133,7 @@ func (r *repository) UpdatePipeline(pipeline datamodel.Pipeline) error { // We ignore the name column since it can not be updated if result := r.db.Model(&datamodel.Pipeline{}). - Omit(`"pipelines"."name"`). + Omit("pipeline.name"). Where("name = ? AND namespace = ?", pipeline.Name, pipeline.Namespace). Updates(pipeline); result.Error != nil { l.Error(fmt.Sprintf("Error occur: %v", result.Error)) diff --git a/pkg/service/mock_model_grpc_test.go b/pkg/service/mock_model_grpc_test.go index 880be032..d94a67f3 100644 --- a/pkg/service/mock_model_grpc_test.go +++ b/pkg/service/mock_model_grpc_test.go @@ -56,6 +56,26 @@ func (mr *MockModelServiceClientMockRecorder) CreateModelBinaryFileUpload(arg0 i return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateModelBinaryFileUpload", reflect.TypeOf((*MockModelServiceClient)(nil).CreateModelBinaryFileUpload), varargs...) } +// CreateModelByGitHub mocks base method. +func (m *MockModelServiceClient) CreateModelByGitHub(arg0 context.Context, arg1 *modelv1alpha.CreateModelByGitHubRequest, arg2 ...grpc.CallOption) (*modelv1alpha.CreateModelByGitHubResponse, error) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "CreateModelByGitHub", varargs...) + ret0, _ := ret[0].(*modelv1alpha.CreateModelByGitHubResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateModelByGitHub indicates an expected call of CreateModelByGitHub. +func (mr *MockModelServiceClientMockRecorder) CreateModelByGitHub(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateModelByGitHub", reflect.TypeOf((*MockModelServiceClient)(nil).CreateModelByGitHub), varargs...) +} + // DeleteModel mocks base method. func (m *MockModelServiceClient) DeleteModel(arg0 context.Context, arg1 *modelv1alpha.DeleteModelRequest, arg2 ...grpc.CallOption) (*modelv1alpha.DeleteModelResponse, error) { m.ctrl.T.Helper() diff --git a/pkg/service/mock_repository_test.go b/pkg/service/mock_repository_test.go index 35b1cd65..b45252e6 100644 --- a/pkg/service/mock_repository_test.go +++ b/pkg/service/mock_repository_test.go @@ -78,12 +78,12 @@ func (mr *MockRepositoryMockRecorder) GetPipelineByName(arg0, arg1 interface{}) } // ListPipelines mocks base method. -func (m *MockRepository) ListPipelines(arg0 datamodel.ListPipelineQuery) ([]datamodel.Pipeline, uint64, uint64, error) { +func (m *MockRepository) ListPipelines(arg0 datamodel.ListPipelineQuery) ([]datamodel.Pipeline, uint, uint, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ListPipelines", arg0) ret0, _ := ret[0].([]datamodel.Pipeline) - ret1, _ := ret[1].(uint64) - ret2, _ := ret[2].(uint64) + ret1, _ := ret[1].(uint) + ret2, _ := ret[2].(uint) ret3, _ := ret[3].(error) return ret0, ret1, ret2, ret3 } diff --git a/pkg/service/service.go b/pkg/service/service.go index 92839804..6b6e6a2f 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -22,7 +22,7 @@ import ( type Service interface { CreatePipeline(pipeline datamodel.Pipeline) (datamodel.Pipeline, error) - ListPipelines(query datamodel.ListPipelineQuery) ([]datamodel.Pipeline, uint64, uint64, error) + ListPipelines(query datamodel.ListPipelineQuery) ([]datamodel.Pipeline, uint, uint, error) GetPipelineByName(namespace string, pipelineName string) (datamodel.Pipeline, error) UpdatePipeline(pipeline datamodel.Pipeline) (datamodel.Pipeline, error) DeletePipeline(namespace string, pipelineName string) error @@ -82,7 +82,7 @@ func (p *service) CreatePipeline(pipeline datamodel.Pipeline) (datamodel.Pipelin } } -func (p *service) ListPipelines(query datamodel.ListPipelineQuery) ([]datamodel.Pipeline, uint64, uint64, error) { +func (p *service) ListPipelines(query datamodel.ListPipelineQuery) ([]datamodel.Pipeline, uint, uint, error) { return p.repository.ListPipelines(query) } @@ -131,8 +131,8 @@ func (p *service) ValidateTriggerPipeline(namespace string, pipelineName string, } // Pipeline is inactive - if !pipeline.Active { - return status.Error(codes.FailedPrecondition, "This pipeline has been deactivated") + if pipeline.Status == datamodel.StatusInactive { + return status.Error(codes.FailedPrecondition, "This pipeline is inactive") } // Pipeline not belong to this requester