Skip to content

Commit b3853f6

Browse files
authored
Merge pull request #8517 from dolthub/db/dolt-ci-init
[no-release-notes] add dolt ci init and destroy
2 parents 57cbe40 + c07ba36 commit b3853f6

23 files changed

+2677
-12
lines changed

go/cmd/dolt/commands/ci/ci.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2024 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ci
16+
17+
import (
18+
"github.com/dolthub/dolt/go/cmd/dolt/cli"
19+
)
20+
21+
var Commands = cli.NewHiddenSubCommandHandler("ci", "Commands for working with Dolt continuous integration configuration.", []cli.Command{
22+
InitCmd{},
23+
DestroyCmd{},
24+
})

go/cmd/dolt/commands/ci/destroy.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2024 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ci
16+
17+
import (
18+
"context"
19+
20+
"github.com/dolthub/dolt/go/libraries/doltcore/env/actions/dolt_ci"
21+
22+
"github.com/dolthub/dolt/go/cmd/dolt/cli"
23+
"github.com/dolthub/dolt/go/cmd/dolt/commands"
24+
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
25+
"github.com/dolthub/dolt/go/libraries/doltcore/env"
26+
"github.com/dolthub/dolt/go/libraries/utils/argparser"
27+
)
28+
29+
var destroyDocs = cli.CommandDocumentationContent{
30+
ShortDesc: "Drops database tables used to store continuous integration configuration",
31+
LongDesc: "Drops database tables used to store continuous integration configuration",
32+
Synopsis: []string{
33+
"{{.LessThan}}destroy{{.GreaterThan}}",
34+
},
35+
}
36+
37+
type DestroyCmd struct{}
38+
39+
// Name implements cli.Command.
40+
func (cmd DestroyCmd) Name() string {
41+
return "destroy"
42+
}
43+
44+
// Description implements cli.Command.
45+
func (cmd DestroyCmd) Description() string {
46+
return destroyDocs.ShortDesc
47+
}
48+
49+
// RequiresRepo implements cli.Command.
50+
func (cmd DestroyCmd) RequiresRepo() bool {
51+
return true
52+
}
53+
54+
// Docs implements cli.Command.
55+
func (cmd DestroyCmd) Docs() *cli.CommandDocumentation {
56+
ap := cmd.ArgParser()
57+
return cli.NewCommandDocumentation(destroyDocs, ap)
58+
}
59+
60+
// Hidden should return true if this command should be hidden from the help text
61+
func (cmd DestroyCmd) Hidden() bool {
62+
return true
63+
}
64+
65+
// ArgParser implements cli.Command.
66+
func (cmd DestroyCmd) ArgParser() *argparser.ArgParser {
67+
ap := argparser.NewArgParserWithMaxArgs(cmd.Name(), 0)
68+
return ap
69+
}
70+
71+
// Exec implements cli.Command.
72+
func (cmd DestroyCmd) Exec(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEnv, cliCtx cli.CliContext) int {
73+
ap := cmd.ArgParser()
74+
_, usage := cli.HelpAndUsagePrinters(cli.CommandDocsForCommandString(commandStr, initDocs, ap))
75+
76+
if !cli.CheckEnvIsValid(dEnv) {
77+
return 1
78+
}
79+
80+
queryist, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
81+
if err != nil {
82+
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
83+
}
84+
if closeFunc != nil {
85+
defer closeFunc()
86+
}
87+
88+
db, err := newDatabase(sqlCtx, sqlCtx.GetCurrentDatabase(), dEnv, false)
89+
if err != nil {
90+
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
91+
}
92+
93+
name, email, err := env.GetNameAndEmail(dEnv.Config)
94+
if err != nil {
95+
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
96+
}
97+
98+
var verr errhand.VerboseError
99+
err = dolt_ci.DestroyDoltCITables(sqlCtx, db, queryist.Query, name, email)
100+
if err != nil {
101+
verr = errhand.VerboseErrorFromError(err)
102+
}
103+
104+
return commands.HandleVErrAndExitCode(verr, usage)
105+
}

