diff --git a/src/dispatch/database/manage.py b/src/dispatch/database/manage.py index f6c85cd87980..1920d1976960 100644 --- a/src/dispatch/database/manage.py +++ b/src/dispatch/database/manage.py @@ -10,8 +10,6 @@ from dispatch import config from dispatch.organization.models import Organization -from dispatch.project import service as project_service -from dispatch.project.models import ProjectCreate from dispatch.search import fulltext from dispatch.search.fulltext import ( sync_trigger, @@ -92,8 +90,8 @@ def init_database(engine): def init_schema(*, engine, organization: Organization): """Initializes a new schema.""" - schema_name = f"{DISPATCH_ORGANIZATION_SCHEMA_PREFIX}_{organization.slug}" + if not engine.dialect.has_schema(engine, schema_name): with engine.connect() as connection: connection.execute(CreateSchema(schema_name)) @@ -125,19 +123,6 @@ def init_schema(*, engine, organization: Organization): organization = db_session.merge(organization) db_session.add(organization) - - # create any required default values in schema here - # - # - project_service.get_or_create( - db_session=db_session, - project_in=ProjectCreate( - name="default", - default=True, - description="Default Dispatch project.", - organization=organization, - ), - ) db_session.commit() return organization diff --git a/src/dispatch/organization/views.py b/src/dispatch/organization/views.py index 071903304db4..6fb9cb73f4f2 100644 --- a/src/dispatch/organization/views.py +++ b/src/dispatch/organization/views.py @@ -4,19 +4,20 @@ from sqlalchemy.orm import Session from sqlalchemy.exc import IntegrityError -from dispatch.database.core import get_db -from dispatch.database.service import common_parameters, search_filter_sort_paginate - -from dispatch.enums import UserRoles from dispatch.auth.models import DispatchUser -from dispatch.auth.service import get_current_user - from dispatch.auth.permissions import ( OrganizationOwnerPermission, PermissionsDependency, ) +from dispatch.auth.service import get_current_user +from dispatch.database.core import get_db +from dispatch.database.service import common_parameters, search_filter_sort_paginate +from dispatch.enums import UserRoles from dispatch.exceptions import ExistsError from dispatch.models import PrimaryKey +from dispatch.project import flows as project_flows +from dispatch.project import service as project_service +from dispatch.project.models import ProjectCreate from .models import ( OrganizationCreate, @@ -54,14 +55,28 @@ def create_organization( detail=[{"msg": "An organization with this name already exists."}], ) - # create organization + # we create the organization organization = create(db_session=db_session, organization_in=organization_in) - # add creator as organization owner + # we add the creator as organization owner add_user( db_session=db_session, organization=organization, user=current_user, role=UserRoles.owner ) + # we create the default project + project_in = ProjectCreate( + name="default", + default=True, + description="Default Dispatch project.", + organization=organization, + ) + project = project_service.create(db_session=db_session, project_in=project_in) + + # we initialize the default project + project_flows.project_init_flow( + project_id=project.id, organization_slug=organization.slug, db_session=db_session + ) + return organization diff --git a/src/dispatch/project/flows.py b/src/dispatch/project/flows.py index 06f2b9cfccad..70e3304924e1 100644 --- a/src/dispatch/project/flows.py +++ b/src/dispatch/project/flows.py @@ -21,7 +21,8 @@ @background_task -def project_create_flow(*, organization_slug: str, project_id: int, db_session=None): +def project_init_flow(*, project_id: int, organization_slug: str, db_session=None): + """Initializes a new project with default settings.""" project = get(db_session=db_session, project_id=project_id) # Add all plugins in disabled mode diff --git a/src/dispatch/project/views.py b/src/dispatch/project/views.py index 994dac3eb6a8..70daba218a46 100644 --- a/src/dispatch/project/views.py +++ b/src/dispatch/project/views.py @@ -14,7 +14,7 @@ from dispatch.exceptions import ExistsError from dispatch.models import OrganizationSlug, PrimaryKey -from .flows import project_create_flow +from .flows import project_init_flow from .models import ( ProjectCreate, ProjectRead, @@ -56,7 +56,7 @@ def create_project( project = create(db_session=db_session, project_in=project_in) background_tasks.add_task( - project_create_flow, project_id=project.id, organization_slug=organization + project_init_flow, project_id=project.id, organization_slug=organization ) return project