schemaxpy 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. schemaxpy-0.1.0/.gitignore +67 -0
  2. schemaxpy-0.1.0/.pre-commit-config.yaml +22 -0
  3. schemaxpy-0.1.0/Makefile +73 -0
  4. schemaxpy-0.1.0/PKG-INFO +555 -0
  5. schemaxpy-0.1.0/README.md +517 -0
  6. schemaxpy-0.1.0/SETUP.md +327 -0
  7. schemaxpy-0.1.0/pyproject.toml +106 -0
  8. schemaxpy-0.1.0/pytest.ini +14 -0
  9. schemaxpy-0.1.0/src/schemax/__init__.py +106 -0
  10. schemaxpy-0.1.0/src/schemax/cli.py +970 -0
  11. schemaxpy-0.1.0/src/schemax/commands/__init__.py +31 -0
  12. schemaxpy-0.1.0/src/schemax/commands/apply.py +483 -0
  13. schemaxpy-0.1.0/src/schemax/commands/diff.py +177 -0
  14. schemaxpy-0.1.0/src/schemax/commands/import_assets.py +251 -0
  15. schemaxpy-0.1.0/src/schemax/commands/rollback.py +895 -0
  16. schemaxpy-0.1.0/src/schemax/commands/snapshot_rebase.py +451 -0
  17. schemaxpy-0.1.0/src/schemax/commands/sql.py +261 -0
  18. schemaxpy-0.1.0/src/schemax/commands/validate.py +200 -0
  19. schemaxpy-0.1.0/src/schemax/core/__init__.py +69 -0
  20. schemaxpy-0.1.0/src/schemax/core/deployment.py +758 -0
  21. schemaxpy-0.1.0/src/schemax/core/storage.py +581 -0
  22. schemaxpy-0.1.0/src/schemax/core/version.py +148 -0
  23. schemaxpy-0.1.0/src/schemax/providers/__init__.py +44 -0
  24. schemaxpy-0.1.0/src/schemax/providers/base/__init__.py +41 -0
  25. schemaxpy-0.1.0/src/schemax/providers/base/batching.py +194 -0
  26. schemaxpy-0.1.0/src/schemax/providers/base/dependency_graph.py +375 -0
  27. schemaxpy-0.1.0/src/schemax/providers/base/exceptions.py +43 -0
  28. schemaxpy-0.1.0/src/schemax/providers/base/executor.py +104 -0
  29. schemaxpy-0.1.0/src/schemax/providers/base/hierarchy.py +50 -0
  30. schemaxpy-0.1.0/src/schemax/providers/base/models.py +39 -0
  31. schemaxpy-0.1.0/src/schemax/providers/base/operations.py +84 -0
  32. schemaxpy-0.1.0/src/schemax/providers/base/optimization.py +136 -0
  33. schemaxpy-0.1.0/src/schemax/providers/base/provider.py +286 -0
  34. schemaxpy-0.1.0/src/schemax/providers/base/reverse_generator.py +28 -0
  35. schemaxpy-0.1.0/src/schemax/providers/base/sql_generator.py +651 -0
  36. schemaxpy-0.1.0/src/schemax/providers/base/sql_parser.py +212 -0
  37. schemaxpy-0.1.0/src/schemax/providers/base/state_differ.py +107 -0
  38. schemaxpy-0.1.0/src/schemax/providers/registry.py +127 -0
  39. schemaxpy-0.1.0/src/schemax/providers/unity/__init__.py +41 -0
  40. schemaxpy-0.1.0/src/schemax/providers/unity/auth.py +171 -0
  41. schemaxpy-0.1.0/src/schemax/providers/unity/executor.py +265 -0
  42. schemaxpy-0.1.0/src/schemax/providers/unity/hierarchy.py +32 -0
  43. schemaxpy-0.1.0/src/schemax/providers/unity/models.py +174 -0
  44. schemaxpy-0.1.0/src/schemax/providers/unity/operations.py +455 -0
  45. schemaxpy-0.1.0/src/schemax/providers/unity/provider.py +1850 -0
  46. schemaxpy-0.1.0/src/schemax/providers/unity/safety_validator.py +373 -0
  47. schemaxpy-0.1.0/src/schemax/providers/unity/sql_generator.py +2921 -0
  48. schemaxpy-0.1.0/src/schemax/providers/unity/state_differ.py +1040 -0
  49. schemaxpy-0.1.0/src/schemax/providers/unity/state_reducer.py +490 -0
  50. schemaxpy-0.1.0/tests/README.md +40 -0
  51. schemaxpy-0.1.0/tests/__init__.py +5 -0
  52. schemaxpy-0.1.0/tests/conftest.py +269 -0
  53. schemaxpy-0.1.0/tests/integration/test_command_workflows.py +117 -0
  54. schemaxpy-0.1.0/tests/integration/test_import_live_databricks.py +176 -0
  55. schemaxpy-0.1.0/tests/integration/test_import_workflow.py +111 -0
  56. schemaxpy-0.1.0/tests/integration/test_live_command_matrix.py +436 -0
  57. schemaxpy-0.1.0/tests/integration/test_view_dependencies.py +734 -0
  58. schemaxpy-0.1.0/tests/integration/test_workflows.py +679 -0
  59. schemaxpy-0.1.0/tests/integration/test_workflows_e2e.py +522 -0
  60. schemaxpy-0.1.0/tests/providers/base/test_hierarchy.py +53 -0
  61. schemaxpy-0.1.0/tests/resources/projects/schemax_demo/.schemax/changelog.json +6 -0
  62. schemaxpy-0.1.0/tests/resources/projects/schemax_demo/.schemax/project.json +70 -0
  63. schemaxpy-0.1.0/tests/resources/projects/schemax_demo/.schemax/snapshots/v0.1.0.json +54772 -0
  64. schemaxpy-0.1.0/tests/resources/projects/schemax_demo/README.md +21 -0
  65. schemaxpy-0.1.0/tests/resources/sql/README.md +51 -0
  66. schemaxpy-0.1.0/tests/resources/sql/unity_command_fixture.sql +59 -0
  67. schemaxpy-0.1.0/tests/resources/sql/unity_import_fixture.sql +241 -0
  68. schemaxpy-0.1.0/tests/unit/test_apply_command.py +380 -0
  69. schemaxpy-0.1.0/tests/unit/test_batching.py +341 -0
  70. schemaxpy-0.1.0/tests/unit/test_catalog_mapping.py +648 -0
  71. schemaxpy-0.1.0/tests/unit/test_cli_commands.py +278 -0
  72. schemaxpy-0.1.0/tests/unit/test_dependency_graph.py +446 -0
  73. schemaxpy-0.1.0/tests/unit/test_deployment_tracker.py +47 -0
  74. schemaxpy-0.1.0/tests/unit/test_diff_command.py +99 -0
  75. schemaxpy-0.1.0/tests/unit/test_drop_operations_fix.py +361 -0
  76. schemaxpy-0.1.0/tests/unit/test_import_command.py +473 -0
  77. schemaxpy-0.1.0/tests/unit/test_optimization.py +217 -0
  78. schemaxpy-0.1.0/tests/unit/test_provider.py +1209 -0
  79. schemaxpy-0.1.0/tests/unit/test_rollback_command.py +664 -0
  80. schemaxpy-0.1.0/tests/unit/test_rollback_idempotency.py +209 -0
  81. schemaxpy-0.1.0/tests/unit/test_snapshot_rebase.py +173 -0
  82. schemaxpy-0.1.0/tests/unit/test_sql_command.py +113 -0
  83. schemaxpy-0.1.0/tests/unit/test_sql_generator.py +1968 -0
  84. schemaxpy-0.1.0/tests/unit/test_sql_parser.py +382 -0
  85. schemaxpy-0.1.0/tests/unit/test_state_differ.py +2237 -0
  86. schemaxpy-0.1.0/tests/unit/test_state_reducer.py +676 -0
  87. schemaxpy-0.1.0/tests/unit/test_storage_v4.py +347 -0
  88. schemaxpy-0.1.0/tests/unit/test_unity_executor.py +103 -0
  89. schemaxpy-0.1.0/tests/unit/test_validate_command.py +139 -0
  90. schemaxpy-0.1.0/tests/utils/__init__.py +5 -0
  91. schemaxpy-0.1.0/tests/utils/cli_helpers.py +24 -0
  92. schemaxpy-0.1.0/tests/utils/live_databricks.py +136 -0
  93. schemaxpy-0.1.0/tests/utils/operation_builders.py +619 -0
  94. schemaxpy-0.1.0/uv.lock +981 -0
