pkg

package
v0.0.0-...-0d27ab4 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Generators = map[reflect.Type]func(*dag.Graph) Command{

	reflect.TypeOf(DropDatabase{}): func(g *dag.Graph) Command { return DropDatabase{dag.Nodes[*Database](g).Any()} },
	reflect.TypeOf(DropIndex{}):    func(g *dag.Graph) Command { return DropIndex{dag.Nodes[*Index](g).Any()} },
	reflect.TypeOf(DropSchema{}):   func(g *dag.Graph) Command { return DropSchema{dag.Nodes[*Schema](g).Any()} },
	reflect.TypeOf(DropTable{}):    func(g *dag.Graph) Command { return DropTable{dag.Nodes[*Table](g).Any()} },

	reflect.TypeOf(CreateDatabase{}): func(g *dag.Graph) Command {

		if len(dag.Nodes[*Database](g)) > 5 {
			return nil
		}
		return CreateDatabase{Name: RandomString()}
	},
	reflect.TypeOf(CreateSchema{}): func(g *dag.Graph) Command {

		if len(dag.Nodes[*Schema](g)) > 2 {
			return nil
		}
		return CreateSchema{Database: dag.Nodes[*Database](g).Any(), Name: RandomString()}
	},
	reflect.TypeOf(CreateTable{}): func(g *dag.Graph) Command {
		return CreateTable{
			Schema: dag.Nodes[*Schema](g).Any(),
			Name:   RandomString(),
		}
	},
	reflect.TypeOf(CreateIndex{}): func(g *dag.Graph) Command {
		table := dag.Nodes[*Table](g, func(t *Table) bool {
			return len(t.Columns()) > 1
		}).Any()
		return CreateIndex{
			Table:   table,
			Name:    RandomString(),
			Columns: table.Columns().PickUpTo(3),
			Unique:  FlipCoin(),
		}
	},

	reflect.TypeOf(RenameTable{}): func(g *dag.Graph) Command {
		return RenameTable{
			Table: dag.Any[*Table](g),
			Name:  RandomString(),
		}
	},
	reflect.TypeOf(RenameSchema{}): func(g *dag.Graph) Command {
		return RenameSchema{
			Schema: dag.Any[*Schema](g, NotPublic),
			Name:   RandomString(),
		}
	},
	reflect.TypeOf(RenameDatabase{}): func(g *dag.Graph) Command {
		return RenameDatabase{
			Database: dag.Any[*Database](g),
			Name:     RandomString(),
		}
	},

	reflect.TypeOf(DropColumn{}):               func(g *dag.Graph) Command { return DropColumn{dag.Nodes[*Column](g).Any()} },
	reflect.TypeOf(DropForeignKeyConstraint{}): func(g *dag.Graph) Command { panic("not implemented") },
	reflect.TypeOf(AddColumn{}): func(g *dag.Graph) Command {
		return AddColumn{
			Table:    dag.Nodes[*Table](g).Any(),
			Name:     RandomString(),
			Nullable: false,
		}
	},
	reflect.TypeOf(CreateForeignKeyConstraint{}): func(g *dag.Graph) Command {

		to := dag.Nodes[*Index](g, func(i *Index) bool {
			return i.Unique && len(i.Columns()) == 1
		}).Any().Columns()[0]

		from := dag.Nodes[*Column](g, func(c *Column) bool {
			return c.Table().Schema().Database() == to.Table().Schema().Database() && c.Table() != to.Table()
		}).Any()

		return CreateForeignKeyConstraint{
			Name: RandomString(),
			From: from,
			To:   to,
		}
	},
}

TODO: There's probably no reason to use reflect.TypeOf here. Command is fine.

Functions

func AsDDL

func AsDDL(cmd Command) string

func AsDML

func AsDML(cmd Command) string

func CommandToString

func CommandToString(cmd Command) string

func FlipCoin

func FlipCoin() bool

func FullyQualifiedName

func FullyQualifiedName(el dag.INode) string

func NewOracle

