lib

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2020 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// OptLogInfo triggers verbose logging
	OptLogInfo = 1 << iota
	// OptLogDebug triggers extremely verbose logging
	OptLogDebug
	// OptSilent suppresses all logging
	OptSilent
	// OptReverse reverses the function
	OptReverse
	// OptSummary shows a summary of the actions instead of a raw stdout dump
	OptSummary
	// OptClean cleans
	OptClean
	// OptForce forces
	OptForce
)

Variables

This section is empty.

Functions

func DataTypeToFormatString added in v1.5.0

func DataTypeToFormatString(column *Column) (fieldType string)

DataTypeToFormatString converts a database type to its equivalent golang datatype

func DataTypeToGoTypeString added in v1.5.0

func DataTypeToGoTypeString(column *Column) (fieldType string)

DataTypeToGoTypeString converts a database type to its equivalent golang datatype

func Debug

func Debug(content string, options Options)

Debug writes a debug level log

func Debugf

func Debugf(content string, options Options, a ...interface{})

Debugf writes a formatted debug level log

func DirExists added in v1.5.0

func DirExists(dirPath string) bool

func DirIsEmpty added in v1.5.0

func DirIsEmpty(dirPath string) bool

func EnsureDir added in v1.5.0

func EnsureDir(dir string) (e error)

EnsureDir creates a new dir if the dir is not found

func Error

func Error(content string, options Options)

Error writes an error level log

func Errorf

func Errorf(content string, options Options, a ...interface{})

Errorf writes a formatted error level log

func FetchDirFileNames

func FetchDirFileNames(dirPath string) (fileNames []string, e error)

FetchDirFileNames fetches returns a slice of strings of the name of the directories in a directory (non-recursive)

func FetchFile

func FetchFile(filePath string) (changesetFileString string, e error)

FetchFile fetches a file contents

func FetchNonDirFileNames

func FetchNonDirFileNames(dirPath string) (files []string, e error)

FetchNonDirFileNames returns a list of files in a directory that are only regular files

func FileExists added in v1.5.0

func FileExists(filePath string) bool

func FmtGoCode added in v1.5.0

func FmtGoCode(filePath string) (e error)

FmtGoCode formats a go file

func FormatCode added in v1.5.0

func FormatCode(code string) ([]byte, error)

FormatCode formats the code

func FormatFieldList added in v1.5.0

func FormatFieldList(src []byte, fieldList *ast.FieldList, pkgName string) (fields []string)

FormatFieldList returns a list of formatted list of fields

func GetFuncDeclarationReceiverType added in v1.5.0

func GetFuncDeclarationReceiverType(fd *ast.FuncDecl) (expr ast.Expr, e error)

GetFuncDeclarationReceiverType returns the receiver type of a function declaration

func GetReceiverTypeName added in v1.5.0

func GetReceiverTypeName(src []byte, fl interface{}) (name string, funcDef *ast.FuncDecl)

GetReceiverTypeName returns the name of the receiver type and the declaration

func HashFileMd5

func HashFileMd5(filePath string) (string, error)

HashFileMd5 returns an MD5 checksum of the file at `filePath`

func HashStringMd5 added in v1.5.0

func HashStringMd5(s string) string

HashStringMd5 genereates an MD5 hash of a string

func Info

func Info(content string, options Options)

Info writes an info level log

func Infof

func Infof(content string, options Options, a ...interface{})

Infof writes a formatted info level log

func IsString added in v1.5.0

func IsString(column *Column) bool

func IsValidSQLType added in v1.5.0

func IsValidSQLType(str string) bool

DataTypeToGoTypeString converts a database type to its equivalent golang datatype

func Log

func Log(content string, logLevel LogLevel, options Options)

Log writes to a log based on its log level and the runtime options passed

func ReadCliInput added in v1.5.0

func ReadCliInput(reader *bufio.Reader, title string) string

func RunCommand added in v1.1.0

func RunCommand(name string, args ...string) (stdout string, stderr string, exitCode int)

RunCommand runs a system command

func Warn

func Warn(content string, options Options)

