Skip to content

Commit d0acedd

Browse files
committed
Dev: Add initial migration and Running migration instruction to README
Update README update readme apply black fix typo include altering existing column
1 parent d717c93 commit d0acedd

File tree

4 files changed

+168
-2
lines changed

4 files changed

+168
-2
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,43 @@ To run black:
209209
black .
210210
```
211211

212+
### Run migration Scripts
213+
#### If this is you first time running migration:
214+
Run the below command to attach the current alembic version number on the repo to your local db.
215+
* Note: For new contributors who just completed environment setup, run the following command **AFTER** running the application for the first time.
216+
217+
```
218+
flask db stamp head
219+
```
220+
221+
#### If you have run migration before and have just pulled the latest update from upstream that includes newer version/s of migration script.
222+
223+
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
224+
```
225+
flask db history // to check the list of version history on the repo
226+
flask db current // to check your latest version on the local db
227+
```
228+
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`.
229+
230+
```
231+
flask db upgrade <missing version number>
232+
```
233+
234+
#### If you are making changes to db model/s
235+
1. make sure you have the latest version as per the upstream repo
236+
2. make the necessary schema changes on your db model/s
237+
3. run the following command to add the update onto the migration version history
238+
```
239+
flask db migrate -m "<message>"
240+
```
241+
where `<message>` is a clear message what the change was about. This will be your alembic version title.
242+
You should see the new version inside the `versions` folder
243+
3. run the following command to apply the update to your local db
244+
```
245+
flask db upgrade
246+
```
247+
**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.
248+
212249
## Documentation
213250

