コード例 #1
0
def init_database():
    """Initializes a new database."""
    from sqlalchemy_utils import create_database, database_exists
    from dispatch.database.core import SessionLocal
    from dispatch.organization.models import OrganizationCreate
    from dispatch.organization import service as organization_service
    from dispatch.project.models import ProjectCreate
    from dispatch.project import service as project_service

    db_session = SessionLocal()

    if not database_exists(str(config.SQLALCHEMY_DATABASE_URI)):
        create_database(str(config.SQLALCHEMY_DATABASE_URI))
    Base.metadata.create_all(engine)
    alembic_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                "alembic.ini")
    alembic_cfg = AlembicConfig(alembic_path)
    alembic_command.stamp(alembic_cfg, "head")

    sync_triggers()

    # create any required default values in database

    # default organization
    click.secho("Creating default organization...", fg="blue")
    default_org = organization_service.get_or_create(
        db_session=db_session,
        organization_in=OrganizationCreate(
            name="default",
            default=True,
            description="Default dispatch organization.",
        ),
    )
    click.secho("Creating default project...", fg="blue")
    project_service.get_or_create(
        db_session=db_session,
        project_in=ProjectCreate(
            name="default",
            default=True,
            description="Default dispatch project.",
            organization=default_org,
        ),
    )

    click.secho("Success.", fg="green")
コード例 #2
0
def test_create(session):
    from dispatch.organization.service import create
    from dispatch.organization.models import OrganizationCreate

    name = "name"
    description = "description"
    default = True
    banner_enabled = True
    banner_color = "red"
    banner_text = "banner"

    organization_in = OrganizationCreate(
        name=name,
        description=description,
        default=default,
        banner_enabled=banner_enabled,
        banner_color=banner_color,
        banner_text=banner_text,
    )
    organization = create(db_session=session, organization_in=organization_in)
    assert organization