Warn writes a warn level log

func Warnf

func Warnf(content string, options Options, a ...interface{})

Warnf writes a formatted warn level log

func WriteGoCodeToFile added in v1.5.0

func WriteGoCodeToFile(goCode string, filePath string) (e error)

WriteGoCodeToFile writes a string of golang code to a file and then formats it with `go fmt`

Types

type CLITable added in v1.5.0

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

func NewCLITable added in v1.5.0

func NewCLITable(colNames []string) *CLITable

func (*CLITable) AddRow added in v1.5.0

func (c *CLITable) AddRow(row []string)

func (*CLITable) Col added in v1.5.0

func (c *CLITable) Col(val string)

func (*CLITable) Colf added in v1.5.0

func (c *CLITable) Colf(template string, args ...interface{})

func (*CLITable) NewRow added in v1.5.0

func (c *CLITable) NewRow() []string

func (*CLITable) Row added in v1.5.0

func (c *CLITable) Row()

func (*CLITable) String added in v1.5.0

func (c *CLITable) String() string

type ChangeFile

type ChangeFile struct {
	ID          int64
	DateCreated int64
	Hash        string
	Name        string
	DVCSetID    int64
	IsRun       bool
	IsDeleted   bool
	Content     string
	FullPath    string
	Ordinal     int
}

ChangeFile represents both a physical file on the local file system along with the entry in the changefile database

type Changeset

type Changeset struct {
	ChangeFiles map[string]ChangeFile
	Signature   string
}

Changeset represents all of the changes in an environment and their changes

type Column

type Column struct {
	Name         string `json:"column"`
	Position     int    `json:"position"`
	Default      string `json:"default"`
	IsNullable   bool   `json:"isNullable"`
	IsUnsigned   bool   `json:"isUnsigned"`
	DataType     string `json:"dataType"`
	MaxLength    int    `json:"maxLength"`
	Precision    int    `json:"precision"`
	CharSet      string `json:"charSet"`
	Type         string `json:"type"`
	ColumnKey    string `json:"columnKey"`
	NumericScale int    `json:"numericScale"`
	Extra        string `json:"extra"`
	FmtType      string `json:"fmtType"`
	GoType       string `json:"goType"`
}

Column represents a column in a table

type ColumnWithTable added in v1.5.0

type ColumnWithTable struct {
	*Column
	TableName string
}

ColumnWithTable is a column with the table name included

type Command

type Command struct {
	Options Options
}

Command is the command line functionality

type Config

type Config struct {
	ChangeSetPath string            `toml:"changesetPath"`
	DatabaseType  string            `toml:"databaseType"`
	BasePackage   string            `toml:"basePackage"`
	Enums         []string          `toml:"enums"`
	OneToMany     map[string]string `toml:"onetomany"`
	OneToOne      map[string]string `toml:"onetoone"`
	ManyToOne     map[string]string `toml:"manytoone"`
	Connection    struct {
		Host         string `toml:"host"`
		DatabaseName string `toml:"databaseName"`
		Username     string `toml:"username"`
		Password     string `toml:"password"`
	}
	Packages struct {
		Cache    string `toml:"cache"`
		Models   string `toml:"models"`
		Schema   string `toml:"schema"`
		Repos    string `toml:"repos"`
		Services string `toml:"services"`
		API      string `toml:"api"`
	}

	Dirs struct {
		Dal         string `toml:"dal"`
		Repos       string `toml:"repos"`
		Cache       string `toml:"cache"`
		Models      string `toml:"models"`
		Schema      string `toml:"schema"`
		Typescript  string `toml:"typescript"`
		Services    string `toml:"services"`
		Definitions string `toml:"definitions"`
		API         string `toml:"api"`
	}
}

Config contains a set of configuration values used throughout the application

type Database

type Database struct {
	RunID         int64
	Name          string
	Host          string `json:"-"`
	SortedSetKeys []string
	Tables        map[string]*Table
	Enums         map[string][]map[string]interface{}
}

Database represents a database

func ReadSchemaFromFile

