数据库连接的配置和监视
mara-db的Python项目详细描述
马拉数据库
用于在单个项目中配置和访问多个数据库的小程序包。通过对数据库使用“别名”来分离数据库的使用及其配置。
文件mara_db/dbs.py包含postgresql、mysql、sql server、oracle和sqlite的抽象数据库配置。通过覆盖mara_db/config.py:
中的databases
函数来配置项目的数据库连接。
importmara_db.configimportmara_db.dbs## configure database connections for different aliasesmara_db.config.databases=lambda:{'mara':mara_db.dbs.PostgreSQLDB(host='localhost',user='root',database='mara'),'dwh':mara_db.dbs.PostgreSQLDB(database='dwh'),'source-1':mara_db.dbs.MysqlDB(host='some-localhost',database='my_app',user='dwh'),'source-2':mara_db.dbs.SQLServerDB(user='dwh_read',password='123abc',database='db1',host='some-sql-server')}## access individual database configurations with `dbs.db`:print(mara_db.dbs.db('mara'))# -> <PostgreSQLDB: host=localhost, database=mara>
(postgresql)数据库模式的可视化
mara_db/views.py包含使用graphviz(当前仅限postgresql)的所有已配置数据库的架构可视化。它基本上显示所选模式的表以及它们之间的外键关系。
为了查找缺少的外键约束,遵循特定命名模式(可通过config.schema_ui_foreign_key_column_regex
配置,默认为*_fk
)且不属于外键约束的列将以粉色绘制。
快速批处理:使用shell命令访问数据库
文件mara_db/shell.py包含创建通过命令行客户端访问数据库的命令的函数。
例如,query_command
函数创建一个shell命令,可以从stdin接收sql查询并执行它:
importmara_db.shellprint(mara_db.shell.query_command('source-1'))# -> mysql --default-character-set=utf8mb4 --user=dwh --host=some-localhost my_appprint(mara_db.shell.query_command('dwh',timezone='Europe/Lisbon',echo_queries=False))# -> PGTZ=Europe/Lisbon PGOPTIONS=--client-min-messages=warning psql --no-psqlrc --set ON_ERROR_STOP=on dwh
函数copy_to_stdout_command
创建一个shell命令,该命令接收对stdin的查询并将结果以表格形式写入stdout:
print(mara_db.shell.copy_to_stdout_command('source-1'))# -> mysql --default-character-set=utf8mb4 --user=dwh --host=some-localhost my_app --skip-column-names
类似地,copy_from_stdin_command
创建一个客户端命令,从stdin接收表格数据并将其写入目标表:
print(mara_db.shell.copy_from_stdin_command('dwh',target_table='some_table',delimiter_char=';'))# -> PGTZ=Europe/Berlin PGOPTIONS=--client-min-messages=warning psql --echo-all --no-psqlrc --set ON_ERROR_STOP=on dwh \# --command="COPY some_table FROM STDIN WITH DELIMITER AS ';'"
最后,copy_command
创建一个shell命令,接收来自stdin的sql查询,在source_db
中执行查询,然后将的结果写入target_db
:
print(mara_db.shell.copy_command('source-2','dwh',target_table='some_table'))# -> sed 's/\\\\$/\$/g;s/\$/\\\\$/g' \# | sqsh -U dwh_read -P 123abc -S some-sql-server -D db1 -m csv \# | PGTZ=Europe/Berlin PGOPTIONS=--client-min-messages=warning psql --echo-all --no-psqlrc --set ON_ERROR_STOP=on dwh \# --command = "COPY some_table FROM STDIN WITH CSV HEADER"
以下命令行客户机用于访问各种数据库:
Database | Client binary | Comments |
---|---|---|
Postgresql | ^{ | Included in standard distributions. |
MariaDB / Mysql | ^{ | Included in standard distributions. |
SQL Server | ^{ | From https://sourceforge.net/projects/sqsh/, usually messy to get working. On ubuntu, use http://ppa.launchpad.net/jasc/sqsh/ubuntu/ backport. On Mac, try the homebrew version or install from source. |
Oracle | ^{ | See the Oracle Instant Client homepage for details. On Mac, follow these instructions. Then ^{ |
SQLite | ^{ | Available in standard distributions. Version >3.20.x required (not the case on Ubuntu 14.04). |
就这样!sqlalchemy模型的自动迁移
Alembic has a feature可以在数据库状态和应用程序的orm模型之间创建差异。此功能在mara_db/auto_migrate.py中用于自动执行所有必需的数据库转换,而无需中间迁移文件:
# define a model / tableclassMyTable(sqlalchemy.ext.declarative.declarative_base()):__tablename__='my_table'my_table_id=sqlalchemy.Column(sqlalchemy.Integer,primary_key=True)column_1=sqlalchemy.Column(sqlalchemy.TEXT,nullable=False,index=True)db=mara_db.dbs.SQLiteDB(file_name='/tmp/test.sqlite')# create database and table mara_db.auto_migration.auto_migrate(engine=mara_db.auto_migration.engine(db),models=[MyTable])# -># Created database "sqlite:////tmp/test.sqlite"## CREATE TABLE my_table (# my_table_id SERIAL NOT NULL,# column_1 TEXT NOT NULL,# PRIMARY KEY (my_table_id)# );## CREATE INDEX ix_my_table_column_1 ON my_table (column_1);
当模型稍后更改时,^ {< CD18>}对现有数据库创建一个差异,并应用它:
# remove index and add another columnclassMyTable(sqlalchemy.ext.declarative.declarative_base()):__tablename__='my_table'my_table_id=sqlalchemy.Column(sqlalchemy.Integer,primary_key=True)column_1=sqlalchemy.Column(sqlalchemy.TEXT,nullable=False)column_2=sqlalchemy.Column(sqlalchemy.Integer)auto_migrate(engine=engine(db),models=[MyTable])# -># ALTER TABLE my_table ADD COLUMN column_2 INTEGER;## DROP INDEX ix_my_table_text_column_1;
小心使用!有很多变化。我们建议在部署到生产环境之前,首先在登台系统上测试每个AUT迁移。有时需要手动迁移脚本。
安装
pip install mara-db
或
pip install git+https://github.com/mara/mara-db.git