コード例 #1
0
ファイル: manage.py プロジェクト: Netflix/dispatch
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))

    # set the schema for table creation
    tables = get_tenant_tables()

    schema_engine = engine.execution_options(
        schema_translate_map={
            None: schema_name,
        }
    )

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

    # put schema under version control
    version_schema(script_location=config.ALEMBIC_TENANT_REVISION_PATH)

    with engine.connect() as connection:
        # we need to map this for full text search as it uses sql literal strings
        # and schema translate map does not apply
        for t in tables:
            t.schema = schema_name

        setup_fulltext_search(connection, tables)

    session = sessionmaker(bind=schema_engine)
    db_session = session()

    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
コード例 #2
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")
コード例 #3
0
def test_create(session, organization):
    from dispatch.project.service import create
    from dispatch.project.models import ProjectCreate

    name = "name"
    description = "description"
    default = True
    color = "red"

    project_in = ProjectCreate(
        name=name,
        description=description,
        default=default,
        color=color,
        organization=organization,
    )
    project = create(db_session=session, project_in=project_in)
    assert project