Merge pull request #4010 from dolthub/andy/dolt-docs-table

go/{commands,doltcore/sqle}: Allow `dolt_docs` to be created from SQL
This commit is contained in:
AndyA
2022-08-03 18:24:23 -07:00
committed by GitHub
7 changed files with 98 additions and 54 deletions
+33 -36
View File
@@ -32,8 +32,8 @@ import (
)
var readDocs = cli.CommandDocumentationContent{
ShortDesc: "Reads Dolt docs from the file system into the database",
LongDesc: "Reads Dolt docs from the file system into the database",
ShortDesc: "Reads Dolt Docs from the file system into the database",
LongDesc: "Reads Dolt Docs from the file system into the database",
Synopsis: []string{
"{{.LessThan}}doc{{.GreaterThan}} {{.LessThan}}file{{.GreaterThan}}",
},
@@ -48,7 +48,7 @@ func (cmd ReadCmd) Name() string {
// Description implements cli.Command.
func (cmd ReadCmd) Description() string {
return writeDocs.ShortDesc
return readDocs.ShortDesc
}
// RequiresRepo implements cli.Command.
@@ -114,10 +114,6 @@ func readDoltDoc(ctx context.Context, dEnv *env.DoltEnv, docName, fileName strin
return err
}
if err := maybeCreateDoltDocs(ctx, dEnv); err != nil {
return err
}
eng, err := engine.NewSqlEngineForEnv(ctx, dEnv)
if err != nil {
return err
@@ -138,7 +134,6 @@ const (
func writeDocToTable(ctx context.Context, eng *engine.SqlEngine, docName, content string) (*doltdb.RootValue, error) {
var (
sctx *sql.Context
iter sql.RowIter
err error
roots map[string]*doltdb.RootValue
)
@@ -149,11 +144,36 @@ func writeDocToTable(ctx context.Context, eng *engine.SqlEngine, docName, conten
}
sctx.Session.SetClient(sql.Client{User: "root", Address: "%", Capabilities: 0})
content = strings.ReplaceAll(content, `"`, `\"`)
_, iter, err = eng.Query(sctx, fmt.Sprintf(writeDocTemplate, docName, content))
if err != nil {
if err = execQuery(sctx, eng, doltdb.DocsMaybeCreateTableStmt); err != nil {
return nil, err
}
content = strings.ReplaceAll(content, `"`, `\"`)
update := fmt.Sprintf(writeDocTemplate, docName, content)
if err = execQuery(sctx, eng, update); err != nil {
return nil, err
}
if roots, err = eng.GetRoots(sctx); err != nil {
return nil, err
}
if len(roots) != 1 {
return nil, fmt.Errorf("cannot access docs in multi-database mode")
}
for _, rv := range roots {
return rv, nil
}
panic("unreachable")
}
func execQuery(sctx *sql.Context, eng *engine.SqlEngine, q string) (err error) {
var iter sql.RowIter
_, iter, err = eng.Query(sctx, q)
if err != nil {
return err
}
defer func() {
if cerr := iter.Close(sctx); err == nil {
err = cerr
@@ -167,31 +187,8 @@ func writeDocToTable(ctx context.Context, eng *engine.SqlEngine, docName, conten
break
}
if err != nil {
return nil, err
return err
}
}
if roots, err = eng.GetRoots(sctx); err != nil {
return nil, err
}
assertTrue(len(roots) == 1)
for _, rv := range roots {
return rv, nil
}
panic("unreachable")
}
func maybeCreateDoltDocs(ctx context.Context, dEnv *env.DoltEnv) error {
ws, err := dEnv.WorkingSet(ctx)
if err != nil {
return err
}
root := ws.WorkingRoot()
root, err = doltdb.MaybeCreateDoltDocsTable(ctx, root)
if err != nil {
return err
}
return dEnv.UpdateWorkingRoot(ctx, root)
return
}
+2 -2
View File
@@ -31,8 +31,8 @@ import (
)
var writeDocs = cli.CommandDocumentationContent{
ShortDesc: "Writes Dolt docs to stdout",
LongDesc: "Writes Dolt docs to stdout",
ShortDesc: "Writes Dolt Docs to stdout",
LongDesc: "Writes Dolt Docs to stdout",
Synopsis: []string{
"{{.LessThan}}doc{{.GreaterThan}}",
},
+7 -12
View File
@@ -115,17 +115,6 @@ func GetAllTableNames(ctx context.Context, root *RootValue) ([]string, error) {
return append(n, s...), nil
}
func MaybeCreateDoltDocsTable(ctx context.Context, root *RootValue) (*RootValue, error) {
_, ok, err := root.GetTable(ctx, DocTableName)
if err != nil {
return nil, err
}
if ok {
return root, nil
}
return root.CreateEmptyTable(ctx, DocTableName, DocsSchema)
}
// The set of reserved dolt_ tables that should be considered part of user space, like any other user-created table,
// for the purposes of the dolt command line. These tables cannot be created or altered explicitly, but can be updated
// like normal SQL tables.
@@ -173,13 +162,19 @@ const (
ReadmeDoc = "README.md"
)
// todo(andy)
var doltDocsColumns = schema.NewColCollection(
schema.NewColumn(DocPkColumnName, schema.DocNameTag, types.StringKind, true, schema.NotNullConstraint{}),
schema.NewColumn(DocTextColumnName, schema.DocTextTag, types.StringKind, false),
)
var DocsSchema = schema.MustSchemaFromCols(doltDocsColumns)
var DocsMaybeCreateTableStmt = `
CREATE TABLE IF NOT EXISTS dolt_docs (
doc_name varchar(16383) NOT NULL,
doc_text varchar(16383),
PRIMARY KEY (doc_name)
);`
const (
// DocTableName is the name of the dolt table containing documents such as the license and readme
DocTableName = "dolt_docs"
+6 -1
View File
@@ -761,7 +761,12 @@ func (db Database) DropTable(ctx *sql.Context, tableName string) error {
// CreateTable creates a table with the name and schema given.
func (db Database) CreateTable(ctx *sql.Context, tableName string, sch sql.PrimaryKeySchema) error {
if doltdb.HasDoltPrefix(tableName) {
if strings.ToLower(tableName) == doltdb.DocTableName {
// validate correct schema
if !dtables.DoltDocsSqlSchema.Equals(sch.Schema) {
return fmt.Errorf("incorrect schema for dolt_docs table")
}
} else if doltdb.HasDoltPrefix(tableName) {
return ErrReservedTableName.New(tableName)
}
@@ -0,0 +1,28 @@
// Copyright 2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dtables
import (
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
)
var DoltDocsSqlSchema sql.PrimaryKeySchema
func init() {
DoltDocsSqlSchema, _ = sqlutil.FromDoltSchema(doltdb.DocTableName, doltdb.DocsSchema)
}
+1 -2
View File
@@ -82,8 +82,7 @@ teardown() {
[[ "$output" =~ "Invalid table name" ]] || false
run dolt table cp test1 dolt_docs
[ "$status" -eq 1 ]
[[ "$output" =~ "Invalid table name" ]] || false
[[ "$output" =~ "reserved" ]] || false
[[ "$output" =~ "incorrect schema for dolt_docs table" ]] || false
run dolt table cp test1 dolt_query_catalog
[ "$status" -eq 1 ]
[[ "$output" =~ "Invalid table name" ]] || false
+21 -1
View File
@@ -45,7 +45,7 @@ teardown() {
teardown_common
}
@test "docs: doc update creates dolt_docs table" {
@test "docs: doc read creates dolt_docs table" {
run dolt ls
[ "$status" -eq 0 ]
[[ ! $output =~ "dolt_docs" ]] || false
@@ -102,6 +102,26 @@ teardown() {
[[ "$output" =~ "LICENSE.md" ]] || false
}
@test "docs: docs can be created from SQL" {
# must use correct schema
run dolt sql -q "CREATE TABLE dolt_docs (x int);"
[ "$status" -ne 0 ]
[[ "$output" =~ "incorrect schema for dolt_docs table" ]] || false
dolt sql <<SQL
CREATE TABLE dolt_docs (
doc_name varchar(16383) NOT NULL,
doc_text varchar(16383),
PRIMARY KEY (doc_name)
);
SQL
dolt sql -q "INSERT INTO dolt_docs VALUES ('README.md', 'this is a README')"
run dolt sql -q "SELECT * FROM dolt_docs"
[ "$status" -eq 0 ]
[[ "$output" =~ "this is a README" ]] || false
}
@test "docs: docs diff" {
dolt docs read LICENSE.md LICENSE.md
dolt add -A && dolt commit -m "added LICENSE"