dbschema

package
v1.27.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2021 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 ValidateColumnName added in v1.7.1

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 added in v1.7.1

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 added in v0.35.2

func (column Column) String() string

type ColumnData added in v1.6.1

type ColumnData struct {
	Column string
	Value  string
}

ColumnData is a value of a column within a row.

func (ColumnData) String added in v1.6.1

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 added in v1.25.1

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 added in v0.35.2

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 added in v0.35.2

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 added in v1.6.1

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
}

Schema is the database structure.

func (*Schema) DropIndex added in v1.7.1

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 added in v1.7.1

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

FindIndex finds index in the schema.

func (*Schema) FindTable added in v1.25.2

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

FindTable returns the specified table.

func (*Schema) Sort

func (schema *Schema) Sort()

Sort sorts tables and indexes.

func (Schema) String added in v0.35.2

func (schema Schema) String() string

type Sections added in v1.6.1

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 added in v1.6.1

func NewSections(script string) Sections

NewSections constructs a Sections from a sql script.

func (Sections) LookupSection added in v1.6.1

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 added in v1.25.1

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
}

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 added in v1.25.2

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 added in v0.35.2

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