| 1 | # Asterisk Database Manager |
| 2 | |
| 3 | Asterisk includes optional database integration for a variety of features. |
| 4 | The purpose of this effort is to assist in managing the database schema |
| 5 | for Asterisk database integration. |
| 6 | |
| 7 | This is implemented as a set of repositories that contain database schema |
| 8 | migrations, using [Alembic](http://alembic.readthedocs.org). The existing |
| 9 | repositories include: |
| 10 | |
| 11 | * `cdr` - Table used for Asterisk to store CDR records |
| 12 | * `config` - Tables used for Asterisk realtime configuration |
| 13 | * `queue_log` - Table used for Asterisk to store Queue Log records |
| 14 | * `voicemail` - Tables used for `ODBC_STORAGE` of voicemail messages |
| 15 | |
| 16 | Alembic uses SQLAlchemy, which has support for |
| 17 | [many databases](http://docs.sqlalchemy.org/en/rel_0_8/dialects/index.html). |
| 18 | |
| 19 | |
| 20 | ## Example Usage |
| 21 | |
| 22 | First, create an ini file that contains database connection details. For help |
| 23 | with connection string details, see the |
| 24 | [SQLAlchemy docs](http://docs.sqlalchemy.org/en/rel_0_8/core/engines.html#database-urls). |
| 25 | |
| 26 | $ cp config.ini.sample config.ini |
| 27 | ... edit config.ini and change sqlalchemy.url ... |
| 28 | |
| 29 | Next, bring the database up to date with the current schema. |
| 30 | |
| 31 | $ alembic -c config.ini upgrade head |
| 32 | |
| 33 | In the future, as additional database migrations are added, you can run |
| 34 | alembic again to migrate the existing tables to the latest schema. |
| 35 | |
| 36 | $ alembic -c config.ini upgrade head |
| 37 | |
| 38 | The migrations support both upgrading and downgrading. You could go all the |
| 39 | way back to where you started with no tables by downgrading back to the base |
| 40 | revision. |
| 41 | |
| 42 | $ alembic -c config.ini downgrade base |
| 43 | |
| 44 | `base` and `head` are special revisions. You can refer to specific revisions |
| 45 | to upgrade or downgrade to, as well. |
| 46 | |
| 47 | $ alembic -c config.ini upgrade 4da0c5f79a9c |
| 48 | |
| 49 | ## Offline Mode |
| 50 | |
| 51 | If you would like to just generate the SQL statements that would have been |
| 52 | executed, you can use alembic's offline mode. |
| 53 | |
| 54 | $ alembic -c config.ini upgrade head --sql |
| 55 | |
| 56 | ## Adding Database Migrations |
| 57 | |
| 58 | The best way to learn about how to add additional database migrations is to |
| 59 | refer to the [Alembic documentation](http://alembic.readthedocs.org). |
| 60 | |
| 61 | ### Notes |
| 62 | |
| 63 | * For boolean columns, always use the AST_BOOL_VALUES type. |
| 64 | Example: |
| 65 | |
| 66 | ``` |
| 67 | from alembic import op |
| 68 | import sqlalchemy as sa |
| 69 | # This works for MySQL/MariaDB and others as well |
| 70 | from sqlalchemy.dialects.postgresql import ENUM |
| 71 | |
| 72 | AST_BOOL_NAME = 'ast_bool_values' |
| 73 | AST_BOOL_VALUES = [ '0', '1', |
| 74 | 'off', 'on', |
| 75 | 'false', 'true', |
| 76 | 'no', 'yes' ] |
| 77 | |
| 78 | def upgrade(): |
| 79 | # ast_bool_values have already been created, so use postgres enum object type |
| 80 | # to get around "already created" issue - works okay with MySQL/MariaDB and others. |
| 81 | ast_bool_values = ENUM(*AST_BOOL_VALUES, name=AST_BOOL_NAME, create_type=False) |
| 82 | op.add_column('ps_endpoints', sa.Column('suppress_moh_on_sendonly', ast_bool_values)) |
| 83 | |
| 84 | def downgrade(): |
| 85 | if op.get_context().bind.dialect.name == 'mssql': |
| 86 | op.drop_constraint('ck_ps_endpoints_suppress_moh_on_sendonly_ast_bool_values', 'ps_endpoints') |
| 87 | op.drop_column('ps_endpoints', 'suppress_moh_on_sendonly') |
| 88 | ``` |
| 89 | |
| 90 | |
| 91 | Older scripts used YESNO_VALUES but that is no longer supported. |
| 92 |