func ReadSchemaFromFile(filePath string) (database *Database, e error)

ReadSchemaFromFile Unmarshal's database json to a Database object

func (*Database) FindTableByName

func (d *Database) FindTableByName(tableName string) (table *Table, e error)

FindTableByName finds a table by its name in the database

func (*Database) ToSortedTables added in v1.5.0

func (d *Database) ToSortedTables() SortedTables

ToSortedTables returns SortedTables

type DatabaseType added in v1.1.1

type DatabaseType string

DatabaseType is the type of database to be used.

const (
	// DatabaseTypeMysql is the MySQL flavor of database
	DatabaseTypeMysql DatabaseType = "mysql"
	// DatabaseTypeSqlite is the Sqlite flavor of database
	DatabaseTypeSqlite DatabaseType = "sqlite"
)

type Executor added in v1.5.0

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

Executor executes commands

func NewExecutor added in v1.5.0

func NewExecutor(config *Config, connector IConnector) *Executor

NewExecutor returns a new Executor

func (*Executor) Connect added in v1.5.0

func (x *Executor) Connect() (server *Server)

Connect connects

func (*Executor) RunSQL added in v1.5.0

func (x *Executor) RunSQL(sql string) (e error)

RunSQL runs the sql produced by the `CompareSchema` command against the target database @command compare [reverse] apply

type Files

type Files struct {
}

Files contains functionality pertaining to files

func (*Files) BuildChangeFile

func (f *Files) BuildChangeFile(sqlPath string, ordinal int) (changeFile *ChangeFile, e error)

BuildChangeFile builds a changeFile object based on a path and an ordinal

func (*Files) BuildChangeFiles

func (f *Files) BuildChangeFiles(sqlPaths []string) (changeFiles []ChangeFile, e error)

BuildChangeFiles returns a collection of ChangeFile objects based on a collection of sql paths

func (*Files) FetchLocalChangesetList

func (f *Files) FetchLocalChangesetList(rootPath string) (sqlPaths []string, e error)

FetchLocalChangesetList gets the contents of the changesets.json file and returns a collection of paths ([]string)

func (*Files) ScanDir

func (f *Files) ScanDir(rootPath string, extension string) (paths []string, e error)

ScanDir scans the changeset directory for any file with an extension

type GoFileImports added in v1.5.0

type GoFileImports []string

GoFileImports is a collection of imports for a model file

func (*GoFileImports) Append added in v1.5.0

func (i *GoFileImports) Append(m string)

Append adds to the collection of imports

func (*GoFileImports) Get added in v1.5.0

func (i *GoFileImports) Get(idx int) string

Get returns an import at an index

func (*GoFileImports) Len added in v1.5.0

func (i *GoFileImports) Len() int

Len returns the length of the collection of imports

func (*GoFileImports) ToString added in v1.5.0

func (i *GoFileImports) ToString() string

ToString returns a string representation of imports on a go file

type GoStruct added in v1.5.0

type GoStruct struct {
	Package  string
	Name     string
	Fields   *GoStructFields
	Methods  *GoStructMethods
	Comments string
	Imports  *GoFileImports
}

GoStruct represents a model struct

func NewGoStruct added in v1.5.0

func NewGoStruct() *GoStruct

NewGoStruct returns a new GoStruct

type GoStructField added in v1.5.0

type GoStructField struct {
	Name     string
	DataType string
	Tags     []*GoStructFieldTag
	Comments string
}

GoStructField is a field on a Model struct

func (*GoStructField) ToString added in v1.5.0

func (m *GoStructField) ToString() string

ToString returns a tring representation of a go struct field

type GoStructFieldTag added in v1.5.0

type GoStructFieldTag struct {
	Name    string
	Value   string
	Options []string
}

GoStructFieldTag is a tag on a field on a model struct

func (*GoStructFieldTag) ToString added in v1.5.0

func (t *GoStructFieldTag) ToString() string

ToString returns a string representation of a go struct field tag

type GoStructFields added in v1.5.0

type GoStructFields []*GoStructField

GoStructFields is a collection of a go struct fields

