From b3d873a4b9bb0daac9a7be3c9136d1fda14ede25 Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Wed, 28 Aug 2024 15:50:58 -0500 Subject: [PATCH] Make all green --- .../autogpt_server/data/execution.py | 6 +++--- .../autogpt_server/data/graph.py | 1 + .../autogpt_server/executor/manager.py | 2 +- .../autogpt_server/executor/scheduler.py | 3 ++- .../migration.sql | 20 +++++++++++++++++++ .../migration.sql | 8 ++++++++ rnd/autogpt_server/postgres/schema.prisma | 14 ++++++------- rnd/autogpt_server/schema.prisma | 16 +++++++-------- 8 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 rnd/autogpt_server/migrations/20240828204627_make_user_id_optional/migration.sql create mode 100644 rnd/autogpt_server/postgres/migrations/20240828204820_make_user_id_optional/migration.sql diff --git a/rnd/autogpt_server/autogpt_server/data/execution.py b/rnd/autogpt_server/autogpt_server/data/execution.py index 62c41baa0..b677838f8 100644 --- a/rnd/autogpt_server/autogpt_server/data/execution.py +++ b/rnd/autogpt_server/autogpt_server/data/execution.py @@ -120,7 +120,7 @@ async def create_graph_execution( graph_id: str, graph_version: int, nodes_input: list[tuple[str, BlockInput]], - user_id: str, + user_id: str | None = None, ) -> tuple[str, list[ExecutionResult]]: """ Create a new AgentGraphExecution record. @@ -148,7 +148,7 @@ async def create_graph_execution( }, "userId": user_id, }, - include={"AgentNodeExecutions": {"include": EXECUTION_RESULT_INCLUDE}}, + include={"AgentNodeExecutions": {"include": EXECUTION_RESULT_INCLUDE}}, # type: ignore ) return result.id, [ @@ -284,7 +284,7 @@ async def get_graph_execution( """ execution = await AgentGraphExecution.prisma().find_first( where={"id": graph_exec_id, "userId": user_id}, - include={"AgentNodeExecutions": {"include": EXECUTION_RESULT_INCLUDE}}, + include={"AgentNodeExecutions": {"include": EXECUTION_RESULT_INCLUDE}}, # type: ignore ) return execution diff --git a/rnd/autogpt_server/autogpt_server/data/graph.py b/rnd/autogpt_server/autogpt_server/data/graph.py index 060793a2e..0a99f66e2 100644 --- a/rnd/autogpt_server/autogpt_server/data/graph.py +++ b/rnd/autogpt_server/autogpt_server/data/graph.py @@ -294,6 +294,7 @@ async def get_graphs_meta( Args: filter_by: An optional filter to either select templates or active graphs. + user_id: An optional user ID to filter the graphs by. Returns: list[GraphMeta]: A list of objects representing the retrieved graph metadata. diff --git a/rnd/autogpt_server/autogpt_server/executor/manager.py b/rnd/autogpt_server/autogpt_server/executor/manager.py index 307b0bf2a..4f9fe3821 100644 --- a/rnd/autogpt_server/autogpt_server/executor/manager.py +++ b/rnd/autogpt_server/autogpt_server/executor/manager.py @@ -466,7 +466,7 @@ class ExecutionManager(AppService): @expose def add_execution( - self, graph_id: str, data: BlockInput, user_id: str + self, graph_id: str, data: BlockInput, user_id: str | None = None ) -> dict[str, Any]: graph: Graph | None = self.run_and_wait(get_graph(graph_id, user_id=user_id)) if not graph: diff --git a/rnd/autogpt_server/autogpt_server/executor/scheduler.py b/rnd/autogpt_server/autogpt_server/executor/scheduler.py index e55aebf05..47b1a42ce 100644 --- a/rnd/autogpt_server/autogpt_server/executor/scheduler.py +++ b/rnd/autogpt_server/autogpt_server/executor/scheduler.py @@ -37,7 +37,8 @@ class ExecutionScheduler(AppService): def __refresh_jobs_from_db(self, scheduler: BackgroundScheduler): schedules = self.run_and_wait(model.get_active_schedules(self.last_check)) for schedule in schedules: - self.last_check = max(self.last_check, schedule.last_updated) + if schedule.last_updated: + self.last_check = max(self.last_check, schedule.last_updated) if not schedule.is_enabled: log(f"Removing recurring job {schedule.id}: {schedule.schedule}") diff --git a/rnd/autogpt_server/migrations/20240828204627_make_user_id_optional/migration.sql b/rnd/autogpt_server/migrations/20240828204627_make_user_id_optional/migration.sql new file mode 100644 index 000000000..b7948b299 --- /dev/null +++ b/rnd/autogpt_server/migrations/20240828204627_make_user_id_optional/migration.sql @@ -0,0 +1,20 @@ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_AgentGraphExecutionSchedule" ( + "id" TEXT NOT NULL PRIMARY KEY, + "agentGraphId" TEXT NOT NULL, + "agentGraphVersion" INTEGER NOT NULL DEFAULT 1, + "schedule" TEXT NOT NULL, + "isEnabled" BOOLEAN NOT NULL DEFAULT true, + "inputData" TEXT NOT NULL, + "lastUpdated" DATETIME NOT NULL, + "userId" TEXT, + CONSTRAINT "AgentGraphExecutionSchedule_agentGraphId_agentGraphVersion_fkey" FOREIGN KEY ("agentGraphId", "agentGraphVersion") REFERENCES "AgentGraph" ("id", "version") ON DELETE RESTRICT ON UPDATE CASCADE, + CONSTRAINT "AgentGraphExecutionSchedule_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE +); +INSERT INTO "new_AgentGraphExecutionSchedule" ("agentGraphId", "agentGraphVersion", "id", "inputData", "isEnabled", "lastUpdated", "schedule", "userId") SELECT "agentGraphId", "agentGraphVersion", "id", "inputData", "isEnabled", "lastUpdated", "schedule", "userId" FROM "AgentGraphExecutionSchedule"; +DROP TABLE "AgentGraphExecutionSchedule"; +ALTER TABLE "new_AgentGraphExecutionSchedule" RENAME TO "AgentGraphExecutionSchedule"; +CREATE INDEX "AgentGraphExecutionSchedule_isEnabled_idx" ON "AgentGraphExecutionSchedule"("isEnabled"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/rnd/autogpt_server/postgres/migrations/20240828204820_make_user_id_optional/migration.sql b/rnd/autogpt_server/postgres/migrations/20240828204820_make_user_id_optional/migration.sql new file mode 100644 index 000000000..3d268f8aa --- /dev/null +++ b/rnd/autogpt_server/postgres/migrations/20240828204820_make_user_id_optional/migration.sql @@ -0,0 +1,8 @@ +-- DropForeignKey +ALTER TABLE "AgentGraphExecutionSchedule" DROP CONSTRAINT "AgentGraphExecutionSchedule_userId_fkey"; + +-- AlterTable +ALTER TABLE "AgentGraphExecutionSchedule" ALTER COLUMN "userId" DROP NOT NULL; + +-- AddForeignKey +ALTER TABLE "AgentGraphExecutionSchedule" ADD CONSTRAINT "AgentGraphExecutionSchedule_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/rnd/autogpt_server/postgres/schema.prisma b/rnd/autogpt_server/postgres/schema.prisma index db1a0476c..459ccbd8c 100644 --- a/rnd/autogpt_server/postgres/schema.prisma +++ b/rnd/autogpt_server/postgres/schema.prisma @@ -12,11 +12,11 @@ generator client { // User model to mirror Auth provider users model User { - id String @id // This should match the Supabase user ID - email String @unique - name String? - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + id String @id // This should match the Supabase user ID + email String @unique + name String? + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt // Relations AgentGraphs AgentGraph[] @@ -184,8 +184,8 @@ model AgentGraphExecutionSchedule { lastUpdated DateTime @updatedAt // Link to User model - userId String - user User @relation(fields: [userId], references: [id]) + userId String? + user User? @relation(fields: [userId], references: [id]) @@index([isEnabled]) } diff --git a/rnd/autogpt_server/schema.prisma b/rnd/autogpt_server/schema.prisma index d98f645e6..01614d6ab 100644 --- a/rnd/autogpt_server/schema.prisma +++ b/rnd/autogpt_server/schema.prisma @@ -11,11 +11,11 @@ generator client { // User model to mirror Auth provider users model User { - id String @id // This should match the Supabase user ID - email String @unique - name String? - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + id String @id // This should match the Supabase user ID + email String @unique + name String? + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt // Relations AgentGraphs AgentGraph[] @@ -183,8 +183,8 @@ model AgentGraphExecutionSchedule { lastUpdated DateTime @updatedAt // Link to User model - userId String - user User @relation(fields: [userId], references: [id]) + userId String? + user User? @relation(fields: [userId], references: [id]) @@index([isEnabled]) -} \ No newline at end of file +}