package models type DatabaseType string const ( PostgresqlDatabaseType DatabaseType = "pgsql" MSSQLDatabaseType DatabaseType = "mssql" SqlLiteDatabaseType DatabaseType = "sqlite" ) // Database represents the complete database schema type Database struct { Name string `json:"name" yaml:"name"` Schemas []*Schema `json:"schemas" yaml:"schemas" xml:"schemas"` Comment string `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"` DatabaseType DatabaseType `json:"database_type,omitempty" yaml:"database_type,omitempty" xml:"database_type,omitempty"` DatabaseVersion string `json:"database_version,omitempty" yaml:"database_version,omitempty" xml:"database_version,omitempty"` } type Schema struct { Name string `json:"name" yaml:"name" xml:"name"` Tables []*Table `json:"tables" yaml:"tables" xml:"-"` Owner string `json:"owner" yaml:"owner" xml:"owner"` Permissions map[string]string `json:"permissions,omitempty" yaml:"permissions,omitempty" xml:"-"` Comment string `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"` Metadata map[string]interface{} `json:"metadata,omitempty" yaml:"metadata,omitempty" xml:"-"` Scripts []*Script `json:"scripts,omitempty" yaml:"scripts,omitempty" xml:"scripts,omitempty"` } type Table struct { Name string `json:"name" yaml:"name" xml:"name"` Schema string `json:"schema" yaml:"schema" xml:"schema"` Columns map[string]*Column `json:"columns" yaml:"columns" xml:"-"` Constraints map[string]*Constraint `json:"constraints" yaml:"constraints" xml:"-"` Indexes map[string]*Index `json:"indexes,omitempty" yaml:"indexes,omitempty" xml:"-"` Relationships map[string]*Relationship `json:"relationships,omitempty" yaml:"relationships,omitempty" xml:"-"` Comment string `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"` Tablespace string `json:"tablespace,omitempty" yaml:"tablespace,omitempty" xml:"tablespace,omitempty"` Metadata map[string]interface{} `json:"metadata,omitempty" yaml:"metadata,omitempty" xml:"-"` } func (m Table) GetPrimaryKey() *Column { for _, column := range m.Columns { if column.IsPrimaryKey { return column } } return nil } func (m Table) GetForeignKeys() []*Constraint { keys := make([]*Constraint, 0) for _, c := range m.Constraints { if c.Type == ForeignKeyConstraint { keys = append(keys, c) } } return keys } // Column represents a table column type Column struct { Name string `json:"name" yaml:"name" xml:"name"` Table string `json:"table" yaml:"table" xml:"table"` Schema string `json:"schema" yaml:"schema" xml:"schema"` Type string `json:"type" yaml:"type" xml:"type"` Length int `json:"length,omitempty" yaml:"length,omitempty" xml:"length,omitempty"` Precision int `json:"precision,omitempty" yaml:"precision,omitempty" xml:"precision,omitempty"` Scale int `json:"scale,omitempty" yaml:"scale,omitempty" xml:"scale,omitempty"` NotNull bool `json:"not_null" yaml:"not_null" xml:"not_null"` Default interface{} `json:"default,omitempty" yaml:"default,omitempty" xml:"default,omitempty"` AutoIncrement bool `json:"auto_increment" yaml:"auto_increment" xml:"auto_increment"` IsPrimaryKey bool `json:"is_primary_key" yaml:"is_primary_key" xml:"is_primary_key"` Comment string `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"` Collation string `json:"collation,omitempty" yaml:"collation,omitempty" xml:"collation,omitempty"` } type Index struct { Name string `json:"name" yaml:"name" xml:"name"` Table string `json:"table,omitempty" yaml:"table,omitempty" xml:"table,omitempty"` Schema string `json:"schema,omitempty" yaml:"schema,omitempty" xml:"schema,omitempty"` Columns []string `json:"columns" yaml:"columns" xml:"columns"` Unique bool `json:"unique" yaml:"unique" xml:"unique"` Type string `json:"type" yaml:"type" xml:"type"` // btree, hash, gin, gist, etc. Where string `json:"where,omitempty" yaml:"where,omitempty" xml:"where,omitempty"` // partial index condition Concurrent bool `json:"concurrent,omitempty" yaml:"concurrent,omitempty" xml:"concurrent,omitempty"` Include []string `json:"include,omitempty" yaml:"include,omitempty" xml:"include,omitempty"` // INCLUDE columns Comment string `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"` } type RelationType string const ( OneToOne RelationType = "one_to_one" OneToMany RelationType = "one_to_many" ManyToMany RelationType = "many_to_many" ) type Relationship struct { Name string `json:"name" yaml:"name" xml:"name"` Type RelationType `json:"type" yaml:"type" xml:"type"` FromTable string `json:"from_table" yaml:"from_table" xml:"from_table"` FromSchema string `json:"from_schema" yaml:"from_schema" xml:"from_schema"` ToTable string `json:"to_table" yaml:"to_table" xml:"to_table"` ToSchema string `json:"to_schema" yaml:"to_schema" xml:"to_schema"` ForeignKey string `json:"foreign_key" yaml:"foreign_key" xml:"foreign_key"` Properties map[string]string `json:"properties" yaml:"properties" xml:"-"` ThroughTable string `json:"through_table,omitempty" yaml:"through_table,omitempty" xml:"through_table,omitempty"` // For many-to-many ThroughSchema string `json:"through_schema,omitempty" yaml:"through_schema,omitempty" xml:"through_schema,omitempty"` Description string `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"` } type Constraint struct { Name string `json:"name" yaml:"name" xml:"name"` Type ConstraintType `json:"type" yaml:"type" xml:"type"` Columns []string `json:"columns" yaml:"columns" xml:"columns"` Expression string `json:"expression,omitempty" yaml:"expression,omitempty" xml:"expression,omitempty"` Schema string `json:"schema,omitempty" yaml:"schema,omitempty" xml:"schema,omitempty"` Table string `json:"table,omitempty" yaml:"table,omitempty" xml:"table,omitempty"` ReferencedTable string `json:"referenced_table" yaml:"referenced_table" xml:"referenced_table"` ReferencedSchema string `json:"referenced_schema" yaml:"referenced_schema" xml:"referenced_schema"` ReferencedColumns []string `json:"referenced_columns" yaml:"referenced_columns" xml:"referenced_columns"` OnDelete string `json:"on_delete" yaml:"on_delete" xml:"on_delete"` // CASCADE, SET NULL, RESTRICT, etc. OnUpdate string `json:"on_update" yaml:"on_update" xml:"on_update"` Deferrable bool `json:"deferrable,omitempty" yaml:"deferrable,omitempty" xml:"deferrable,omitempty"` InitiallyDeferred bool `json:"initially_deferred,omitempty" yaml:"initially_deferred,omitempty" xml:"initially_deferred,omitempty"` } type ConstraintType string const ( PrimaryKeyConstraint ConstraintType = "primary_key" ForeignKeyConstraint ConstraintType = "foreign_Key" UniqueConstraint ConstraintType = "unique" CheckConstraint ConstraintType = "check" NotNullConstraint ConstraintType = "not_null" ) type Script struct { Name string `json:"name" yaml:"name" xml:"name"` Description string `json:"description" yaml:"description" xml:"description"` SQL string `json:"sql" yaml:"sql" xml:"sql"` Rollback string `json:"rollback,omitempty" yaml:"rollback,omitempty" xml:"rollback,omitempty"` RunAfter []string `json:"run_after,omitempty" yaml:"run_after,omitempty" xml:"run_after,omitempty"` Schema string `json:"schema,omitempty" yaml:"schema,omitempty" xml:"schema,omitempty"` Version string `json:"version,omitempty" yaml:"version,omitempty" xml:"version,omitempty"` Priority int `json:"priority,omitempty" yaml:"priority,omitempty" xml:"priority,omitempty"` }