Skip to content

Commit

Permalink
Docs: Update database migration instructions (#916)
Browse files Browse the repository at this point in the history
Expand the database documentation to introduce the user to the migration
process without going too deep into Alembic. Helps complete the example
presented on the page
  • Loading branch information
bartek committed Apr 28, 2020
1 parent a42657d commit c89e5cb
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/database.md
Expand Up @@ -86,6 +86,9 @@ app = Starlette(
)
```

Finally, you will need to create the database tables. It is recommended to use
Alembic, which we briefly go over in [Migrations](#migrations)

## Queries

Queries may be made with as [SQLAlchemy Core queries][sqlalchemy-core].
Expand Down Expand Up @@ -262,6 +265,34 @@ target_metadata = app.metadata
...
```

Then, using our notes example above, create an initial revision:

```shell
alembic revision -m "Create notes table"
```

And populate the new file (within `migrations/versions`) with the necessary directives:

```python

def upgrade():
op.create_table(
'notes',
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column("text", sqlalchemy.String),
sqlalchemy.Column("completed", sqlalchemy.Boolean),
)

def downgrade():
op.drop_table('notes')
```

And run your first migration. Our notes app can now run!

```shell
alembic upgrade head
```

**Running migrations during testing**

It is good practice to ensure that your test suite runs the database migrations
Expand Down

0 comments on commit c89e5cb

Please sign in to comment.