Skip to content

# Alembic

The tool to check the availability or syntax of domain, IP or URL.

::

    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provides our very own alembic interface.

Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks: https://pyfunceble.github.io/special-thanks.html

Contributors: https://pyfunceble.github.io/contributors.html

Project link: https://github.com/funilrys/PyFunceble

Project documentation: https://docs.pyfunceble.com

Project homepage: https://pyfunceble.github.io/

License: ::

    Copyright 2017, 2018, 2019, 2020, 2022, 2023, 2024 Nissar Chababy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

## `Alembic`

Provides our very own alambic handler.

Source code in `PyFunceble/cli/migrators/alembic.py`

     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222

    class Alembic:
        """
        Provides our very own alambic handler.
        """

        db_session: Optional[Session] = None
        migrator_base: Optional[DBMigratorBase] = None

        alembic_config: Optional[alembic.config.Config] = None

        def __init__(self, db_session: Session) -> None:
            self.db_session = db_session

            self.migrator_base = DBMigratorBase()
            self.migrator_base.db_session = db_session

        def execute_if_authorized(default: Any = None):  # pylint: disable=no-self-argument
            """
            Executes the decorated method only if we are authorized to process.
            Otherwise, apply the given :code:`default`.
            """

            def inner_metdhod(func):
                @functools.wraps(func)
                def wrapper(self, *args, **kwargs):
                    if self.authorized:
                        return func(self, *args, **kwargs)  # pylint: disable=not-callable
                    return self if default is None else default

                return wrapper

            return inner_metdhod

        @property
        def authorized(self):
            """
            Provides the authorization to process.
            """

            # Here we explicitly start because the usage of alembic may be out
            # of our running context.
            return PyFunceble.cli.facility.CredentialLoader.is_already_loaded()

        @property
        def migration_directory(self) -> str:
            """
            Provides the location of our migration directory.
            """

            with package_resources.path(
                f"PyFunceble.data.{PyFunceble.cli.storage.ALEMBIC_DIRECTORY_NAME}",
                "__init__.py",
            ) as file_path:
                result = os.path.split(file_path)[0]

            if PyFunceble.storage.CONFIGURATION.cli_testing.db_type == "postgresql":
                return os.path.join(result, "postgresql")
            return os.path.join(result, "mysql")

        @execute_if_authorized(None)
        def configure(self) -> "Alembic":
            """
            Configure our alembic configuration based on what we need.
            """

            if self.alembic_config is None:
                self.alembic_config = alembic.config.Config()

            self.alembic_config.set_main_option("script_location", self.migration_directory)
            self.alembic_config.set_main_option(
                "sqlalchemy.url",
                PyFunceble.cli.facility.CredentialLoader.get_uri(),
            )

            return self

        def is_revision_different(self, revision: str) -> bool:
            """
            Checks if the given revision is already set.

            :param revision:
                The revision to check
            """

            revision_id = (
                ScriptDirectory.from_config(self.alembic_config)
                .get_revision(revision)
                .revision
            )

            statement = text(
                "SELECT * from alembic_version WHERE version_num = :db_revision"
            )

            result = self.db_session.execute(statement, {"db_revision": revision_id})

            return result.fetchone() is None

        @execute_if_authorized(None)
        def upgrade(self, revision: str = "head") -> "Alembic":
            """
            Upgrades the database structure.

            :param revision:
                The revision to apply.
            """

            self.configure()

            if not self.migrator_base.does_table_exists(
                "alembic_version"
            ) or self.is_revision_different(revision):
                PyFunceble.facility.Logger.info(
                    "Started update (%r) of the database schema(s).", revision
                )

                alembic_command.upgrade(self.alembic_config, revision)

                PyFunceble.facility.Logger.info(
                    "Finished update (%r) of the database schema(s).", revision
                )

        @execute_if_authorized(None)
        def downgrade(self, revision: str = "head") -> "Alembic":
            """
            Upgrades the database structure.

            :param revision:
                The revision to apply.
            """

            self.configure()

            if not self.migrator_base.does_table_exists(
                "alembic_version"
            ) or self.is_revision_different(revision):
                PyFunceble.facility.Logger.info(
                    "Started downgrade (%r) of the database schema(s).", revision
                )

                alembic_command.downgrade(self.alembic_config, revision)

                PyFunceble.facility.Logger.info(
                    "Finished downgrade (%r) of the database schema(s).", revision
                )

