schemaspy

package module
v0.0.0-...-72c92d7 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2017 License: MIT Imports: 3 Imported by: 0

README

What

Schemaspy gets a schema from a PostgreSQL server as a simple Go datastructure. It describes which tables there are, their columns, &c. Schemaspy only reads; any changes to the database need to be done by other means, such as ALTER TABLE.

Use cases

how this is used:

  • a maintenance script which creates and archives partitioned tables. It needs to know which tables are there already, and which need to be created or have an outdated definition.
  • to compare on deployment the current database (as returned by schemaspy) against the wanted state, so the deploy process can warn about missing database changes.

Test

The tests need access to a PostgreSQL server, with a database schemaspy:

# su  -c "createuser -DRS yourusername" postgres
# su  -c "createdb -O yourusername schemaspy" postgres
$ make int

Documentation

Overview

schemaspy reads the definition of a PostgreSQL schema: tables, functions, indexes &c. and returns it as a datastructure.

It can't change things; the use case is for maintenance and setup scripts which need to inspect the current state of the database. Those scripts are expected to draw their conclusions and if needed apply their changes with `ALTER` commands.

Example
package main

import (
	"fmt"
	"log"

	"github.com/alicebob/schemaspy"
)

func main() {
	pgURL := "postgres://localhost"
	schema, err := schemaspy.Public(pgURL)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("schema: %s\n", schema.Name)
	for _, t := range schema.Tables {
		table := schema.Relations[t]
		fmt.Printf("table: %s (%d cols)\n", t, len(table.Columns))
		for _, index := range table.Indexes {
			fmt.Printf("  index: %s\n", index)
		}
	}

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	Type     string
	NotNull  bool
	Position int
}

type Function

type Function struct {
	Language      string
	ArgumentTypes []string
	Src           string
}

type Index

type Index struct {
	Table   string
	Type    string
	Unique  bool
	Primary bool
	Columns []string // column name or '[function]' for expressions
}

type Relation

type Relation struct {
	// Type is "table", "view", or "materialized view"
	Type     string
	Columns  map[string]Column
	Inherits []string
	Children []string
	Indexes  []string
}

Relation is a table, view, or materialized view.

func (*Relation) ColumnNames

func (t *Relation) ColumnNames() []string

ColumnNames lists all columns in database order

type Schema

type Schema struct {
	Name string

	// Relations are all tables, views, and materialized views
	Relations map[string]Relation

	// all plain tables, ordered alphabetically. Every table has an entry in
	// Relations
	Tables []string

	// all views, ordered alphabetically. Every view has an entry in Relations
	Views []string

	// all materialized views, ordered alphabetically. Every materialized view
	// has an entry in Relations
	Materialized []string

	Sequences map[string]Sequence

	Indexes map[string]Index

	Functions map[string]Function
}

func Describe

func Describe(conn *pgx.ConnPool, schema string) (*Schema, error)

Describe a schema. Leave schema empty for the public schema.

func DescribeConn

func DescribeConn(conn *pgx.Conn, schema string) (*Schema, error)

Describe a schema. Leave schema empty for the public schema.

func DescribeTx

func DescribeTx(tx *pgx.Tx, schema string) (*Schema, error)

Describe a schema. Leave schema empty for the public schema.

func Public

func Public(pgURL string) (*Schema, error)

Public is a wrapper around Describe. It needs a pg URL (such as "postgres://localhost"), and it'll return the public schema.

type Sequence

type Sequence struct {
	IncrementBy int
	MinValue    int
	MaxValue    int
	Start       int
	Cycle       bool
}

Jump to

Keyboard shortcuts

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