From b1f617522629be91bc9f594e7e97d91a1f525f52 Mon Sep 17 00:00:00 2001 From: Shane C Date: Sun, 3 Nov 2024 15:46:46 -0500 Subject: [PATCH] add db migrations, add taskfile --- Taskfile.yml | 35 ++++++++++++++++++++ cmd/migration.go | 75 +++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ migrations/main.go | 5 +++ web/assets/js/main.ts | 9 ++++++ 6 files changed, 127 insertions(+) create mode 100644 Taskfile.yml create mode 100644 cmd/migration.go create mode 100644 migrations/main.go create mode 100644 web/assets/js/main.ts diff --git a/Taskfile.yml b/Taskfile.yml new file mode 100644 index 0000000..7da12a0 --- /dev/null +++ b/Taskfile.yml @@ -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' . \ No newline at end of file diff --git a/cmd/migration.go b/cmd/migration.go new file mode 100644 index 0000000..ed25d9c --- /dev/null +++ b/cmd/migration.go @@ -0,0 +1,75 @@ +//go:build dev + +/* +Package cmd +Copyright © 2024 Shane C. +*/ +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 +} diff --git a/go.mod b/go.mod index 6b91ef0..2397b92 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index a03ceaf..9f05962 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/migrations/main.go b/migrations/main.go new file mode 100644 index 0000000..d23f889 --- /dev/null +++ b/migrations/main.go @@ -0,0 +1,5 @@ +package migrations + +import "github.com/uptrace/bun/migrate" + +var Migrations = migrate.NewMigrations() diff --git a/web/assets/js/main.ts b/web/assets/js/main.ts new file mode 100644 index 0000000..1afe174 --- /dev/null +++ b/web/assets/js/main.ts @@ -0,0 +1,9 @@ +import Alpine from 'alpinejs'; + +declare global { + interface Window { Alpine: typeof Alpine; } +} + +window.Alpine = Alpine; + +window.Alpine.start(); \ No newline at end of file