Beispiel #1
0
def create_fake_entries(
    settings: configuration.Settings,
    version: versions.Version,
    stylist: style.Stylist = style.noop_stylist,
) -> None:
    """
    Write entries in the migration table for all existing migrations
    up until the given version (included).
    """
    # Fake migrations <= init_version
    known_versions = files.get_known_versions(settings=settings)
    versions_to_fake = list(utils.until(known_versions, version))
    logger.info("Will now fake all migrations up to version %s (included)",
                version)

    with stylist.activate("title") as echo:
        echo("Faking migrations.")

    for version in versions_to_fake:
        logger.info("Faking migrations from version %s", version)
        migrations_to_apply = files.get_migrations_files_mapping(
            settings=settings, version=version)
        migs = list(migrations_to_apply)
        migs.sort()
        for migration_name in migs:
            logger.info("Faking %s", migration_name)
            with stylist.checkbox(
                    content="Faking {}...".format(migration_name),
                    content_after="Faked {}".format(migration_name),
            ):
                db.write_migration(settings=settings,
                                   version=version,
                                   name=migration_name)
Beispiel #2
0
def get_closest_version(
    settings: configuration.Settings,
    target_version: Optional[versions.Version],
    sql_tpl: str,
    existing_files: Iterable[str],
    force_version: Optional[versions.Version] = None,
) -> Optional[versions.Version]:
    """
    Get the version of a file (schema or fixtures) to use to init a DB.
    Take the closest to the target_version. Can be the same version, or older.
    """
    # get known versions
    known_versions = files.get_known_versions(settings=settings)
    # find target version

    if not target_version:
        previous_versions = known_versions
    else:
        try:
            previous_versions = list(
                utils.until(known_versions, target_version))
        except ValueError:
            raise ValueError(
                "settings.TARGET_VERSION is improperly configured: "
                "version {} not found.".format(target_version))

    # should we set a version from settings ?
    if force_version:
        if force_version not in previous_versions:
            raise ValueError(
                "settings.TARGET_VERSION is improperly configured: "
                "settings.SCHEMA_VERSION is more recent.")

        file = sql_tpl.format(force_version.original_string)
        if file in existing_files:
            return force_version

        # not found
        return None

    for version in previous_versions[::-1]:
        schema_file = sql_tpl.format(version.original_string)
        if schema_file in existing_files:
            return version

    # not found
    return None
Beispiel #3
0
def build_migration_plan(
        settings: configuration.Settings,
        from_version: versions.Version) -> Iterable[Dict[str, Any]]:
    """
    Return the list of migrations by version,
    from the version used to init the DB to the current target version.
    """
    # get known versions
    known_versions = files.get_known_versions(settings=settings)
    target_version = settings.TARGET_VERSION

    # get all versions to apply
    if not target_version:
        versions_to_apply = known_versions
    else:
        try:
            versions_to_apply = list(
                utils.until(known_versions, target_version))
        except ValueError:
            raise ValueError(
                "settings.TARGET_VERSION is improperly configured: "
                "version {} not found.".format(target_version))

    versions_to_apply = list(utils.since(versions_to_apply, from_version))

    # get plan for each version to apply
    for version in versions_to_apply:
        version_plan = []
        # get applied migrations
        applied_migrations = db.get_applied_migrations(settings=settings,
                                                       version=version)
        # get migrations to apply
        migrations_to_apply = files.get_migrations_files_mapping(
            settings=settings, version=version)
        migs = list(migrations_to_apply)
        migs.sort()
        # build plan
        for mig in migs:
            applied = mig in applied_migrations
            path = migrations_to_apply[mig]
            contents = files.file_lines_generator(path)
            is_manual = files.is_manual_migration(migration_path=path,
                                                  migration_contents=contents)
            version_plan.append((mig, applied, path, is_manual))
        yield {"version": version, "plan": version_plan}
Beispiel #4
0
def test_until_error():
    with pytest.raises(ValueError):
        list(until(range(5), 10))
Beispiel #5
0
def test_until():
    values = list(until(range(300), 3))

    assert values == [0, 1, 2, 3]