### `authorized` `property`

Provides the authorization to process.

### `migration_directory: str` `property`

Provides the location of our migration directory.

### `configure()`

Configure our alembic configuration based on what we need.

Source code in `PyFunceble/cli/migrators/alembic.py`

    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152

    @execute_if_authorized(None)
    def configure(self) -> "Alembic":
        """
        Configure our alembic configuration based on what we need.
        """

        if self.alembic_config is None:
            self.alembic_config = alembic.config.Config()

        self.alembic_config.set_main_option("script_location", self.migration_directory)
        self.alembic_config.set_main_option(
            "sqlalchemy.url",
            PyFunceble.cli.facility.CredentialLoader.get_uri(),
        )

        return self

### `downgrade(revision='head')`

Upgrades the database structure.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`revision` | `str` | The revision to apply. | `'head'`

Source code in `PyFunceble/cli/migrators/alembic.py`

    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222

    @execute_if_authorized(None)
    def downgrade(self, revision: str = "head") -> "Alembic":
        """
        Upgrades the database structure.

        :param revision:
            The revision to apply.
        """

        self.configure()

        if not self.migrator_base.does_table_exists(
            "alembic_version"
        ) or self.is_revision_different(revision):
            PyFunceble.facility.Logger.info(
                "Started downgrade (%r) of the database schema(s).", revision
            )

            alembic_command.downgrade(self.alembic_config, revision)

            PyFunceble.facility.Logger.info(
                "Finished downgrade (%r) of the database schema(s).", revision
            )

### `execute_if_authorized(default=None)`

Executes the decorated method only if we are authorized to process. Otherwise, apply the given :code:`default`.

Source code in `PyFunceble/cli/migrators/alembic.py`

     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109

    def execute_if_authorized(default: Any = None):  # pylint: disable=no-self-argument
        """
        Executes the decorated method only if we are authorized to process.
        Otherwise, apply the given :code:`default`.
        """

        def inner_metdhod(func):
            @functools.wraps(func)
            def wrapper(self, *args, **kwargs):
                if self.authorized:
                    return func(self, *args, **kwargs)  # pylint: disable=not-callable
                return self if default is None else default

            return wrapper

        return inner_metdhod

### `is_revision_different(revision)`

Checks if the given revision is already set.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`revision` | `str` | The revision to check | required

Source code in `PyFunceble/cli/migrators/alembic.py`

    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174

    def is_revision_different(self, revision: str) -> bool:
        """
        Checks if the given revision is already set.

        :param revision:
            The revision to check
        """

        revision_id = (
            ScriptDirectory.from_config(self.alembic_config)
            .get_revision(revision)
            .revision
        )

        statement = text(
            "SELECT * from alembic_version WHERE version_num = :db_revision"
        )

        result = self.db_session.execute(statement, {"db_revision": revision_id})

        return result.fetchone() is None

### `upgrade(revision='head')`

Upgrades the database structure.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`revision` | `str` | The revision to apply. | `'head'`

Source code in `PyFunceble/cli/migrators/alembic.py`

    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198

    @execute_if_authorized(None)
    def upgrade(self, revision: str = "head") -> "Alembic":
        """
        Upgrades the database structure.

        :param revision:
            The revision to apply.
        """

        self.configure()

        if not self.migrator_base.does_table_exists(
            "alembic_version"
        ) or self.is_revision_different(revision):
            PyFunceble.facility.Logger.info(
                "Started update (%r) of the database schema(s).", revision
            )

            alembic_command.upgrade(self.alembic_config, revision)

            PyFunceble.facility.Logger.info(
                "Finished update (%r) of the database schema(s).", revision
            )