214251
Documentation for the project is hosted [here](https://anitab-org.github.io/mentorship-backend/). We use Docusaurus for maintaining the documentation of the project.

migrations/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
config.set_main_option(
2727
"sqlalchemy.url",
28-
current_app.config.get("SQLALCHEMY_DATABASE_URI").replace("%", "%%"),
28+
str(current_app.extensions["migrate"].db.engine.url).replace("%", "%%"),
2929
)
3030
target_metadata = current_app.extensions["migrate"].db.metadata
3131

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""Initial migration
2+
3+
Revision ID: 0541c68e5b13
4+
Revises:
5+
Create Date: 2021-07-07 12:20:10.523552
6+
7+
"""
8+
from alembic import op
9+
from app.database.db_types.JsonCustomType import JsonCustomType
10+
import sqlalchemy as sa
11+
12+
13+
# revision identifiers, used by Alembic.
14+
revision = "0541c68e5b13"
15+
down_revision = None
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
# ### commands auto generated by Alembic - please adjust! ###
22+
op.create_table(
23+
"tasks_list",
24+
sa.Column("id", sa.Integer(), nullable=False),
25+
sa.Column("tasks", JsonCustomType, nullable=True),
26+
sa.Column("next_task_id", sa.Integer(), nullable=True),
27+
sa.PrimaryKeyConstraint("id"),
28+
)
29+
op.create_table(
30+
"users",
31+
sa.Column("id", sa.Integer(), nullable=False),
32+
sa.Column("name", sa.String(length=30), nullable=True),
33+
sa.Column("username", sa.String(length=30), nullable=True),
34+
sa.Column("email", sa.String(length=254), nullable=True),
35+
sa.Column("password_hash", sa.String(length=100), nullable=True),
36+
sa.Column("registration_date", sa.Float(), nullable=True),
37+
sa.Column("terms_and_conditions_checked", sa.Boolean(), nullable=True),
38+
sa.Column("is_admin", sa.Boolean(), nullable=True),
39+
sa.Column("is_email_verified", sa.Boolean(), nullable=True),
40+
sa.Column("email_verification_date", sa.DateTime(), nullable=True),
41+
sa.Column("current_mentorship_role", sa.Integer(), nullable=True),
42+
sa.Column("membership_status", sa.Integer(), nullable=True),
43+
sa.Column("bio", sa.String(length=500), nullable=True),
44+
sa.Column("location", sa.String(length=80), nullable=True),
45+
sa.Column("occupation", sa.String(length=80), nullable=True),
46+
sa.Column("organization", sa.String(length=80), nullable=True),
47+
sa.Column("slack_username", sa.String(length=80), nullable=True),
48+
sa.Column("social_media_links", sa.String(length=500), nullable=True),
49+
sa.Column("skills", sa.String(length=500), nullable=True),
50+
sa.Column("interests", sa.String(length=200), nullable=True),
51+
sa.Column("resume_url", sa.String(length=200), nullable=True),
52+
sa.Column("photo_url", sa.String(length=200), nullable=True),
53+
sa.Column("need_mentoring", sa.Boolean(), nullable=True),
54+
sa.Column("available_to_mentor", sa.Boolean(), nullable=True),
55+
sa.PrimaryKeyConstraint("id"),
56+
sa.UniqueConstraint("email"),
57+
sa.UniqueConstraint("username"),
58+
)
59+
op.create_table(
60+
"mentorship_relations",
61+
sa.Column("id", sa.Integer(), nullable=False),
62+
sa.Column("mentor_id", sa.Integer(), nullable=True),
63+
sa.Column("mentee_id", sa.Integer(), nullable=True),
64+
sa.Column("action_user_id", sa.Integer(), nullable=False),
65+
sa.Column("creation_date", sa.Float(), nullable=False),
66+
sa.Column("accept_date", sa.Float(), nullable=True),
67+
sa.Column("start_date", sa.Float(), nullable=True),
68+
sa.Column("end_date", sa.Float(), nullable=True),
69+
sa.Column(
70+
"state",
71+
sa.Enum(
72+
"PENDING",
73+
"ACCEPTED",
74+
"REJECTED",
75+
"CANCELLED",
76+
"COMPLETED",
77+
name="mentorshiprelationstate",
78+
),
79+
nullable=False,
80+
),
81+
sa.Column("notes", sa.String(length=400), nullable=True),
82+
sa.Column("tasks_list_id", sa.Integer(), nullable=True),
83+
sa.ForeignKeyConstraint(
84+
["mentee_id"],
85+
["users.id"],
86+
),
87+
sa.ForeignKeyConstraint(
88+
["mentor_id"],
89+
["users.id"],
90+
),
91+
sa.ForeignKeyConstraint(
92+
["tasks_list_id"],
93+
["tasks_list.id"],
94+
),
95+
sa.PrimaryKeyConstraint("id"),
96+
)
97+
op.create_table(
98+
"tasks_comments",
99+
sa.Column("id", sa.Integer(), nullable=False),
100+
sa.Column("user_id", sa.Integer(), nullable=True),
101+
sa.Column("task_id", sa.Integer(), nullable=True),
102+
sa.Column("relation_id", sa.Integer(), nullable=True),
103+
sa.Column("creation_date", sa.Float(), nullable=False),
104+
sa.Column("modification_date", sa.Float(), nullable=True),
105+
sa.Column("comment", sa.String(length=400), nullable=False),
106+
sa.ForeignKeyConstraint(
107+
["relation_id"],
108+
["mentorship_relations.id"],
109+
),
110+
sa.ForeignKeyConstraint(
111+
["task_id"],
112+
["tasks_list.id"],
113+
),
114+
sa.ForeignKeyConstraint(
115+
["user_id"],
116+
["users.id"],
117+
),
118+
sa.PrimaryKeyConstraint("id"),
119+
)
120+
# ### end Alembic commands ###
121+
122+
123+
def downgrade():
124+
# ### commands auto generated by Alembic - please adjust! ###
125+
op.drop_table("tasks_comments")
126+
op.drop_table("mentorship_relations")
127+
op.drop_table("users")
128+
op.drop_table("tasks_list")
129+
# ### end Alembic commands ###

run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def create_app(config_filename: str) -> Flask:
1414

1515
db.init_app(app)
1616

17-
migrate = Migrate(app, db)
17+
migrate = Migrate(app, db, render_as_batch=True)
1818

1919
from app.api.jwt_extension import jwt
2020

0 commit comments

Comments
 (0)