Skip to content

Commit

Permalink
Add PostreSQL as a main database
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasha Radchenko committed Mar 31, 2021
1 parent 096508f commit 2a0164a
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 57 deletions.
9 changes: 7 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ MAIL_USE_SSL=True
MAIL_USERNAME=<[email protected]>
MAIL_PASSWORD=<account_password>

SQLALCHEMY_DATABASE_URI="sqlite:///autopsy.db"
SQLALCHEMY_TRACK_MODIFICATIONS=True
DATABASE_HOST=<database_host>
DATABASE_USER=docker
DATABASE_PASSWORD=docker
DATABASE_PORT=5432
DATABASE_NAME=docker

SQLALCHEMY_DATABASE_URI="postgresql://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}"
SQLALCHEMY_TRACK_MODIFICATIONS=True
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ venv*
.env
__pycache__*
*.db
pgdata/*
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ Autopsy spreads postmortems culture, involves people to learn on the failures.
## Features

* Dashboard shows latest and random postmortems
* Postmortems shows list of postmortems with CRUD
* Postmortems provides lists postmortems with CRU(D) capabilities
* Search performs search operation in Postmortems' names
* Admin page manages all the service's data in Flask-admin way
* Admin manages all the service's data in Flask-admin way
* Users can register, login, update the profile, create support tickets (attaching screenshot), logout and restore their password via email.

## Roadmap
Expand All @@ -52,7 +52,7 @@ Autopsy spreads postmortems culture, involves people to learn on the failures.
## Requirements

* python3
* sqlite*
* PostgreSQL
* SMTP server (for password reset)

## Installation
Expand Down Expand Up @@ -88,17 +88,28 @@ cp .env.example .env
|MAIL_USE_SSL| True | depends | SMTP server SSL|
|MAIL_USERNAME| <[email protected]> | Yes | Username for no-reply@ account|
|MAIL_PASSWORD| <account_password>| Yes | Password for no-reply account|
|SQLALCHEMY_DATABASE_URI| "sqlite:///autopsy.db"| Yes | Database URI|
|SQLALCHEMY_TRACK_MODIFICATIONS| True | depends| Track DB modification|
|DATABASE_HOST| <database_host> | Yes | Database URI|
|DATABASE_USER| docker | Yes | Database Username|
|DATABASE_PASSWORD| docker | Yes | Database Password|
|DATABASE_PORT| 5432 | depends | Database Port|
|DATABASE_NAME| docker | Yes | Database Name|
|SQLALCHEMY_DATABASE_URI| "postgresql://...."| Yes | Database URI|
|SQLALCHEMY_TRACK_MODIFICATIONS| True | No | Track DB modification|

#### Install dependencies
```
pip3 install -r requirements.txt
```

#### Apply DB migrations
```
flask db migrate
flas db upgrade
```

#### Run app
```
ussgi app.ini
uswgi app.ini
```

#### Go to
Expand All @@ -107,6 +118,24 @@ http://localhost:5000/
```


## PostgreSQL development instance
It is available to spin-up PostgreSQL database in container via docker-compose.
```
cd docker/db
docker-compose up -d
```

Database credentials can be found in `docker/db/docker-compose.yml`:
```
- APP_DB_USER=docker
- APP_DB_PASS=docker
- APP_DB_NAME=docker
```

:hankey: Be aware from running the production database instance this ways and other monsters!



## Icon authors:
[DinosoftLabs](https://www.flaticon.com/authors/dinosoftlabs)
[FreePik](https://www.freepik.com/)
Expand Down
10 changes: 10 additions & 0 deletions docker/db/db_init/01-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e
export PGPASSWORD=$POSTGRES_PASSWORD;
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER $APP_DB_USER WITH PASSWORD '$APP_DB_PASS';
CREATE DATABASE $APP_DB_NAME;
GRANT ALL PRIVILEGES ON DATABASE $APP_DB_NAME TO $APP_DB_USER;
\connect $APP_DB_NAME $APP_DB_USER
EOSQL

22 changes: 22 additions & 0 deletions docker/db/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3'

services:
postgres:
image: postgres
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
timeout: 45s
interval: 10s
retries: 10
restart: always
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=password
- APP_DB_USER=docker
- APP_DB_PASS=docker
- APP_DB_NAME=docker
volumes:
- ./db_init:/docker-entrypoint-initdb.d/
- ./pgdata:/var/lib/postgresql/data
ports:
- 5432:5432
59 changes: 59 additions & 0 deletions migrations/versions/ee9c2caf9d31_initial_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Initial migration
Revision ID: ee9c2caf9d31
Revises:
Create Date: 2021-04-01 00:39:58.002389
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'ee9c2caf9d31'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_name', sa.String(length=20), nullable=False),
sa.Column('user_email', sa.String(length=20), nullable=False),
sa.Column('user_password', sa.String(length=100), nullable=False),
sa.Column('user_image', sa.String(length=20), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('user_email')
)
op.create_table('mortems',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('mortem_name', sa.String(length=100), nullable=False),
sa.Column('mortem_url', sa.String(length=16), nullable=False),
sa.Column('mortem_content', sa.Text(), nullable=False),
sa.Column('mortem_created', sa.DateTime(), nullable=False),
sa.Column('mortem_updated', sa.DateTime(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('support',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('support_subject', sa.String(length=100), nullable=False),
sa.Column('support_content', sa.Text(), nullable=False),
sa.Column('support_created', sa.DateTime(), nullable=False),
sa.Column('support_attach', sa.LargeBinary(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('support')
op.drop_table('mortems')
op.drop_table('users')
# ### end Alembic commands ###
49 changes: 0 additions & 49 deletions migrations/versions/f674c4c14cf9_.py

This file was deleted.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ itsdangerous==1.1.0
Jinja2==2.11.3
Mako==1.1.4
MarkupSafe==1.1.1
psycopg2==2.8.6
pycparser==2.20
python-dateutil==2.8.1
python-dotenv==0.16.0
Expand Down

0 comments on commit 2a0164a

Please sign in to comment.