def init_database()

in src/dispatch/database/manage.py [0:0]


def init_database(engine):
    """Initializes the database."""
    if not database_exists(str(config.SQLALCHEMY_DATABASE_URI)):
        create_database(str(config.SQLALCHEMY_DATABASE_URI))

    schema_name = "dispatch_core"
    if not engine.dialect.has_schema(engine, schema_name):
        with engine.connect() as connection:
            connection.execute(CreateSchema(schema_name))

    tables = get_core_tables()

    Base.metadata.create_all(engine, tables=tables)

    version_schema(script_location=config.ALEMBIC_CORE_REVISION_PATH)
    setup_fulltext_search(engine, tables)

    # setup an required database functions
    session = sessionmaker(bind=engine)
    db_session = session()

    # we create the default organization if it doesn't exist
    organization = (
        db_session.query(Organization).filter(Organization.name == "default").one_or_none()
    )
    if not organization:
        print("Creating default organization...")
        organization = Organization(
            name="default",
            slug="default",
            default=True,
            description="Default Dispatch organization.",
        )

        db_session.add(organization)
        db_session.commit()

    # we initialize the database schema
    init_schema(engine=engine, organization=organization)

    # we install all plugins
    from dispatch.common.utils.cli import install_plugins
    from dispatch.plugins.base import plugins

    install_plugins()

    for p in plugins.all():
        plugin = Plugin(
            title=p.title,
            slug=p.slug,
            type=p.type,
            version=p.version,
            author=p.author,
            author_url=p.author_url,
            multiple=p.multiple,
            description=p.description,
        )
        db_session.add(plugin)
    db_session.commit()

    # we create the default project if it doesn't exist
    project = db_session.query(Project).filter(Project.name == "default").one_or_none()
    if not project:
        print("Creating default project...")
        project = Project(
            name="default",
            default=True,
            description="Default Dispatch project.",
            organization=organization,
        )
        db_session.add(project)
        db_session.commit()

        # we initialize the project with defaults
        from dispatch.project import flows as project_flows

        print("Initializing default project...")
        project_flows.project_init_flow(
            project_id=project.id, organization_slug=organization.slug, db_session=db_session
        )