func (*GoStructFields) Append added in v1.5.0

func (i *GoStructFields) Append(m *GoStructField)

Append adds to the collection of imports

func (*GoStructFields) Get added in v1.5.0

func (i *GoStructFields) Get(idx int) *GoStructField

Get returns an import at an index

func (*GoStructFields) Len added in v1.5.0

func (i *GoStructFields) Len() int

Len returns the length of the collection of imports

type GoStructMethod added in v1.5.0

type GoStructMethod struct {
	Code string
	Docs []string
}

GoStructMethod is a method on a struct

type GoStructMethods added in v1.5.0

type GoStructMethods []*GoStructMethod

GoStructMethods is a collection of go methods on a struct

func (*GoStructMethods) Append added in v1.5.0

func (g *GoStructMethods) Append(m *GoStructMethod)

Append adds a new struct to the struct collection

func (*GoStructMethods) Get added in v1.5.0

func (g *GoStructMethods) Get(index int) (m *GoStructMethod)

Get gets a struct at index

func (*GoStructMethods) Len added in v1.5.0

func (g *GoStructMethods) Len() int

Len returns the number of methods

type IConnector

type IConnector interface {
	Connect() (server *Server, e error)
	FetchDatabases(server *Server) (databases map[string]*Database, e error)
	FetchEnums(server *Server) (enums map[string][]map[string]interface{})
	FetchEnum(server *Server, tableName string) []map[string]interface{}
	UseDatabase(server *Server, databaseName string) (e error)
	FetchDatabaseTables(server *Server, databaseName string) (tables map[string]*Table, e error)
	FetchTableColumns(server *Server, databaseName string, tableName string) (columns map[string]*Column, e error)
	CreateChangeSQL(localSchema *Database, remoteSchema *Database) (sql string, e error)
	CompareEnums(remoteSchema *Database, localSchema *Database, tableName string) (sql string)
}

IConnector defines the shape of a connector to a database

type LogLevel

type LogLevel uint

LogLevel is a custom type for passing the level of logging specified by runtime flags

const (

	// LogLevelAlways indicates that a log should always be printed regardless of flags
	LogLevelAlways LogLevel = 0

	// LogLevelError indicates that a log should
	LogLevelError LogLevel = 1

	// LogLevelWarning indicates a potential problem in the system.
	LogLevelWarning LogLevel = 2

	// LogLevelInfo indicates that a log should be shown if the OptVerbose flag is passed
	LogLevelInfo LogLevel = 3

	// LogLevelDebug indicates that a log should be written if the OptVerboseDebug flag is passed
	LogLevelDebug LogLevel = 4
)

type Method added in v1.5.0

type Method struct {
	Code      string
	Documents []string
}

Method describes code and Documents for a method

func ParseStruct added in v1.5.0

func ParseStruct(src []byte, structName string, copyDocuments bool, copyTypeDocuments bool, pkgName string) (methods []Method, imports []string, typeDoc string)

ParseStruct parses a struct

func (*Method) Lines added in v1.5.0

func (m *Method) Lines() []string

Lines return a a slice of documentation and code

type Options

type Options uint

Options are the available runtime flags

type Query

type Query struct{}

Query contains functionality for generating sql queries

func (*Query) AddIndex

func (q *Query) AddIndex(table *Table, column *Column) (sql string, e error)

AddIndex returns an alter table sql statement that adds an index to a table

func (*Query) AddUniqueIndex

func (q *Query) AddUniqueIndex(table *Table, column *Column) (sql string, e error)

AddUniqueIndex returns an alter table sql statement that adds a unique index to a table

func (*Query) AlterTableCreateColumn

func (q *Query) AlterTableCreateColumn(table *Table, column *Column) (sql string, e error)

AlterTableCreateColumn returns an alter table sql statement that adds a column

func (*Query) AlterTableDropColumn

func (q *Query) AlterTableDropColumn(table *Table, column *Column) (sql string, e error)

AlterTableDropColumn returns an alter table sql statement that drops a column

func (*Query) ChangeColumn

