示例#1
0
def revision(message, empty=False):
    command_args = dict(
        message=message,
        autogenerate=True,
        sql=False,
        head='head',
        splice=False,
        branch_label=None,
        version_path=None,
        rev_id=None,
        depends_on=None,
    )
    env = PackageEnvironment(DEFAULT_LOCATION)

    revision_context = autogen.RevisionContext(
        env.config,
        env.script_dir,
        command_args,
    )

    def get_rev(rev, context):
        # autogen._produce_migration_diffs(context, template_args, imports)
        if not empty:
            revision_context.run_autogenerate(rev, context)
        return []

    revision_context.template_args['autonomie_version'] = autonomie_version()
    env.run_env(
        get_rev,
        as_sql=False,
        revision_context=revision_context,
        template_args=revision_context.template_args,
    )
    scripts = [script for script in revision_context.generate_scripts()]
    return scripts
示例#2
0
    def revision(self,
                 message,
                 empty=False,
                 branch='default',
                 parent='head',
                 splice=False,
                 depend=None,
                 label=None,
                 path=None):
        """Create a new revision.  By default, auto-generate operations by comparing models and database.

        :param message: description of revision
        :param empty: don't auto-generate operations
        :param branch: use this independent branch name
        :param parent: parent revision(s) of this revision
        :param splice: allow non-head parent revision
        :param depend: revision(s) this revision depends on
        :param label: label(s) to apply to this revision
        :param path: where to store this revision
        :return: list of new revisions
        """

        if parent is None:
            parent = ['head']
        elif isinstance(parent, string_types):
            parent = [parent]
        else:
            parent = [getattr(r, 'revision', r) for r in parent]

        if label is None:
            label = []
        elif isinstance(label, string_types):
            label = [label]
        else:
            label = list(label)

        # manage independent branches
        if branch:
            for i, item in enumerate(parent):
                if item in ('base', 'head'):
                    parent[i] = '{}@{}'.format(branch, item)

            if not path:
                branch_path = dict(
                    item for item in current_app.config['ALEMBIC']
                    ['version_locations']
                    if not isinstance(item, string_types)).get(branch)

                if branch_path:
                    path = branch_path

            try:
                branch_exists = any(
                    r
                    for r in self.script_directory.revision_map.get_revisions(
                        branch) if r is not None)
            except ResolutionError:
                branch_exists = False

            if not branch_exists:
                # label the first revision of a separate branch and start it from base
                label.insert(0, branch)
                parent = ['base']

        if not path:
            path = self.script_directory.dir

        # relative path is relative to app root
        if path and not os.path.isabs(path) and ':' not in path:
            path = os.path.join(current_app.root_path, path)

        revision_context = autogenerate.RevisionContext(
            self.config, self.script_directory, {
                'message': message,
                'sql': False,
                'head': parent,
                'splice': splice,
                'branch_label': label,
                'version_path': path,
                'rev_id': self.rev_id(),
                'depends_on': depend
            })

        def do_revision(revision, context):
            if empty:
                revision_context.run_no_autogenerate(revision, context)
            else:
                revision_context.run_autogenerate(revision, context)

            return []

        if not empty or util.asbool(
                self.config.get_main_option('revision_environment')):
            self.run_migrations(do_revision)

        return list(revision_context.generate_scripts())
