dbschema

package
v1.119.0-rc Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package dbschema package implements querying and comparing schemas for testing.

Index

Constants

View Source
const (
	NewData  = "NEW DATA"
	OldData  = "OLD DATA"
	Main     = "MAIN"
	MainData = "MAIN DATA"
)

These consts are the names of the sections that are typical in our scripts.

Variables

This section is empty.

Functions

func SortIndexes added in v1.118.4

func SortIndexes(indexes []*Index)

SortIndexes sorts Index records in a slice by table name then index name.

func SortTables added in v1.118.4

func SortTables(tables []*Table)

SortTables sorts Table records in a slice by table name.

func ValidateColumnName

func ValidateColumnName(column string) error

ValidateColumnName checks column has at least 1 character and it's only formed by lower and upper case letters, numbers, underscores or dashes where dashes cannot be at the beginning of the end and not in a row.

func ValidateTableName

func ValidateTableName(table string) error

ValidateTableName checks table has at least 1 character and it's only formed by lower and upper case letters, numbers, underscores or dashes where dashes cannot be at the beginning of the end and not in a row. One dot is allowed for scoping tables in a schema (e.g. public.my_table).

Types

type Column

type Column struct {
	Name       string
	Type       string
	IsNullable bool
	Default    string
	Reference  *Reference
}

Column is a sql column.

func (Column) String

func (column Column) String() string

type ColumnData

type ColumnData struct {
	Column string
	Value  string
}

ColumnData is a value of a column within a row.

func (ColumnData) String

func (c ColumnData) String() string

String returns a string representation of the column.

type Data

type Data struct {
	Tables []*TableData
}

Data is the database content formatted as strings.

func QueryData

func QueryData(ctx context.Context, db Queryer, schema *Schema, quoteColumn func(string) string) (*Data, error)

QueryData loads all data from tables.

func (*Data) AddTable

func (data *Data) AddTable(table *TableData)

AddTable adds a new table.

func (*Data) DropTable

func (data *Data) DropTable(tableName string)

DropTable removes the specified table.

func (*Data) FindTable

func (data *Data) FindTable(tableName string) (*TableData, bool)

FindTable finds a table by name.

func (*Data) Sort

func (data *Data) Sort()

Sort sorts all tables.

type Index

type Index struct {
	Name    string
	Table   string
	Columns []string
	Unique  bool
	Partial string // partial expression
}

Index is an index for a table.

func (Index) String

func (index Index) String() string

type Queryer

type Queryer interface {
	// QueryRowContext executes a query that returns a single row.
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	// QueryContext executes a query that returns rows, typically a SELECT.
	QueryContext(ctx context.Context, query string, args ...interface{}) (tagsql.Rows, error)
}

Queryer is a representation for something that can query.

type Reference

type Reference struct {
	Table    string
	Column   string
	OnDelete string
	OnUpdate string
}

Reference is a column foreign key.

func (*Reference) String

func (reference *Reference) String() string

type RowData

type RowData []ColumnData

RowData is content of a single row.

func (RowData) Clone

func (row RowData) Clone() RowData

Clone returns a clone of row data.

func (RowData) Less

func (row RowData) Less(b RowData) bool

Less returns true if one row is less than the other.

type Schema

type Schema struct {
	Tables    []*Table
	Indexes   []*Index
	Sequences []string
}

Schema is the database structure.

func (*Schema) DropIndex

func (schema *Schema) DropIndex(name string)

DropIndex removes the specified index.

func (*Schema) DropTable

func (schema *Schema) DropTable(tableName string)

DropTable removes the specified table.

func (*Schema) EnsureTable

func (schema *Schema) EnsureTable(tableName string) *Table

EnsureTable returns the table with the specified name and creates one if needed.

func (*Schema) FindIndex

func (schema *Schema) FindIndex(name string) (*Index, bool)

FindIndex finds index in the schema.

func (*Schema) FindTable

func (schema *Schema) FindTable(tableName string) (*Table, bool)

FindTable returns the specified table.

func (Schema) HasSequence

func (schema Schema) HasSequence(name string) bool

HasSequence returns with true if sequence is added to the scheme.

func (*Schema) Sort

func (schema *Schema) Sort()

Sort sorts tables and indexes.

func (Schema) String

func (schema Schema) String() string

type Sections

type Sections struct {
	Script   string
	Sections map[string]string
}

Sections is a type to keep track of the sections inside of a sql script.

func NewSections

func NewSections(script string) Sections

NewSections constructs a Sections from a sql script.

func (Sections) LookupSection

func (s Sections) LookupSection(section string) string

LookupSection finds the named section in the script or returns an empty string.

type Snapshot

type Snapshot struct {
	Version int
	Sections
	*Schema
	*Data
}

Snapshot defines a particular snapshot of schema and data.

func (*Snapshot) DropTable

func (snapshot *Snapshot) DropTable(tableName string)

DropTable removes the specified table.

type Snapshots

type Snapshots struct {
	List []*Snapshot
}

Snapshots defines a collection of snapshot.

func (*Snapshots) Add

func (snapshots *Snapshots) Add(snap *Snapshot)

Add adds a new snapshot.

func (*Snapshots) FindVersion

func (snapshots *Snapshots) FindVersion(version int) (*Snapshot, bool)

FindVersion finds a snapshot with the specified version.

func (*Snapshots) Sort

func (snapshots *Snapshots) Sort()

Sort sorts the snapshots by version.

type Table

type Table struct {
	Name       string
	Columns    []*Column
	PrimaryKey []string
	Unique     [][]string
	Checks     []string
}

Table is a sql table.

func (*Table) AddColumn

func (table *Table) AddColumn(column *Column)

AddColumn adds the column to the table.

func (*Table) ColumnNames

func (table *Table) ColumnNames() []string

ColumnNames returns column names.

func (*Table) FindColumn

func (table *Table) FindColumn(columnName string) (*Column, bool)

FindColumn finds a column in the table.

func (*Table) RemoveColumn

func (table *Table) RemoveColumn(columnName string)

RemoveColumn removes the column from the table.

func (*Table) Sort

func (table *Table) Sort()

Sort sorts columns, primary keys and unique.

func (Table) String

func (table Table) String() string

type TableData

type TableData struct {
	Name    string
	Columns []string
	Rows    []RowData
}

TableData is content of a sql table.

func (*TableData) AddRow

func (table *TableData) AddRow(row RowData) error

AddRow adds a new row.

func (*TableData) Sort

func (table *TableData) Sort()

Sort sorts all rows.

Jump to

Keyboard shortcuts

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