refactor: move structs to models

This commit is contained in:
Marc 2024-07-10 23:11:14 -04:00
parent 9c7c12b76d
commit be782fb9c2
Signed by: marc
GPG key ID: 048E042F22B5DC79
3 changed files with 37 additions and 32 deletions

View file

@ -5,15 +5,6 @@ import (
"fmt" "fmt"
) )
type MigrationGraph struct {
// Reference to the root of the graph.
Root *Migration
// Name to struct mapping of all migrations part of the graph.
Migrations map[string]Migration
// Mapping of all migrations to their parent, if any.
parentage map[string]*Migration
}
func NewMigrationGraph() MigrationGraph { func NewMigrationGraph() MigrationGraph {
return MigrationGraph{Migrations: map[string]Migration{}, parentage: map[string]*Migration{}} return MigrationGraph{Migrations: map[string]Migration{}, parentage: map[string]*Migration{}}
} }

View file

@ -17,21 +17,6 @@ import (
"strconv" "strconv"
) )
type MigrationHeaders struct {
Requirements []string
}
type Migration struct {
Path string
Name string
Requires string
Run bool
}
type DB interface {
Exec(sql string, args ...any) (sql.Result, error)
}
var commentPrefix = []byte("--") var commentPrefix = []byte("--")
var requirementPrefix = []byte("requires:") var requirementPrefix = []byte("requires:")
@ -143,14 +128,6 @@ func getMigrations(migrationsRoot string) ([]Migration, error) {
return migrationGraph.GetLinearHistory() return migrationGraph.GetLinearHistory()
} }
type DatabaseConfiguration struct {
Host string
User string
Password string
DatabaseName string
Port int
}
func ConnectToDatabase(config DatabaseConfiguration) (*sql.DB, error) { func ConnectToDatabase(config DatabaseConfiguration) (*sql.DB, error) {
connectionString := fmt.Sprintf("postgresql://%s:%s@%s:%d?sslmode=disable", config.User, config.Password, config.Host, config.Port) connectionString := fmt.Sprintf("postgresql://%s:%s@%s:%d?sslmode=disable", config.User, config.Password, config.Host, config.Port)
return sql.Open(config.DatabaseName, connectionString) return sql.Open(config.DatabaseName, connectionString)

37
models.go Normal file
View file

@ -0,0 +1,37 @@
package main
import (
"database/sql"
)
type MigrationHeaders struct {
Requirements []string
}
type Migration struct {
Path string
Name string
Requires string
Run bool
}
type DatabaseConfiguration struct {
Host string
User string
Password string
DatabaseName string
Port int
}
type MigrationGraph struct {
// Reference to the root of the graph.
Root *Migration
// Name to struct mapping of all migrations part of the graph.
Migrations map[string]Migration
// Mapping of all migrations to their parent, if any.
parentage map[string]*Migration
}
type DB interface {
Exec(sql string, args ...any) (sql.Result, error)
}