示例#3
0
    def revision(
        self,
        message,
        empty=False,
        branch="default",
        parent="head",
        splice=False,
        depend=None,
        label=None,
        path=None,
    ):
        """Create a new revision. By default, auto-generate operations
        by comparing models and database.

        :param message: Description of revision.
        :param empty: Don't auto-generate operations.
        :param branch: Use this independent branch name.
        :param parent: Parent revision(s) of this revision.
        :param splice: Allow non-head parent revision.
        :param depend: Revision(s) this revision depends on.
        :param label: Label(s) to apply to this revision.
        :param path: Where to store this revision.
        :return: List of new revisions.
        """
        if parent is None:
            parent = ["head"]
        elif isinstance(parent, string_types):
            parent = [parent]
        else:
            parent = [getattr(r, "revision", r) for r in parent]

        if label is None:
            label = []
        elif isinstance(label, string_types):
            label = [label]
        else:
            label = list(label)

        # manage independent branches
        if branch:
            for i, item in enumerate(parent):
                if item in ("base", "head"):
                    parent[i] = "{}@{}".format(branch, item)

            if not path:
                branch_path = dict(
                    item for item in current_app.config["ALEMBIC"]
                    ["version_locations"]
                    if not isinstance(item, string_types)).get(branch)

                if branch_path:
                    path = branch_path

            try:
                branch_exists = any(
                    r
                    for r in self.script_directory.revision_map.get_revisions(
                        branch) if r is not None)
            except ResolutionError:
                branch_exists = False

            if not branch_exists:
                # label the first revision of a separate branch and start it from base
                label.insert(0, branch)
                parent = ["base"]

        if not path:
            path = self.script_directory.dir

        # relative path is relative to app root
        if path and not os.path.isabs(path) and ":" not in path:
            path = os.path.join(current_app.root_path, path)

        revision_context = autogenerate.RevisionContext(
            self.config,
            self.script_directory,
            {
                "message": message,
                "sql": False,
                "head": parent,
                "splice": splice,
                "branch_label": label,
                "version_path": path,
                "rev_id": self.rev_id(),
                "depends_on": depend,
            },
        )

        def do_revision(revision, context):
            if empty:
                revision_context.run_no_autogenerate(revision, context)
            else:
                revision_context.run_autogenerate(revision, context)

            return []

        if not empty or util.asbool(
                self.config.get_main_option("revision_environment")):
            self.run_migrations(do_revision)

        return list(revision_context.generate_scripts())
示例#4
0
    def revision(
        self,
        message=None,
        head="head",
        splice=False,
        branch_label=None,
        version_path=None,
        rev_id=None,
        depends_on=None,
        process_revision_directives=None,
    ):
        """Create a new revision file.

        :param message: string message to apply to the revision; this is the
        ``-m`` option to ``alembic revision``.

        :param head: head revision to build the new revision upon as a parent;
        this is the ``--head`` option to ``alembic revision``.

        :param splice: whether or not the new revision should be made into a
        new head of its own; is required when the given ``head`` is not itself
        a head.  This is the ``--splice`` option to ``alembic revision``.

        :param branch_label: string label to apply to the branch; this is the
        ``--branch-label`` option to ``alembic revision``.

        :param version_path: string symbol identifying a specific version path
        from the configuration; this is the ``--version-path`` option to
        ``alembic revision``.

        :param rev_id: optional revision identifier to use instead of having
        one generated; this is the ``--rev-id`` option to ``alembic revision``.

        :param depends_on: optional list of "depends on" identifiers; this is the
        ``--depends-on`` option to ``alembic revision``.

        :param process_revision_directives: this is a callable that takes the
        same form as the callable described at
        :paramref:`.EnvironmentContext.configure.process_revision_directives`;
        will be applied to the structure generated by the revision process
        where it can be altered programmatically.   Note that unlike all
        the other parameters, this option is only available via programmatic
        use of :func:`.command.revision`

        .. versionadded:: 0.9.0

        """
        command_args = dict(
            message=message,
            autogenerate=False,
            sql=False,
            head=head,
            splice=splice,
            branch_label=branch_label,
            version_path=version_path,
            rev_id=rev_id,
            depends_on=depends_on,
        )
        revision_context = autogenerate.RevisionContext(
            self.config,
            self.script_directory,
            command_args,
            process_revision_directives=process_revision_directives,
        )

        scripts = [script for script in revision_context.generate_scripts()]
        if len(scripts) == 1:
            return scripts[0]
        else:
            return scripts