add db migrations, add taskfile

This commit is contained in:
Shane C. 2024-11-03 15:46:46 -05:00
parent 33235d8f1e
commit b1f6175226
Signed by: Shane C.
GPG key ID: E46B5FEA35B22FF9
6 changed files with 127 additions and 0 deletions

35
Taskfile.yml Normal file
View file

@ -0,0 +1,35 @@
version: 3
tasks:
run:
cmds:
- air --build.include_ext "go,css,ts,templ"
tool-install:
desc: Install needed CLI tools
cmds:
- go install github.com/a-h/templ/cmd/templ@latest
- go install github.com/spf13/cobra-cli@latest
- go install github.com/air-verse/air@latest
- go get
- bun install
tool-upgrade:
desc: Upgrade CLI tools
cmds:
- go install github.com/a-h/templ/cmd/templ@latest
- go install github.com/spf13/cobra-cli@latest
- go install github.com/air-verse/air@latest
make-handler:
desc: Generates a handler
cmds:
- go run -tags dev *.go generate handler
make-migration:
desc: Generates a migration
cmds:
- go run -tags dev *.go generate migration
cloc:
desc: Lines of Code of project (for fun)
cmds:
- cloc --exclude-dir=node_modules,dist,playwright-report,.git,build,.idea,.vscode --exclude-ext=md --not-match-f='.*_templ\.go' .

75
cmd/migration.go Normal file
View file

@ -0,0 +1,75 @@
//go:build dev
/*
Package cmd
Copyright © 2024 Shane C. <shane@scaffoe.com>
*/
package cmd
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/spf13/cobra"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/migrate"
"gitlab.com/omnibill/tui/textinput"
"log"
"omnibill.net/omnibill/migrations"
"omnibill.net/omnibill/shared"
"strings"
"unicode"
)
// migrationCmd represents the migration command
var migrationCmd = &cobra.Command{
Use: "migration",
Short: "Generates a migration",
Run: func(cmd *cobra.Command, args []string) {
migrationName, err := textinput.New(textinput.InputData{
Question: "Name of Migration?",
})
if err != nil {
log.Fatalln(err)
}
dsn := shared.GetPostgresURI()
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
log.Fatalln(err)
}
pool, err := pgxpool.NewWithConfig(context.Background(), config)
if err != nil {
log.Fatalln(err)
}
sqldb := stdlib.OpenDBFromPool(pool)
db := bun.NewDB(sqldb, pgdialect.New())
migrator := migrate.NewMigrator(db, migrations.Migrations)
name := strings.ToLower(strings.ReplaceAll(removeSymbols(*migrationName), " ", "_"))
_, err = migrator.CreateGoMigration(context.Background(), name)
if err != nil {
log.Fatalln(err)
}
},
}
func init() {
generateCmd.AddCommand(migrationCmd)
}
func removeSymbols(s string) string {
var result string
for _, char := range s {
if unicode.IsLetter(char) || unicode.IsDigit(char) {
result += string(char)
}
}
return result
}

1
go.mod
View file

@ -53,6 +53,7 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/uptrace/bun/dialect/pgdialect v1.2.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.57.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect

2
go.sum
View file

@ -110,6 +110,8 @@ github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/uptrace/bun v1.2.5 h1:gSprL5xiBCp+tzcZHgENzJpXnmQwRM/A6s4HnBF85mc=
github.com/uptrace/bun v1.2.5/go.mod h1:vkQMS4NNs4VNZv92y53uBSHXRqYyJp4bGhMHgaNCQpY=
github.com/uptrace/bun/dialect/pgdialect v1.2.5 h1:dWLUxpjTdglzfBks2x+U2WIi+nRVjuh7Z3DLYVFswJk=
github.com/uptrace/bun/dialect/pgdialect v1.2.5/go.mod h1:stwnlE8/6x8cuQ2aXcZqwDK/d+6jxgO3iQewflJT6C4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.57.0 h1:Xw8SjWGEP/+wAAgyy5XTvgrWlOD1+TxbbvNADYCm1Tg=

5
migrations/main.go Normal file
View file

@ -0,0 +1,5 @@
package migrations
import "github.com/uptrace/bun/migrate"
var Migrations = migrate.NewMigrations()

9
web/assets/js/main.ts Normal file
View file

@ -0,0 +1,9 @@
import Alpine from 'alpinejs';
declare global {
interface Window { Alpine: typeof Alpine; }
}
window.Alpine = Alpine;
window.Alpine.start();