mpt-tool 5.0.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 (48) hide show
  1. mpt_tool-5.0.0/.gitignore +173 -0
  2. mpt_tool-5.0.0/LICENSE +201 -0
  3. mpt_tool-5.0.0/PKG-INFO +299 -0
  4. mpt_tool-5.0.0/docs/PROJECT_DESCRIPTION.md +286 -0
  5. mpt_tool-5.0.0/mpt_tool/__init__.py +0 -0
  6. mpt_tool-5.0.0/mpt_tool/cli.py +76 -0
  7. mpt_tool-5.0.0/mpt_tool/commands/__init__.py +4 -0
  8. mpt_tool-5.0.0/mpt_tool/commands/base.py +27 -0
  9. mpt_tool-5.0.0/mpt_tool/commands/data.py +23 -0
  10. mpt_tool-5.0.0/mpt_tool/commands/errors.py +9 -0
  11. mpt_tool-5.0.0/mpt_tool/commands/factory.py +45 -0
  12. mpt_tool-5.0.0/mpt_tool/commands/fake.py +26 -0
  13. mpt_tool-5.0.0/mpt_tool/commands/list.py +33 -0
  14. mpt_tool-5.0.0/mpt_tool/commands/new_data.py +30 -0
  15. mpt_tool-5.0.0/mpt_tool/commands/new_schema.py +30 -0
  16. mpt_tool-5.0.0/mpt_tool/commands/schema.py +23 -0
  17. mpt_tool-5.0.0/mpt_tool/commands/validators.py +24 -0
  18. mpt_tool-5.0.0/mpt_tool/config.py +22 -0
  19. mpt_tool-5.0.0/mpt_tool/constants.py +4 -0
  20. mpt_tool-5.0.0/mpt_tool/enums.py +41 -0
  21. mpt_tool-5.0.0/mpt_tool/errors.py +9 -0
  22. mpt_tool-5.0.0/mpt_tool/managers/__init__.py +5 -0
  23. mpt_tool-5.0.0/mpt_tool/managers/encoders.py +15 -0
  24. mpt_tool-5.0.0/mpt_tool/managers/errors.py +25 -0
  25. mpt_tool-5.0.0/mpt_tool/managers/file_migration.py +132 -0
  26. mpt_tool-5.0.0/mpt_tool/managers/state/__init__.py +0 -0
  27. mpt_tool-5.0.0/mpt_tool/managers/state/airtable.py +108 -0
  28. mpt_tool-5.0.0/mpt_tool/managers/state/base.py +49 -0
  29. mpt_tool-5.0.0/mpt_tool/managers/state/factory.py +32 -0
  30. mpt_tool-5.0.0/mpt_tool/managers/state/file.py +64 -0
  31. mpt_tool-5.0.0/mpt_tool/migration/__init__.py +4 -0
  32. mpt_tool-5.0.0/mpt_tool/migration/base.py +25 -0
  33. mpt_tool-5.0.0/mpt_tool/migration/data_base.py +10 -0
  34. mpt_tool-5.0.0/mpt_tool/migration/mixins/__init__.py +4 -0
  35. mpt_tool-5.0.0/mpt_tool/migration/mixins/airtable_client.py +29 -0
  36. mpt_tool-5.0.0/mpt_tool/migration/mixins/mpt_client.py +31 -0
  37. mpt_tool-5.0.0/mpt_tool/migration/schema_base.py +10 -0
  38. mpt_tool-5.0.0/mpt_tool/models.py +140 -0
  39. mpt_tool-5.0.0/mpt_tool/py.typed +0 -0
  40. mpt_tool-5.0.0/mpt_tool/renders.py +30 -0
  41. mpt_tool-5.0.0/mpt_tool/templates.py +12 -0
  42. mpt_tool-5.0.0/mpt_tool/use_cases/__init__.py +11 -0
  43. mpt_tool-5.0.0/mpt_tool/use_cases/apply_migration.py +42 -0
  44. mpt_tool-5.0.0/mpt_tool/use_cases/errors.py +17 -0
  45. mpt_tool-5.0.0/mpt_tool/use_cases/list_migrations.py +35 -0
  46. mpt_tool-5.0.0/mpt_tool/use_cases/new_migration.py +22 -0
  47. mpt_tool-5.0.0/mpt_tool/use_cases/run_migrations.py +98 -0
  48. mpt_tool-5.0.0/pyproject.toml +215 -0
@@ -0,0 +1,173 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ out.xml
49
+ *.cover
50
+ *.py,cover
51
+ .hypothesis/
52
+ .pytest_cache/
53
+ cover/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ .pybuilder/
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ # For a library or package, you might want to ignore these files since the code is
88
+ # intended to run in multiple environments; otherwise, check them in:
89
+ # .python-version
90
+
91
+ # pipenv
92
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
94
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
95
+ # install all needed dependencies.
96
+ #Pipfile.lock
97
+
98
+ # poetry
99
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
100
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
101
+ # commonly ignored for libraries.
102
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
103
+ #poetry.lock
104
+
105
+ # pdm
106
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
107
+ #pdm.lock
108
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
109
+ # in version control.
110
+ # https://pdm.fming.dev/#use-with-ide
111
+ .pdm.toml
112
+
113
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
114
+ __pypackages__/
115
+
116
+ # Celery stuff
117
+ celerybeat-schedule
118
+ celerybeat.pid
119
+
120
+ # SageMath parsed files
121
+ *.sage.py
122
+
123
+ # Environments
124
+ .env
125
+ .venv
126
+ env/
127
+ venv/
128
+ ENV/
129
+ env.bak/
130
+ venv.bak/
131
+
132
+ # Spyder project settings
133
+ .spyderproject
134
+ .spyproject
135
+
136
+ # Rope project settings
137
+ .ropeproject
138
+
139
+ # mkdocs documentation
140
+ /site
141
+
142
+ # mypy
143
+ .mypy_cache/
144
+ .dmypy.json
145
+ dmypy.json
146
+
147
+ # Pyre type checker
148
+ .pyre/
149
+
150
+ # pytype static type analyzer
151
+ .pytype/
152
+
153
+ # Cython debug symbols
154
+ cython_debug/
155
+
156
+ # PyCharm
157
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
158
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
159
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
160
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
161
+ #.idea/
162
+
163
+ .devcontainer/
164
+ .vscode
165
+
166
+ .DS_Store
167
+ .ruff_cache
168
+ .idea
169
+
170
+ # Specific project gitignore
171
+ migrations/
172
+ .migrations-state.json
173
+ settings.py
mpt_tool-5.0.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,299 @@
1
+ Metadata-Version: 2.4
2
+ Name: mpt-tool
3
+ Version: 5.0.0
4
+ Summary: Migration tool for extensions
5
+ Author: SoftwareOne AG
6
+ License: Apache-2.0 license
7
+ License-File: LICENSE
8
+ Requires-Python: <4,>=3.12
9
+ Requires-Dist: mpt-api-client==5.0.*
10
+ Requires-Dist: pyairtable==3.3.*
11
+ Requires-Dist: typer>=0.9.0
12
+ Description-Content-Type: text/markdown
13
+
14
+ # mpt-tool CLI
15
+
16
+ mpt-tool is a command-line utility to scaffold, run, and audit migrations for MPT extensions.
17
+
18
+ ## Quick Start
19
+ 1. **Install the tool:**
20
+ ```bash
21
+ pip install mpt-tool
22
+ ```
23
+ 2. **Create your first migration:**
24
+ ```bash
25
+ mpt-tool migrate --new-data sync_users
26
+ ```
27
+ 3. **Edit the generated file in the migrations/ folder**
28
+ 4. **Run all pending migrations**
29
+ ```bash
30
+ mpt-tool migrate --data
31
+ ```
32
+
33
+ ## Installation
34
+
35
+ Install with pip or your favorite PyPI package manager:
36
+
37
+ ```bash
38
+ pip install mpt-tool
39
+ ```
40
+
41
+ ```bash
42
+ uv add mpt-tool
43
+ ```
44
+
45
+ ## Prerequisites
46
+
47
+ - Python 3.12+ in your environment
48
+ - A `migrations/` folder in your project (it will be created automatically the first time you create a migration)
49
+ - Environment variables. See [Environment Variables](#environment-variables) for details.
50
+
51
+ ## Environment Variables
52
+
53
+ The tool uses the following environment variables:
54
+ - `STORAGE_TYPE`: Storage backend for migration state (`local` or `airtable`, default: `local`). See [Storage Configuration](#storage)
55
+ - `MPT_API_TOKEN`: Your MPT API key (required when using `MPTAPIClientMixin`)
56
+ - `MPT_API_BASE_URL`: The MPT API base url (required when using `MPTAPIClientMixin`)
57
+ - `AIRTABLE_API_KEY`: Your Airtable API key (required when using `AirtableAPIClientMixin` or when `STORAGE_TYPE=airtable`)
58
+
59
+ ## Configuration
60
+
61
+ ### Storage
62
+
63
+ The tool supports two storage backends: local and Airtable. By default, it uses the local storage.
64
+
65
+ Local storage is the simplest option and is suitable for development and testing. However, it is not suitable for production deployments.
66
+ The state is stored in a `.migrations-state.json` file in your project root.
67
+
68
+ Airtable storage is recommended for production deployments. It allows you to track migration progress across multiple deployments.
69
+
70
+ #### Local Storage
71
+ No additional configuration is required.
72
+
73
+ #### Airtable Storage
74
+
75
+ Airtable configuration is done via environment variables:
76
+ - `AIRTABLE_API_KEY`: Your Airtable API key
77
+ - `STORAGE_AIRTABLE_BASE_ID`: Your Airtable base ID
78
+ - `STORAGE_AIRTABLE_TABLE_NAME`: The name of the table to store migration state
79
+
80
+ Your Airtable table must have the following columns:
81
+
82
+ | Column Name | Field Type | Required |
83
+ |---------------|-----------------------------|:--------:|
84
+ | order_id | number | ✅ |
85
+ | migration_id | singleLineText | ✅ |
86
+ | started_at | dateTime | ❌ |
87
+ | applied_at | dateTime | ❌ |
88
+ | type | singleSelect (data, schema) | ✅ |
89
+
90
+
91
+ **Airtable configuration steps:**
92
+ 1. Create a new table in your Airtable base (or use an existing one)
93
+ 2. Add the columns listed above with the specified field types
94
+ 3. Set the environment variables with your base ID and table name
95
+
96
+ ## Usage
97
+
98
+ ### Creating a New Migration
99
+ 1. Decide the migration type (**data** or **schema**).
100
+ - **Data**: run after a release is deployed. Can take hours or days. Executed while MPT is running (e.g., updating product parameters, synchronizing Assets with external data)
101
+ - **Schema**: run before a release is deployed. Must be fast (not more than 15 min). Executed without ensuring the MPT is running (e.g., adding columns in Airtable)
102
+ 2. Run the appropriate command:
103
+ ```bash
104
+ # Data migration
105
+ mpt-tool migrate --new-data "migration_name"
106
+ ```
107
+ ```bash
108
+ # Schema migration
109
+ mpt-tool migrate --new-schema "migration_name"
110
+ ```
111
+
112
+ A new file is created in `migrations/` with a timestamped prefix (e.g., `20260113180013_migration_name.py`) and a prefilled `Command` class.
113
+
114
+ order_id: timestamp prefix (e.g., `20260113180013`)
115
+ migration_id: user-provided name (e.g., `migration_name`)
116
+ file: generated file name (e.g., `20260113180013_migration_name.py`)
117
+
118
+ **Generated file structure:**
119
+
120
+ ```python
121
+ from mpt_tool.migration import DataBaseMigration # or SchemaBaseMigration
122
+
123
+
124
+ class Migration(DataBaseMigration):
125
+ def run(self):
126
+ # implement your logic here
127
+ pass
128
+ ```
129
+
130
+ #### Using Mixins
131
+ You can add mixins to your migration commands to access external services:
132
+
133
+ ```python
134
+ from mpt_tool.migration import DataBaseMigration
135
+ from mpt_tool.migration.mixins import MPTAPIClientMixin, AirtableAPIClientMixin
136
+
137
+
138
+ class Migration(DataBaseMigration, MPTAPIClientMixin, AirtableAPIClientMixin):
139
+ def run(self):
140
+ # Access MPT API
141
+ agreement = self.mpt_client.commerce.agreements.get("AGR-1234-5678-9012")
142
+ self.log.info(f"Agreement id: {agreement.id}")
143
+
144
+ # Access Airtable
145
+ table = self.airtable_client.table("app_id", "table_name")
146
+ records = table.all()
147
+
148
+ self.log.info(f"Processed {len(records)} records")
149
+ ```
150
+
151
+ ### Running Migrations
152
+ - **Run all pending data migrations:**
153
+ ```bash
154
+ mpt-tool migrate --data
155
+ ```
156
+ - **Run all pending schema migrations:**
157
+ ```bash
158
+ mpt-tool migrate --schema
159
+ ```
160
+
161
+ Migrations are executed in order based on their order_id (timestamp). The tool automatically:
162
+ - Validates the migration folder structure
163
+ - Skips migrations that have already been applied (applied_at is not null)
164
+ - Tracks execution status in the state storage (`.migrations-state.json` or Airtable table)
165
+ - Logs migration progress
166
+ - Handles errors gracefully and updates state accordingly
167
+
168
+ **Migration State File (`.migrations-state.json`):**
169
+ ```json
170
+ {
171
+ "data_example": {
172
+ "migration_id": "data_example",
173
+ "order_id": 20260113180013,
174
+ "started_at": "2026-01-13T18:05:20.000000",
175
+ "applied_at": "2026-01-13T18:05:23.123456",
176
+ "type": "data"
177
+ },
178
+ "schema_example": {
179
+ "migration_id": "schema_example",
180
+ "order_id": 20260214121033,
181
+ "started_at": null,
182
+ "applied_at": null,
183
+ "type": "schema"
184
+ }
185
+ }
186
+ ```
187
+ **Migration Table (Airtable):**
188
+
189
+ | order_id | migration_id | started_at | applied_at | type |
190
+ |----------------|----------------|----------------------------|----------------------------|--------|
191
+ | 20260113180013 | data_example | 2026-01-13T18:05:20.000000 | 2026-01-13T18:05:23.123456 | data |
192
+ | 20260214121033 | schema_example | | | schema |
193
+
194
+
195
+ If a migration succeeds during execution:
196
+ * The started_at timestamp is recorded
197
+ * The applied_at timestamp is recorded
198
+
199
+ If a migration fails during execution:
200
+ * The started_at timestamp is recorded
201
+ * The applied_at field remains null
202
+ * The error is logged
203
+ * Later runs will retry the failed migration as applied_at is null, unless `--fake` is used to mark it as applied
204
+
205
+
206
+ ### Fake Mode
207
+ To mark a migration as applied without running it:
208
+
209
+ ```bash
210
+ mpt-tool migrate --fake MIGRATION_ID
211
+ ```
212
+
213
+ Where `MIGRATION_ID` is the filename without `order_id` and `.py` (e.g., `test1`).
214
+
215
+ **Example:**
216
+ - File: `20260113180013_sync_users.py`
217
+ - Migration ID: `sync_users`
218
+
219
+ If the migration doesn't exist in the migrations folder:
220
+ * An error is logged and the command exits
221
+
222
+ If the migration exists:
223
+ * The migration state is created if it doesn't exist yet or updated:
224
+ * The started_at field is set as null
225
+ * The applied_at timestamp is recorded
226
+
227
+ ### Listing Migrations
228
+ To see all migrations and their status:
229
+
230
+ ```bash
231
+ mpt-tool migrate --list
232
+ ```
233
+
234
+ The output shows execution order, status, and timestamps.
235
+
236
+ The status column is derived from the persisted timestamps:
237
+
238
+ | Status | Condition |
239
+ |-------------|---------------------------------------------------------------|
240
+ | running | `started_at` is set and `applied_at` is empty |
241
+ | failed | `started_at` and `applied_at` are empty for an existing state |
242
+ | faked | `started_at` is empty and `applied_at` is set |
243
+ | applied | Both `started_at` and `applied_at` are set |
244
+ | not applied | No state entry exists for the migration file |
245
+
246
+ ### Getting Help
247
+ Run `mpt-tool --help` to see all available commands and params:
248
+ ```bash
249
+ mpt-tool --help
250
+ mpt-tool migrate --help
251
+ ```
252
+
253
+
254
+ ## Best Practices
255
+
256
+ ### Migration Naming
257
+ - Use descriptive, snake_case names (e.g., `add_user_table`, `fix_null_emails`, `sync_agreements_from_api`)
258
+ - Keep names concise but meaningful
259
+ - Avoid generic names like `migration1`, `fix_bug`, or `update`
260
+
261
+ ### Version Control
262
+ - Never modify a migration that has been applied in production
263
+ - Create a new migration to fix issues from a previous one
264
+
265
+
266
+ ## Troubleshooting
267
+
268
+ ### Common Issues
269
+
270
+ **Migrations not detected:**
271
+ - Ensure files are in the `migrations/` folder
272
+ - Verify filename follows the pattern: `<timestamp>_<migration_id>.py` (e.g., `20260121120000_migration_name.py`)
273
+
274
+ **Migration fails to run:**
275
+ - Review the error message in the terminal output
276
+ - Check your `Migration.run()` implementation for syntax errors
277
+ - Fix the issue and re-run the migration or use `--fake` to mark it as applied
278
+
279
+ **NOTE:** There is currently no automatic rollback mechanism. If a migration partially modifies data before failing, you must manually revert those changes or create a new migration to fix the state.
280
+
281
+ **Mixin errors (ValueError):**
282
+ - Verify all required environment variables are set
283
+ - Check variable names match exactly (case-sensitive)
284
+
285
+ **Duplicate migration IDs:**
286
+ - The tool prevents duplicate migration IDs automatically
287
+ - If you see this error, check for files with the same name in the `migrations/` folder
288
+ - Delete or rename the duplicate file
289
+
290
+ **Migration already applied:**
291
+ - If you need to re-run a migration, either:
292
+ - Remove its entry from the state storage (use with caution)
293
+ - Create a new migration with the updated logic
294
+ - Never modify an already-applied migration in production
295
+
296
+
297
+ ## Development
298
+
299
+ For development purposes, please, check the Readme in the GitHub repository.