db

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatParamNameInNumberedPosition

func FormatParamNameInNumberedPosition(paramNames []string) string

FormatParamNameInNumberedPosition formats the param name in numbered positions.

func FormatParamNameInQuestionMark

func FormatParamNameInQuestionMark(paramNames []string) string

FormatParamNameInQuestionMark formats the param name in question mark. For example, it will be WHERE hello = ? AND world = ?.

func Register

func Register(dbType Type, f driverFunc)

Register makes a database driver available by the provided type. If Register is called twice with the same name or if driver is nil, it panics.

Types

type Column

type Column struct {
	Name     string
	Position int
	Default  *string
	// Nullable isn't supported for ClickHouse.
	Nullable bool
	Type     string
	// CharacterSet isn't supported for Postgres, ClickHouse, SQLite.
	CharacterSet string
	// Collation isn't supported for ClickHouse, SQLite.
	Collation string
	// Comment isn't supported for SQLite.
	Comment string
}

Column the database table column.

type ConnectionConfig

type ConnectionConfig struct {
	Host      string
	Port      string
	Username  string
	Password  string
	Database  string
	TLSConfig TLSConfig
	// ReadOnly is only supported for Postgres at the moment.
	ReadOnly bool
}

ConnectionConfig is the configuration for connections.

type ConnectionContext

type ConnectionContext struct {
	EnvironmentName string
	InstanceName    string
}

ConnectionContext is the context for connection. It's not used for establishing the db connection, but is useful for logging.

type Driver

type Driver interface {
	// A driver might support multiple engines (e.g. MySQL driver can support both MySQL and TiDB),
	// So we pass the dbType to tell the exact engine.
	Open(ctx context.Context, dbType Type, config ConnectionConfig, connCtx ConnectionContext) (Driver, error)
	// Remember to call Close to avoid connection leak
	Close(ctx context.Context) error
	Ping(ctx context.Context) error
	GetDbConnection(ctx context.Context, database string) (*sql.DB, error)
	GetVersion(ctx context.Context) (string, error)
	SyncSchema(ctx context.Context) ([]*User, []*Schema, error)
	Execute(ctx context.Context, statement string, useTransaction bool) error
	// Used for execute readonly SELECT statement
	// limit is the maximum row count returned. No limit enforced if limit <= 0
	Query(ctx context.Context, statement string, limit int) ([]interface{}, error)

	// Migration related
	// Check whether we need to setup migration (e.g. creating/upgrading the migration related tables)
	NeedsSetupMigration(ctx context.Context) (bool, error)
	// Create or upgrade migration related tables
	SetupMigrationIfNeeded(ctx context.Context) error
	// Execute migration will apply the statement and record the migration history, the schema after migration on success.
	// The migration type is determined by m.Type. Note, it can also perform data migration (DML) in addition to schema migration (DDL).
	// It returns the migration history id and the schema after migration on success.
	ExecuteMigration(ctx context.Context, m *MigrationInfo, statement string) (int64, string, error)
	// Find the migration history list and return most recent item first.
	FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error)

	// Dump and restore
	// Dump the database, if dbName is empty, then dump all databases.
	Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error
	// Restore the database from sc.
	Restore(ctx context.Context, sc *bufio.Scanner) error
}

Driver is the interface for database driver.

func Open

func Open(ctx context.Context, dbType Type, driverConfig DriverConfig, connectionConfig ConnectionConfig, connCtx ConnectionContext) (Driver, error)

Open opens a database specified by its database driver type and connection config

type DriverConfig

type DriverConfig struct {
	Logger *zap.Logger
}

DriverConfig is the driver configuration.

type Index

type Index struct {
	Name string
	// This could refer to a column or an expression
	Expression string
	Position   int
	// Type isn't supported for SQLite.
	Type   string
	Unique bool
	// Visible isn't supported for Postgres, SQLite.
	Visible bool
	// Comment isn't supported for SQLite.
	Comment string
}

Index is the database index.

type MigrationHistory

type MigrationHistory struct {
	ID int

	Creator   string
	CreatedTs int64
	Updater   string
	UpdatedTs int64

	ReleaseVersion      string
	Namespace           string
	Sequence            int
	Source              MigrationSource
	Type                MigrationType
	Status              MigrationStatus
	Version             string
	Description         string
	Statement           string
	Schema              string
	SchemaPrev          string
	ExecutionDurationNs int64
	IssueID             string
	Payload             string
}

MigrationHistory is the API message for migration history.

type MigrationHistoryFind

type MigrationHistoryFind struct {
	ID *int

	Database *string
	Version  *string
	// If specified, then it will only fetch "Limit" most recent migration histories
	Limit *int
}

MigrationHistoryFind is the API message for finding migration historys.

type MigrationInfo

type MigrationInfo struct {
	ReleaseVersion string
	Version        string
	Namespace      string
	Database       string
	Environment    string
	Source         MigrationSource
	Type           MigrationType
	Status         MigrationStatus
	Description    string
	Creator        string
	IssueID        string
	Payload        string
	CreateDatabase bool
}

