LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
fix: Updated run-platform to create schema for new users (#820)
Chandrasekharan M125a9304cc30
diff --git a/backend/backend/settings/base.py b/backend/backend/settings/base.pyindex 090aa7054..8b3860853 100644--- a/backend/backend/settings/base.py+++ b/backend/backend/settings/base.py@@ -218,6 +218,8 @@ SHARED_APPS = ("plugins","feature_flag","django_celery_beat",+ # For additional helper commands+ "commands",)v2_apps = ("migrating.v2",diff --git a/backend/commands/__init__.py b/backend/commands/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/backend/commands/management/commands/__init__.py b/backend/commands/management/commands/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/backend/commands/management/commands/create_schema.py b/backend/commands/management/commands/create_schema.pynew file mode 100644index 000000000..81c2e00bc--- /dev/null+++ b/backend/commands/management/commands/create_schema.py@@ -0,0 +1,37 @@+import os++from django.core.management.base import BaseCommand+from django.db import connection++SCHEMA_NAME = os.getenv("DB_SCHEMA", None)+++class Command(BaseCommand):+ help = (+ "Create schema if it does not exist. Relies on optional argument '--schema'"+ "or env 'DB_SCHEMA' for the schema name"+ )++ def add_arguments(self, parser):+ parser.add_argument(+ "--schema",+ type=str,+ help=(+ "Optional schema name to create. Overrides env 'DB_SCHEMA' if specified"+ ),+ )++ def handle(self, *args, **kwargs):+ schema_name = kwargs["schema"] or os.getenv("DB_SCHEMA")++ if not schema_name:+ raise ValueError(+ "No schema name provided. Set 'DB_SCHEMA' in the environment "+ "or use '--schema' argument."+ )++ with connection.cursor() as cursor:+ cursor.execute(f"CREATE SCHEMA IF NOT EXISTS {schema_name}")+ self.stdout.write(+ self.style.SUCCESS(f'Schema "{schema_name}" checked/created successfully.')+ )diff --git a/backend/commands/management/commands/drop_schema.py b/backend/commands/management/commands/drop_schema.pynew file mode 100644index 000000000..cd83bdc1a--- /dev/null+++ b/backend/commands/management/commands/drop_schema.py@@ -0,0 +1,37 @@+import os++from django.core.management.base import BaseCommand+from django.db import connection++SCHEMA_NAME = os.getenv("DB_SCHEMA", None)+++class Command(BaseCommand):+ help = (+ "Drops schema if it exists. Relies on optional argument '--schema'"+ "or env 'DB_SCHEMA' for the schema to drop"+ )++ def add_arguments(self, parser):+ parser.add_argument(+ "--schema",+ type=str,+ help=(+ "Optional schema name to drop. Overrides env 'DB_SCHEMA' if specified"+ ),+ )++ def handle(self, *args, **kwargs):+ schema_name = kwargs["schema"] or os.getenv("DB_SCHEMA")++ if not schema_name:+ raise ValueError(+ "No schema name provided. Set 'DB_SCHEMA' in the environment "+ "or use '--schema' argument."+ )++ with connection.cursor() as cursor:+ cursor.execute(f"DROP SCHEMA IF EXISTS {schema_name} CASCADE")+ self.stdout.write(+ self.style.SUCCESS(f'Schema "{schema_name}" dropped successfully.')+ )diff --git a/backend/entrypoint.sh b/backend/entrypoint.shindex 686755124..1d58e8e27 100755--- a/backend/entrypoint.sh+++ b/backend/entrypoint.sh@@ -4,6 +4,11 @@ cmd=$1if [ "$cmd" = "migrate" ]; thenecho "Migration initiated".venv/bin/python manage.py migrate+elif [ "$cmd" = "prepare_and_migrate" ]; then+ echo "Creating schema in database"+ .venv/bin/python manage.py create_schema+ echo "Migration initiated"+ .venv/bin/python manage.py migratefi# NOTE: Leaving below for reference incase required in the futurediff --git a/backend/migrating/v2/README.md b/backend/migrating/v2/README.mdindex 81cd1e061..0015e68c2 100644--- a/backend/migrating/v2/README.md+++ b/backend/migrating/v2/README.md@@ -70,7 +70,7 @@ psql -h localhost -p 5432 -U unstract_dev -d unstract_db_v2 -f unstract-backup-<- Run the command supported from [/backend/manage.py](/backend/manage.py), for example in case of `v2````shell-python manage.py create_v2_schema+python manage.py create_schema```### Run schema migrations@@ -127,7 +127,7 @@ python manage.py helpTo set up the v2 schema in your database, execute:```bash-python manage.py create_v2_schema+python manage.py create_schema```This command initializes the schema required for v2 migrations.@@ -137,7 +137,7 @@ This command initializes the schema required for v2 migrations.To remove the v2 schema from your database, run:```bash-python manage.py drop_v2_schema+python manage.py drop_schema```Note: Use this command with caution as it will permanently delete the v2 schema and its associated data.diff --git a/backend/migrating/v2/management/commands/create_v2_schema.py b/backend/migrating/v2/management/commands/create_v2_schema.pydeleted file mode 100644index 5be10a121..000000000--- a/backend/migrating/v2/management/commands/create_v2_schema.py+++ /dev/null@@ -1,18 +0,0 @@-from django.core.management.base import BaseCommand-from django.db import connection-from migrating.v2.constants import V2---class Command(BaseCommand):- help = "Create v2 schema if it does not exist"-- def handle(self, *args, **kwargs):- if not V2.SCHEMA_NAME:- raise ValueError("SCHEMA_NAME is not defined.")- with connection.cursor() as cursor:- cursor.execute(f"CREATE SCHEMA IF NOT EXISTS {V2.SCHEMA_NAME}")- self.stdout.write(- self.style.SUCCESS(- f'Schema "{V2.SCHEMA_NAME}" checked/created successfully.'- )- )diff --git a/backend/migrating/v2/management/commands/drop_v2_schema.py b/backend/migrating/v2/management/commands/drop_v2_schema.pydeleted file mode 100644index 128a40119..000000000--- a/backend/migrating/v2/management/commands/drop_v2_schema.py+++ /dev/null@@ -1,16 +0,0 @@-from django.core.management.base import BaseCommand-from django.db import connection-from migrating.v2.constants import V2---class Command(BaseCommand):- help = "Drop v2 schema if it exists"-- def handle(self, *args, **kwargs):- if not V2.SCHEMA_NAME:- raise ValueError("SCHEMA_NAME is not defined.")- with connection.cursor() as cursor:- cursor.execute(f"DROP SCHEMA IF EXISTS {V2.SCHEMA_NAME} CASCADE")- self.stdout.write(- self.style.SUCCESS(f'Schema "{V2.SCHEMA_NAME}" deleted successfully.')- )diff --git a/backend/migrating/v2/migrate-to-v2.sh b/backend/migrating/v2/migrate-to-v2.shindex 18e07a50e..74b9ac55d 100755--- a/backend/migrating/v2/migrate-to-v2.sh+++ b/backend/migrating/v2/migrate-to-v2.sh@@ -12,9 +12,6 @@ red_text='\033[31m'default_text='\033[39m'yellow_text='\033[33m'-# set -x/xtrace uses PS4 for more info-PS4="$blue_text""${0}:${LINENO}: ""$default_text"-# Function to check and create virtual environmentactivate_venv() {if [[ ! -d ".venv" ]]; then@@ -37,11 +34,15 @@ echo -e "${blue_text}Configuring environment variables for the migration${defaulcd $manage_script_diractivate_venv+# This implementation is easier than docker compose run sanity checks and usage parametersecho -e "${blue_text}Running schema creation command...${default_text}"-python manage.py create_v2_schema || { echo "Schema creation failed"; exit 1; }+DB_HOST="localhost" REDIS_HOST="localhost" CELERY_BROKER_URL="redis://localhost:6379" \+python manage.py create_schema || { echo "Schema creation failed"; exit 1; }echo -e "${blue_text}Running schema migration command...${default_text}"+DB_HOST="localhost" REDIS_HOST="localhost" CELERY_BROKER_URL="redis://localhost:6379" \python manage.py migrate || { echo "Schema migration failed"; exit 1; }echo -e "${blue_text}Running data migration command...${default_text}"+DB_HOST="localhost" REDIS_HOST="localhost" CELERY_BROKER_URL="redis://localhost:6379" \SCHEMAS_TO_MIGRATE=_ALL_ python manage.py migrate_to_v2 || { echo "Data migration failed"; exit 1; }diff --git a/run-platform.sh b/run-platform.shindex 4b4a39880..32d42687e 100755--- a/run-platform.sh+++ b/run-platform.sh@@ -268,6 +268,21 @@ build_services() {fi}+create_backend_schema() {++ if [ "$first_setup" = false ]; then+ return+ fi++ pushd ${script_dir}/docker 1>/dev/null++ echo -e "$blue_text""Creating a schema for Unstract in the database""$default_text"+ VERSION=$opt_version $docker_compose_cmd run backend prepare_and_migrate+ # TODO: Run migrations here once its removed from backend's entrypoint++ popd 1>/dev/null+}+run_services() {pushd ${script_dir}/docker 1>/dev/null@@ -330,6 +345,7 @@ parse_args $*do_git_pullsetup_envbuild_services+create_backend_schemarun_services## Run Unstract platform - END