Skip to content
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
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# Ignore all files in the root directory
/*

# Do not ignore directories in the root directory
!/*/

# Do not ignore files with extensions in the root directory
!/*.*

!/Makefile
.env
*.md

10 changes: 10 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: build run test

build:
go build -o valkyrie

run:
go build -o valkyrie && ./valkyrie

test:
go test -v ./...
41 changes: 41 additions & 0 deletions migration/dialect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package migration

import (
"strings"
"valkyrie/schema"
)

type Dialect interface {
GetSQLType(sf *schema.ScalarField) string
GetSQLDefault(dv *schema.DefaultValue, pslType string) string
QuoteIdent(name string) string
}

func GetDialect(provider schema.DbProvider) Dialect {
switch provider {
case schema.Mysql:
return nil //TODO
case schema.Postgres, schema.Postgresql:
return &PostgresDialect{}
case schema.Sqlite:
return &SqliteDialect{}
default:
return nil
}
}

func getSQLType(sf *schema.ScalarField, provider schema.DbProvider) string {
dialect := GetDialect(provider)
if dialect == nil {
return strings.ToUpper(sf.SQLType)
}
return dialect.GetSQLType(sf)
}

func getSQLDefault(dv *schema.DefaultValue, pslType string, provider schema.DbProvider) string {
dialect := GetDialect(provider)
if dialect == nil {
return ""
}
return dialect.GetSQLDefault(dv, pslType)
}
56 changes: 56 additions & 0 deletions migration/postgresDialect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package migration

import (
"fmt"
"strings"
"valkyrie/schema"
)

type PostgresDialect struct{}

func (d PostgresDialect) QuoteIdent(name string) string {
return `"` + name + `"`
}

func (d PostgresDialect) GetSQLType(sf *schema.ScalarField) string {
// If its a custom PG enum, return the quoted enum name
if sf.EnumRef != nil {
enumName := sf.EnumRef.Name
if sf.EnumRef.TableMapName != "" {
enumName = sf.EnumRef.TableMapName
}
return d.QuoteIdent(enumName)
}
return strings.ToUpper(sf.SQLType)
}

func (d PostgresDialect) GetSQLDefault(dv *schema.DefaultValue, pslType string) string {
switch dv.Kind {
case schema.DefaultLiteral:
if pslType == schema.TypeBoolean {
return strings.ToUpper(dv.Literal)
}
if pslType == schema.TypeInt || pslType == schema.TypeBigInt || pslType == schema.TypeFloat || pslType == schema.TypeDecimal {
return dv.Literal
}
return fmt.Sprintf("'%s'", strings.ReplaceAll(dv.Literal, "'", "''"))

case schema.DefaultEnumValue:
return fmt.Sprintf("'%s'", strings.ReplaceAll(dv.EnumValue, "'", "''"))

case schema.DefaultFunc:
switch dv.FuncName {
case "now":
return "CURRENT_TIMESTAMP"
case "uuid":
return "gen_random_uuid()"
case "cuid":
return ""
}

case schema.DefaultDBGenerated:
return dv.DBExpression
}

return ""
}
60 changes: 60 additions & 0 deletions migration/sqliteDialect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package migration

import (
"fmt"
"strings"
"valkyrie/schema"
)

type SqliteDialect struct{}

func (SqliteDialect) QuoteIdent(name string) string {
return `"` + name + `"`
}

func (SqliteDialect) GetSQLType(sf *schema.ScalarField) string {
sqlType := strings.ToUpper(sf.SQLType)
switch sqlType {
case "VARCHAR", "TEXT", "UUID", "TIMESTAMP":
return "TEXT"
case "INTEGER", "BIGINT", "BOOLEAN":
return "INTEGER"
case "DOUBLE PRECISION", "NUMERIC":
return "REAL"
case "JSONB", "BYTEA":
return "BLOB"
default:
return "TEXT"
}
}

func (SqliteDialect) GetSQLDefault(dv *schema.DefaultValue, pslType string) string {
switch dv.Kind {
case schema.DefaultLiteral:
if pslType == schema.TypeBoolean {
return strings.ToUpper(dv.Literal)
}
if pslType == schema.TypeInt || pslType == schema.TypeBigInt || pslType == schema.TypeFloat || pslType == schema.TypeDecimal {
return dv.Literal
}
return fmt.Sprintf("'%s'", strings.ReplaceAll(dv.Literal, "'", "''"))

case schema.DefaultEnumValue:
return fmt.Sprintf("'%s'", strings.ReplaceAll(dv.EnumValue, "'", "''"))

case schema.DefaultFunc:
switch dv.FuncName {
case "now":
return "CURRENT_TIMESTAMP"
case "uuid":
return ""
case "cuid":
return ""
}

case schema.DefaultDBGenerated:
return dv.DBExpression
}

return ""
}
Loading
Loading