Template for working with alembic
and PostGIS extension for PostgreSQL.
- This project uses Anaconda as environment manager. Please, make sure you have installed Anaconda or Miniconda on your machine.
- Docker-Compose is used to create a local instance of
PostGIS
database calledgeo
. See docker-compose.yml for more info. - Configuration settings are located in config.py - please, do not hardcode secrets here.
- This project uses git-hooks to check code quality when creating commits. See Git-Hooks section for more info.
- Create:
conda env create -f env.yaml
- Update:
conda env update -f env.yaml
- Start DB container:
docker-compose up -d
- Run migration scripts: see Migrations section
This project uses pre-commit
package for managing and maintaining pre-commit hooks.
To ensure code quality - please make sure that you have it configured.
- Install
pre-commit
and following packages:isort
,black
,flake8
,pytest
. You can do so by updating your current environment. Activate your environment and then run:conda env update -f ./env.yaml
- Install pre-commit hooks by running:
pre-commit install
- The command above will automatically run formatters, code checks and other steps defined in the
.pre-commit-config.yaml
- All of those checks will also be run whenever a new commit is being created i.e. when you run
git commit -m "blah"
- You can also run it manually with this command:
pre-commit run --all-files
- IMPORTANT! You can manually disable pre-commit hooks by running:
pre-commit uninstall
Use this only in exceptional cases.
Ensure you have access to the DB. By default, locally hosted PostgreSQL instance is used. See config.py for connection details.
This project uses Alembic
for migration management. Change your current working directory to:
./src/db
. After that you'll be able to run basic commands:
(click to expand)
Check history
alembic history
Add new (empty) migration manually
alembic revision -m "migration message"
This will generate migration script like this:
"""migration message
Revision ID: 3487cab3febf
Revises:
Create Date: 2021-11-03 18:00:25.288281
"""
# revision identifiers, used by Alembic.
revision = '3487cab3febf'
down_revision = None
branch_labels = None
from alembic import op
import sqlalchemy as sa
def upgrade():
pass
def downgrade():
pass
Add new autogenerated migration
alembic revision --autogenerate -m "migration message"
Assuming that we have 1 new entity named Location, this should result in a migration script like this:
"""Add location entity
Revision ID: bc805f79e5e5
Revises: 3487cab3febf
Create Date: 2021-11-03 18:15:13.302657
"""
import geoalchemy2
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision = "bc805f79e5e5"
down_revision = "3487cab3febf"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"location",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(), nullable=True),
sa.Column("created_date", sa.DateTime(timezone=True), nullable=True),
sa.Column(
"extent",
geoalchemy2.types.Geometry(geometry_type="POLYGON", srid=4326, from_text="ST_GeomFromEWKT", name="geometry"),
nullable=True,
),
sa.PrimaryKeyConstraint("id"),
schema="geo",
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("location", schema="geo")
# ### end Alembic commands ###
Upgrade DB to the newest migration
This will run all migrations in order, that they were created.
alembic upgrade head
Rollback last migration
This will roll back last migration.
alembic downgrade -1
Rollback N last migrations
This will roll back last N migrations.
alembic downgrade -N
Rollback all migrations
This will roll back all migrations.
alembic downgrade base
Always make sure the autogenerated migration scripts are valid and no unwanted stuff was added.
To learn more visit this resources: