Skip to content

Commit

Permalink
Make running migrations easier and provide instructions in README
Browse files Browse the repository at this point in the history
  • Loading branch information
CalPinSW committed Nov 19, 2024
1 parent e4ba9e4 commit ec40dd0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,12 @@ The frontend and backend applications are deployed to two Azure Web Applications
3. Sending a curl request to the appropriate Webhook url:
- Frontend: `curl -dH -X POST "Frontend Webhook from Azure"`
- Backend: `curl -dH -X POST "Backend Webhook from Azure"`


## Database Migrations

Database migrations can be run with the following steps:
1. Navigate to the /backend folder.
3. Run Python locally (e.g. `python3`).
4. From the Python terminal import the migration functions you wish to run. (e.g. `from src.database.migrations.m_241030_add_access_tokens_table import up, down`).
5. Running the desired migration (e.g. `up()`).
2 changes: 1 addition & 1 deletion backend/src/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, make_response, redirect
from flask import Flask, redirect
from flask_cors import CORS
from src.controllers.database import database_controller
from src.controllers.spotify import spotify_controller
Expand Down
10 changes: 5 additions & 5 deletions backend/src/database/migrations/m_240909_add_tracks_table.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from src.database.models import (
DbTrack,
TrackArtistRelationship,
db_wrapper,
)
from src.database.migrations.setup import database


def up():
with db_wrapper.database:
db_wrapper.database.create_tables(
with database:
database.create_tables(
[
DbTrack,
TrackArtistRelationship,
Expand All @@ -16,8 +16,8 @@ def up():


def down():
with db_wrapper.database:
db_wrapper.database.drop_tables(
with database:
database.drop_tables(
[
DbTrack,
TrackArtistRelationship,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from src.database.models import DbAccessToken, db_wrapper
from src.database.models import DbAccessToken
from src.database.migrations.setup import database


def up():
with db_wrapper.database:
db_wrapper.database.create_tables(
with database:
database.create_tables(
[
DbAccessToken,
]
)


def down():
with db_wrapper.database:
db_wrapper.database.drop_tables(
with database:
database.drop_tables(
[
DbAccessToken,
]
Expand Down
10 changes: 5 additions & 5 deletions backend/src/database/migrations/m_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
DbPlaylist,
PlaylistAlbumRelationship,
DbUser,
db_wrapper,
)
from src.database.migrations.setup import database


def up():
with db_wrapper.database:
db_wrapper.database.create_tables(
with database:
database.create_tables(
[
DbUser,
DbPlaylist,
Expand All @@ -28,8 +28,8 @@ def up():


def down():
with db_wrapper.database:
db_wrapper.database.drop_tables(
with database:
database.drop_tables(
[
DbUser,
DbPlaylist,
Expand Down
12 changes: 12 additions & 0 deletions backend/src/database/migrations/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from flask import Flask
from src.database.models import (
db_wrapper,
)
from src.flask_config import Config
from dotenv import load_dotenv

load_dotenv()
app = Flask(__name__)
app.config["DATABASE"] = Config().DB_CONNECTION_STRING
db_wrapper.init_app(app)
database = db_wrapper.database

0 comments on commit ec40dd0

Please sign in to comment.