Skip to content

Commit

Permalink
Add create table (#26)
Browse files Browse the repository at this point in the history
* Add CreateTable in postgres deparser

* Add tests: Postgres Deparser CreateTable
  • Loading branch information
davimnz authored May 23, 2022
1 parent b4a51c0 commit 4a7c1d2
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 5 deletions.
60 changes: 58 additions & 2 deletions drivers/postgres/deparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,71 @@ package postgres

import (
"fmt"
"sort"

"github.com/kenji-yamane/mgr8/domain"
)

type deparser struct { }

func inStringList(stringList []string, needle string) bool {
isIn := false
for _, s := range stringList {
if needle == s {
isIn = true
}
}
return isIn
}

func hasSingleArg(datatype string) bool {
singleArgTypes := []string{"char", "varchar", "bit", "varbit", "time", "timestamp"}
if inStringList(singleArgTypes, datatype) {
return true
} else {
return false
}
}

func hasDoubleArg(datatype string) bool {
doubleArgTypes := []string{"decimal", "numeric"}
if inStringList(doubleArgTypes, datatype) {
return true
} else {
return false
}
}

func (d *deparser) CreateTable(table *domain.Table) string {
// TODO: how to mount this string?
return ""
statement := fmt.Sprintf("CREATE TABLE %s (\n", table.Name)

columnKeys := []string{}
for columnName, _ := range table.Columns {
columnKeys = append(columnKeys, columnName)
}
sort.Strings(columnKeys)

for _, key := range columnKeys {
column := table.Columns[key]

statement = statement + fmt.Sprintf("%s %s", key, column.Datatype)

if hasSingleArg(column.Datatype) {
statement = statement + fmt.Sprintf("(%d)", column.Parameters["size"])
} else if hasDoubleArg(column.Datatype) {
statement = statement + fmt.Sprintf("(%d,%d)", column.Parameters["precision"], column.Parameters["scale"])
}

if column.IsNotNull {
statement = statement + fmt.Sprintf(" NOT NULL")
}

statement = statement + fmt.Sprintf(",\n")
}

statement = statement[0:len(statement) - 2]
statement = statement + fmt.Sprintf("\n)")
return statement
}

func (d *deparser) DropTable(tableName string) string {
Expand Down
59 changes: 56 additions & 3 deletions drivers/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
)

var _ = Describe("Postgres Driver", func() {
var (
subject *postgresDriver
)

Context("Parse Migration", func() {
var (
subject *postgresDriver
)

BeforeEach(func() {
subject = NewPostgresDriver()
})
Expand Down Expand Up @@ -55,4 +56,56 @@ var _ = Describe("Postgres Driver", func() {
})
})
})

Context("Deparse Migration", func() {
var (
subject deparser
)

BeforeEach(func() {
subject = deparser{}
})

When("Table has 1 as maximum argument in data type", func() {
It("Generate CREATE TABLE statement", func() {
table := &domain.Table{
Name: "users",
Columns: map[string]*domain.Column{
"social_number": {Datatype: "varchar", IsNotNull: false, Parameters: map[string]interface{}{"size": int32(9)}},
"favorite_number": {Datatype: "bit", IsNotNull: false, Parameters: map[string]interface{}{"size": int32(6)}},
"size": {Datatype: "int4", IsNotNull: false, Parameters: map[string]interface{}{}},
},
}

statement := subject.CreateTable(table)
answer := `CREATE TABLE users (
favorite_number bit(6),
size int4,
social_number varchar(9)
)`
Expect(statement).To(Equal(answer))
})
})

When("Table has 2 as maximum argument in data type", func() {
It("Generate CREATE TABLE statement", func() {
table := &domain.Table{
Name: "users",
Columns: map[string]*domain.Column{
"area": {Datatype: "decimal", IsNotNull: false, Parameters: map[string]interface{}{"precision": int32(9), "scale": int32(1)}},
"perimeter": {Datatype: "numeric", IsNotNull: false, Parameters: map[string]interface{}{"precision": int32(6), "scale": int32(4)}},
"name": {Datatype: "char", IsNotNull: false, Parameters: map[string]interface{}{"size": int32(10)}},
},
}

statement := subject.CreateTable(table)
answer := `CREATE TABLE users (
area decimal(9,1),
name char(10),
perimeter numeric(6,4)
)`
Expect(statement).To(Equal(answer))
})
})
})
})

0 comments on commit 4a7c1d2

Please sign in to comment.