func (q *Query) ChangeColumn(table *Table, localColumn *Column, remoteColumn *Column) (sql string, e error)

ChangeColumn returns an alter table sql statement that adds or removes an index from a column if and only if the one (e.g. local) has a column and the other (e.g. remote) does not Truth table

Remote 	| 	Local 	| 	Result

--------------------------------------------------------- 1. MUL | none | Drop index 2. UNI | none | Drop unique index 3. none | MUL | Create index 4. none | UNI | Create unique index 5. MUL | UNI | Drop index; Create unique index 6. UNI | MUL | Drop unique index; Create index 7. none | none | Do nothing 8. MUL | MUL | Do nothing 9. UNI | UNI | Do nothing

func (*Query) CreateChangeSQL

func (q *Query) CreateChangeSQL(localSchema *Database, remoteSchema *Database) (sql string, e error)

CreateChangeSQL generates sql statements based off of comparing two database objects localSchema is authority, remoteSchema will be upgraded to match localSchema

func (*Query) CreateColumn

func (q *Query) CreateColumn(column *Column) (sql string, e error)

CreateColumn returns a table column sql segment

func (*Query) CreateTable

func (q *Query) CreateTable(table *Table) (sql string, e error)

CreateTable returns a create table sql statement

func (*Query) CreateTableChangeSQL

func (q *Query) CreateTableChangeSQL(localTable *Table, remoteTable *Table) (sql string, e error)

CreateTableChangeSQL returns a set of statements that alter a table's structure if and only if there is a difference between the local and remote tables If no change is found, an empty string is returned.

func (*Query) DropIndex

func (q *Query) DropIndex(table *Table, column *Column) (sql string, e error)

DropIndex returns an alter table sql statement that drops an index

func (*Query) DropTable

func (q *Query) DropTable(table *Table) (sql string, e error)

DropTable returns a drop table sql statement

func (*Query) DropUniqueIndex

func (q *Query) DropUniqueIndex(table *Table, column *Column) (sql string, e error)

DropUniqueIndex returns an alter table sql statement that drops a unique index

type Server

type Server struct {
	Name            string `json:"name"`
	Host            string `json:"host"`
	Databases       map[string]*Database
	Connection      *sql.DB
	CurrentDatabase string
}

Server represents a server

func (*Server) DatabaseExists

func (s *Server) DatabaseExists(databaseName string) bool

DatabaseExists checks if the database `databaseName` exists in its list of databases

type SortedColumns

type SortedColumns []*Column

SortedColumns is a slice of Column objects

func (SortedColumns) Len

func (c SortedColumns) Len() int

Len is part of sort.Interface.

func (SortedColumns) Less

func (c SortedColumns) Less(i, j int) bool

Less is part of sort.Interface. We use count as the value to sort by

func (SortedColumns) Swap

func (c SortedColumns) Swap(i, j int)

Swap is part of sort.Interface.

type SortedTables added in v1.5.0

type SortedTables []*Table

SortedTables is a slice of Table objects

func (SortedTables) Len added in v1.5.0

func (c SortedTables) Len() int

Len is part of sort.Interface.

func (SortedTables) Less added in v1.5.0

func (c SortedTables) Less(i, j int) bool

Less is part of sort.Interface. We use count as the value to sort by

func (SortedTables) Swap added in v1.5.0

func (c SortedTables) Swap(i, j int)

Swap is part of sort.Interface.

type Table

type Table struct {
	Name          string             `json:"name"`
	Engine        string             `json:"engine"`
	Version       int                `json:"version"`
	RowFormat     string             `json:"rowFormat"`
	Rows          int64              `json:"-"`
	DataLength    int64              `json:"-"`
	Collation     string             `json:"collation"`
	AutoIncrement int64              `json:"-"`
	Columns       map[string]*Column `json:"columns"`
}

Table represents a table in a database

func (*Table) ToSortedColumns added in v1.5.0

func (table *Table) ToSortedColumns() SortedColumns

ToSortedColumns returns SortedColumns

Jump to

Keyboard shortcuts

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