Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VNEXT] feat: Multi-DB type support #291

Merged
merged 37 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f86a05b
feat: Multi-DB type URL formats and config
tankerkiller125 Oct 19, 2024
be88954
fix: remove legacy sqlite path config and minor other things
tankerkiller125 Oct 19, 2024
85f2e9f
fix: dumb eslint issues
tankerkiller125 Oct 19, 2024
4915f0d
fix: dumb eslint issues
tankerkiller125 Oct 19, 2024
1e55e8a
fix: application can be tested with sqlite
tankerkiller125 Oct 19, 2024
c0e605a
fix: minor config formatting
tankerkiller125 Oct 19, 2024
2d6b05e
chore: some cleanup
tankerkiller125 Oct 19, 2024
a3e7946
feat: postgres migration creation now works
tankerkiller125 Oct 27, 2024
0d2c037
chore: revert some strings back to bytes as they should be
tankerkiller125 Oct 27, 2024
ce8e83e
feat: improve languages support
tonyaellie Oct 22, 2024
f349c17
feat: add locale time ago formatting and the local name for the langu…
tonyaellie Oct 23, 2024
0d69441
Update FUNDING.yml
tankerkiller125 Oct 26, 2024
1c600ee
chore: remove some more mysql stuff
tankerkiller125 Oct 27, 2024
785f790
fix: coderabbit security recommendations
tankerkiller125 Oct 27, 2024
e33652f
fix: validate postgres sslmode
tankerkiller125 Oct 27, 2024
4a8b51e
Update migrations.go
tankerkiller125 Oct 27, 2024
621770c
fix: postgres migration creation now works
tankerkiller125 Oct 27, 2024
016c4f6
fix: errors in raw sql queries
tankerkiller125 Oct 27, 2024
fff1501
fix: lint error, and simpler SQL query
tankerkiller125 Oct 27, 2024
33611c8
fix: migrations directory string
tankerkiller125 Oct 27, 2024
4c5b24f
Merge branch 'main' into mk/vnext-multi-db
tankerkiller125 Oct 27, 2024
23ff930
fix: stats related test
tankerkiller125 Oct 27, 2024
012f0cf
Merge remote-tracking branch 'origin/mk/vnext-multi-db' into mk/vnext…
tankerkiller125 Oct 27, 2024
5c71c98
fix: sql query
tankerkiller125 Oct 27, 2024
72d8431
Merge branch 'main' into mk/vnext-multi-db
tankerkiller125 Oct 28, 2024
620b254
Update TextArea.vue
tankerkiller125 Oct 28, 2024
a0f19ee
Update TextField.vue
tankerkiller125 Oct 28, 2024
74d1781
Merge branch 'vnext' into mk/vnext-multi-db
tankerkiller125 Oct 29, 2024
4853ad2
chore: run integration testing on multiple postgresql versions
tankerkiller125 Oct 29, 2024
ba5c82d
chore: jobs should run for vnext branch PRs
tankerkiller125 Oct 29, 2024
5033a9c
fix: missed $ for Postgres testing
tankerkiller125 Oct 30, 2024
2040fe9
fix: environment variable for db ssl mode
tankerkiller125 Oct 30, 2024
44e80dc
fix: lint issue from a merge
tankerkiller125 Oct 30, 2024
a9f4acf
chore: trying to fix postgresql testing
tankerkiller125 Oct 30, 2024
2b398d3
chore: trying to fix postgresql testing
tankerkiller125 Oct 30, 2024
1219744
fix: trying to fix postgresql testing
tankerkiller125 Oct 30, 2024
a1607e0
fix: trying to fix postgresql testing
tankerkiller125 Oct 30, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/docker-publish-arm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- 'Dockerfile'
- 'Dockerfile.rootless'
- '.dockerignore'
- '.github/workflows'
- '.github/workflows/**'
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
Expand All @@ -22,7 +22,7 @@ on:
- 'Dockerfile'
- 'Dockerfile.rootless'
- '.dockerignore'
- '.github/workflows'
- '.github/workflows/**'

env:
# Use docker.io for Docker Hub if empty
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docker-publish-rootless-arm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- 'Dockerfile'
- 'Dockerfile.rootless'
- '.dockerignore'
- '.github/workflows'
- '.github/workflows/**'
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
Expand All @@ -22,7 +22,7 @@ on:
- 'Dockerfile'
- 'Dockerfile.rootless'
- '.dockerignore'
- '.github/workflows'
- '.github/workflows/**'

