Web Analytics

dbslice

⭐ 140 stars Simplified Chinese by nabroleonx

dbslice logo

dbslice

PyPI version License: MIT Python 3.10+

提取最小且参照完整的数据库子集,用于本地开发和调试。

问题

将整个生产数据库复制到您的机器上是不可行的。但复现一个错误通常需要拥有导致该错误的准确数据。dbslice 通过仅提取您需要的记录,沿着外键关系确保参照完整性,从而解决了这个问题。

dbslice — seed to subset

快速开始

# Install globally
uv tool install dbslice   # or: pip install dbslice

Extract an order and all related records

dbslice extract postgres://localhost/myapp --seed "orders.id=12345" > subset.sql

Import into local database

psql -d localdb < subset.sql

特性

数据库支持

| 数据库 | 状态 | |------------|-----------------------| | PostgreSQL | 完全支持 | | MySQL | 计划中(尚未实现) | | SQLite | 计划中(尚未实现) |

安装

# Install with uv (recommended)
uv add dbslice

Try without installing

uvx dbslice --help

Or with pip

pip install dbslice

使用方法

基本提取

# Extract by primary key
dbslice extract postgres://user:pass@host:5432/db --seed "orders.id=12345"

Extract with WHERE clause

dbslice extract postgres://localhost/db --seed "orders:status='failed' AND created_at > '2024-01-01'"

Multiple seeds

dbslice extract postgres://localhost/db \ --seed "orders.id=100" \ --seed "orders.id=101"

控制遍历

# Limit depth (default: 3)
dbslice extract postgres://... --seed "orders.id=1" --depth 2

Direction: up (parents only), down (children only), both (default)

dbslice extract postgres://... --seed "orders.id=1" --direction up

匿名化

# Auto-anonymize detected sensitive fields
dbslice extract postgres://... --seed "users.id=1" --anonymize

Redact additional fields

dbslice extract postgres://... --seed "users.id=1" --anonymize --redact "audit_logs.ip_address"

列映射界面

可视化映射列,应用合规配置文件,并生成可直接使用的配置——全部通过本地浏览器界面完成。

dbslice map postgres://localhost/myapp

Custom port

dbslice map postgres://localhost/myapp --port 8888

Also works with uvx (no install needed)

uvx dbslice map postgres://localhost/myapp
将列映射到匿名化规则 生成并导出配置
列映射 生成的配置

运行于 127.0.0.1:9473,使用一次性会话令牌——数据不会离开您的机器。只需一键应用 GDPR、HIPAA 或 PCI-DSS 配置文件,审核将被屏蔽的内容,然后下载 YAML 文件。

合规配置文件

# HIPAA Safe Harbor — auto-masks all 18 identifier types
dbslice extract postgres://... --seed "patients.id=1" --compliance hipaa --compliance-strict

Multiple profiles + audit manifest

dbslice extract postgres://... --seed "users.id=1" --compliance gdpr --compliance pci-dss -f subset.sql

Produces subset.sql + subset.manifest.json

输出格式

# SQL (default)
dbslice extract postgres://... --seed "orders.id=1" --output sql

JSON fixtures

dbslice extract postgres://... --seed "orders.id=1" --output json --out-file fixtures/

CSV

dbslice extract postgres://... --seed "orders.id=1" --output csv --out-file data/

虚拟外键

对于数据库模式中未定义的关系(Django GenericForeignKeys,隐式关系):

# dbslice.yaml
database:
  url: postgres://localhost:5432/myapp

virtual_foreign_keys:

  • source_table: notifications
source_columns: [object_id] target_table: orders description: "Generic FK to orders via ContentType"

  • source_table: audit_log
source_columns: [user_id] target_table: users description: "Implicit FK without DB constraint"

dbslice extract --config dbslice.yaml --seed "users.id=1"

检查模式

dbslice inspect postgres://localhost/myapp

配置文件

# Generate config from database
dbslice init postgres://localhost/myapp --out-file dbslice.yaml

Use config

dbslice extract --config dbslice.yaml --seed "orders.id=12345"

工作原理

比较

| 功能 | dbslice | Jailer | Greenmask | slice-db | |---------|---------|--------|-----------|----------| | 语言 | Python | Java | Go | Ruby | | 配置 | 零配置 | 需要模型文件 | 需要配置 | 手动 YAML | | 设置时间 | 几秒 | 几小时 | 中等 | 中等 | | 匿名化 | 内置(Faker) | 插件式 | 高级转换器 | 不支持 | | 合规配置 | GDPR, HIPAA, PCI-DSS | 无 | 无 | 无 | | 列映射界面 | 内置(本地) | 无 | 无 | 无 | | PII 值扫描 | 双阶段(掩码前后) | 无 | 无 | 无 | | 子集化 | 外键遍历 | 外键遍历 | 有限 | 外键遍历 | | 输出格式 | SQL, JSON, CSV | SQL, XML, CSV | SQL | 仅 SQL | | 循环处理 | 自动 | 手动配置 | 不适用 | 手动 | | 流式处理 | 内置 | 可配置 | 内置 | 不支持 | | 维护状态 | 活跃 | 活跃 | 活跃 | 不维护 |

dbslice 是轻量级、零配置的 Python 选项:安装后一分钟内即可提取。

开发

git clone https://github.com/nabroleonx/dbslice.git
cd dbslice
uv sync --dev
uv run pytest

License

MIT

--- Tranlated By Open Ai Tx | Last indexed: 2026-07-19 ---