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

fix: separate modusdb instance by app #729

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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.")
}
}
Loading