env:
# Use docker.io for Docker Hub if empty
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docker-publish-rootless.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- 'Dockerfile'
- 'Dockerfile.rootless'
- '.dockerignore'
- '.github/workflows'
- '.github/workflows/**'
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
Expand All @@ -22,7 +22,7 @@ on:
- 'Dockerfile'
- 'Dockerfile.rootless'
- '.dockerignore'
- '.github/workflows'
- '.github/workflows/**'


env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docker-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- 'Dockerfile'
- 'Dockerfile.rootless'
- '.dockerignore'
- '.github/workflows'
- '.github/workflows/**'
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
Expand All @@ -22,7 +22,7 @@ on:
- 'Dockerfile'
- 'Dockerfile.rootless'
- '.dockerignore'
- '.github/workflows'
- '.github/workflows/**'

env:
# Use docker.io for Docker Hub if empty
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pull-requests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
paths:
- 'backend/**'
- 'frontend/**'
- '.github/workflows/**'

jobs:
backend-tests:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
backend/.data/*
config.yml
homebox.db
homebox.db-journal
homebox.db-shm
homebox.db-wal
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved
.idea
.DS_Store
test-mailer.json
Expand Down
54 changes: 52 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ version: "3"

env:
HBOX_LOG_LEVEL: debug
HBOX_STORAGE_SQLITE_URL: .data/homebox.db?_pragma=busy_timeout=1000&_pragma=journal_mode=WAL&_fk=1
HBOX_DATABASE_DRIVER: sqlite3
HBOX_DATABASE_SQLITE_PATH: .data/homebox.db?_pragma=busy_timeout=1000&_pragma=journal_mode=WAL&_fk=1
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved
HBOX_OPTIONS_ALLOW_REGISTRATION: true
UNSAFE_DISABLE_PASSWORD_PROJECTION: "yes_i_am_sure"
tasks:
Expand Down Expand Up @@ -60,6 +61,23 @@ tasks:
- go run ./app/api/ {{ .CLI_ARGS }}
silent: false

go:run:postgresql:
env:
HBOX_DATABASE_DRIVER: postgres
HBOX_DATABASE_USERNAME: homebox
HBOX_DATABASE_PASSWORD: homebox
HBOX_DATABASE_DATABASE: homebox
HBOX_DATABASE_HOST: localhost
HBOX_DATABASE_PORT: 5432
HBOX_DATABASE_SSLMODE: disable
desc: Starts the backend api server with postgresql (depends on generate task)
dir: backend
deps:
- generate
cmds:
- go run ./app/api/ {{ .CLI_ARGS }}
silent: false
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved

go:test:
desc: Runs all go tests using gotestsum - supports passing gotestsum args
dir: backend
Expand Down Expand Up @@ -114,6 +132,21 @@ tasks:
cmds:
- cd backend && go run app/tools/migrations/main.go {{ .CLI_ARGS }}

db:migration:postgresql:
env:
HBOX_DATABASE_DRIVER: postgres
HBOX_DATABASE_USERNAME: homebox
HBOX_DATABASE_PASSWORD: homebox
HBOX_DATABASE_DATABASE: homebox
HBOX_DATABASE_HOST: localhost
HBOX_DATABASE_PORT: 5432
HBOX_DATABASE_SSLMODE: disable
desc: Runs the database diff engine to generate a SQL migration files for postgresql
deps:
- db:generate
cmds:
- cd backend && go run app/tools/migrations/main.go {{ .CLI_ARGS }}

tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved
ui:watch:
desc: Starts the vitest test runner in watch mode
dir: frontend
Expand Down Expand Up @@ -143,7 +176,24 @@ tasks:
cmds:
- cd backend && go build ./app/api
- backend/api &
- sleep 5
- sleep 10
- cd frontend && pnpm run test:ci
silent: true

test:ci:postgresql:
env:
HBOX_DATABASE_DRIVER: postgres
HBOX_DATABASE_USERNAME: homebox
HBOX_DATABASE_PASSWORD: homebox
HBOX_DATABASE_DATABASE: homebox
HBOX_DATABASE_HOST: localhost
HBOX_DATABASE_PORT: 5432
HBOX_DATABASE_SSLMODE: disable
desc: Runs end-to-end test on a live server with postgresql (only for use in CI)
cmds:
- cd backend && go build ./app/api
- backend/api &
- sleep 10
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved
- cd frontend && pnpm run test:ci
silent: true

Expand Down
67 changes: 54 additions & 13 deletions backend/app/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"bytes"
"context"
"fmt"
"github.com/google/uuid"
"net/http"
"os"
"path/filepath"
"strings"
"time"

atlas "ariga.io/atlas/sql/migrate"
Expand All @@ -28,6 +30,7 @@ import (
"github.com/sysadminsmedia/homebox/backend/internal/sys/config"
"github.com/sysadminsmedia/homebox/backend/internal/web/mid"

_ "github.com/lib/pq"
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved
_ "github.com/sysadminsmedia/homebox/backend/pkgs/cgofreesqlite"
)

Expand All @@ -46,6 +49,19 @@ func build() string {
return fmt.Sprintf("%s, commit %s, built at %s", version, short, buildTime)
}

func validatePostgresSSLMode(sslMode string) bool {
validModes := map[string]bool{
"": true,
"disable": true,
"allow": true,
"prefer": true,
"require": true,
"verify-ca": true,
"verify-full": true,
}
return validModes[strings.ToLower(strings.TrimSpace(sslMode))]
}
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved

// @title Homebox API
// @version 1.0
// @description Track, Manage, and Organize your Things.
Expand Down Expand Up @@ -80,13 +96,32 @@ func run(cfg *config.Config) error {
log.Fatal().Err(err).Msg("failed to create data directory")
}

c, err := ent.Open("sqlite3", cfg.Storage.SqliteURL)
if strings.ToLower(cfg.Database.Driver) == "postgres" {
if !validatePostgresSSLMode(cfg.Database.SslMode) {
log.Fatal().Str("sslmode", cfg.Database.SslMode).Msg("invalid sslmode")
}
}

// Set up the database URL based on the driver because for some reason a common URL format is not used
databaseURL := ""
switch strings.ToLower(cfg.Database.Driver) {
case "sqlite3":
databaseURL = cfg.Database.SqlitePath
case "postgres":
databaseURL = fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", cfg.Database.Host, cfg.Database.Port, cfg.Database.Username, cfg.Database.Password, cfg.Database.Database, cfg.Database.SslMode)
default:
log.Fatal().Str("driver", cfg.Database.Driver).Msg("unsupported database driver")
}

c, err := ent.Open(strings.ToLower(cfg.Database.Driver), databaseURL)
if err != nil {
log.Fatal().
Err(err).
Str("driver", "sqlite").
Str("url", cfg.Storage.SqliteURL).
Msg("failed opening connection to sqlite")
Str("driver", strings.ToLower(cfg.Database.Driver)).
Str("host", cfg.Database.Host).
Str("port", cfg.Database.Port).
Str("database", cfg.Database.Database).
Msg("failed opening connection to {driver} database at {host}:{port}/{database}")
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved
}
defer func(c *ent.Client) {
err := c.Close()
Expand All @@ -95,9 +130,14 @@ func run(cfg *config.Config) error {
}
}(c)

temp := filepath.Join(os.TempDir(), "migrations")
// Always create a random temporary directory for migrations
tempUUID, err := uuid.NewUUID()
if err != nil {
return err
}
temp := filepath.Join(os.TempDir(), fmt.Sprintf("homebox-%s", tempUUID.String()))

err = migrations.Write(temp)
err = migrations.Write(temp, cfg.Database.Driver)
if err != nil {
return err
}
Expand All @@ -117,17 +157,18 @@ func run(cfg *config.Config) error {
if err != nil {
log.Error().
Err(err).
Str("driver", "sqlite").
Str("url", cfg.Storage.SqliteURL).
Str("driver", cfg.Database.Driver).
Str("url", databaseURL).
Msg("failed creating schema resources")
return err
}

err = os.RemoveAll(temp)
if err != nil {
log.Error().Err(err).Msg("failed to remove temporary directory for database migrations")
return err
}
defer func() {
err := os.RemoveAll(temp)
if err != nil {
log.Error().Err(err).Msg("failed to remove temporary directory for database migrations")
}
}()
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved

collectFuncs := []currencies.CollectorFunc{
currencies.CollectDefaults(),
Expand Down
Loading
Loading