From 4aced0920284c30fce4400a0e7d4812dce4ea03f Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Fri, 24 Jan 2025 17:21:10 -0800 Subject: [PATCH] fix: separate modusdb instance by app (#729) --- .gitignore | 5 +-- .trunk/configs/cspell.json | 1 + CHANGELOG.md | 4 +++ runtime/db/modusdb.go | 69 ++++++++++++++++++++++++++++++++++---- 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 6faf0c95..52030c1c 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/.trunk/configs/cspell.json b/.trunk/configs/cspell.json index f3e77e02..3d95a8ae 100644 --- a/.trunk/configs/cspell.json +++ b/.trunk/configs/cspell.json @@ -117,6 +117,7 @@ "millis", "minilm", "modfile", + "modusdb", "Msgf", "mydb", "mydgraph", diff --git a/CHANGELOG.md b/CHANGELOG.md index c3095539..ff22b3aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/runtime/db/modusdb.go b/runtime/db/modusdb.go index eb12ccb8..3ab384c1 100644 --- a/runtime/db/modusdb.go +++ b/runtime/db/modusdb.go @@ -10,7 +10,10 @@ package db import ( + "bufio" "context" + "errors" + "os" "path/filepath" "runtime" @@ -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 } } @@ -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.") + } +}