Documentation
¶
Overview ¶
Package schemaextract provides comprehensive database schema extraction, comparison, and migration management for MySQL and PostgreSQL databases.
Overview ¶
This library enables developers to:
- Extract complete database schemas including tables, views, functions, triggers, and more
- Compare schemas to detect differences and generate migration plans
- Export schemas to JSON, YAML, or SQL formats
- Create point-in-time snapshots for schema versioning
- Generate human-readable schema comparison reports
Supported Databases ¶
- MySQL 5.7+, 8.0+
- PostgreSQL 10+
Quick Start ¶
Extract a MySQL schema:
import (
"database/sql"
"github.com/NSXBet/sql-schema/extractor/mysql"
_ "github.com/go-sql-driver/mysql"
)
db, _ := sql.Open("mysql", "user:pass@tcp(localhost:3306)/mydb")
schema, err := mysql.Extract(db)
if err != nil {
log.Fatal(err)
}
Compare two schemas:
import "github.com/NSXBet/sql-schema/comparer"
diffs, err := comparer.CompareSchemas(oldSchema, newSchema, nil)
if err != nil {
log.Fatal(err)
}
Export schema to JSON:
import "github.com/NSXBet/sql-schema/exporter"
err := exporter.ExportJSON(schema, "schema.json")
if err != nil {
log.Fatal(err)
}
Architecture ¶
The library is organized into several key packages:
- extractor/mysql, extractor/postgres: Database-specific schema extraction
- comparer: Schema comparison and difference detection
- planner: Migration planning and strategy generation
- formatter: Human-readable report generation (text, markdown, JSON)
- exporter: Schema export to various formats
- snapshot: Point-in-time schema snapshots with versioning
Security Considerations ¶
All database queries use parameterized statements to prevent SQL injection. Database credentials should be managed securely using environment variables or secret management systems. Schema information should be treated as sensitive data.
Thread Safety ¶
All exported functions are safe for concurrent use unless otherwise documented.
Beta Status ¶
This library is currently in beta (v0.1.x). API breaking changes may occur between minor versions. Production use is supported but extensive testing is recommended.
Package schemaextract provides database schema extraction for MySQL and PostgreSQL.
Index ¶
- type CheckConstraint
- type Column
- type DatabaseSchema
- type EnumType
- type Event
- type Extension
- type ForeignKey
- type Function
- type Generation
- type GenerationType
- type Identity
- type Index
- type MaterializedView
- type Partition
- type PartitionType
- type Procedure
- type Rule
- type Schema
- type Sequence
- type Snapshot
- type SnapshotMetadata
- type SpatialConfig
- type Table
- type Trigger
- type View
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CheckConstraint ¶
type CheckConstraint struct {
Name string `json:"name" yaml:"name"`
Expression string `json:"expression" yaml:"expression"`
}
CheckConstraint represents a CHECK constraint.
type Column ¶
type Column struct {
Name string `json:"name" yaml:"name"`
Position int32 `json:"position" yaml:"position"`
Type string `json:"type" yaml:"type"`
Nullable bool `json:"nullable" yaml:"nullable"`
Default string `json:"default,omitempty" yaml:"default,omitempty"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
CharacterSet string `json:"characterSet,omitempty" yaml:"characterSet,omitempty"`
Collation string `json:"collation,omitempty" yaml:"collation,omitempty"`
OnUpdate string `json:"onUpdate,omitempty" yaml:"onUpdate,omitempty"` // MySQL: ON UPDATE CURRENT_TIMESTAMP
Generation *Generation `json:"generation,omitempty" yaml:"generation,omitempty"`
Identity *Identity `json:"identity,omitempty" yaml:"identity,omitempty"` // PostgreSQL: IDENTITY columns
}
Column represents a table column.
type DatabaseSchema ¶
type DatabaseSchema struct {
Name string `json:"name" yaml:"name"`
CharacterSet string `json:"characterSet,omitempty" yaml:"characterSet,omitempty"` // MySQL only
Collation string `json:"collation,omitempty" yaml:"collation,omitempty"`
SearchPath []string `json:"searchPath,omitempty" yaml:"searchPath,omitempty"` // PostgreSQL only
Schemas []*Schema `json:"schemas" yaml:"schemas"`
}
DatabaseSchema represents the complete schema metadata for a database.
type EnumType ¶
type EnumType struct {
Name string `json:"name" yaml:"name"`
Values []string `json:"values" yaml:"values"`
Schema string `json:"schema" yaml:"schema"`
}
EnumType represents a PostgreSQL enum type.
type Event ¶
type Event struct {
Name string `json:"name" yaml:"name"`
Definition string `json:"definition" yaml:"definition"`
TimeZone string `json:"timeZone,omitempty" yaml:"timeZone,omitempty"`
SQLMode string `json:"sqlMode,omitempty" yaml:"sqlMode,omitempty"`
CharacterSetClient string `json:"characterSetClient,omitempty" yaml:"characterSetClient,omitempty"`
CollationConnection string `json:"collationConnection,omitempty" yaml:"collationConnection,omitempty"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
}
Event represents a MySQL scheduled event.
type Extension ¶
type Extension struct {
Name string `json:"name" yaml:"name"`
Schema string `json:"schema" yaml:"schema"`
Version string `json:"version" yaml:"version"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
}
Extension represents a PostgreSQL extension.
type ForeignKey ¶
type ForeignKey struct {
Name string `json:"name" yaml:"name"`
Columns []string `json:"columns" yaml:"columns"`
ReferencedTable string `json:"referencedTable" yaml:"referencedTable"`
ReferencedColumns []string `json:"referencedColumns" yaml:"referencedColumns"`
OnDelete string `json:"onDelete,omitempty" yaml:"onDelete,omitempty"` // CASCADE, SET NULL, RESTRICT, NO ACTION
OnUpdate string `json:"onUpdate,omitempty" yaml:"onUpdate,omitempty"`
MatchType string `json:"matchType,omitempty" yaml:"matchType,omitempty"` // MySQL: SIMPLE, PARTIAL, FULL
}
ForeignKey represents a foreign key constraint.
type Function ¶
type Function struct {
Name string `json:"name" yaml:"name"`
Definition string `json:"definition" yaml:"definition"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
// MySQL-specific
SQLMode string `json:"sqlMode,omitempty" yaml:"sqlMode,omitempty"`
CharacterSetClient string `json:"characterSetClient,omitempty" yaml:"characterSetClient,omitempty"`
CollationConnection string `json:"collationConnection,omitempty" yaml:"collationConnection,omitempty"`
DatabaseCollation string `json:"databaseCollation,omitempty" yaml:"databaseCollation,omitempty"`
}
Function represents a database function.
type Generation ¶
type Generation struct {
Type GenerationType `json:"type" yaml:"type"` // VIRTUAL or STORED
Expression string `json:"expression" yaml:"expression"`
}
Generation represents a generated column.
type GenerationType ¶
type GenerationType string
GenerationType indicates how a generated column is stored.
const ( GenerationTypeVirtual GenerationType = "VIRTUAL" GenerationTypeStored GenerationType = "STORED" )
type Identity ¶
type Identity struct {
Seed int64 `json:"seed" yaml:"seed"` // Starting value
Increment int64 `json:"increment" yaml:"increment"` // Increment value
Always bool `json:"always" yaml:"always"` // ALWAYS vs BY DEFAULT
Cycle bool `json:"cycle" yaml:"cycle"` // Cycle when reaching max/min
Cache int64 `json:"cache,omitempty" yaml:"cache,omitempty"`
MinValue int64 `json:"minValue,omitempty" yaml:"minValue,omitempty"`
MaxValue int64 `json:"maxValue,omitempty" yaml:"maxValue,omitempty"`
}
Identity represents a PostgreSQL IDENTITY column.
type Index ¶
type Index struct {
Name string `json:"name" yaml:"name"`
Type string `json:"type" yaml:"type"` // BTREE, HASH, FULLTEXT, SPATIAL, GIN, GIST, etc.
Expressions []string `json:"expressions" yaml:"expressions"`
Primary bool `json:"primary" yaml:"primary"`
Unique bool `json:"unique" yaml:"unique"`
Visible bool `json:"visible" yaml:"visible"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
KeyLength []int64 `json:"keyLength,omitempty" yaml:"keyLength,omitempty"` // MySQL: prefix length, -1 means full column
Descending []bool `json:"descending,omitempty" yaml:"descending,omitempty"`
WhereClause string `json:"whereClause,omitempty" yaml:"whereClause,omitempty"` // Partial index condition
Spatial *SpatialConfig `json:"spatial,omitempty" yaml:"spatial,omitempty"` // Spatial index configuration
}
Index represents a table index.
type MaterializedView ¶
type MaterializedView struct {
Name string `json:"name" yaml:"name"`
Definition string `json:"definition" yaml:"definition"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
Columns []*Column `json:"columns,omitempty" yaml:"columns,omitempty"`
}
MaterializedView represents a PostgreSQL materialized view.
type Partition ¶
type Partition struct {
Name string `json:"name" yaml:"name"`
Type PartitionType `json:"type" yaml:"type"`
Expression string `json:"expression,omitempty" yaml:"expression,omitempty"`
Value string `json:"value,omitempty" yaml:"value,omitempty"`
Subpartitions []*Partition `json:"subpartitions,omitempty" yaml:"subpartitions,omitempty"`
UseDefault string `json:"useDefault,omitempty" yaml:"useDefault,omitempty"` // Default partition count
}
Partition represents a table partition.
type PartitionType ¶
type PartitionType string
PartitionType represents the partition type.
const ( PartitionTypeRange PartitionType = "RANGE" PartitionTypeRangeColumns PartitionType = "RANGE_COLUMNS" PartitionTypeList PartitionType = "LIST" PartitionTypeListColumns PartitionType = "LIST_COLUMNS" PartitionTypeHash PartitionType = "HASH" PartitionTypeKey PartitionType = "KEY" PartitionTypeLinearHash PartitionType = "LINEAR_HASH" PartitionTypeLinearKey PartitionType = "LINEAR_KEY" )
type Procedure ¶
type Procedure struct {
Name string `json:"name" yaml:"name"`
Definition string `json:"definition" yaml:"definition"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
// MySQL-specific
SQLMode string `json:"sqlMode,omitempty" yaml:"sqlMode,omitempty"`
CharacterSetClient string `json:"characterSetClient,omitempty" yaml:"characterSetClient,omitempty"`
CollationConnection string `json:"collationConnection,omitempty" yaml:"collationConnection,omitempty"`
DatabaseCollation string `json:"databaseCollation,omitempty" yaml:"databaseCollation,omitempty"`
}
Procedure represents a database stored procedure.
type Rule ¶
type Rule struct {
Name string `json:"name" yaml:"name"`
Event string `json:"event" yaml:"event"` // SELECT, INSERT, UPDATE, DELETE
Timing string `json:"timing" yaml:"timing"` // INSTEAD, ALSO
Definition string `json:"definition" yaml:"definition"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
}
Rule represents a PostgreSQL rule.
type Schema ¶
type Schema struct {
Name string `json:"name" yaml:"name"`
Tables []*Table `json:"tables,omitempty" yaml:"tables,omitempty"`
Views []*View `json:"views,omitempty" yaml:"views,omitempty"`
Functions []*Function `json:"functions,omitempty" yaml:"functions,omitempty"`
Procedures []*Procedure `json:"procedures,omitempty" yaml:"procedures,omitempty"`
Events []*Event `json:"events,omitempty" yaml:"events,omitempty"` // MySQL only
// PostgreSQL-specific
MaterializedViews []*MaterializedView `json:"materializedViews,omitempty" yaml:"materializedViews,omitempty"`
Sequences []*Sequence `json:"sequences,omitempty" yaml:"sequences,omitempty"`
Extensions []*Extension `json:"extensions,omitempty" yaml:"extensions,omitempty"`
EnumTypes []*EnumType `json:"enumTypes,omitempty" yaml:"enumTypes,omitempty"`
Rules []*Rule `json:"rules,omitempty" yaml:"rules,omitempty"` // PostgreSQL rules (schema-level)
}
Schema represents a database schema (namespace). MySQL has a single unnamed schema, PostgreSQL can have multiple named schemas.
type Sequence ¶
type Sequence struct {
Name string `json:"name" yaml:"name"`
DataType string `json:"dataType" yaml:"dataType"`
Start int64 `json:"start" yaml:"start"`
Increment int64 `json:"increment" yaml:"increment"`
Min int64 `json:"min" yaml:"min"`
Max int64 `json:"max" yaml:"max"`
Cache int64 `json:"cache" yaml:"cache"`
Cycle bool `json:"cycle" yaml:"cycle"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
}
Sequence represents a PostgreSQL sequence.
type Snapshot ¶
type Snapshot struct {
Metadata SnapshotMetadata `json:"metadata" yaml:"metadata"`
Schema *DatabaseSchema `json:"schema" yaml:"schema"`
}
Snapshot wraps a DatabaseSchema with metadata for tracking and management. It provides a point-in-time capture of a database schema with additional context.
type SnapshotMetadata ¶
type SnapshotMetadata struct {
Timestamp time.Time `json:"timestamp" yaml:"timestamp"`
Version string `json:"version" yaml:"version"` // Snapshot format version
DatabaseName string `json:"databaseName" yaml:"databaseName"` // Name of the database
DatabaseEngine string `json:"databaseEngine" yaml:"databaseEngine"` // "mysql" or "postgres"
HostInfo string `json:"hostInfo,omitempty" yaml:"hostInfo,omitempty"` // Optional host connection info
Tags map[string]string `json:"tags,omitempty" yaml:"tags,omitempty"` // User-defined tags (e.g., "environment": "production")
Description string `json:"description,omitempty" yaml:"description,omitempty"` // User-provided description
ChecksumMD5 string `json:"checksumMd5,omitempty" yaml:"checksumMd5,omitempty"` // MD5 checksum of schema
ChecksumSHA256 string `json:"checksumSha256,omitempty" yaml:"checksumSha256,omitempty"` // SHA256 checksum of schema
}
SnapshotMetadata contains tracking information for a schema snapshot.
type SpatialConfig ¶
type SpatialConfig struct {
BoundingBox string `json:"boundingBox,omitempty" yaml:"boundingBox,omitempty"` // MySQL: bounding box specification
Tessellation string `json:"tessellation,omitempty" yaml:"tessellation,omitempty"` // MySQL: tessellation scheme
Storage string `json:"storage,omitempty" yaml:"storage,omitempty"` // Storage parameters
}
SpatialConfig represents spatial index configuration.
type Table ¶
type Table struct {
Name string `json:"name" yaml:"name"`
Columns []*Column `json:"columns" yaml:"columns"`
Indexes []*Index `json:"indexes,omitempty" yaml:"indexes,omitempty"`
ForeignKeys []*ForeignKey `json:"foreignKeys,omitempty" yaml:"foreignKeys,omitempty"`
CheckConstraints []*CheckConstraint `json:"checkConstraints,omitempty" yaml:"checkConstraints,omitempty"`
Triggers []*Trigger `json:"triggers,omitempty" yaml:"triggers,omitempty"`
Partitions []*Partition `json:"partitions,omitempty" yaml:"partitions,omitempty"`
Rules []*Rule `json:"rules,omitempty" yaml:"rules,omitempty"` // PostgreSQL table-level rules
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
// MySQL-specific
Engine string `json:"engine,omitempty" yaml:"engine,omitempty"`
Collation string `json:"collation,omitempty" yaml:"collation,omitempty"`
RowCount int64 `json:"rowCount,omitempty" yaml:"rowCount,omitempty"`
DataSize int64 `json:"dataSize,omitempty" yaml:"dataSize,omitempty"`
IndexSize int64 `json:"indexSize,omitempty" yaml:"indexSize,omitempty"`
DataFree int64 `json:"dataFree,omitempty" yaml:"dataFree,omitempty"`
CreateOptions string `json:"createOptions,omitempty" yaml:"createOptions,omitempty"`
Charset string `json:"charset,omitempty" yaml:"charset,omitempty"`
}
Table represents a database table.
type Trigger ¶
type Trigger struct {
Name string `json:"name" yaml:"name"`
Event string `json:"event" yaml:"event"` // INSERT, UPDATE, DELETE
Timing string `json:"timing" yaml:"timing"` // BEFORE, AFTER
Body string `json:"body" yaml:"body"`
// MySQL-specific
SQLMode string `json:"sqlMode,omitempty" yaml:"sqlMode,omitempty"`
CharacterSetClient string `json:"characterSetClient,omitempty" yaml:"characterSetClient,omitempty"`
CollationConnection string `json:"collationConnection,omitempty" yaml:"collationConnection,omitempty"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
}
Trigger represents a database trigger.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package comparer provides schema comparison and difference detection for MySQL and PostgreSQL databases.
|
Package comparer provides schema comparison and difference detection for MySQL and PostgreSQL databases. |
|
engine
Package engine provides database-specific comparison engines for MySQL and PostgreSQL schema comparison.
|
Package engine provides database-specific comparison engines for MySQL and PostgreSQL schema comparison. |
|
engine/base
Package defaultcomparer provides shared comparison utilities and base implementations for database schema comparison engines.
|
Package defaultcomparer provides shared comparison utilities and base implementations for database schema comparison engines. |
|
engine/mysql
Package mysql provides MySQL-specific schema comparison engine.
|
Package mysql provides MySQL-specific schema comparison engine. |
|
engine/postgres
Package postgres provides PostgreSQL-specific schema comparison engine.
|
Package postgres provides PostgreSQL-specific schema comparison engine. |
|
Package diff defines types for representing schema differences between two database schemas.
|
Package diff defines types for representing schema differences between two database schemas. |
|
snapshot
command
|
|
|
Package exporter provides schema export functionality to various formats including JSON, YAML, and SQL.
|
Package exporter provides schema export functionality to various formats including JSON, YAML, and SQL. |
|
extractor
|
|
|
mysql
Package mysql provides MySQL-specific database schema extraction.
|
Package mysql provides MySQL-specific database schema extraction. |
|
postgres
Package postgres provides PostgreSQL-specific database schema extraction.
|
Package postgres provides PostgreSQL-specific database schema extraction. |
|
Package formatter provides human-readable formatting for schema differences and comparison reports.
|
Package formatter provides human-readable formatting for schema differences and comparison reports. |
|
Package planner provides migration planning and strategy generation for schema changes.
|
Package planner provides migration planning and strategy generation for schema changes. |
|
Package snapshot provides point-in-time schema snapshots with versioning and comparison capabilities.
|
Package snapshot provides point-in-time schema snapshots with versioning and comparison capabilities. |