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"
)
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 {
return MigrationGraph{Migrations: map[string]Migration{}, parentage: map[string]*Migration{}}
}

View file

@ -17,21 +17,6 @@ import (
"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 requirementPrefix = []byte("requires:")
@ -143,14 +128,6 @@ func getMigrations(migrationsRoot string) ([]Migration, error) {
return migrationGraph.GetLinearHistory()
}
type DatabaseConfiguration struct {
Host string
User string
Password string
DatabaseName string
Port int
}
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)
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)
}