跳转到主要内容

RDBMS之间的逻辑数据库迁移

项目描述

摘要

logiskip是一个用Python编写的命令行工具(使用SQLAlchemy),可以在不同的RDMS之间迁移应用程序数据,例如在MySQL和PostgreSQL之间。

它是模块化的,可以定义用于单独类(所谓的“负载”)中的应用程序的迁移逻辑。

logiskip可以用于简单的任务,如仅复制表,也可以用于更复杂的任务(例如,将图像转换为其他格式,转换复杂类型等)。

安装

可以使用pip安装logiskip。在这样做的时候,需要的数据库引擎可以作为附加项传递。要安装logiskip并具有在MySQL和PostgreSQL之间转换的能力

pip3 install 'logiskip[mysql,postgres]'

用法

该软件包安装了logiskip命令,它接受以下选项

--source TEXT        URI of source database
--destination TEXT   URI of destination database
--load-name TEXT     Name of load plugin for migrated application
--load-version TEXT  Version of migrated application/schema
--dry-run            Roll back transaction instead of commiting
-v, --verbosity LVL  Either CRITICAL, ERROR, WARNING, INFO or DEBUG.
--help               Show this message and exit.

以下示例将Roundcube 1.4.1的安装从MySQL迁移到PostgreSQL

logiskip --source 'mysql://roundcube:secret@localhost/roundcube' \
         --destination 'postgresql://roundcube:secret@localhost/roundcube' \
         --load-name roundcube --load-version 1.4.1

负载

logiskip中的负载定义了单个应用程序在版本约束内的迁移。以下是一个人工的示例

from logiskip.load import BaseLoad
from sqlalchemy.orm import sessionmaker

class ExampleLoad(BaseLoad, name="example", version_constraint=">=1.0,<2.0"):
    """Load for the application example within the semver constraint 1.x"""

    # Table map for all migrations
    # Tables mapped to None are skipped
    default_tables = {
        "cache": None  # Do not migrate the cache table
    }

    # Table map for migrating from MySQL to PostgreSQL
    postgresql_mysql_tables = {
        "geolocations": None,  # Application supports GIS only in PostgreSQL
        "user": "users"  # Historic naming issue
    }

    # Field map for the "user" table when migrating from PostgreSQL to MySQL
    postgresql_mysql_fields_user = {
        "geolocation_fk": None  # See above
    }

    def mysql_postgresql_row_users(self, src_table, src_dict):
        """Do reverse-geolocation for user addresses when migrating to PostgreSQL"""
        # First, do the default conversion
        dest_row = super()._convert_row_default(src_table, src_dict)

        # Get geolocation for address
        lat, lon = geocoder.reverse(dest_row["address"])

        # Use SQLAlchemy to create a new geolocation entry
        session = sessionmaker(bind=self.dest_engine)()
        geoloc = self.dest_base.classes.geolocations(lat=lat, lon=lon)
        session.commit()

        # Set foreign key to geolocation
        dest_row["geolocation_fk"] = geoloc.id
        return dest_row

    # More examples include:
    #   x_y_field_tablename__fieldname(self, src_value) - Do a conversion on a single field value
    #   x_y_table_tablename - Do the full table conversion manually
    # x_y can be default in all places to be used for any migration pair

鸣谢

logiskip由以下公司赞助

Copyright 2021 Dominik George <dominik.george@credativ.de>

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://apache.ac.cn/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.

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

logiskip-0.1.0.post1.tar.gz (11.2 kB 查看哈希值)

上传时间 源代码

构建分发

logiskip-0.1.0.post1-py3-none-any.whl (15.8 kB 查看哈希值)

上传时间 Python 3

由以下支持