|
1 | 1 | # definitions-lib |
2 | 2 |
|
| 3 | +This is an extension library for |
| 4 | +[alembic](https://alembic.sqlalchemy.org/en/latest/), a database migration tool |
| 5 | +for usage with SQLAlchemy. |
3 | 6 |
|
| 7 | +# Features |
4 | 8 |
|
5 | | -actually repeats all alembic commands |
| 9 | +Note that currently, all of the features are postgres-exclusive, and |
| 10 | +making them database-agnostic is **not** the main focus. |
6 | 11 |
|
7 | | -except you need to write `python3 -m definitions` instead of `alembic` |
| 12 | +- access group management (regular/sensitive read/write groups for each schema |
| 13 | + for fine-grained access control) |
| 14 | +- seamless encryption (column-based encryption & decryption routines, key |
| 15 | + generation, integration with access groups) \[work in progress\] |
| 16 | +- autogeneration support for each feature |
| 17 | +- mirrored alembic interface (suitable as a drop-in replacement) |
8 | 18 |
|
9 | | -ex. `python3 -m definitions init migrations` |
| 19 | +# Requirements |
| 20 | + |
| 21 | +- alembic `>=1.17.2` |
| 22 | +- python (exact versions to be determined) |
| 23 | +- sqlalchemy that works with required alembic version |
| 24 | + |
| 25 | +# Installation & usage |
| 26 | + |
| 27 | +First of all, you need an alembic configuration, see |
| 28 | +[alembic docs](https://alembic.sqlalchemy.org/en/latest/tutorial.html) for setup |
| 29 | +steps. |
| 30 | + |
| 31 | +After you have a working alembic setup, run `pip install definitions-lib`, and |
| 32 | +you are done. |
| 33 | + |
| 34 | +To use definitions-lib, simply replace `alembic` command with `python -m |
| 35 | +definitions` in your usual workflow. For example: |
| 36 | + |
| 37 | +- initialize database state: `python -m definitions init` |
| 38 | +- generate revision: `python -m definitions revision --autogenerate -m message` |
| 39 | +- e.t.c. |
| 40 | + |
| 41 | +Note: You can still use `alembic` for some commands, like |
| 42 | +`upgrade`/`downgrade`. But there has not been enough experimentation to know for |
| 43 | +sure which commands do and which do not require running `python -m definitions` |
| 44 | + |
| 45 | +# Notes for the user |
| 46 | + |
| 47 | +- Just like alembic, definitions-lib needs to generate and manage a table to |
| 48 | + store metadata. Unlike alembic, this table contains records for |
| 49 | + each table in your database. Additionally, definitions-lib may shadow-manage |
| 50 | + some tables (key tables, for instance). For a detailed explaination, see API |
| 51 | + section. |
| 52 | +- This table will be placed in the same schema as `alembic_version` (default |
| 53 | + schema is `public`). |
| 54 | + |
| 55 | +# API |
| 56 | + |
| 57 | +All of the magic happenes when you define additional fields in the `info` |
| 58 | +dict for your table (`__table_args__.info` in case of declarative definition). |
| 59 | + |
| 60 | +## sensitive |
| 61 | + |
| 62 | +To mark table as sensitive, set `info["sensitive"] = True`. This will tell |
| 63 | +definitions-lib that an additional set of read/write groups must be created for |
| 64 | +the current schema. There are no additional arguments. |
| 65 | + |
| 66 | +If `info["sensitive"]` is set to `False` or removed after the sensitive groups |
| 67 | +are created, these groups will be dropped (unless other tables require |
| 68 | +them). |
| 69 | + |
| 70 | +## encrypted |
| 71 | + |
| 72 | +To mark table as encrypted, set `info["encrypted"] = True`. In addition to that, |
| 73 | +you must set `info["encryption"] = param_dict`, where `param_dict` has following |
| 74 | +parameters: |
| 75 | + |
| 76 | +- `id: str` - identiry column name for encryption. Used to retrieve and generate |
| 77 | + encryption keys (they are unique accross id columns) |
| 78 | +- `keys: str` - table name that stores encryption keys, in format "{schema}.{table}" |
| 79 | + OR "{table}". |
| 80 | +- `keys_schema: str` (optional) - if `keys` does not specify schema, this parameter |
| 81 | + may be used to specify it. |
| 82 | +- `columns: list[str]`: array of column names to be encrypted. Should never |
| 83 | + contain `id`. |
| 84 | + |
| 85 | +Currently, `keys` table must exist before encryption operation is ran, and it |
| 86 | +must follow a defined structure. However, it is planned to make key table |
| 87 | +automatically managed, keeping an option to manage it manually, so that the user |
| 88 | +does not have to pollute their definitions. |
| 89 | + |
| 90 | +# Current TODOs: |
| 91 | + |
| 92 | +## Encryption |
| 93 | + |
| 94 | +- [ ] Create custom CreateTableOp/DropTableOp to support shadow tables and info |
| 95 | + table inserts. |
| 96 | +- [ ] Create render functions for encryption operations |
| 97 | +- [ ] Finish encryption comparators (use info table) |
| 98 | + |
| 99 | +## Tests |
| 100 | + |
| 101 | +- [ ] Write tests (using sqlalchemy.testing) ### Documentation |
| 102 | +- [ ] info dict API |
| 103 | +- [ ] docs.profcomff.com |
| 104 | + |
| 105 | +## Historical tables |
| 106 | + |
| 107 | +This feature is to be discussed, but the basic idea is to implement TTLs for |
| 108 | +historic tables. |
| 109 | + |
| 110 | +# Contribution |
| 111 | + |
| 112 | +Until testing suite is added, there are no guidelines. |
| 113 | + |
| 114 | +TODO: review this section. |
0 commit comments