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

feat(db-postgres): migrate from mongodb to postgres #1753

Merged
merged 186 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
186 commits
Select commit Hold shift + click to select a range
d3e1dc1
add postgres and pgadmin to docker compose
aakrem May 31, 2024
e459753
Merge branch 'main' into postgres
aakrem Jun 3, 2024
dde0dad
integrate sqlalchemy
aakrem Jun 3, 2024
10e5aad
converting schema to sqlalchemy
aakrem Jun 3, 2024
b9b17fc
format
aakrem Jun 3, 2024
6b2a11a
add db_mode
aakrem Jun 4, 2024
dba0597
fixes so that the app starts without errors
aakrem Jun 4, 2024
e35fa6c
format
aakrem Jun 4, 2024
8053342
fix timezone
aakrem Jun 4, 2024
6a52de2
add a helper to convert a value to utc
aakrem Jun 4, 2024
84bf031
remove deprecated add new tables HumanEvaluationScenarioInputsDB and …
aakrem Jun 4, 2024
aeb04e2
move some models to shared models
aakrem Jun 4, 2024
4d7e0c2
cleanup
aakrem Jun 4, 2024
0234a7b
move config to a table
aakrem Jun 4, 2024
cc06cf6
adjust schemas for evaluations
aakrem Jun 5, 2024
5408d14
Add a relation Evaluation > AppVariantDB
aakrem Jun 5, 2024
7c736f0
remove config db
aakrem Jun 5, 2024
2683205
fix config
aakrem Jun 5, 2024
d1c7811
move from integer to uuid
aakrem Jun 5, 2024
f3acf3d
move to jsonb for better indexing and efficiency
aakrem Jun 5, 2024
99f032b
fix deployment relationships
aakrem Jun 6, 2024
256c249
make template type enum
aakrem Jun 6, 2024
c584f0c
opt for json inputs and outputs
aakrem Jun 6, 2024
35c2be9
rename and refactor db uri
aakrem Jun 6, 2024
dbbd21e
fix relationship
aakrem Jun 6, 2024
7b135c7
use schema definition standards
aakrem Jun 6, 2024
276d1dc
add types as comments
aakrem Jun 6, 2024
400c6e0
fixes to missing schema relationships
aakrem Jun 7, 2024
f737511
initial migration
aakrem Jun 7, 2024
73e6677
fix evaluations
aakrem Jun 7, 2024
e35dcc7
evaluation schema fixes
aakrem Jun 7, 2024
5297e91
move shared models to a separate file
aakrem Jun 8, 2024
10a9fc7
fix issue of order of table creation
aakrem Jun 10, 2024
2f755f0
refactor (backend): improved session life-cycle and ensured atomicity…
aybruhm Jun 11, 2024
4d31e5d
refactor (backend): set template_uri, docker_uri and tags column to b…
aybruhm Jun 11, 2024
110e339
refactor (backend): cleanup api models and update ground_truth_keys_d…
aybruhm Jun 11, 2024
c10dd88
refactor (backend): migrate beanie odm query to sqlalchemy
aybruhm Jun 11, 2024
4c447bc
refactor (backend): improve clarity of code in app and evaluator mana…
aybruhm Jun 11, 2024
24ab8c4
refactor (backend): migrate beanie odm query to sqlalchemy
aybruhm Jun 11, 2024
30500df
refactor (backend): update routers to work with sqlalchemy migration
aybruhm Jun 11, 2024
44aefa8
refactor (backend): update converts that translate db models into pyd…
aybruhm Jun 11, 2024
355891f
feat (backend): implement close method in db_engine to ensure that al…
aybruhm Jun 11, 2024
17c1fac
chore (backend): format codebase with black@23.12.0
aybruhm Jun 11, 2024
c120d1f
Merge branch 'postgres' into postgres-migration
aakrem Jun 11, 2024
d223acd
refactor (backend): added cascade to foreignkey and improved relation…
aybruhm Jun 13, 2024
5dacbfd
minor refactor (backend): remove redundant imports
aybruhm Jun 13, 2024
457ad5e
refactor (backend): added cascade to fk relationships
aybruhm Jun 13, 2024
fe2ef53
minor refactor (backend): update evaluator config model
aybruhm Jun 13, 2024
6a8e4fd
refactor (backend): removed code redundancy and simplified use of con…
aybruhm Jun 13, 2024
3a71718
refactor (backend): remove redundant code in app manager, and improve…
aybruhm Jun 13, 2024
74285a2
refactor (backend): migrate evaluation service from Beanie to SQLAlch…
aybruhm Jun 13, 2024
94bf6ce
refactor (backend): migrate testsets from Beanie to SQLAlchemy and fi…
aybruhm Jun 13, 2024
1e6d19b
minor refactor (backend): rename get_deployment_by_objectid to get_de…
aybruhm Jun 13, 2024
151cda8
refactor (backend): improve queries by making use of joinedload and l…
aybruhm Jun 13, 2024
def4d75
minor refactor (tools): comment out redundant compose services
aybruhm Jun 14, 2024
0285079
chore (backend): format codebase with black@23.12.0
aybruhm Jun 14, 2024
163abd5
Merge branch 'postgres' into migration/beanie-to-sqlalchemy
aybruhm Jun 14, 2024
b59bf53
chore (backend): format db_manager with black@23.12.0
aybruhm Jun 14, 2024
947731d
refactor (tools): added volumes for persistent storage
aybruhm Jun 14, 2024
99672cf
refactor (tools): removed redundant mongo services and added postgres…
aybruhm Jun 14, 2024
e3aba63
small improvements
aakrem Jun 14, 2024
f0715d7
minor refactor (tests): improve event_loop conftest fixture and fix i…
aybruhm Jun 14, 2024
38859c7
refactor (backend): move human evaluation db queries from evaluation_…
aybruhm Jun 14, 2024
3cd5d29
refactor (backend): improve converters and results_service for human …
aybruhm Jun 14, 2024
1243050
refactor (backend): ensured that exception is properly raise with its…
aybruhm Jun 14, 2024
9f9bf43
refactor (backend): created human evaluation variants model, improved…
aybruhm Jun 14, 2024
dcb490c
chore (backend): format codebase with black@23.12.0
aybruhm Jun 14, 2024
c338930
chore (tools): reintroduce mongo services
aybruhm Jun 14, 2024
669c1d4
refactor (backend): created human evaluation variant db model, added …
aybruhm Jun 14, 2024
9c2d4c3
minor refactor (backend): remove redundant imports and added cascade …
aybruhm Jun 14, 2024
bec1b36
Merge pull request #1788 from Agenta-AI/fk-cascade
aakrem Jun 15, 2024
987eb4d
Merge pull request #1793 from Agenta-AI/postgres
aakrem Jun 15, 2024
26cbbe2
refactor (backend): modified ondelete behaviour for evaluation config…
aybruhm Jun 18, 2024
568d0c2
refactor (backend): improve code clarity for remove_app service function
aybruhm Jun 18, 2024
dcf3879
refactor (backend): move fetch_evaluations_by_resource to db_manager …
aybruhm Jun 18, 2024
8983b27
refactor (backend): update fetch_evaluations_by_resource import from …
aybruhm Jun 18, 2024
2dc3e21
minor refactor (backend): revert back to using jsonresponse for 403 e…
aybruhm Jun 18, 2024
8da15c9
refactor (backend): prefetch variant revision and fix failing /config…
aybruhm Jun 18, 2024
d806afc
minor refactor (backend): improve code readability for create_image
aybruhm Jun 18, 2024
9f2fb2b
chore (backend): format codebase with black@23.12.0
aybruhm Jun 18, 2024
909ca67
minor refactor (backend): added logic to remove app related resources
aybruhm Jun 18, 2024
e42df10
minor refactor (backend): revert assertion back to conditional check …
aybruhm Jun 18, 2024
35946c3
minor refactor (backend): change model_dump() to dict() when updating…
aybruhm Jun 18, 2024
df7e48d
refactor (tools): installed sqlalchemy-json for jsonb mutation tracking
aybruhm Jun 18, 2024
f75c46a
refactor (backend): update testset and user api models
aybruhm Jun 19, 2024
500fd1d
refactor (backend): fix 500 error in user-profile and improve get_sin…
aybruhm Jun 19, 2024
8783d32
refactor (backend): improve update functions for evaluator_config and…
aybruhm Jun 19, 2024
69c1919
minor refactor (frontend): make use of record.id instead of record._id
aybruhm Jun 19, 2024
019eb1f
feat (backend): make use of mutable_json_type for jsonb mutation trac…
aybruhm Jun 19, 2024
43015b9
chore (backend): format codebase with black@23.12.0
aybruhm Jun 19, 2024
5f39d21
minor refactor (frontend): change testset interface from _id to id
aybruhm Jun 19, 2024
81c48d4
refactor (backend): revert record.id back to record._id
aybruhm Jun 19, 2024
07d0306
refactor (backend): resolve oss migration bugs
aybruhm Jun 19, 2024
96b8ebe
minor refactor (backend): simply update_human_evaluation_scenario logic
aybruhm Jun 19, 2024
fc61782
Merge pull request #1781 from Agenta-AI/migration/beanie-to-sqlalchemy
aybruhm Jun 19, 2024
98fa8e4
Merge branch 'main' into postgres
aybruhm Jun 19, 2024
517dd49
refactor (cli): fix failing backend endpoint for variant serve
aybruhm Jun 19, 2024
182be7f
Merge branch 'postgres' into postgres-migration
aakrem Jun 19, 2024
70269ea
refactor (backend): fix logic for updating variant image
aybruhm Jun 19, 2024
b0ad9fe
test(frontend): modified match regex to get app_id from url
bekossy Jun 20, 2024
936a87d
refactor (tests): added agenta-web service to test compose
aybruhm Jun 20, 2024
14c89a8
minor refactor (tools): remove agenta-web service from test compose a…
aybruhm Jun 20, 2024
9e3dff9
minor refactor (tools): modified step 2 for run-frontend-tests workflow
aybruhm Jun 20, 2024
718361d
minor refactor (tools): remove external: true from test compose
aybruhm Jun 20, 2024
04075eb
fix human evaluations
aakrem Jun 21, 2024
f93ba1d
add human evaluations variant
aakrem Jun 21, 2024
e79d713
refactor (tests): resolve failing backend tests
aybruhm Jun 21, 2024
1d6e204
chore (tests): format tests/ codebase with black@23.12.0
aybruhm Jun 21, 2024
28c188c
refactor (backend): rewrite user_service to make use of sqlalchemy orm
aybruhm Jun 21, 2024
a9e6169
refactor (db): rewrite db_engine to intialize postgres x mongodb engi…
aybruhm Jun 21, 2024
2819cde
Merge branch 'postgres' into postgres-migration
aakrem Jun 21, 2024
256f132
refactor (backend): improve service function to create a new user
aybruhm Jun 22, 2024
405a1f6
add db name and create db on postgres startup if doesn't exist
aakrem Jun 23, 2024
c96d25a
remove comment from chatgpt
aakrem Jun 24, 2024
1417aca
add separated messages
aakrem Jun 24, 2024
808439c
add printing traceback
aakrem Jun 24, 2024
316fa20
improve migration output
aakrem Jun 24, 2024
92145d3
add filtering by table
aakrem Jun 24, 2024
74ba503
replace id with _id
aakrem Jun 24, 2024
4c48c40
fix base
aakrem Jun 24, 2024
a950c40
add assertion to check length of variants and variants_revisions
aakrem Jun 25, 2024
1d201ab
add migration steps
aakrem Jun 25, 2024
92fd40d
fix deployed_app_variant_revision
aakrem Jun 25, 2024
1d45104
refactor (backend -> db orm): improve queries to include cloud/ee rel…
aybruhm Jun 25, 2024
82e2b92
refactor (backend -> api routers): remove redundant imports, added ex…
aybruhm Jun 25, 2024
e1b389e
refactor (backend): improve use of relational attribute uIDs and remo…
aybruhm Jun 25, 2024
0a1ffc4
refactor (backend): improve db to api model converters
aybruhm Jun 25, 2024
8f1fe91
chore (backend): format codebase with black@23.12.0
aybruhm Jun 25, 2024
8e69391
Merge branch 'postgres' into cloud-migration/beanie-to-sqlalchemy
aybruhm Jun 25, 2024
7d016c3
fix(tool): [bug] Incorrect Configuration for Variants fix migration
mmabrouk Jun 25, 2024
582b60d
fix human evaluation variants results
aakrem Jun 25, 2024
b396da1
fix tqdm progress length
aakrem Jun 25, 2024
0c6520f
move all previous migrations to a separate folder
aakrem Jun 26, 2024
1002e64
add backup
aakrem Jun 26, 2024
bdcfdf5
minor refactor (backend): ensure that when docker_images are deleted,…
aybruhm Jun 26, 2024
50a3b03
refactor (backend): deploy current LLM app configuration to productio…
aybruhm Jun 26, 2024
a6afc7f
refactor (backend): ensures that a 404 exception is raised in the cas…
aybruhm Jun 26, 2024
3f07796
refactor (backend): fix error when updating variant image during CLI …
aybruhm Jun 26, 2024
183456a
chore (backend): format codebase with black@23.12.0
aybruhm Jun 26, 2024
49c8f9d
add assertion to prevent duplicated values
aakrem Jun 27, 2024
95bf502
minor refactor (backend): resolve NameError: name 'organization_id' i…
aybruhm Jun 27, 2024
fca96ea
support skipping documents in case of duplicated values
aakrem Jun 30, 2024
831711b
Merge pull request #1828 from Agenta-AI/cloud-migration/beanie-to-sql…
aakrem Jun 30, 2024
3fa794b
separate db engine for migration
aakrem Jun 30, 2024
3c43f07
minor refactor (backend): remove redundant UserWorkspaceDB model from…
aybruhm Jul 1, 2024
339c036
Merge pull request #1830 from Agenta-AI/cloud-migration/beanie-to-sql…
aybruhm Jul 1, 2024
abc39ab
small refactoring
aakrem Jul 1, 2024
e374973
refactor (docs): moved and renamed migration.mdx to migration folder …
aybruhm Jul 1, 2024
f114e42
feat (docs): created documentation for Postgres migration
aybruhm Jul 1, 2024
9d4b31c
minor refactor (docs): replace <version> tag with 'version'
aybruhm Jul 1, 2024
573ca2a
refactor (backend): replace deprecated legacy query api .one_or_none(…
aybruhm Jul 2, 2024
0b965ea
Merge pull request #1836 from Agenta-AI/refactor/replace-one_or_none-…
aakrem Jul 2, 2024
03ff817
Merge pull request #1831 from Agenta-AI/docs/postgres-migration-docum…
mmabrouk Jul 2, 2024
747b5be
refactor (tools): set db name in postgres_uri to backend and celery_w…
aybruhm Jul 2, 2024
3a05bcd
refactor (backend): move initialization of async sqlalchemy engine to…
aybruhm Jul 3, 2024
8dd92a2
minor refactor (tests): initialize db_engine and ensure that database…
aybruhm Jul 3, 2024
3124725
minor refactor (tests): ensure that test db is dropped before closing…
aybruhm Jul 3, 2024
a8c0551
minor refactor (tools): revert back to using default database created
aybruhm Jul 3, 2024
2e3ee66
Merge pull request #1838 from Agenta-AI/resolve-failing-backend-tests
aakrem Jul 3, 2024
65fe52b
Merge pull request #1811 from Agenta-AI/cloud-migration/beanie-to-sql…
aakrem Jul 3, 2024
1688567
Merge branch 'main' into postgres
aybruhm Jul 3, 2024
f518b23
Merge pull request #1839 from Agenta-AI/postgres
aakrem Jul 3, 2024
dd03cf2
Merge pull request #1772 from Agenta-AI/postgres-migration
aakrem Jul 3, 2024
8b3b083
data-migration fixes for human evaluation scenario (outputs variants …
aakrem Jul 3, 2024
1dd2e92
fix (backend): remove logic that unpublishes other variants' environm…
aybruhm Jul 5, 2024
81ea552
style (backend): format code with black@23.12.0
aybruhm Jul 5, 2024
169b1c0
adjust db engine and small fixes
aakrem Jul 5, 2024
90076bb
minor refactor (backend): remove redundant db_manager 'list_environme…
aybruhm Jul 5, 2024
61e4f33
Merge pull request #1845 from Agenta-AI/bug-fix/variant-deletion-unpu…
mmabrouk Jul 5, 2024
a995950
enhancements to mongo db engine
aakrem Jul 5, 2024
da90b93
feat (backend): created db_manager 'fetch_corresponding_object_uuid' …
aybruhm Jul 5, 2024
233f7ef
minor refactor (backend): remove options from 'fetch_corresponding_ob…
aybruhm Jul 5, 2024
46cd3f8
rewriting mongo db engine
aakrem Jul 5, 2024
e5e086d
Merge pull request #1851 from Agenta-AI/refactor/add-function-to-fetc…
aakrem Jul 6, 2024
d55d30f
refactor (backend): update user lookup logic for backward compatibili…
aybruhm Jul 7, 2024
fbd6f99
refactor (backend): add feature flag for conditional user UID query i…
aybruhm Jul 7, 2024
e61da32
minor refactor (backend): set image_id to be nullable
aybruhm Jul 7, 2024
d871e64
Merge pull request #1852 from Agenta-AI/feature/age-395-unable-to-cre…
aakrem Jul 7, 2024
5efcb37
Merge pull request #1853 from Agenta-AI/bug/deleting-docker-image-res…
aakrem Jul 7, 2024
5ae47c6
Merge branch 'main' into postgres
aybruhm Jul 7, 2024
5801ad3
minor refactor (backend): resolve ImportError in evaluators and aggre…
aybruhm Jul 7, 2024
9728818
refactor (backend): resolve 'super' object has no attribute 'coerce' …
aybruhm Jul 7, 2024
48de96a
tests (backend): make use of EVALUATION_INITIALIZED enum and not EVAL…
aybruhm Jul 7, 2024
a12a206
Merge pull request #1854 from Agenta-AI/main-to-postgres-merge
aakrem Jul 7, 2024
53ba46c
feat (backend): create db_manager 'get_object_uuid' function and use …
aybruhm Jul 8, 2024
ffd9278
Merge pull request #1858 from Agenta-AI/bugfix/backward-compatibility…
aakrem Jul 8, 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
2 changes: 1 addition & 1 deletion .github/workflows/run-frontend-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
NEXT_PUBLIC_OPENAI_API_KEY: ${{ secrets.NEXT_PUBLIC_OPENAI_API_KEY }}
run: |
sudo apt install curl -y
docker-compose -f "docker-compose.test.yml" up -d --build
OPENAI_API_KEY=${{ secrets.NEXT_PUBLIC_OPENAI_API_KEY }} ENVIRONMENT=github docker-compose -f "docker-compose.test.yml" up -d --build

- name: Restart Backend Service To Fetch Template(s)
run: docker container restart agenta-backend-test
Expand Down
6 changes: 4 additions & 2 deletions agenta-backend/agenta_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
health_router,
)
from agenta_backend.utils.common import isEE, isCloudProd, isCloudDev, isOss, isCloudEE
from agenta_backend.models.db_engine import DBEngine
from agenta_backend.models.db_engine import db_engine
from agenta_backend.open_api import open_api_tags_metadata

if isEE() or isCloudProd():
Expand Down Expand Up @@ -52,9 +52,11 @@ async def lifespan(application: FastAPI, cache=True):
application: FastAPI application.
cache: A boolean value that indicates whether to use the cached data or not.
"""
await DBEngine().init_db()

await db_engine.init_db()
await templates_manager.update_and_sync_templates(cache=cache)
yield
await db_engine.close()


app = FastAPI(lifespan=lifespan, openapi_tags=open_api_tags_metadata)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```bash
docker ps
docker exec -it {backend-container-id} bash
cd /app/agenta_backend/migrations/mongo_to_postgres
python3 migration.py
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import os
import logging
from asyncio import current_task
from typing import AsyncGenerator
from contextlib import asynccontextmanager

from sqlalchemy.ext.asyncio import (
AsyncSession,
create_async_engine,
async_sessionmaker,
async_scoped_session,
)

from agenta_backend.utils.common import isCloudEE

if isCloudEE():
from agenta_backend.commons.observability.models.db import SpanDB
from agenta_backend.commons.models.db_models import (
APIKeyDB,
WorkspaceDB,
OrganizationDB,
AppDB_ as AppDB,
UserDB_ as UserDB,
ImageDB_ as ImageDB,
TestSetDB_ as TestSetDB,
AppVariantDB_ as AppVariantDB,
EvaluationDB_ as EvaluationDB,
DeploymentDB_ as DeploymentDB,
VariantBaseDB_ as VariantBaseDB,
AppEnvironmentDB_ as AppEnvironmentDB,
AppEnvironmentRevisionDB_ as AppEnvironmentRevisionDB,
EvaluatorConfigDB_ as EvaluatorConfigDB,
HumanEvaluationDB_ as HumanEvaluationDB,
EvaluationScenarioDB_ as EvaluationScenarioDB,
HumanEvaluationScenarioDB_ as HumanEvaluationScenarioDB,
)
else:
from agenta_backend.models.db_models import (
AppDB,
UserDB,
ImageDB,
TestSetDB,
EvaluationDB,
DeploymentDB,
AppVariantDB,
VariantBaseDB,
AppEnvironmentDB,
AppEnvironmentRevisionDB,
EvaluatorConfigDB,
HumanEvaluationDB,
EvaluationScenarioDB,
HumanEvaluationScenarioDB,
)

from agenta_backend.models.db_models import (
TemplateDB,
AppVariantRevisionsDB,
)

models = [
AppDB,
UserDB,
ImageDB,
TestSetDB,
TemplateDB,
AppVariantDB,
DeploymentDB,
EvaluationDB,
VariantBaseDB,
AppEnvironmentDB,
AppEnvironmentRevisionDB,
EvaluatorConfigDB,
HumanEvaluationDB,
EvaluationScenarioDB,
AppVariantRevisionsDB,
HumanEvaluationScenarioDB,
]

if isCloudEE():
models.extend([OrganizationDB, WorkspaceDB, APIKeyDB]) # type: ignore


# Configure and set logging level
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


class DBEngine:
"""
Database engine to initialize SQLAlchemy and return the engine based on mode.
"""

def __init__(self) -> None:
self.mode = os.environ.get("DATABASE_MODE", "v2")
self.db_url = f"{os.environ.get('POSTGRES_URI')}"
self.engine = create_async_engine(url=self.db_url)
self.async_session_maker = async_sessionmaker(
bind=self.engine, class_=AsyncSession, expire_on_commit=False
)
self.async_session = async_scoped_session(
session_factory=self.async_session_maker, scopefunc=current_task
)

async def init_db(self):
"""
Initialize the database based on the mode and create all tables.
"""
async with self.engine.begin() as conn:
# Drop all existing tables (if needed)
# await conn.run_sync(Base.metadata.drop_all)
# Create tables
for model in models:
await conn.run_sync(model.metadata.create_all)
logger.info(f"Using {self.mode} database...")

async def remove_db(self) -> None:
"""
Remove the database based on the mode.
"""
async with self.engine.begin() as conn:
for model in models:
await conn.run_sync(model.metadata.drop_all)

@asynccontextmanager
async def get_session(self) -> AsyncGenerator[AsyncSession, None]:
session = self.async_session()
try:
yield session
except Exception as e:
await session.rollback()
raise e
finally:
await session.close()

async def close(self):
"""
Closes and dispose all the connections using the engine.
:raises Exception: if engine is initialized
"""

if self.engine is None:
raise Exception("DBEngine is not initialized")

await self.engine.dispose()

self.engine = None
self.async_session_maker = None
self.async_session = None


db_engine = DBEngine()
Loading
Loading