Skip to content

Commit

Permalink
fix: separate modusdb instance by app (#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Jan 25, 2025
1 parent 45a6258 commit 4aced09
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
*.gz
*.zip

# Ignore Go debuger and generated files
# Ignore Go debugger and generated files
__debug_bin*
*_generated.go
*.generated.go

# Ignore build output directories
# Ignore build output directories and modus databases
build/
.modusdb/

# Ignore node_modules folders
node_modules/
Expand Down
1 change: 1 addition & 0 deletions .trunk/configs/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"millis",
"minilm",
"modfile",
"modusdb",
"Msgf",
"mydb",
"mydgraph",
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 2025-01-24 - Runtime 0.17.1

- fix: separate modusdb instance by app [#729](https://github.com/hypermodeinc/modus/pull/729)

## 2025-01-24 - Runtime 0.17.0

- feat: add modusdb model tracing for local dev
Expand Down
69 changes: 62 additions & 7 deletions runtime/db/modusdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
package db

import (
"bufio"
"context"
"errors"
"os"
"path/filepath"
"runtime"

Expand All @@ -22,13 +25,26 @@ import (
var GlobalModusDbEngine *modusdb.Engine

func InitModusDb(ctx context.Context) {
if app.IsDevEnvironment() && runtime.GOOS != "windows" {
dataDir := filepath.Join(app.ModusHomeDir(), "data")
if eng, err := modusdb.NewEngine(modusdb.NewDefaultConfig(dataDir)); err != nil {
logger.Fatal(ctx).Err(err).Msg("Failed to initialize modusdb.")
} else {
GlobalModusDbEngine = eng
}
if !app.IsDevEnvironment() || runtime.GOOS == "windows" {
// ModusDB should only be initialized in dev environment,
// and currently does not work on Windows.
return
}

var dataDir string
appPath := app.Config().AppPath()
if filepath.Base(appPath) == "build" {
// this keeps the data directory outside of the build directory
dataDir = filepath.Join(appPath, "..", ".modusdb")
addToGitIgnore(ctx, filepath.Dir(appPath))
} else {
dataDir = filepath.Join(appPath, ".modusdb")
}

if eng, err := modusdb.NewEngine(modusdb.NewDefaultConfig(dataDir)); err != nil {
logger.Fatal(ctx).Err(err).Msg("Failed to initialize modusdb.")
} else {
GlobalModusDbEngine = eng
}
}

Expand All @@ -37,3 +53,42 @@ func CloseModusDb(ctx context.Context) {
GlobalModusDbEngine.Close()
}
}

func addToGitIgnore(ctx context.Context, rootPath string) {
gitIgnorePath := filepath.Join(rootPath, ".gitignore")
gitIgnoreContents := ".modusdb/"

// if .gitignore file does not exist, create it and add .modusdb/ to it
if _, err := os.Stat(gitIgnorePath); errors.Is(err, os.ErrNotExist) {
if err := os.WriteFile(gitIgnorePath, []byte(gitIgnoreContents+"\n"), 0644); err != nil {
logger.Err(ctx, err).Msg("Failed to create .gitignore file.")
}
return
}

// check if .modusdb/ is already in the .gitignore file
file, err := os.Open(gitIgnorePath)
if err != nil {
logger.Err(ctx, err).Msg("Failed to open .gitignore file.")
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() == gitIgnoreContents {
// found .modusdb/ in the file
return
}
}

// .modusdb/ is not in the file, so append it
file, err = os.OpenFile(gitIgnorePath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
logger.Err(ctx, err).Msg("Failed to open .gitignore file.")
return
}
defer file.Close()
if _, err := file.WriteString("\n" + gitIgnoreContents + "\n"); err != nil {
logger.Err(ctx, err).Msg("Failed to append .modusdb/ to .gitignore file.")
}
}

0 comments on commit 4aced09

Please sign in to comment.