Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test setup #5

Merged
merged 13 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/release_build.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: goreleaser

on:
push:
tags:
- "*"

jobs:
goreleaser:
runs-on: ubuntu-latest
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: test
on: [ push ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Start container with test databases
run: docker-compose -f test/docker-compose.yaml up -d
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Install tparse for pretty test results
run: go install github.com/mfridman/tparse@latest
- name: Run test
run: make test-all
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
result.mmd
mermerd
coverage.html
4 changes: 2 additions & 2 deletions .mermerd.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ outputFileName: "my-db.mmd"

# These connection strings are available as suggestions in the cli (use tab to access)
connectionStringSuggestions:
- postgresql://user:password@localhost:5432/yourDb
- mysql://root:password@tcp(127.0.0.1:3306)/yourDb
- postgresql://user:password@localhost:5432/mermerd_test
- mysql://root:password@tcp(127.0.0.1:3306)/mermerd_test
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_target := "./..."

.PHONY: test-coverage
test-coverage:
go test -cover -coverprofile=coverage.out ./...; go tool cover -html=coverage.out -o coverage.html; rm coverage.out

# https://github.com/vektra/mockery is needed
.PHONY: create-mocks
create-mocks:
mockery --all

# https://github.com/mfridman/tparse is needed
.PHONY: test-all
test-all:
go test $(test_target) -cover -json | tparse -all

# https://github.com/mfridman/tparse is needed
.PHONY: test-unit
test-unit:
go test --short $(test_target) -cover -json | tparse -all

137 changes: 87 additions & 50 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,121 @@ package analyzer

import (
"errors"
"github.com/AlecAivazis/survey/v2"
"mermerd/config"
"mermerd/database"
"mermerd/util"
)

func Analyze() (*database.Result, error) {
loading, err := util.NewLoadingSpinner()
type analyzer struct {
loadingSpinner util.LoadingSpinner
config config.MermerdConfig
connectorFactory database.ConnectorFactory
questioner Questioner
}

type Analyzer interface {
Analyze() (*database.Result, error)

GetConnectionString() (string, error)
GetSchema(db database.Connector) (string, error)
GetTables(db database.Connector, selectedSchema string) ([]string, error)
GetColumnsAndConstraints(db database.Connector, selectedTables []string) ([]database.TableResult, error)
}

func NewAnalyzer(config config.MermerdConfig, connectorFactory database.ConnectorFactory, questioner Questioner) Analyzer {
loadingSpinner := util.NewLoadingSpinner()
return analyzer{loadingSpinner, config, connectorFactory, questioner}
}

func (a analyzer) Analyze() (*database.Result, error) {
connectionString, err := a.GetConnectionString()
if err != nil {
return nil, err
}

connectionString := config.ConnectionString()
if config.ConnectionString() == "" {
err = survey.AskOne(ConnectionQuestion(), &connectionString, survey.WithValidator(survey.Required))
if err != nil {
return nil, err
}
db, err := a.connectorFactory.NewConnector(connectionString)
if err != nil {
return nil, err
}

loading.Start("Connecting to database and getting schemas")
db, err := database.NewConnector(connectionString)
a.loadingSpinner.Start("Connecting to database")
if err = db.Connect(); err != nil {
return nil, err
}
defer db.Close()
a.loadingSpinner.Stop()

selectedSchema, err := a.GetSchema(db)
if err != nil {
return nil, err
}

err = db.Connect()
selectedTables, err := a.GetTables(db, selectedSchema)
if err != nil {
return nil, err
}
defer db.Close()

selectedSchema := config.Schema()
if selectedSchema == "" {
schemas, err := db.GetSchemas()
if err != nil {
return nil, err
}
loading.Stop()

switch len(schemas) {
case 0:
return nil, errors.New("no schemas available")
case 1:
selectedSchema = schemas[0]
break
default:
err = survey.AskOne(SchemaQuestion(schemas), &selectedSchema)
if err != nil {
return nil, err
}
}
tableResults, err := a.GetColumnsAndConstraints(db, selectedTables)
if err != nil {
return nil, err
}

// get tables
var selectedTables []string
loading.Start("Getting tables")
return &database.Result{Tables: tableResults}, nil
}

func (a analyzer) GetConnectionString() (string, error) {
if connectionString := a.config.ConnectionString(); connectionString != "" {
return connectionString, nil
}

return a.questioner.AskConnectionQuestion(a.config.ConnectionStringSuggestions())
}

func (a analyzer) GetSchema(db database.Connector) (string, error) {
if selectedSchema := a.config.Schema(); selectedSchema != "" {
return selectedSchema, nil
}

a.loadingSpinner.Start("Getting schemas")
schemas, err := db.GetSchemas()
a.loadingSpinner.Stop()

if err != nil {
return "", err
}

switch len(schemas) {
case 0:
return "", errors.New("no schemas available")
case 1:
return schemas[0], nil
default:
return a.questioner.AskSchemaQuestion(schemas)
}
}

func (a analyzer) GetTables(db database.Connector, selectedSchema string) ([]string, error) {
if selectedTables := a.config.SelectedTables(); len(selectedTables) > 0 {
return selectedTables, nil
}

a.loadingSpinner.Start("Getting tables")
tables, err := db.GetTables(selectedSchema)
if err != nil {
return nil, err
}
loading.Stop()
a.loadingSpinner.Stop()

if config.UseAllTables() {
selectedTables = tables
} else if len(config.SelectedTables()) > 0 {
selectedTables = config.SelectedTables()
if a.config.UseAllTables() {
return tables, nil
} else {
err = survey.AskOne(TableQuestion(tables), &selectedTables, survey.WithValidator(survey.MinItems(1)))
if err != nil {
return nil, err
}
return a.questioner.AskTableQuestion(tables)
}
}

// get columns and constraints
func (a analyzer) GetColumnsAndConstraints(db database.Connector, selectedTables []string) ([]database.TableResult, error) {
var tableResults []database.TableResult
loading.Start("Getting columns and constraints")
a.loadingSpinner.Start("Getting columns and constraints")
for _, table := range selectedTables {
columns, err := db.GetColumns(table)
if err != nil {
Expand All @@ -92,7 +130,6 @@ func Analyze() (*database.Result, error) {

tableResults = append(tableResults, database.TableResult{TableName: table, Columns: columns, Constraints: constraints})
}
loading.Stop()

return &database.Result{Tables: tableResults}, nil
a.loadingSpinner.Stop()
return tableResults, nil
}
Loading