feat(rnd): Add postgres schema check on linting step + auto gen migration and schema sync command (poetry run schema) (#7667)

### Background

Migration not being synced has caused issues and CI is not catching this yet.

### Changes 🏗️

* Added schema check in both sqlite & postgres prisma.schema on linting step.
* Introduced `poetry run schema` for syncing prisma.schema + generating migration in both files.
This commit is contained in:
Zamil Majdy
2024-08-01 17:11:07 +04:00
committed by GitHub
parent 08905d71f9
commit c9d41e69bd
4 changed files with 39 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
import os
import re
import subprocess
directory = os.path.dirname(os.path.realpath(__file__))
@@ -19,9 +20,43 @@ def lint():
print("Lint failed, try running `poetry run format` to fix the issues: ", e)
raise e
try:
run("schema_lint")
except subprocess.CalledProcessError as e:
print("Lint failed, try running `poetry run schema` to fix the issues: ", e)
raise e
def format():
run("ruff", "check", "--fix", ".")
run("isort", "--profile", "black", ".")
run("black", ".")
run("pyright", ".")
def schema():
file = os.path.join(directory, "schema.prisma")
text = open(file, "r").read()
text = re.sub(r'provider\s+=\s+".*"', 'provider = "postgresql"', text, 1)
text = re.sub(r'url\s+=\s+".*"', 'url = env("DATABASE_URL")', text, 1)
text = "// THIS FILE IS AUTO-GENERATED, RUN `poetry run schema` TO UPDATE\n" + text
with open(os.path.join(directory, "postgres", "schema.prisma"), "w") as f:
f.write(text)
run("prisma", "format", "--schema", "schema.prisma")
run("prisma", "format", "--schema", "postgres/schema.prisma")
run("prisma", "migrate", "dev", "--schema", "schema.prisma")
run("prisma", "migrate", "dev", "--schema", "postgres/schema.prisma")
def schema_lint():
def read_schema(path: str) -> list[str]:
with open(path, "r") as f:
return [v for v in f.read().splitlines() if not v.startswith("//")]
sqlite_schema = read_schema(os.path.join(directory, "schema.prisma"))
postgres_schema = read_schema(os.path.join(directory, "postgres", "schema.prisma"))
diff = [line.strip() for line in set(sqlite_schema) ^ set(postgres_schema)]
if line := next((v for v in diff if not v.startswith(("provider", "url"))), None):
raise Exception(f"schema.prisma & postgres/schema.prisma mismatch: {line}")

View File

@@ -1,3 +1,4 @@
// THIS FILE IS AUTO-GENERATED, RUN `poetry run schema` TO UPDATE
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")

View File

@@ -61,6 +61,8 @@ app = "autogpt_server.app:main"
cli = "autogpt_server.cli:main"
format = "linter:format"
lint = "linter:lint"
schema = "linter:schema"
schema_lint = "linter:schema_lint"
# https://poethepoet.natn.io/index.html
[tool.poe]

View File

@@ -65,7 +65,7 @@ model AgentNodeLink {
agentNodeSinkId String
AgentNodeSink AgentNode @relation("AgentNodeSink", fields: [agentNodeSinkId], references: [id])
sinkName String
// Default: the data coming from the source can only be consumed by the sink once, Static: input data will be reused.
isStatic Boolean @default(false)
}