Merge pull request #554 from Pythagora-io/autofix-database-paths

load project from random db
This commit is contained in:
LeonOstrez
2024-01-19 11:59:14 -08:00
committed by GitHub
3 changed files with 41 additions and 0 deletions

View File

@@ -28,3 +28,6 @@ DB_USER=
DB_PASSWORD=
USE_GPTPILOT_FOLDER=true
# Load database imported from another location/system - EXPERIMENTAL
# AUTOFIX_FILE_PATHS=false

View File

@@ -1,3 +1,5 @@
from pathlib import Path
from os.path import commonprefix, join, sep
from peewee import AutoField, CharField, TextField, ForeignKeyField
from database.models.components.base_models import BaseModel
@@ -16,3 +18,36 @@ class File(BaseModel):
indexes = (
(('app', 'name', 'path'), True),
)
@staticmethod
def update_paths():
workspace_dir = Path(__file__).parent.parent.parent.parent / "workspace"
if not workspace_dir.exists():
# This should only happen on first run
return
paths = [file.full_path for file in File.select(File.full_path).distinct()]
if not paths:
# No paths in the database, so nothing to fix
return
common_prefix = commonprefix(paths)
if commonprefix([common_prefix, str(workspace_dir)]) == str(workspace_dir):
# Paths are up to date, nothing to fix
return
common_sep = "\\" if ":\\" in common_prefix else "/"
common_parts = common_prefix.split(common_sep)
try:
workspace_index = common_parts.index("workspace")
except ValueError:
# There's something strange going on, better not touch anything
return
old_prefix = common_sep.join(common_parts[:workspace_index + 1])
print(f"Updating file paths from {old_prefix} to {workspace_dir}")
for file in File.select().where(File.full_path.startswith(old_prefix)):
parts = file.full_path.split(common_sep)
new_path = str(workspace_dir) + sep + sep.join(parts[workspace_index + 1:])
file.full_path = new_path
file.save()

View File

@@ -80,6 +80,9 @@ class Project:
self.development_plan = development_plan
self.dot_pilot_gpt = DotGptPilot(log_chat_completions=enable_dot_pilot_gpt)
if os.getenv("AUTOFIX_FILE_PATHS", "").lower() in ["true", "1", "yes"]:
File.update_paths()
def set_root_path(self, root_path: str):
self.root_path = root_path
self.dot_pilot_gpt.with_root_path(root_path)