Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev: Add initial migration and Running migration instruction to README #1133

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,43 @@ To run black:
black .
```

### Run migration Scripts
#### If this is you first time running migration:
Run the below command to attach the current alembic version number on the repo to your local db.
* Note: For new contributors who just completed environment setup, run the following command **AFTER** running the application for the first time.

```
flask db stamp head
```

#### If you have run migration before and have just pulled the latest update from upstream that includes newer version/s of migration script.

Confirm you have the same version number as the second last one on the repo (if there're multiple versions). To do this, run the below commmand
```
flask db history // to check the list of version history on the repo
flask db current // to check your latest version on the local db
```
If you're missing other updates listed beside the latest version, run the below command **IN SEQUENTIAL ORDER** one command at a time from the first update you've missed. If you are up to date and only need to update to the latest version, you don't need to specify the `missing version number`.

```
flask db upgrade <missing version number>
```

#### If you are making changes to db model/s
1. make sure you have the latest version as per the upstream repo
2. make the necessary schema changes on your db model/s
3. run the following command to add the update onto the migration version history
```
flask db migrate -m "<message>"
```
where `<message>` is a clear message what the change was about. This will be your alembic version title.
You should see the new version inside the `versions` folder
3. run the following command to apply the update to your local db
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
3. run the following command to apply the update to your local db
4. run the following command to apply the update to your local db

careful with spaces between lines, not sure this will render the way you are thinking. I viewed the source and this number does not show up

```
flask db upgrade
```
**IMPORTANT!!** If you are **altering/modifying** an existing column inside an existing table, **YOU MUST CHECK** the auto-generated alembic script (new version file) to make sure the script is reflecting the schema changes. If this is not the case, you **MUST MODIFY** this script yourself accordingly.

## Documentation

Documentation for the project is hosted [here](https://anitab-org.github.io/mentorship-backend/). We use Docusaurus for maintaining the documentation of the project.
Expand Down
2 changes: 1 addition & 1 deletion migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

config.set_main_option(
"sqlalchemy.url",
current_app.config.get("SQLALCHEMY_DATABASE_URI").replace("%", "%%"),
str(current_app.extensions["migrate"].db.engine.url).replace("%", "%%"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change @mtreacy002 ? just asking because at the moment this came to the project, I thought it was kind of auto-generated code 🤔 but i am not sure

)
target_metadata = current_app.extensions["migrate"].db.metadata

Expand Down
129 changes: 129 additions & 0 deletions migrations/versions/0541c68e5b13_initial_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"""Initial migration

Revision ID: 0541c68e5b13
Revises:
Create Date: 2021-07-07 12:20:10.523552

"""
from alembic import op
from app.database.db_types.JsonCustomType import JsonCustomType
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "0541c68e5b13"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"tasks_list",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("tasks", JsonCustomType, nullable=True),
sa.Column("next_task_id", sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"users",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(length=30), nullable=True),
sa.Column("username", sa.String(length=30), nullable=True),
sa.Column("email", sa.String(length=254), nullable=True),
sa.Column("password_hash", sa.String(length=100), nullable=True),
sa.Column("registration_date", sa.Float(), nullable=True),
sa.Column("terms_and_conditions_checked", sa.Boolean(), nullable=True),
sa.Column("is_admin", sa.Boolean(), nullable=True),
sa.Column("is_email_verified", sa.Boolean(), nullable=True),
sa.Column("email_verification_date", sa.DateTime(), nullable=True),
sa.Column("current_mentorship_role", sa.Integer(), nullable=True),
sa.Column("membership_status", sa.Integer(), nullable=True),
sa.Column("bio", sa.String(length=500), nullable=True),
sa.Column("location", sa.String(length=80), nullable=True),
sa.Column("occupation", sa.String(length=80), nullable=True),
sa.Column("organization", sa.String(length=80), nullable=True),
sa.Column("slack_username", sa.String(length=80), nullable=True),
sa.Column("social_media_links", sa.String(length=500), nullable=True),
sa.Column("skills", sa.String(length=500), nullable=True),
sa.Column("interests", sa.String(length=200), nullable=True),
sa.Column("resume_url", sa.String(length=200), nullable=True),
sa.Column("photo_url", sa.String(length=200), nullable=True),
sa.Column("need_mentoring", sa.Boolean(), nullable=True),
sa.Column("available_to_mentor", sa.Boolean(), nullable=True),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("email"),
sa.UniqueConstraint("username"),
)
op.create_table(
"mentorship_relations",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("mentor_id", sa.Integer(), nullable=True),
sa.Column("mentee_id", sa.Integer(), nullable=True),
sa.Column("action_user_id", sa.Integer(), nullable=False),
sa.Column("creation_date", sa.Float(), nullable=False),
sa.Column("accept_date", sa.Float(), nullable=True),
sa.Column("start_date", sa.Float(), nullable=True),
sa.Column("end_date", sa.Float(), nullable=True),
sa.Column(
"state",
sa.Enum(
"PENDING",
"ACCEPTED",
"REJECTED",
"CANCELLED",
"COMPLETED",
name="mentorshiprelationstate",
),
nullable=False,
),
sa.Column("notes", sa.String(length=400), nullable=True),
sa.Column("tasks_list_id", sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(
["mentee_id"],
["users.id"],
),
sa.ForeignKeyConstraint(
["mentor_id"],
["users.id"],
),
sa.ForeignKeyConstraint(
["tasks_list_id"],
["tasks_list.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"tasks_comments",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=True),
sa.Column("task_id", sa.Integer(), nullable=True),
sa.Column("relation_id", sa.Integer(), nullable=True),
sa.Column("creation_date", sa.Float(), nullable=False),
sa.Column("modification_date", sa.Float(), nullable=True),
sa.Column("comment", sa.String(length=400), nullable=False),
sa.ForeignKeyConstraint(
["relation_id"],
["mentorship_relations.id"],
),
sa.ForeignKeyConstraint(
["task_id"],
["tasks_list.id"],
),
sa.ForeignKeyConstraint(
["user_id"],
["users.id"],
),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("tasks_comments")
op.drop_table("mentorship_relations")
op.drop_table("users")
op.drop_table("tasks_list")
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def create_app(config_filename: str) -> Flask:

db.init_app(app)

migrate = Migrate(app, db)
migrate = Migrate(app, db, render_as_batch=True)

from app.api.jwt_extension import jwt

Expand Down