goerd

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: MIT Imports: 12 Imported by: 0

README

goerd

This is a New Era of migrations PostgreSQL databases with Golang.

We no longer want to ensure the database schema with incremental patches. Shema MUST match what the app wants. It doesn't matter what was in the database before that.

It could be empty, it could contain some data in any of its previous versions. It doesn't matter anymore. It is important that we carefully transform it into the target state. We will no longer be guessing about the target state of the database by patchset. This is a thing of the past.

This tool allows you to turn schemas into instructions for the database, including migrations between schemas. Create easy-to-read data models as contracts for agreement between architects, development teams, and team leaders. This tool provides agility to change the huge data-layered models.

Conceptual view

API

Use as library: Productive usage example

Install schema tool

go install github.com/covrom/goerd/cmd/goerd@latest

Features
  • Generate yaml short schema description from database
  • Generate plantuml view of schema
  • Create posgresql migrations as a set of SQL queries that apply changes between two schemas, a schema and a database, or two databases using a schema definition that is stored in a yaml or plantuml file.

Migration of the following objects is currently supported:

  • Tables
  • Columns
  • Indexes
  • Constraints and Foreign Keys
  • Views

This set covers 99% of PostgreSQL usecases in Golang services.

Example of generated plantuml:

Plantuml view

Tool usage

Save database schema to yaml schema or PlantUML:

goerd -from "postgres://username:password@url:port/dbName" -to ./schema1.yaml

goerd -from "postgres://username:password@url:port/dbName" -to ./schema1.puml

Print SQL queries migration between two schemas, dropping queries is commented out:

goerd -from schema1.yml -to schema2.yaml

goerd -from schema1.yml -to "postgres://username:password@url:port/dbName"

With dropping queries:

goerd -drop -from schema1.yml -to schema2.yml

goerd -drop -from schema1.yml -to "postgres://username:password@url:port/dbName"

Save PlantUML from schema:

goerd -from schema1.yml -to schema1.puml

goerd -from "postgres://username:password@url:port/dbName" -to schema1.puml

Apply schema from yaml to database:

goerd -c apply -from schema1.yml -to "postgres://username:password@url:port/dbName"

Apply schema from DB1 to DB2:

goerd -c apply -from "postgres://username:password@url:port/DB1" -to "postgres://username:password@url:port/DB2"

Docs

Docs.

Testing and Examples

docker-compose up and see ./output/schema.yaml

Productive usage example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnsWithout added in v1.0.6

func ColumnsWithout(cols []string, skip ...string) []string

func Commit added in v1.0.6

func Commit(ctx context.Context) error

func Excluded added in v1.0.6

func Excluded(cols []string) []string

func GenerateMigrationSQL

func GenerateMigrationSQL(sfrom, sto *schema.Schema) ([]string, error)

GenerateMigrationSQL generates an array of SQL DDL queries for postgres that modify database tables, columns, indexes, etc.

func HasTx added in v1.0.6

func HasTx(ctx context.Context) bool

func ReplaceQuery added in v1.0.6

func ReplaceQuery(d Columner, idCols ...string) string

func Replacers added in v1.0.6

func Replacers(cnt int) string

func Rollback added in v1.0.6

func Rollback(ctx context.Context) error

func SchemaFromPostgresDB

func SchemaFromPostgresDB(db *sql.DB) (*schema.Schema, error)

SchemaFromPostgresDB reads database schema from postgres *sql.DB

func SchemaFromPostgresWithConnect added in v1.0.2

func SchemaFromPostgresWithConnect(dsn string) (*schema.Schema, error)

SchemaFromPostgresDB reads database schema from postgres *sql.DB

func SchemaFromYAML

func SchemaFromYAML(r io.Reader) (*schema.Schema, error)

SchemaFromYAML loads the schema from the yaml file

func SchemaToPlantUML added in v1.0.2

func SchemaToPlantUML(s *schema.Schema, w io.Writer, distance int) error

SchemaToPlantUML saves the schema to a puml file, distance - maximum path length for relationships between plantuml objects

func SchemaToYAML

func SchemaToYAML(s *schema.Schema, w io.Writer) error

SchemaToYAML saves the schema to a yaml file

func SqlxDbFromContext added in v1.0.6

func SqlxDbFromContext(ctx context.Context) *sqlx.DB

func SqlxTxFromContext added in v1.0.6

func SqlxTxFromContext(ctx context.Context) *sqlx.Tx

func WithSqlxDb added in v1.0.6

func WithSqlxDb(ctx context.Context, d *sqlx.DB) context.Context

func WithSqlxTx added in v1.0.6

func WithSqlxTx(ctx context.Context, tx *sqlx.Tx) context.Context

func WithTx added in v1.0.6

func WithTx(ctx context.Context, f func(context.Context) error) error

Types

type Columner added in v1.0.6

type Columner interface {
	Table() string
	Columns() []string
}

type CtxSqlxDb added in v1.0.6

type CtxSqlxDb struct{}

type CtxSqlxTx added in v1.0.6

type CtxSqlxTx struct{}

type ModelSet added in v1.0.7

type ModelSet struct {
	// contains filtered or unexported fields
}

func NewModelSet added in v1.0.7

func NewModelSet(mds ...objectModel) *ModelSet

func (*ModelSet) Migrate added in v1.0.7

func (md *ModelSet) Migrate(d *sqlx.DB, dbSchema string) error

func (*ModelSet) WithRelations added in v1.0.7

func (md *ModelSet) WithRelations(rels ...*schema.Relation) *ModelSet

type ObjectField added in v1.0.7

type ObjectField[T any] struct {
	// contains filtered or unexported fields
}

func Field added in v1.0.7

func Field[T any](column *schema.Column) *ObjectField[T]

func FieldVirtual added in v1.0.7

func FieldVirtual[T any](dbName string) *ObjectField[T]

func (*ObjectField[T]) Column added in v1.0.7

func (f *ObjectField[T]) Column() *schema.Column

type ObjectModel added in v1.0.7

type ObjectModel[T any] struct {
	// contains filtered or unexported fields
}

func Model added in v1.0.7

func Model[T any](table string, fields ...objectField) *ObjectModel[T]

func (*ObjectModel[T]) Field added in v1.0.7

func (m *ObjectModel[T]) Field(dbName string) objectField

func (*ObjectModel[T]) New added in v1.0.7

func (m *ObjectModel[T]) New() *T

func (*ObjectModel[T]) SchemaTable added in v1.0.7

func (m *ObjectModel[T]) SchemaTable() *schema.Table

func (*ObjectModel[T]) WithComment added in v1.0.7

func (m *ObjectModel[T]) WithComment(c string) *ObjectModel[T]

func (*ObjectModel[T]) WithConstraint added in v1.0.7

func (m *ObjectModel[T]) WithConstraint(ctrs ...*schema.Constraint) *ObjectModel[T]

func (*ObjectModel[T]) WithDef added in v1.0.7

func (m *ObjectModel[T]) WithDef(def string) *ObjectModel[T]

func (*ObjectModel[T]) WithIndex added in v1.0.7

func (m *ObjectModel[T]) WithIndex(idxs ...*schema.Index) *ObjectModel[T]

func (*ObjectModel[T]) WithType added in v1.0.7

func (m *ObjectModel[T]) WithType(t string) *ObjectModel[T]

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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