github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/dsess/session_db_provider.go (about) 1 // Copyright 2021 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 dsess 16 17 import ( 18 "context" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 22 "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" 23 "github.com/dolthub/dolt/go/libraries/doltcore/env" 24 "github.com/dolthub/dolt/go/libraries/utils/filesys" 25 "github.com/dolthub/dolt/go/store/types" 26 ) 27 28 // RevisionDatabase allows callers to query a revision database for the commit, branch, or tag it is pinned to. For 29 // example, when using a database with a branch revision specification, that database is only able to use that branch 30 // and cannot change branch heads. Calling `Revision` on that database will return the branch name. Similarly, for 31 // databases using a commit revision spec or a tag revision spec, Revision will return the commit or tag, respectively. 32 // Currently, only explicit branch names, commit hashes, and tag names are allowed as database revision specs. Other 33 // refspecs, such as "HEAD~2" are not supported yet. 34 type RevisionDatabase interface { 35 // Revision returns the specific branch, commit, or tag to which this revision database has been pinned. Other 36 // revision specifications (e.g. "HEAD~2") are not supported. If a database implements RevisionDatabase, but 37 // is not pinned to a specific revision, the empty string is returned. 38 Revision() string 39 // RevisionType returns the type of revision this database is pinned to. 40 RevisionType() RevisionType 41 // RevisionQualifiedName returns the fully qualified name of the database, which includes the revision if one is 42 // specified. 43 RevisionQualifiedName() string 44 // RequestedName returns the name of the database as requested by the user when the name was resolved to this 45 // database. 46 RequestedName() string 47 // Versioned returns whether this database implementation supports more than a single revision. 48 // TODO: This shouldn't be a necessary part of the interface, but it's required to differentiate between dolt-backed 49 // databases and others that we serve for custom purposes with similar pieces of functionality, and the session 50 // management logic intermixes these concerns. 51 Versioned() bool 52 } 53 54 // RevisionType represents the type of revision a database is pinned to. For branches and tags, the revision is a 55 // string naming that branch or tag. For other revision specs, e.g. "HEAD~2", the revision is a commit hash. 56 type RevisionType int 57 58 const ( 59 RevisionTypeNone RevisionType = iota 60 RevisionTypeBranch 61 RevisionTypeTag 62 RevisionTypeCommit 63 ) 64 65 // RemoteReadReplicaDatabase is a database that pulls from a connected remote when a transaction begins. 66 type RemoteReadReplicaDatabase interface { 67 // ValidReplicaState returns whether this read replica is in a valid state to pull from the remote 68 ValidReplicaState(ctx *sql.Context) bool 69 // PullFromRemote performs a pull from the remote and returns any error encountered 70 PullFromRemote(ctx *sql.Context) error 71 } 72 73 type DoltDatabaseProvider interface { 74 sql.MutableDatabaseProvider 75 // FileSystem returns the filesystem used by this provider, rooted at the data directory for all databases. 76 FileSystem() filesys.Filesys 77 DbFactoryUrl() string 78 // FileSystemForDatabase returns a filesystem, with the working directory set to the root directory 79 // of the requested database. If the requested database isn't found, a database not found error 80 // is returned. 81 FileSystemForDatabase(dbname string) (filesys.Filesys, error) 82 // GetRemoteDB returns the remote database for given env.Remote object using the local database's vrw, and 83 // withCaching defines whether the remoteDB gets cached or not. 84 // This function replaces env.Remote's GetRemoteDB method during SQL session to access dialer in order 85 // to get remote database associated to the env.Remote object. 86 GetRemoteDB(ctx context.Context, format *types.NomsBinFormat, r env.Remote, withCaching bool) (*doltdb.DoltDB, error) 87 // CloneDatabaseFromRemote clones the database from the specified remoteURL as a new database in this provider. 88 // dbName is the name for the new database, branch is an optional parameter indicating which branch to clone 89 // (otherwise all branches are cloned), remoteName is the name for the remote created in the new database, and 90 // remoteUrl is a URL (e.g. "file:///dbs/db1") or an <org>/<database> path indicating a database hosted on DoltHub. 91 CloneDatabaseFromRemote(ctx *sql.Context, dbName, branch, remoteName, remoteUrl string, depth int, remoteParams map[string]string) error 92 // SessionDatabase returns the SessionDatabase for the specified database, which may name a revision of a base 93 // database. 94 SessionDatabase(ctx *sql.Context, dbName string) (SqlDatabase, bool, error) 95 // BaseDatabase returns the base database for the specified database name. Meant for informational purposes when 96 // managing the session initialization only. Use SessionDatabase for normal database retrieval. 97 BaseDatabase(ctx *sql.Context, dbName string) (SqlDatabase, bool) 98 // DoltDatabases returns all databases known to this provider. 99 DoltDatabases() []SqlDatabase 100 // UndropDatabase attempts to restore the database |dbName| that was previously dropped. 101 // The restored database will appear identically when accessed through the SQL 102 // interface, but may be stored in a slightly different location on disk 103 // (e.g. a root database will be restored as a regular/non-root database, 104 // databases original stored with hyphens in their directory name will be rewritten 105 // to underscores to match their SQL database name). 106 // If the database is unable to be restored, an error is returned explaining why. 107 UndropDatabase(ctx *sql.Context, dbName string) error 108 // ListDroppedDatabases returns a list of the database names for dropped databases that are still 109 // available on disk and can be restored with dolt_undrop(). 110 ListDroppedDatabases(ctx *sql.Context) ([]string, error) 111 // PurgeDroppedDatabases permanently deletes any dropped databases that are being held in temporary storage 112 // in case they need to be restored. This operation is not reversible, so use with caution! 113 PurgeDroppedDatabases(ctx *sql.Context) error 114 } 115 116 type SessionDatabaseBranchSpec struct { 117 RepoState env.RepoStateReadWriter 118 Branch string 119 } 120 121 type SqlDatabase interface { 122 sql.Database 123 SessionDatabase 124 RevisionDatabase 125 126 // WithBranchRevision returns a copy of this database with the revision set to the given branch revision, and the 127 // database name set to the given name. 128 WithBranchRevision(requestedName string, branchSpec SessionDatabaseBranchSpec) (SqlDatabase, error) 129 130 // TODO: get rid of this, it's managed by the session, not the DB 131 GetRoot(*sql.Context) (doltdb.RootValue, error) 132 // TODO: remove ddb from the below, it's separable and is 95% of the uses of this method 133 DbData() env.DbData 134 // DoltDatabases returns all underlying DoltDBs for this database. 135 DoltDatabases() []*doltdb.DoltDB 136 // Schema returns the schema of the database. 137 Schema() string 138 }