@@ -0,0 +1,67 @@
1
+ # Node.js
2
+ node_modules/
3
+ *.vsix
4
+ .vscode-test/
5
+
6
+ # Build outputs (extension)
7
+ packages/vscode-extension/dist/
8
+ packages/vscode-extension/media/
9
+
10
+ # Docusaurus build/cache
11
+ docs/schemax/.docusaurus/
12
+ docs/schemax/build/
13
+
14
+ # Python
15
+ __pycache__/
16
+ *.py[cod]
17
+ *$py.class
18
+ *.so
19
+ .Python
20
+ build/
21
+ develop-eggs/
22
+ dist/
23
+ downloads/
24
+ eggs/
25
+ .eggs/
26
+ lib/
27
+ lib64/
28
+ parts/
29
+ sdist/
30
+ var/
31
+ wheels/
32
+ *.egg-info/
33
+ .installed.cfg
34
+ *.egg
35
+ MANIFEST
36
+
37
+ # Virtual environments
38
+ .env
39
+ .venv
40
+ env/
41
+ venv/
42
+ ENV/
43
+ env.bak/
44
+ venv.bak/
45
+
46
+ # Testing
47
+ .pytest_cache/
48
+ .coverage
49
+ .tox/
50
+ htmlcov/
51
+ packages/vscode-extension/coverage/
52
+
53
+ # IDE
54
+ .vscode/
55
+ .idea/
56
+ *.swp
57
+ *.swo
58
+ *~
59
+
60
+ # OS
61
+ .DS_Store
62
+ Thumbs.db
63
+
64
+ # Temporary files
65
+ *.log
66
+ *.tmp
67
+ .temp/
@@ -0,0 +1,22 @@
1
+ repos:
2
+ - repo: https://github.com/astral-sh/ruff-pre-commit
3
+ rev: v0.8.4
4
+ hooks:
5
+ # Run formatter
6
+ - id: ruff-format
7
+ types_or: [python, pyi]
8
+
9
+ # Run linter with auto-fix
10
+ - id: ruff
11
+ types_or: [python, pyi]
12
+ args: [--fix, --exit-non-zero-on-fix]
13
+
14
+ - repo: https://github.com/pre-commit/mirrors-mypy
15
+ rev: v1.13.0
16
+ hooks:
17
+ - id: mypy
18
+ additional_dependencies: [pydantic>=2.0.0, click>=8.0.0, rich>=13.0.0]
19
+ args: [--config-file=pyproject.toml]
20
+ pass_filenames: false
21
+ entry: mypy src/
22
+
@@ -0,0 +1,73 @@
1
+ .PHONY: format fmt lint typecheck test all check ci pre-commit install clean help
2
+
3
+ help:
4
+ @echo "SchemaX Python SDK - Development Commands"
5
+ @echo ""
6
+ @echo "Usage: make [target]"
7
+ @echo ""
8
+ @echo "Targets:"
9
+ @echo " format - Format code with Ruff (auto-fix)"
10
+ @echo " fmt - Alias for format"
11
+ @echo " lint - Lint code with Ruff (auto-fix)"
12
+ @echo " typecheck - Type check with mypy"
13
+ @echo " test - Run tests with pytest"
14
+ @echo " all - Run all checks (format, lint, typecheck, test)"
15
+ @echo " check - Run format/lint checks without modifying files (like CI)"
16
+ @echo " ci - Run exact CI checks (format check + lint check + typecheck + test)"
17
+ @echo " pre-commit - Format, lint, typecheck (recommended before git commit)"
18
+ @echo " install - Install package in development mode"
19
+ @echo " clean - Remove build artifacts and cache"
20
+ @echo ""
21
+
22
+ format:
23
+ @echo "🎨 Formatting with Ruff..."
24
+ uv run ruff format src/ tests/
25
+
26
+ # Alias so "make fmt" works (e.g. from root Makefile or scripts)
27
+ fmt: format
28
+
29
+ lint:
30
+ @echo "🔍 Linting with Ruff..."
31
+ uv run ruff check src/ tests/ --fix
32
+
33
+ typecheck:
34
+ @echo "🔬 Type checking with mypy..."
35
+ uv run mypy src/
36
+
37
+ test:
38
+ @echo "🧪 Running tests..."
39
+ uv run pytest tests/ -v
40
+
41
+ all: format lint typecheck test
42
+ @echo ""
43
+ @echo "✅ All checks passed! Ready to commit."
44
+
45
+ check:
46
+ @echo "🔍 Running format check (no modifications)..."
47
+ @uv run ruff format --check src/ tests/
48
+ @echo "🔍 Running lint check (no modifications)..."
49
+ @uv run ruff check src/ tests/
50
+ @echo ""
51
+ @echo "✅ Format and lint checks passed!"
52
+
53
+ ci: check typecheck test
54
+ @echo ""
55
+ @echo "✅ All CI checks passed!"
56
+
57
+ pre-commit: format lint typecheck
58
+ @echo ""
59
+ @echo "✅ Pre-commit checks complete! Ready to commit."
60
+ @echo ""
61
+ @echo "💡 Tip: Run 'git add -A && git commit -S -m \"your message\"'"
62
+
63
+ install:
64
+ @echo "📦 Installing package in development mode..."
65
+ uv pip install -e ".[dev]"
66
+
67
+ clean:
68
+ @echo "🧹 Cleaning build artifacts..."
69
+ rm -rf build/ dist/ *.egg-info
70
+ rm -rf .pytest_cache .mypy_cache .ruff_cache
71
+ find . -type d -name __pycache__ -exec rm -rf {} +
72
+ find . -type f -name "*.pyc" -delete
73
+
@@ -0,0 +1,555 @@
1
+ Metadata-Version: 2.4
2
+ Name: schemaxpy
3
+ Version: 0.1.0
4
+ Summary: Python SDK and CLI for Databricks Unity Catalog schema management
5
+ Project-URL: Homepage, https://github.com/vb-dbrks/schemax-vscode
6
+ Project-URL: Documentation, https://github.com/vb-dbrks/schemax-vscode/tree/main/docs
7
+ Project-URL: Repository, https://github.com/vb-dbrks/schemax-vscode
8
+ Project-URL: Issues, https://github.com/vb-dbrks/schemax-vscode/issues
9
+ Author: Field Engineering
10
+ Author-email: Varun Bhandary <varun.bhandary@databricks.com>
11
+ License: MIT
12
+ Keywords: cli,databricks,migration,schema,sql,unity-catalog
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Requires-Python: >=3.11
20
+ Requires-Dist: click>=8.0.0
21
+ Requires-Dist: databricks-sdk>=0.18.0
22
+ Requires-Dist: networkx>=3.0
23
+ Requires-Dist: pydantic>=2.0.0
24
+ Requires-Dist: pyyaml>=6.0.0
25
+ Requires-Dist: rich>=13.0.0
26
+ Requires-Dist: sqlglot>=20.0.0
27
+ Provides-Extra: dev
28
+ Requires-Dist: mypy>=1.0.0; extra == 'dev'
29
+ Requires-Dist: pre-commit>=3.0.0; extra == 'dev'
30
+ Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
31
+ Requires-Dist: pytest>=7.0.0; extra == 'dev'
32
+ Requires-Dist: ruff>=0.1.0; extra == 'dev'
33
+ Requires-Dist: sqlglot>=20.0.0; extra == 'dev'
34
+ Requires-Dist: types-networkx>=3.0.0; extra == 'dev'
35
+ Provides-Extra: validation
36
+ Requires-Dist: sqlglot>=20.0.0; extra == 'validation'
37
+ Description-Content-Type: text/markdown
38
+
39
+ # SchemaX Python SDK & CLI
40
+
41
+ **Declarative schema management** for modern data catalogs. Version control your schemas, generate SQL migrations, and deploy with confidence across multiple environments.
42
+
43
+ ## Features
44
+
45
+ - **Multi-Provider Architecture**: Unity Catalog (Databricks), Hive, PostgreSQL, and more
46
+ - **Version-Controlled Schemas**: Git-based workflow with snapshots and changelogs
47
+ - **SQL Migration Generation**: Generate idempotent SQL DDL from schema changes
48
+ - **Environment Management**: Dev, test, prod with catalog name mapping
49
+ - **Deployment Tracking**: Know what's deployed where with database-backed tracking
50
+ - **Auto-Rollback**: Automatically rollback failed deployments with data loss detection (NEW!)
51
+ - **Safety Validation**: Analyze data impact before rollback operations
52
+ - **Type-Safe**: Full type annotations, validated with mypy
53
+ - **CI/CD Ready**: Designed for GitHub Actions, GitLab CI, and other pipelines
54
+ - **Extensible**: Plugin architecture for custom catalog providers
55
+
56
+ ## Why SchemaX?
57
+
58
+ **Provider-agnostic design**: Write your schema once, deploy to any catalog system. Start with Unity Catalog (Databricks) and easily extend to Hive, PostgreSQL, Snowflake, or custom providers.
59
+
60
+ **Git-based workflow**: Your schemas are code. Version them, review them, and deploy them with confidence using familiar Git workflows.
61
+
62
+ **Environment-aware**: Manage dev, test, and prod environments with automatic catalog name mapping. No more hardcoded catalog names in SQL.
63
+
64
+ **Type-safe and tested**: Built with Python 3.11+ type hints, validated with mypy, and covered by 138+ tests. Production-ready from day one.
65
+
66
+ ## Installation
67
+
68
+ ```bash
69
+ pip install schemax-py
70
+ ```
71
+
72
+ ### Development Install
73
+
74
+ ```bash
75
+ git clone https://github.com/vb-dbrks/schemax-vscode.git
76
+ cd schemax-vscode/packages/python-sdk
77
+ pip install -e ".[dev]"
78
+ ```
79
+
80
+ ## Quick Start
81
+
82
+ ### 1. Initialize a New Project
83
+
84
+ ```bash
85
+ # Unity Catalog (Databricks) - default
86
+ schemax init
87
+
88
+ # PostgreSQL
89
+ schemax init --provider postgres
90
+
91
+ # Hive Metastore
92
+ schemax init --provider hive
93
+ ```
94
+
95
+ This creates a `.schemax/` directory with your project configuration.
96
+
97
+ ### 2. Validate Your Schema
98
+
99
+ ```bash
100
+ schemax validate
101
+ ```
102
+
103
+ Validates project structure, provider compatibility, and schema correctness.
104
+
105
+ ### 3. Generate SQL Migration
106
+
107
+ ```bash
108
+ # Generate SQL from changelog
109
+ schemax sql --output migration.sql
110
+
111
+ # Generate for specific environment (with catalog mapping)
112
+ schemax sql --target dev --output dev-migration.sql
113
+ ```
114
+
115
+ ### 4. Apply Changes (Unity Catalog)
116
+
117
+ ```bash
118
+ # Preview changes
119
+ schemax apply --target dev --profile my-databricks --warehouse-id abc123 --dry-run
120
+
121
+ # Apply with automatic rollback on failure (MVP feature!)
122
+ schemax apply --target dev --profile my-databricks --warehouse-id abc123 --auto-rollback
123
+
124
+ # Apply to environment
125
+ schemax apply --target dev --profile my-databricks --warehouse-id abc123
126
+ ```
127
+
128
+ ### 5. Track Deployments
129
+
130
+ ```bash
131
+ # Record deployment (works for all providers)
132
+ schemax record-deployment --environment prod --version v1.0.0 --mark-deployed
133
+ ```
134
+
135
+ ## CLI Commands
136
+
137
+ ### `schemax sql`
138
+
139
+ Generate SQL DDL migration scripts from schema changes.
140
+
141
+ **Options:**
142
+ - `--output, -o`: Output file path (default: stdout)
143
+ - `--target, -t`: Target environment (applies catalog name mapping)
144
+
145
+ **Examples:**
146
+ ```bash
147
+ # Output to stdout
148
+ schemax sql
149
+
150
+ # Save to file
151
+ schemax sql --output migration.sql
152
+
153
+ # Generate for specific environment
154
+ schemax sql --target prod --output prod-migration.sql
155
+ ```
156
+
157
+ ### `schemax apply` (Unity Catalog only)
158
+
159
+ Execute SQL migrations against a Databricks Unity Catalog environment with automatic deployment tracking and optional rollback.
160
+
161
+ **Options:**
162
+ - `--target, -t`: Target environment (required)
163
+ - `--profile, -p`: Databricks CLI profile (required)
164
+ - `--warehouse-id, -w`: SQL Warehouse ID (required)
165
+ - `--sql`: SQL file to execute (optional, generates from changelog if not provided)
166
+ - `--dry-run`: Preview changes without executing
167
+ - `--no-interaction`: Skip confirmation prompts (for CI/CD)
168
+ - `--auto-rollback`: Automatically rollback on failure (NEW!)
169
+
170
+ **Features:**
171
+ - Interactive snapshot prompts (create snapshot before deployment)
172
+ - SQL preview with statement-by-statement display
173
+ - Database-backed deployment tracking in `{catalog}.schemax`
174
+ - Automatic rollback on partial failures (with `--auto-rollback`)
175
+
176
+ **Examples:**
177
+ ```bash
178
+ # Preview changes
179
+ schemax apply --target dev --profile default --warehouse-id abc123 --dry-run
180
+
181
+ # Apply with automatic rollback on failure
182
+ schemax apply --target dev --profile default --warehouse-id abc123 --auto-rollback
183
+
184
+ # Apply with confirmation
185
+ schemax apply --target prod --profile prod --warehouse-id xyz789
186
+
187
+ # Non-interactive (CI/CD)
188
+ schemax apply --target prod --profile prod --warehouse-id xyz789 --no-interaction
189
+ ```
190
+
191
+ ### `schemax rollback` (Unity Catalog only)
192
+
193
+ Rollback failed or unwanted deployments with safety validation. Idempotent design prevents redundant operations by checking database state.
194
+
195
+ **Partial Rollback** - Revert successful operations from a failed deployment:
196
+ ```bash
197
+ schemax rollback --partial --deployment <id> --target dev --profile DEFAULT --warehouse-id <id>
198
+
199
+ # With dry-run
200
+ schemax rollback --partial --deployment <id> --target dev --profile DEFAULT --warehouse-id <id> --dry-run
201
+
202
+ # Only safe operations
203
+ schemax rollback --partial --deployment <id> --target dev --profile DEFAULT --warehouse-id <id> --safe-only
204
+ ```
205
+
206
+ **Complete Rollback** - Rollback to a previous snapshot:
207
+ ```bash
208
+ schemax rollback --to-snapshot v0.2.0 --target dev --profile DEFAULT --warehouse-id <id>
209
+
210
+ # With dry-run
211
+ schemax rollback --to-snapshot v0.2.0 --target dev --profile DEFAULT --warehouse-id <id> --dry-run
212
+ ```
213
+
214
+ **Options:**
215
+ - `--partial`: Rollback successful operations from a failed deployment
216
+ - `--deployment, -d`: Deployment ID to rollback (required for partial)
217
+ - `--to-snapshot`: Snapshot version to rollback to (required for complete)
218
+ - `--target, -t`: Target environment (required)
219
+ - `--profile, -p`: Databricks CLI profile (required)
220
+ - `--warehouse-id, -w`: SQL Warehouse ID (required)
221
+ - `--dry-run`: Preview rollback SQL without executing
222
+ - `--safe-only`: Only execute SAFE operations (skip RISKY/DESTRUCTIVE)
223
+
224
+ **Safety Levels:**
225
+ - **SAFE**: No data loss (e.g., DROP empty table)
226
+ - **RISKY**: Potential data loss (e.g., ALTER COLUMN TYPE)
227
+ - **DESTRUCTIVE**: Certain data loss (e.g., DROP table with data)
228
+
229
+ **Features:**
230
+ - **Idempotent**: Checks database deployment state to prevent redundant rollbacks
231
+ - **SQL Preview**: Shows exact SQL statements before execution (matches `apply` UX)
232
+ - **Database as Source of Truth**: Queries deployment tracking table for accurate state
233
+
234
+ ### `schemax snapshot`
235
+
236
+ Manage schema snapshots with lifecycle commands.
237
+
238
+ **Create Snapshot:**
239
+ ```bash
240
+ # Auto-generate version
241
+ schemax snapshot create --name "Initial schema"
242
+
243
+ # Specify version manually
244
+ schemax snapshot create --name "Production release" --version v1.0.0
245
+
246
+ # With tags
247
+ schemax snapshot create --name "Hotfix" --version v0.2.1 --tags hotfix,urgent
248
+ ```
249
+
250
+ **Validate Snapshots:**
251
+ ```bash
252
+ # Detect stale snapshots after git rebase
253
+ schemax snapshot validate
254
+ ```
255
+
256
+ **Rebase Snapshot:**
257
+ ```bash
258
+ # Rebase a stale snapshot onto new base
259
+ schemax snapshot rebase v0.3.0
260
+ ```
261
+
262
+ **Features:**
263
+ - Semantic versioning (MAJOR.MINOR.PATCH)
264
+ - Detects stale snapshots after Git rebases
265
+ - Unpacks and replays operations on new base
266
+ - Conflict detection with manual UI resolution
267
+ - Validates snapshot lineage
268
+
269
+ ### `schemax validate`
270
+
271
+ Validate `.schemax/` project files for correctness and provider compatibility.
272
+
273
+ **Examples:**
274
+ ```bash
275
+ # Validate current directory
276
+ schemax validate
277
+
278
+ # Validate specific directory
279
+ schemax validate /path/to/project
280
+ ```
281
+
282
+ ### `schemax record-deployment`
283
+
284
+ Manually record deployment metadata (useful for non-Unity Catalog providers).
285
+
286
+ **Options:**
287
+ - `--environment, -e`: Environment name (required)
288
+ - `--version, -v`: Version deployed (default: latest snapshot)
289
+ - `--mark-deployed`: Mark as successfully deployed
290
+
291
+ **Examples:**
292
+ ```bash
293
+ # Record successful deployment
294
+ schemax record-deployment --environment prod --version v1.0.0 --mark-deployed
295
+ ```
296
+
297
+ ### `schemax diff`
298
+
299
+ Compare two schema versions and show the operations needed to transform one into the other.
300
+
301
+ **Examples:**
302
+ ```bash
303
+ # Basic diff
304
+ schemax diff --from v0.1.0 --to v0.2.0
305
+
306
+ # Show generated SQL with logical catalog names
307
+ schemax diff --from v0.1.0 --to v0.2.0 --show-sql
308
+
309
+ # Show SQL with environment-specific catalog names
310
+ schemax diff --from v0.1.0 --to v0.2.0 --show-sql --target dev
311
+
312
+ # Show detailed operation payloads
313
+ schemax diff --from v0.1.0 --to v0.2.0 --show-details
314
+ ```
315
+
316
+ ## Python API
317
+
318
+ ### Generate SQL Programmatically
319
+
320
+ ```python
321
+ from pathlib import Path
322
+ from schemax.core.storage import load_current_state, read_project, get_environment_config
323
+ from schemax.providers.base.operations import Operation
324
+
325
+ # Load schema with provider
326
+ workspace = Path.cwd()
327
+ state, changelog, provider = load_current_state(workspace)
328
+
329
+ print(f"Provider: {provider.info.name} v{provider.info.version}")
330
+
331
+ # Convert ops to Operation objects
332
+ operations = [Operation(**op) for op in changelog["ops"]]
333
+
334
+ # Generate SQL using provider's SQL generator
335
+ generator = provider.get_sql_generator(state)
336
+ sql = generator.generate_sql(operations)
337
+
338
+ print(sql)
339
+ ```
340
+
341
+ ### Environment-Specific SQL Generation
342
+
343
+ ```python
344
+ from pathlib import Path
345
+ from schemax.core.storage import load_current_state, read_project, get_environment_config
346
+
347
+ workspace = Path.cwd()
348
+ state, changelog, provider = load_current_state(workspace)
349
+
350
+ # Get environment configuration
351
+ project = read_project(workspace)
352
+ env_config = get_environment_config(project, "prod")
353
+
354
+ # Build catalog name mapping (logical -> physical)
355
+ catalog_mapping = {}
356
+ for catalog in state.get("catalogs", []):
357
+ logical_name = catalog.get("name", "__implicit__")
358
+ physical_name = env_config.get("catalog", logical_name)
359
+ catalog_mapping[logical_name] = physical_name
360
+
361
+ # Generate SQL with environment-specific catalog names
362
+ generator = provider.get_sql_generator(state)
363
+ generator.catalog_name_mapping = catalog_mapping # For Unity provider
364
+
365
+ operations = [Operation(**op) for op in changelog["ops"]]
366
+ sql = generator.generate_sql(operations)
367
+
368
+ print(sql) # Contains prod catalog names
369
+ ```
370
+
371
+ ### Working with Multiple Providers
372
+
373
+ ```python
374
+ from schemax.providers import ProviderRegistry
375
+
376
+ # List available providers
377
+ providers = ProviderRegistry.get_all_ids()
378
+ print(f"Available providers: {providers}")
379
+
380
+ # Get specific provider
381
+ unity_provider = ProviderRegistry.get("unity")
382
+ if unity_provider:
383
+ print(f"Name: {unity_provider.info.name}")
384
+ print(f"Version: {unity_provider.info.version}")
385
+ print(f"Operations: {len(unity_provider.info.capabilities.supported_operations)}")
386
+ ```
387
+
388
+ ### Validate Schema
389
+
390
+ ```python
391
+ from pathlib import Path
392
+ from schemax.core.storage import read_project, load_current_state
393
+
394
+ try:
395
+ workspace = Path.cwd()
396
+ project = read_project(workspace)
397
+ state, changelog, provider = load_current_state(workspace)
398
+
399
+ # Validate with provider
400
+ validation = provider.validate_state(state)
401
+ if validation.valid:
402
+ print("✓ Schema is valid")
403
+ else:
404
+ print("✗ Validation failed:")
405
+ for error in validation.errors:
406
+ print(f" - {error.field}: {error.message}")
407
+ except Exception as e:
408
+ print(f"✗ Error: {e}")
409
+ ```
410
+
411
+ ## CI/CD Integration
412
+
413
+ ### GitHub Actions (Generic)
414
+
415
+ ```yaml
416
+ name: Schema Management
417
+ on:
418
+ pull_request:
419
+ push:
420
+ branches: [main]
421
+
422
+ jobs:
423
+ validate:
424
+ runs-on: ubuntu-latest
425
+ steps:
426
+ - uses: actions/checkout@v3
427
+
428
+ - uses: actions/setup-python@v4
429
+ with:
430
+ python-version: '3.11'
431
+
432
+ - name: Install SchemaX
433
+ run: pip install schemax-py
434
+
435
+ - name: Validate Schema
436
+ run: schemax validate
437
+
438
+ - name: Generate SQL Preview
439
+ run: schemax sql --target prod --output migration.sql
440
+
441
+ - name: Upload SQL
442
+ uses: actions/upload-artifact@v3
443
+ with:
444
+ name: migration-sql
445
+ path: migration.sql
446
+ ```
447
+
448
+ ### GitHub Actions (Unity Catalog - Automated Deployment)
449
+
450
+ ```yaml
451
+ name: Deploy to Unity Catalog
452
+ on:
453
+ push:
454
+ branches: [main]
455
+
456
+ jobs:
457
+ deploy:
458
+ runs-on: ubuntu-latest
459
+ environment: production
460
+ steps:
461
+ - uses: actions/checkout@v3
462
+
463
+ - uses: actions/setup-python@v4
464
+ with:
465
+ python-version: '3.11'
466
+
467
+ - name: Install SchemaX
468
+ run: pip install schemax-py
469
+
470
+ - name: Validate Schema
471
+ run: schemax validate
472
+
473
+ - name: Apply to Production
474
+ env:
475
+ DATABRICKS_HOST: ${{ secrets.DATABRICKS_HOST }}
476
+ DATABRICKS_TOKEN: ${{ secrets.DATABRICKS_TOKEN }}
477
+ run: |
478
+ schemax apply \
479
+ --target prod \
480
+ --profile default \
481
+ --warehouse-id ${{ secrets.WAREHOUSE_ID }} \
482
+ --no-interaction
483
+ ```
484
+
485
+ ### GitLab CI
486
+
487
+ ```yaml
488
+ validate-schema:
489
+ stage: test
490
+ image: python:3.11
491
+ script:
492
+ - pip install schemax-py
493
+ - schemax validate
494
+ - schemax sql --target prod --output migration.sql
495
+ artifacts:
496
+ paths:
497
+ - migration.sql
498
+ expire_in: 1 week
499
+ ```
500
+
501
+ ## Supported Providers
502
+
503
+ | Provider | Status | Operations | Apply Command | Notes |
504
+ |----------|--------|------------|---------------|-------|
505
+ | **Unity Catalog** | ✅ Stable | 29 | ✅ `schemax apply` | Full Databricks integration |
506
+ | **Hive Metastore** | 🚧 Planned | TBD | Manual | SQL generation only |
507
+ | **PostgreSQL** | 🚧 Planned | TBD | Manual | SQL generation only |
508
+
509
+ Want to add a provider? See [PROVIDER_CONTRACT.md](../../docs/PROVIDER_CONTRACT.md).
510
+
511
+ ## Requirements
512
+
513
+ - **Python 3.11+**
514
+ - A SchemaX project (`.schemax/` directory)
515
+ - For Unity Catalog: Databricks workspace with SQL Warehouse access
516
+
517
+ ## Documentation
518
+
519
+ - [Quick Start Guide](../../docs/QUICKSTART.md)
520
+ - [Architecture Overview](../../docs/ARCHITECTURE.md)
521
+ - [Development Guide](./SETUP.md)
522
+ - [Provider Contract](../../docs/PROVIDER_CONTRACT.md)
523
+
524
+ ## Development
525
+
526
+ See [SETUP.md](./SETUP.md) for complete development setup instructions.
527
+
528
+ **Quick setup:**
529
+ ```bash
530
+ cd packages/python-sdk
531
+ uv pip install -e ".[dev]" # Or use pip
532
+ pre-commit install
533
+ make all # Run all quality checks
534
+ ```
535
+
536
+ **Commands:**
537
+ ```bash
538
+ make format # Format code
539
+ make lint # Lint code
540
+ make typecheck # Type check
541
+ make test # Run tests
542
+ make all # Run all checks
543
+ ```
544
+
545
+ ## License
546
+
547
+ MIT License - see [LICENSE](../../LICENSE) for details.
548
+
549
+ ## Links
550
+
551
+ - **Repository**: https://github.com/vb-dbrks/schemax-vscode
552
+ - **Issues**: https://github.com/vb-dbrks/schemax-vscode/issues
553
+ - **VS Code Extension**: [schemax-vscode](../vscode-extension/)
554
+ - **PyPI**: https://pypi.org/project/schemax-py/
555
+