github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/doltdb/system_table.go (about)

     1  // Copyright 2019 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 doltdb
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"sort"
    21  	"strings"
    22  
    23  	"github.com/dolthub/dolt/go/libraries/utils/funcitr"
    24  
    25  	"github.com/dolthub/dolt/go/libraries/utils/set"
    26  )
    27  
    28  const (
    29  	// DoltNamespace is the name prefix of dolt system tables. We reserve all tables that begin with dolt_ for system use.
    30  	DoltNamespace = "dolt"
    31  )
    32  
    33  var ErrSystemTableCannotBeModified = errors.New("system tables cannot be dropped or altered")
    34  
    35  // HasDoltPrefix returns a boolean whether or not the provided string is prefixed with the DoltNamespace. Users should
    36  // not be able to create tables in this reserved namespace.
    37  func HasDoltPrefix(s string) bool {
    38  	return strings.HasPrefix(strings.ToLower(s), DoltNamespace)
    39  }
    40  
    41  // IsReadOnlySystemTable returns whether the table name given is a system table that should not be included in command line
    42  // output (e.g. dolt status) by default.
    43  func IsReadOnlySystemTable(name string) bool {
    44  	return HasDoltPrefix(name) && !set.NewStrSet(writeableSystemTables).Contains(name)
    45  }
    46  
    47  // GetNonSystemTableNames gets non-system table names
    48  func GetNonSystemTableNames(ctx context.Context, root *RootValue) ([]string, error) {
    49  	tn, err := root.GetTableNames(ctx)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	tn = funcitr.FilterStrings(tn, func(n string) bool {
    54  		return !HasDoltPrefix(n)
    55  	})
    56  	sort.Strings(tn)
    57  	return tn, nil
    58  }
    59  
    60  // GetSystemTableNames gets system table names
    61  func GetSystemTableNames(ctx context.Context, root *RootValue) ([]string, error) {
    62  	p, err := GetPersistedSystemTables(ctx, root)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	g, err := GetGeneratedSystemTables(ctx, root)
    68  	if err != nil {
    69  
    70  	}
    71  
    72  	s := append(p, g...)
    73  	sort.Strings(s)
    74  
    75  	return s, nil
    76  }
    77  
    78  // GetPersistedSystemTables returns table names of all persisted system tables.
    79  func GetPersistedSystemTables(ctx context.Context, root *RootValue) ([]string, error) {
    80  	tn, err := root.GetTableNames(ctx)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	sort.Strings(tn)
    85  	return funcitr.FilterStrings(tn, HasDoltPrefix), nil
    86  }
    87  
    88  // GetGeneratedSystemTables returns table names of all generated system tables.
    89  func GetGeneratedSystemTables(ctx context.Context, root *RootValue) ([]string, error) {
    90  	s := set.NewStrSet(generatedSystemTables)
    91  
    92  	tn, err := root.GetTableNames(ctx)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	for _, pre := range generatedSystemTablePrefixes {
    98  		s.Add(funcitr.MapStrings(tn, func(s string) string { return pre + s })...)
    99  	}
   100  
   101  	return s.AsSlice(), nil
   102  }
   103  
   104  // GetAllTableNames returns table names for all persisted and generated tables.
   105  func GetAllTableNames(ctx context.Context, root *RootValue) ([]string, error) {
   106  	n, err := GetNonSystemTableNames(ctx, root)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  	s, err := GetSystemTableNames(ctx, root)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  	return append(n, s...), nil
   115  }
   116  
   117  // The set of reserved dolt_ tables that should be considered part of user space, like any other user-created table,
   118  // for the purposes of the dolt command line. These tables cannot be created or altered explicitly, but can be updated
   119  // like normal SQL tables.
   120  var writeableSystemTables = []string{
   121  	DoltQueryCatalogTableName,
   122  	SchemasTableName,
   123  	ProceduresTableName,
   124  }
   125  
   126  var persistedSystemTables = []string{
   127  	DocTableName,
   128  	DoltQueryCatalogTableName,
   129  	SchemasTableName,
   130  	ProceduresTableName,
   131  }
   132  
   133  var generatedSystemTables = []string{
   134  	BranchesTableName,
   135  	LogTableName,
   136  	TableOfTablesInConflictName,
   137  	CommitsTableName,
   138  	CommitAncestorsTableName,
   139  	StatusTableName,
   140  }
   141  
   142  var generatedSystemTablePrefixes = []string{
   143  	DoltDiffTablePrefix,
   144  	DoltCommitDiffTablePrefix,
   145  	DoltHistoryTablePrefix,
   146  	DoltConfTablePrefix,
   147  }
   148  
   149  const (
   150  	// DocTableName is the name of the dolt table containing documents such as the license and readme
   151  	DocTableName = "dolt_docs"
   152  	// DocPkColumnName is the name of the pk column in the docs table
   153  	DocPkColumnName = "doc_name"
   154  	//DocTextColumnName is the name of the column containing the document contents in the docs table
   155  	DocTextColumnName = "doc_text"
   156  )
   157  
   158  const (
   159  	// DoltQueryCatalogTableName is the name of the query catalog table
   160  	DoltQueryCatalogTableName = "dolt_query_catalog"
   161  
   162  	// QueryCatalogIdCol is the name of the primary key column of the query catalog table
   163  	QueryCatalogIdCol = "id"
   164  
   165  	// QueryCatalogOrderCol is the column containing the order of the queries in the catalog
   166  	QueryCatalogOrderCol = "display_order"
   167  
   168  	// QueryCatalogNameCol is the name of the column containing the name of a query in the catalog
   169  	QueryCatalogNameCol = "name"
   170  
   171  	// QueryCatalogQueryCol is the name of the column containing the query of a catalog entry
   172  	QueryCatalogQueryCol = "query"
   173  
   174  	// QueryCatalogDescriptionCol is the name of the column containing the description of a query in the catalog
   175  	QueryCatalogDescriptionCol = "description"
   176  )
   177  
   178  const (
   179  	// SchemasTableName is the name of the dolt schema fragment table
   180  	SchemasTableName = "dolt_schemas"
   181  	// SchemasTablesIdCol is an incrementing integer that represents the insertion index.
   182  	SchemasTablesIdCol = "id"
   183  	// Currently: `view` or `trigger`.
   184  	SchemasTablesTypeCol = "type"
   185  	// The name of the database entity.
   186  	SchemasTablesNameCol = "name"
   187  	// The schema fragment associated with the database entity.
   188  	// For example, the SELECT statement for a CREATE VIEW.
   189  	SchemasTablesFragmentCol = "fragment"
   190  	// The name of the index that is on the table.
   191  	SchemasTablesIndexName = "fragment_name"
   192  )
   193  
   194  const (
   195  	// DoltHistoryTablePrefix is the prefix assigned to all the generated history tables
   196  	DoltHistoryTablePrefix = "dolt_history_"
   197  	// DoltdDiffTablePrefix is the prefix assigned to all the generated diff tables
   198  	DoltDiffTablePrefix = "dolt_diff_"
   199  	// DoltCommitDiffTablePrefix is the prefix assigned to all the generated commit diff tables
   200  	DoltCommitDiffTablePrefix = "dolt_commit_diff_"
   201  	// DoltConfTablePrefix is the prefix assigned to all the generated conflict tables
   202  	DoltConfTablePrefix = "dolt_conflicts_"
   203  )
   204  
   205  const (
   206  	// LogTableName is the log system table name
   207  	LogTableName = "dolt_log"
   208  
   209  	// TableOfTablesInConflictName is the conflicts system table name
   210  	TableOfTablesInConflictName = "dolt_conflicts"
   211  
   212  	// BranchesTableName is the branches system table name
   213  	BranchesTableName = "dolt_branches"
   214  
   215  	// CommitsTableName is the commits system table name
   216  	CommitsTableName = "dolt_commits"
   217  
   218  	// CommitAncestorsTableName is the commit_ancestors system table name
   219  	CommitAncestorsTableName = "dolt_commit_ancestors"
   220  
   221  	// StatusTableName is the status system table name.
   222  	StatusTableName = "dolt_status"
   223  )
   224  
   225  const (
   226  	// ProceduresTableName is the name of the dolt stored procedures table.
   227  	ProceduresTableName = "dolt_procedures"
   228  	// ProceduresTableNameCol is the name of the stored procedure. Using CREATE PROCEDURE, will always be lowercase.
   229  	ProceduresTableNameCol = "name"
   230  	// ProceduresTableCreateStmtCol is the CREATE PROCEDURE statement for this stored procedure.
   231  	ProceduresTableCreateStmtCol = "create_stmt"
   232  	// ProceduresTableCreatedAtCol is the time that the stored procedure was created at, in UTC.
   233  	ProceduresTableCreatedAtCol = "created_at"
   234  	// ProceduresTableModifiedAtCol is the time that the stored procedure was last modified, in UTC.
   235  	ProceduresTableModifiedAtCol = "modified_at"
   236  )