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 查看哈希值)
关闭
logiskip-0.1.0.post1.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 442b613142e42252a6d114a96526d49ed2ed73b5e39103bf5421f05e88ad168a |
|
MD5 | 326552c686673d556536d36624e7a560 |
|
BLAKE2b-256 | 8db41c06807cdda0928c52024708e9fdb145d676ee4693a0ce91d1e70fd8981d |
关闭
logiskip-0.1.0.post1-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4d49fa2e421bc145896be7f8aa84cae77e396c2106fa83eb50eff7b26739b979 |
|
MD5 | 7333d5feec805ae3e40cc21cb141b784 |
|
BLAKE2b-256 | 3e6818c588adde5ec868893619f105c6daed3c3a19c7ee87c0b3d7d333f7a058 |