Feat/add-remote-file-upload-api (#9906)
parent
78b74cce8e
commit
9ac2bb30f4
@ -0,0 +1,6 @@
|
|||||||
|
from werkzeug.exceptions import HTTPException
|
||||||
|
|
||||||
|
|
||||||
|
class FilenameNotExistsError(HTTPException):
|
||||||
|
code = 400
|
||||||
|
description = "The specified filename does not exist."
|
||||||
@ -0,0 +1,58 @@
|
|||||||
|
import mimetypes
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import urllib.parse
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class FileInfo(BaseModel):
|
||||||
|
filename: str
|
||||||
|
extension: str
|
||||||
|
mimetype: str
|
||||||
|
size: int
|
||||||
|
|
||||||
|
|
||||||
|
def guess_file_info_from_response(response: httpx.Response):
|
||||||
|
url = str(response.url)
|
||||||
|
# Try to extract filename from URL
|
||||||
|
parsed_url = urllib.parse.urlparse(url)
|
||||||
|
url_path = parsed_url.path
|
||||||
|
filename = os.path.basename(url_path)
|
||||||
|
|
||||||
|
# If filename couldn't be extracted, use Content-Disposition header
|
||||||
|
if not filename:
|
||||||
|
content_disposition = response.headers.get("Content-Disposition")
|
||||||
|
if content_disposition:
|
||||||
|
filename_match = re.search(r'filename="?(.+)"?', content_disposition)
|
||||||
|
if filename_match:
|
||||||
|
filename = filename_match.group(1)
|
||||||
|
|
||||||
|
# If still no filename, generate a unique one
|
||||||
|
if not filename:
|
||||||
|
unique_name = str(uuid4())
|
||||||
|
filename = f"{unique_name}"
|
||||||
|
|
||||||
|
# Guess MIME type from filename first, then URL
|
||||||
|
mimetype, _ = mimetypes.guess_type(filename)
|
||||||
|
if mimetype is None:
|
||||||
|
mimetype, _ = mimetypes.guess_type(url)
|
||||||
|
if mimetype is None:
|
||||||
|
# If guessing fails, use Content-Type from response headers
|
||||||
|
mimetype = response.headers.get("Content-Type", "application/octet-stream")
|
||||||
|
|
||||||
|
extension = os.path.splitext(filename)[1]
|
||||||
|
|
||||||
|
# Ensure filename has an extension
|
||||||
|
if not extension:
|
||||||
|
extension = mimetypes.guess_extension(mimetype) or ".bin"
|
||||||
|
filename = f"{filename}{extension}"
|
||||||
|
|
||||||
|
return FileInfo(
|
||||||
|
filename=filename,
|
||||||
|
extension=extension,
|
||||||
|
mimetype=mimetype,
|
||||||
|
size=int(response.headers.get("Content-Length", -1)),
|
||||||
|
)
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
from libs.exception import BaseHTTPException
|
||||||
|
|
||||||
|
|
||||||
|
class FileTooLargeError(BaseHTTPException):
|
||||||
|
error_code = "file_too_large"
|
||||||
|
description = "File size exceeded. {message}"
|
||||||
|
code = 413
|
||||||
|
|
||||||
|
|
||||||
|
class UnsupportedFileTypeError(BaseHTTPException):
|
||||||
|
error_code = "unsupported_file_type"
|
||||||
|
description = "File type not allowed."
|
||||||
|
code = 415
|
||||||
|
|
||||||
|
|
||||||
|
class TooManyFilesError(BaseHTTPException):
|
||||||
|
error_code = "too_many_files"
|
||||||
|
description = "Only one file is allowed."
|
||||||
|
code = 400
|
||||||
|
|
||||||
|
|
||||||
|
class NoFileUploadedError(BaseHTTPException):
|
||||||
|
error_code = "no_file_uploaded"
|
||||||
|
description = "Please upload your file."
|
||||||
|
code = 400
|
||||||
@ -0,0 +1,71 @@
|
|||||||
|
import urllib.parse
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
|
from flask_login import current_user
|
||||||
|
from flask_restful import Resource, marshal_with, reqparse
|
||||||
|
|
||||||
|
from controllers.common import helpers
|
||||||
|
from core.file import helpers as file_helpers
|
||||||
|
from core.helper import ssrf_proxy
|
||||||
|
from fields.file_fields import file_fields_with_signed_url, remote_file_info_fields
|
||||||
|
from models.account import Account
|
||||||
|
from services.file_service import FileService
|
||||||
|
|
||||||
|
|
||||||
|
class RemoteFileInfoApi(Resource):
|
||||||
|
@marshal_with(remote_file_info_fields)
|
||||||
|
def get(self, url):
|
||||||
|
decoded_url = urllib.parse.unquote(url)
|
||||||
|
try:
|
||||||
|
response = ssrf_proxy.head(decoded_url)
|
||||||
|
return {
|
||||||
|
"file_type": response.headers.get("Content-Type", "application/octet-stream"),
|
||||||
|
"file_length": int(response.headers.get("Content-Length", 0)),
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
return {"error": str(e)}, 400
|
||||||
|
|
||||||
|
|
||||||
|
class RemoteFileUploadApi(Resource):
|
||||||
|
@marshal_with(file_fields_with_signed_url)
|
||||||
|
def post(self):
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument("url", type=str, required=True, help="URL is required")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
url = args["url"]
|
||||||
|
|
||||||
|
response = ssrf_proxy.head(url)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
file_info = helpers.guess_file_info_from_response(response)
|
||||||
|
|
||||||
|
if not FileService.is_file_size_within_limit(extension=file_info.extension, file_size=file_info.size):
|
||||||
|
return {"error": "File size exceeded"}, 400
|
||||||
|
|
||||||
|
response = ssrf_proxy.get(url)
|
||||||
|
response.raise_for_status()
|
||||||
|
content = response.content
|
||||||
|
|
||||||
|
try:
|
||||||
|
user = cast(Account, current_user)
|
||||||
|
upload_file = FileService.upload_file(
|
||||||
|
filename=file_info.filename,
|
||||||
|
content=content,
|
||||||
|
mimetype=file_info.mimetype,
|
||||||
|
user=user,
|
||||||
|
source_url=url,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return {"error": str(e)}, 400
|
||||||
|
|
||||||
|
return {
|
||||||
|
"id": upload_file.id,
|
||||||
|
"name": upload_file.name,
|
||||||
|
"size": upload_file.size,
|
||||||
|
"extension": upload_file.extension,
|
||||||
|
"url": file_helpers.get_signed_file_url(upload_file_id=upload_file.id),
|
||||||
|
"mime_type": upload_file.mime_type,
|
||||||
|
"created_by": upload_file.created_by,
|
||||||
|
"created_at": upload_file.created_at,
|
||||||
|
}, 201
|
||||||
@ -1,56 +0,0 @@
|
|||||||
import urllib.parse
|
|
||||||
|
|
||||||
from flask import request
|
|
||||||
from flask_restful import marshal_with, reqparse
|
|
||||||
|
|
||||||
import services
|
|
||||||
from controllers.web import api
|
|
||||||
from controllers.web.error import FileTooLargeError, NoFileUploadedError, TooManyFilesError, UnsupportedFileTypeError
|
|
||||||
from controllers.web.wraps import WebApiResource
|
|
||||||
from core.helper import ssrf_proxy
|
|
||||||
from fields.file_fields import file_fields, remote_file_info_fields
|
|
||||||
from services.file_service import FileService
|
|
||||||
|
|
||||||
|
|
||||||
class FileApi(WebApiResource):
|
|
||||||
@marshal_with(file_fields)
|
|
||||||
def post(self, app_model, end_user):
|
|
||||||
# get file from request
|
|
||||||
file = request.files["file"]
|
|
||||||
|
|
||||||
parser = reqparse.RequestParser()
|
|
||||||
parser.add_argument("source", type=str, required=False, location="args")
|
|
||||||
source = parser.parse_args().get("source")
|
|
||||||
|
|
||||||
# check file
|
|
||||||
if "file" not in request.files:
|
|
||||||
raise NoFileUploadedError()
|
|
||||||
|
|
||||||
if len(request.files) > 1:
|
|
||||||
raise TooManyFilesError()
|
|
||||||
try:
|
|
||||||
upload_file = FileService.upload_file(file=file, user=end_user, source=source)
|
|
||||||
except services.errors.file.FileTooLargeError as file_too_large_error:
|
|
||||||
raise FileTooLargeError(file_too_large_error.description)
|
|
||||||
except services.errors.file.UnsupportedFileTypeError:
|
|
||||||
raise UnsupportedFileTypeError()
|
|
||||||
|
|
||||||
return upload_file, 201
|
|
||||||
|
|
||||||
|
|
||||||
class RemoteFileInfoApi(WebApiResource):
|
|
||||||
@marshal_with(remote_file_info_fields)
|
|
||||||
def get(self, url):
|
|
||||||
decoded_url = urllib.parse.unquote(url)
|
|
||||||
try:
|
|
||||||
response = ssrf_proxy.head(decoded_url)
|
|
||||||
return {
|
|
||||||
"file_type": response.headers.get("Content-Type", "application/octet-stream"),
|
|
||||||
"file_length": int(response.headers.get("Content-Length", -1)),
|
|
||||||
}
|
|
||||||
except Exception as e:
|
|
||||||
return {"error": str(e)}, 400
|
|
||||||
|
|
||||||
|
|
||||||
api.add_resource(FileApi, "/files/upload")
|
|
||||||
api.add_resource(RemoteFileInfoApi, "/remote-files/<path:url>")
|
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
from flask import request
|
||||||
|
from flask_restful import marshal_with
|
||||||
|
|
||||||
|
import services
|
||||||
|
from controllers.common.errors import FilenameNotExistsError
|
||||||
|
from controllers.web.error import FileTooLargeError, NoFileUploadedError, TooManyFilesError, UnsupportedFileTypeError
|
||||||
|
from controllers.web.wraps import WebApiResource
|
||||||
|
from fields.file_fields import file_fields
|
||||||
|
from services.file_service import FileService
|
||||||
|
|
||||||
|
|
||||||
|
class FileApi(WebApiResource):
|
||||||
|
@marshal_with(file_fields)
|
||||||
|
def post(self, app_model, end_user):
|
||||||
|
file = request.files["file"]
|
||||||
|
source = request.form.get("source")
|
||||||
|
|
||||||
|
if "file" not in request.files:
|
||||||
|
raise NoFileUploadedError()
|
||||||
|
|
||||||
|
if len(request.files) > 1:
|
||||||
|
raise TooManyFilesError()
|
||||||
|
|
||||||
|
if not file.filename:
|
||||||
|
raise FilenameNotExistsError
|
||||||
|
|
||||||
|
if source not in ("datasets", None):
|
||||||
|
source = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
upload_file = FileService.upload_file(
|
||||||
|
filename=file.filename,
|
||||||
|
content=file.read(),
|
||||||
|
mimetype=file.mimetype,
|
||||||
|
user=end_user,
|
||||||
|
source=source,
|
||||||
|
)
|
||||||
|
except services.errors.file.FileTooLargeError as file_too_large_error:
|
||||||
|
raise FileTooLargeError(file_too_large_error.description)
|
||||||
|
except services.errors.file.UnsupportedFileTypeError:
|
||||||
|
raise UnsupportedFileTypeError()
|
||||||
|
|
||||||
|
return upload_file, 201
|
||||||
@ -0,0 +1,69 @@
|
|||||||
|
import urllib.parse
|
||||||
|
|
||||||
|
from flask_login import current_user
|
||||||
|
from flask_restful import marshal_with, reqparse
|
||||||
|
|
||||||
|
from controllers.common import helpers
|
||||||
|
from controllers.web.wraps import WebApiResource
|
||||||
|
from core.file import helpers as file_helpers
|
||||||
|
from core.helper import ssrf_proxy
|
||||||
|
from fields.file_fields import file_fields_with_signed_url, remote_file_info_fields
|
||||||
|
from services.file_service import FileService
|
||||||
|
|
||||||
|
|
||||||
|
class RemoteFileInfoApi(WebApiResource):
|
||||||
|
@marshal_with(remote_file_info_fields)
|
||||||
|
def get(self, url):
|
||||||
|
decoded_url = urllib.parse.unquote(url)
|
||||||
|
try:
|
||||||
|
response = ssrf_proxy.head(decoded_url)
|
||||||
|
return {
|
||||||
|
"file_type": response.headers.get("Content-Type", "application/octet-stream"),
|
||||||
|
"file_length": int(response.headers.get("Content-Length", -1)),
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
return {"error": str(e)}, 400
|
||||||
|
|
||||||
|
|
||||||
|
class RemoteFileUploadApi(WebApiResource):
|
||||||
|
@marshal_with(file_fields_with_signed_url)
|
||||||
|
def post(self):
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument("url", type=str, required=True, help="URL is required")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
url = args["url"]
|
||||||
|
|
||||||
|
response = ssrf_proxy.head(url)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
file_info = helpers.guess_file_info_from_response(response)
|
||||||
|
|
||||||
|
if not FileService.is_file_size_within_limit(extension=file_info.extension, file_size=file_info.size):
|
||||||
|
return {"error": "File size exceeded"}, 400
|
||||||
|
|
||||||
|
response = ssrf_proxy.get(url)
|
||||||
|
response.raise_for_status()
|
||||||
|
content = response.content
|
||||||
|
|
||||||
|
try:
|
||||||
|
upload_file = FileService.upload_file(
|
||||||
|
filename=file_info.filename,
|
||||||
|
content=content,
|
||||||
|
mimetype=file_info.mimetype,
|
||||||
|
user=current_user,
|
||||||
|
source_url=url,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return {"error": str(e)}, 400
|
||||||
|
|
||||||
|
return {
|
||||||
|
"id": upload_file.id,
|
||||||
|
"name": upload_file.name,
|
||||||
|
"size": upload_file.size,
|
||||||
|
"extension": upload_file.extension,
|
||||||
|
"url": file_helpers.get_signed_file_url(upload_file_id=upload_file.id),
|
||||||
|
"mime_type": upload_file.mime_type,
|
||||||
|
"created_by": upload_file.created_by,
|
||||||
|
"created_at": upload_file.created_at,
|
||||||
|
}, 201
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
"""Add upload_files.source_url
|
||||||
|
|
||||||
|
Revision ID: d3f6769a94a3
|
||||||
|
Revises: 43fa78bc3b7d
|
||||||
|
Create Date: 2024-11-01 04:34:23.816198
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import models as models
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'd3f6769a94a3'
|
||||||
|
down_revision = '43fa78bc3b7d'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('upload_files', schema=None) as batch_op:
|
||||||
|
batch_op.add_column(sa.Column('source_url', sa.String(length=255), server_default='', nullable=False))
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('upload_files', schema=None) as batch_op:
|
||||||
|
batch_op.drop_column('source_url')
|
||||||
|
# ### end Alembic commands ###
|
||||||
@ -0,0 +1,52 @@
|
|||||||
|
"""rename conversation variables index name
|
||||||
|
|
||||||
|
Revision ID: 93ad8c19c40b
|
||||||
|
Revises: d3f6769a94a3
|
||||||
|
Create Date: 2024-11-01 04:49:53.100250
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '93ad8c19c40b'
|
||||||
|
down_revision = 'd3f6769a94a3'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
conn = op.get_bind()
|
||||||
|
if conn.dialect.name == 'postgresql':
|
||||||
|
# Rename indexes for PostgreSQL
|
||||||
|
op.execute('ALTER INDEX workflow__conversation_variables_app_id_idx RENAME TO workflow_conversation_variables_app_id_idx')
|
||||||
|
op.execute('ALTER INDEX workflow__conversation_variables_created_at_idx RENAME TO workflow_conversation_variables_created_at_idx')
|
||||||
|
else:
|
||||||
|
# For other databases, use the original drop and create method
|
||||||
|
with op.batch_alter_table('workflow_conversation_variables', schema=None) as batch_op:
|
||||||
|
batch_op.drop_index('workflow__conversation_variables_app_id_idx')
|
||||||
|
batch_op.drop_index('workflow__conversation_variables_created_at_idx')
|
||||||
|
batch_op.create_index(batch_op.f('workflow_conversation_variables_app_id_idx'), ['app_id'], unique=False)
|
||||||
|
batch_op.create_index(batch_op.f('workflow_conversation_variables_created_at_idx'), ['created_at'], unique=False)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
conn = op.get_bind()
|
||||||
|
if conn.dialect.name == 'postgresql':
|
||||||
|
# Rename indexes back for PostgreSQL
|
||||||
|
op.execute('ALTER INDEX workflow_conversation_variables_app_id_idx RENAME TO workflow__conversation_variables_app_id_idx')
|
||||||
|
op.execute('ALTER INDEX workflow_conversation_variables_created_at_idx RENAME TO workflow__conversation_variables_created_at_idx')
|
||||||
|
else:
|
||||||
|
# For other databases, use the original drop and create method
|
||||||
|
with op.batch_alter_table('workflow_conversation_variables', schema=None) as batch_op:
|
||||||
|
batch_op.drop_index(batch_op.f('workflow_conversation_variables_created_at_idx'))
|
||||||
|
batch_op.drop_index(batch_op.f('workflow_conversation_variables_app_id_idx'))
|
||||||
|
batch_op.create_index('workflow__conversation_variables_created_at_idx', ['created_at'], unique=False)
|
||||||
|
batch_op.create_index('workflow__conversation_variables_app_id_idx', ['app_id'], unique=False)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
@ -0,0 +1,41 @@
|
|||||||
|
"""update upload_files.source_url
|
||||||
|
|
||||||
|
Revision ID: f4d7ce70a7ca
|
||||||
|
Revises: 93ad8c19c40b
|
||||||
|
Create Date: 2024-11-01 05:40:03.531751
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import models as models
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'f4d7ce70a7ca'
|
||||||
|
down_revision = '93ad8c19c40b'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('upload_files', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('source_url',
|
||||||
|
existing_type=sa.VARCHAR(length=255),
|
||||||
|
type_=sa.TEXT(),
|
||||||
|
existing_nullable=False,
|
||||||
|
existing_server_default=sa.text("''::character varying"))
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('upload_files', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('source_url',
|
||||||
|
existing_type=sa.TEXT(),
|
||||||
|
type_=sa.VARCHAR(length=255),
|
||||||
|
existing_nullable=False,
|
||||||
|
existing_server_default=sa.text("''::character varying"))
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
@ -0,0 +1,67 @@
|
|||||||
|
"""update type of custom_disclaimer to TEXT
|
||||||
|
|
||||||
|
Revision ID: d07474999927
|
||||||
|
Revises: f4d7ce70a7ca
|
||||||
|
Create Date: 2024-11-01 06:22:27.981398
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import models as models
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'd07474999927'
|
||||||
|
down_revision = 'f4d7ce70a7ca'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.execute("UPDATE recommended_apps SET custom_disclaimer = '' WHERE custom_disclaimer IS NULL")
|
||||||
|
op.execute("UPDATE sites SET custom_disclaimer = '' WHERE custom_disclaimer IS NULL")
|
||||||
|
op.execute("UPDATE tool_api_providers SET custom_disclaimer = '' WHERE custom_disclaimer IS NULL")
|
||||||
|
|
||||||
|
with op.batch_alter_table('recommended_apps', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('custom_disclaimer',
|
||||||
|
existing_type=sa.VARCHAR(length=255),
|
||||||
|
type_=sa.TEXT(),
|
||||||
|
nullable=False)
|
||||||
|
|
||||||
|
with op.batch_alter_table('sites', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('custom_disclaimer',
|
||||||
|
existing_type=sa.VARCHAR(length=255),
|
||||||
|
type_=sa.TEXT(),
|
||||||
|
nullable=False)
|
||||||
|
|
||||||
|
with op.batch_alter_table('tool_api_providers', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('custom_disclaimer',
|
||||||
|
existing_type=sa.VARCHAR(length=255),
|
||||||
|
type_=sa.TEXT(),
|
||||||
|
nullable=False)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('tool_api_providers', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('custom_disclaimer',
|
||||||
|
existing_type=sa.TEXT(),
|
||||||
|
type_=sa.VARCHAR(length=255),
|
||||||
|
nullable=True)
|
||||||
|
|
||||||
|
with op.batch_alter_table('sites', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('custom_disclaimer',
|
||||||
|
existing_type=sa.TEXT(),
|
||||||
|
type_=sa.VARCHAR(length=255),
|
||||||
|
nullable=True)
|
||||||
|
|
||||||
|
with op.batch_alter_table('recommended_apps', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('custom_disclaimer',
|
||||||
|
existing_type=sa.TEXT(),
|
||||||
|
type_=sa.VARCHAR(length=255),
|
||||||
|
nullable=True)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
"""update workflows graph, features and updated_at
|
||||||
|
|
||||||
|
Revision ID: 09a8d1878d9b
|
||||||
|
Revises: d07474999927
|
||||||
|
Create Date: 2024-11-01 06:23:59.579186
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import models as models
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '09a8d1878d9b'
|
||||||
|
down_revision = 'd07474999927'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('conversations', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('inputs',
|
||||||
|
existing_type=postgresql.JSON(astext_type=sa.Text()),
|
||||||
|
nullable=False)
|
||||||
|
|
||||||
|
with op.batch_alter_table('messages', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('inputs',
|
||||||
|
existing_type=postgresql.JSON(astext_type=sa.Text()),
|
||||||
|
nullable=False)
|
||||||
|
|
||||||
|
op.execute("UPDATE workflows SET updated_at = created_at WHERE updated_at IS NULL")
|
||||||
|
op.execute("UPDATE workflows SET graph = '' WHERE graph IS NULL")
|
||||||
|
op.execute("UPDATE workflows SET features = '' WHERE features IS NULL")
|
||||||
|
|
||||||
|
with op.batch_alter_table('workflows', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('graph',
|
||||||
|
existing_type=sa.TEXT(),
|
||||||
|
nullable=False)
|
||||||
|
batch_op.alter_column('features',
|
||||||
|
existing_type=sa.TEXT(),
|
||||||
|
type_=sa.String(),
|
||||||
|
nullable=False)
|
||||||
|
batch_op.alter_column('updated_at',
|
||||||
|
existing_type=postgresql.TIMESTAMP(),
|
||||||
|
nullable=False)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('workflows', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('updated_at',
|
||||||
|
existing_type=postgresql.TIMESTAMP(),
|
||||||
|
nullable=True)
|
||||||
|
batch_op.alter_column('features',
|
||||||
|
existing_type=sa.String(),
|
||||||
|
type_=sa.TEXT(),
|
||||||
|
nullable=True)
|
||||||
|
batch_op.alter_column('graph',
|
||||||
|
existing_type=sa.TEXT(),
|
||||||
|
nullable=True)
|
||||||
|
|
||||||
|
with op.batch_alter_table('messages', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('inputs',
|
||||||
|
existing_type=postgresql.JSON(astext_type=sa.Text()),
|
||||||
|
nullable=True)
|
||||||
|
|
||||||
|
with op.batch_alter_table('conversations', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('inputs',
|
||||||
|
existing_type=postgresql.JSON(astext_type=sa.Text()),
|
||||||
|
nullable=True)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
Loading…
Reference in New Issue