func NewOracle(db *sqlx.DB, log *log.Logger) (*oracle, error)

func NewSUT

func NewSUT(conn *sqlx.DB, log *log.Logger) *sut

func NotPublic

func NotPublic(s *Schema) bool

func RandomString

func RandomString(prefixes ...string) string

func Tpl

func Tpl(body string, vars any) string

func WithFullQualifiedName

func WithFullQualifiedName[T dag.INode](name string) dag.Filter[T]

Types

type AddColumn

type AddColumn struct {
	Table    *Table
	Name     string
	Nullable bool
}

type Column

type Column struct {
	dag.Node
	Name string `db:"name"`
}

func (*Column) Table

func (c *Column) Table() *Table

type Command

type Command interface{}

func GenerateCommand

func GenerateCommand(g *dag.Graph) Command

type CreateDatabase

type CreateDatabase struct {
	Name string
}

type CreateForeignKeyConstraint

type CreateForeignKeyConstraint struct {
	From *Column
	To   *Column
	Name string
}

type CreateIndex

type CreateIndex struct {
	Table   *Table
	Columns []*Column
	Name    string
	Unique  bool
}

type CreateSchema

type CreateSchema struct {
	Database *Database
	Name     string
}

type CreateTable

type CreateTable struct {
	Schema *Schema
	Name   string
}

type Database

type Database struct {
	dag.Node
	Name string `db:"name"`
}

func (*Database) Schemas

func (d *Database) Schemas() []*Schema

type DropColumn

type DropColumn struct {
	Column *Column
}

type DropDatabase

type DropDatabase struct {
	Database *Database
}

type DropForeignKeyConstraint

type DropForeignKeyConstraint struct {
	ForeignKeyConstraint *ForeignKeyConstraint
}

type DropIndex

type DropIndex struct {
	Index *Index
}

type DropSchema

type DropSchema struct {
	Schema *Schema
}

type DropTable

type DropTable struct {
	Table *Table
}

type ForeignKeyConstraint

type ForeignKeyConstraint struct {
	dag.Node
	Name string `db:"name"`
}

func (*ForeignKeyConstraint) From

func (c *ForeignKeyConstraint) From() *Column

func (*ForeignKeyConstraint) To

func (c *ForeignKeyConstraint) To() *Column

TODO relying on order here feels SUPER sketchy. We may need a way to annotate edges...

type Index

type Index struct {
	dag.Node
	Unique bool   `db:"unique"`
	Name   string `db:"name"`
}

func (*Index) Columns

func (i *Index) Columns() []*Column

func (*Index) Table

func (i *Index) Table() *Table

type Queries

type Queries struct {
	Databases             string
	Schemas               string
	Tables                string
	Columns               string
	Indexes               string
	ColumnsToIndexes      string
	ForeignKeyConstraints string
}

type RenameDatabase

type RenameDatabase struct {
	Database *Database
	Name     string
}

type RenameSchema

type RenameSchema struct {
	Schema *Schema
	Name   string
}

type RenameTable

type RenameTable struct {
	Table *Table
	Name  string
}

type Schema

type Schema struct {
	dag.Node
	Name string `db:"name"`
}

func (*Schema) Database

func (s *Schema) Database() *Database

func (*Schema) Tables

func (s *Schema) Tables() []*Table

type Step

type Step struct {
	Command  Command
	Expected *dag.Graph
}

type System

type System interface {
	Execute(context.Context, Command) error

	State(context.Context) (*dag.Graph, error)
}

type Table

type Table struct {
	dag.Node
	Name string `db:"name"`
}

func (*Table) Columns

func (t *Table) Columns() dag.Result[*Column]

func (*Table) Indexes

func (t *Table) Indexes() dag.Result[*Index]

func (*Table) Schema

func (t *Table) Schema() *Schema

type Transcript

type Transcript struct {
	Steps []Step
}

func (*Transcript) Run

func (t *Transcript) Run(ctx context.Context, sys System) error

type Translation

type Translation struct {
	DML string
	DDL string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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