go/cmd/dolt/commands/ci/init.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright 2024 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ci
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
"github.com/dolthub/dolt/go/libraries/doltcore/env/actions/dolt_ci"
22+
23+
"github.com/dolthub/dolt/go/cmd/dolt/cli"
24+
"github.com/dolthub/dolt/go/cmd/dolt/commands"
25+
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
26+
"github.com/dolthub/dolt/go/libraries/doltcore/env"
27+
"github.com/dolthub/dolt/go/libraries/doltcore/sqle"
28+
"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
29+
"github.com/dolthub/dolt/go/libraries/utils/argparser"
30+
)
31+
32+
var initDocs = cli.CommandDocumentationContent{
33+
ShortDesc: "Creates database tables used to store continuous integration configuration",
34+
LongDesc: "Creates database tables used to store continuous integration configuration",
35+
Synopsis: []string{
36+
"{{.LessThan}}init{{.GreaterThan}}",
37+
},
38+
}
39+
40+
type InitCmd struct{}
41+
42+
// Name implements cli.Command.
43+
func (cmd InitCmd) Name() string {
44+
return "init"
45+
}
46+
47+
// Description implements cli.Command.
48+
func (cmd InitCmd) Description() string {
49+
return initDocs.ShortDesc
50+
}
51+
52+
// RequiresRepo implements cli.Command.
53+
func (cmd InitCmd) RequiresRepo() bool {
54+
return true
55+
}
56+
57+
// Docs implements cli.Command.
58+
func (cmd InitCmd) Docs() *cli.CommandDocumentation {
59+
ap := cmd.ArgParser()
60+
return cli.NewCommandDocumentation(initDocs, ap)
61+
}
62+
63+
// Hidden should return true if this command should be hidden from the help text
64+
func (cmd InitCmd) Hidden() bool {
65+
return true
66+
}
67+
68+
// ArgParser implements cli.Command.
69+
func (cmd InitCmd) ArgParser() *argparser.ArgParser {
70+
ap := argparser.NewArgParserWithMaxArgs(cmd.Name(), 0)
71+
return ap
72+
}
73+
74+
// Exec implements cli.Command.
75+
func (cmd InitCmd) Exec(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEnv, cliCtx cli.CliContext) int {
76+
ap := cmd.ArgParser()
77+
_, usage := cli.HelpAndUsagePrinters(cli.CommandDocsForCommandString(commandStr, initDocs, ap))
78+
79+
if !cli.CheckEnvIsValid(dEnv) {
80+
return 1
81+
}
82+
83+
_, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
84+
if err != nil {
85+
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
86+
}
87+
if closeFunc != nil {
88+
defer closeFunc()
89+
}
90+
91+
db, err := newDatabase(sqlCtx, sqlCtx.GetCurrentDatabase(), dEnv, false)
92+
if err != nil {
93+
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
94+
}
95+
96+
name, email, err := env.GetNameAndEmail(dEnv.Config)
97+
if err != nil {
98+
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
99+
}
100+
101+
hasTables, err := dolt_ci.HasDoltCITables(sqlCtx)
102+
if err != nil {
103+
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
104+
}
105+
106+
if hasTables {
107+
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(fmt.Errorf("dolt ci has already been initialized")), usage)
108+
}
109+
110+
var verr errhand.VerboseError
111+
err = dolt_ci.CreateDoltCITables(sqlCtx, db, name, email)
112+
if err != nil {
113+
verr = errhand.VerboseErrorFromError(err)
114+
}
115+
116+
return commands.HandleVErrAndExitCode(verr, usage)
117+
}
118+
119+
func newDatabase(ctx context.Context, name string, dEnv *env.DoltEnv, useBulkEditor bool) (sqle.Database, error) {
120+
deaf := dEnv.DbEaFactory()
121+
if useBulkEditor {
122+
deaf = dEnv.BulkDbEaFactory()
123+
}
124+
tmpDir, err := dEnv.TempTableFilesDir()
125+
if err != nil {
126+
return sqle.Database{}, err
127+
}
128+
opts := editor.Options{
129+
Deaf: deaf,
130+
Tempdir: tmpDir,
131+
}
132+
return sqle.NewDatabase(ctx, name, dEnv.DbData(), opts)
133+
}

go/cmd/dolt/dolt.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/dolthub/dolt/go/cmd/dolt/cli"
4444
"github.com/dolthub/dolt/go/cmd/dolt/commands"
4545
"github.com/dolthub/dolt/go/cmd/dolt/commands/admin"
46+
"github.com/dolthub/dolt/go/cmd/dolt/commands/ci"
4647
"github.com/dolthub/dolt/go/cmd/dolt/commands/cnfcmds"
4748
"github.com/dolthub/dolt/go/cmd/dolt/commands/credcmds"
4849
"github.com/dolthub/dolt/go/cmd/dolt/commands/cvcmds"
@@ -126,6 +127,7 @@ var doltSubCommands = []cli.Command{
126127
commands.ReflogCmd{},
127128
commands.RebaseCmd{},
128129
commands.ArchiveCmd{},
130+
ci.Commands,
129131
}
130132

131133
var commandsWithoutCliCtx = []cli.Command{
@@ -166,6 +168,7 @@ var commandsWithoutGlobalArgSupport = []cli.Command{
166168
sqlserver.SqlServerCmd{VersionStr: doltversion.Version},
167169
commands.VersionCmd{VersionStr: doltversion.Version},
168170
commands.ConfigCmd{},
171+
ci.Commands,
169172
}
170173

171174
// commands that do not need write access for the current directory

go/libraries/doltcore/doltdb/doltdb.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func LoadDoltDBWithParams(ctx context.Context, nbf *types.NomsBinFormat, urlStr
141141
if err != nil {
142142
return nil, err
143143
}
144+
144145
return &DoltDB{db: hooksDatabase{Database: db}, vrw: vrw, ns: ns, databaseName: name}, nil
145146
}
146147

0 commit comments

Comments
 (0)