MigrationInfo is the API message for migration info.

func ParseMigrationInfo

func ParseMigrationInfo(filePath string, filePathTemplate string) (*MigrationInfo, error)

ParseMigrationInfo matches filePath against filePathTemplate If filePath matches, then it will derive MigrationInfo from the filePath. Both filePath and filePathTemplate are the full file path (including the base directory) of the repository.

type MigrationInfoPayload

type MigrationInfoPayload struct {
	VCSPushEvent *vcs.PushEvent `json:"pushEvent,omitempty"`
}

MigrationInfoPayload is the API message for migration info payload.

type MigrationSource

type MigrationSource string

MigrationSource is the migration engine.

const (
	// UI is the migration source type for UI.
	UI MigrationSource = "UI"
	// VCS is the migration source type for VCS.
	VCS MigrationSource = "VCS"
	// LIBRARY is the migration source type for LIBRARY.
	LIBRARY MigrationSource = "LIBRARY"
)

func (MigrationSource) String

func (e MigrationSource) String() string

type MigrationStatus

type MigrationStatus string

MigrationStatus is the status of migration.

const (
	// Pending is the migration status for PENDING.
	Pending MigrationStatus = "PENDING"
	// Done is the migration status for DONE.
	Done MigrationStatus = "DONE"
	// Failed is the migration status for FAILED.
	Failed MigrationStatus = "FAILED"
)

func (MigrationStatus) String

func (e MigrationStatus) String() string

type MigrationType

type MigrationType string

MigrationType is the type of a migration.

const (
	// Baseline is the migration type for BASELINE.
	// Used for establishing schema baseline, this is used when
	// 1. Onboard the database into Bytebase since Bytebase needs to know the current database schema.
	// 2. Had schema drift and need to re-establish the baseline.
	Baseline MigrationType = "BASELINE"
	// Migrate is the migration type for MIGRATE.
	// Used for DDL change.
	Migrate MigrationType = "MIGRATE"
	// Branch is the migration type for BRANCH.
	// Used when restoring from a backup (the restored database branched from the original backup).
	Branch MigrationType = "BRANCH"
	// Data is the migration type for DATA.
	// Used for DML change.
	Data MigrationType = "DATA"
)

func (MigrationType) String

func (e MigrationType) String() string

type Schema

type Schema struct {
	Name string
	// CharacterSet isn't supported for ClickHouse, Snowflake.
	CharacterSet string
	// Collation isn't supported for ClickHouse, Snowflake.
	Collation string
	UserList  []User
	TableList []Table
	ViewList  []View
}

Schema is the database schema.

type TLSConfig

type TLSConfig struct {
	SslCA   string
	SslCert string
	SslKey  string
}

TLSConfig is the configuration for SSL connection.

func (TLSConfig) GetSslConfig

func (tc TLSConfig) GetSslConfig() (*tls.Config, error)

GetSslConfig gets the SSL config for connection.

type Table

type Table struct {
	Name string
	// CreatedTs isn't supported for ClickHouse, SQLite.
	CreatedTs int64
	// UpdatedTs isn't supported for SQLite.
	UpdatedTs int64
	Type      string
	// Engine isn't supported for Postgres, Snowflake, SQLite.
	Engine string
	// Collation isn't supported for Postgres, ClickHouse, Snowflake, SQLite.
	Collation string
	RowCount  int64
	// DataSize isn't supported for SQLite.
	DataSize int64
	// IndexSize isn't supported for ClickHouse, Snowflake, SQLite.
	IndexSize int64
	// DataFree isn't supported for Postgres, ClickHouse, Snowflake, SQLite.
	DataFree int64
	// CreateOptions isn't supported for Postgres, ClickHouse, Snowflake, SQLite.
	CreateOptions string
	// Comment isn't supported for SQLite.
	Comment    string
	ColumnList []Column
	// IndexList isn't supported for ClickHouse, Snowflake.
	IndexList []Index
}

Table is the database table.

type Type

type Type string

Type is the type of a database.

const (
	// ClickHouse is the database type for CLICKHOUSE.
	ClickHouse Type = "CLICKHOUSE"
	// MySQL is the database type for MYSQL.
	MySQL Type = "MYSQL"
	// Postgres is the database type for POSTGRES.
	Postgres Type = "POSTGRES"
	// Snowflake is the database type for SNOWFLAKE.
	Snowflake Type = "SNOWFLAKE"
	// SQLite is the database type for SQLite.
	SQLite Type = "SQLITE"
	// TiDB is the database type for TIDB.
	TiDB Type = "TIDB"
)

type User

type User struct {
	Name  string
	Grant string
}

User is the database user.

type View

type View struct {
	Name string
	// CreatedTs isn't supported for ClickHouse.
	CreatedTs  int64
	UpdatedTs  int64
	Definition string
	Comment    string
}

View is the database view.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL