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

add alembic support #36

Merged
merged 6 commits into from
Sep 28, 2023
Merged

add alembic support #36

merged 6 commits into from
Sep 28, 2023

Conversation

aharjati
Copy link
Contributor

Add alembic to keep track db revisions.

For Test:

  • Created two revision script to add columns.
  • Ran upgrade
  • Verified that new columns were added to financial_institutions db
  • Ran downgrade
  • Verified that downgrade instructions were executed (column was removed)

@github-actions
Copy link

github-actions bot commented Sep 26, 2023

Coverage report

The coverage rate went from 83.51% to 83.51% ➡️
The branch rate is 85%.

None of the new lines are part of the tested code. Therefore, there is no coverage data about them.

@aharjati aharjati linked an issue Sep 26, 2023 that may be closed by this pull request
1 task
@aharjati
Copy link
Contributor Author

Currently alembic files are placed under /src/db_revisions . I can move this to other folder name and path if needed

@lchen-2101
Copy link
Collaborator

Currently alembic files are placed under /src/db_revisions . I can move this to other folder name and path if needed

Yeah, I think I'd like this out of the src folder

@aharjati
Copy link
Contributor Author

Currently alembic files are placed under /src/db_revisions . I can move this to other folder name and path if needed

Yeah, I think I'd like this out of the src folder

@lchen-2101 should I keep the folder name db_revisions or use alembic?

@lchen-2101
Copy link
Collaborator

Currently alembic files are placed under /src/db_revisions . I can move this to other folder name and path if needed

Yeah, I think I'd like this out of the src folder

@lchen-2101 should I keep the folder name db_revisions or use alembic?

db_revisions is good; I'll need to look up the alembic documentation more, but quick question, does alembic just detect the model changes and generate the update scripts automatically or do we need to write the scripts for alembic to execute?

@aharjati
Copy link
Contributor Author

aharjati commented Sep 26, 2023

instruction on how to add new revision:

  1. change directory to root project
  2. run poetry run alembic revision -m "<your script name/title>"
Screenshot 2023-09-26 at 1 05 34 PM
  1. Open up new file created under versions folder and it should display skeleton code (Notice: Alembic auto-fills current version and previous version) and we only need to worry about the upgrade and downgrade functions
Screenshot 2023-09-26 at 1 06 11 PM
  1. Update the upgrade and downgrade with your functions/instructions. in this example, we are adding column for upgrade and we are removing this column if we need to revert or downgrade:
Screenshot 2023-09-26 at 1 06 55 PM
  1. Once saved, you can execute the upgrade function by running poetry run alembic upgrade head and it will execute scripts that have not been executed. In this picture, we have 2 versions that have not been executed, and when we run upgrade, we will see two revisions being executed.
Screenshot 2023-09-26 at 1 08 33 PM
  1. this should be it. you can use preferred db-viewer to double check your changes. In our example, we're adding new columns (account3 and account4) so we should see these new columns in our db now:
Screenshot 2023-09-26 at 1 08 46 PM

@aharjati
Copy link
Contributor Author

db_revisions is good; I'll need to look up the alembic documentation more, but quick question, does alembic just detect the model changes and generate the update scripts automatically or do we need to write the scripts for alembic to execute?

HA! just finished a quick instruction on how to add new alembic script as this comment popped up.
Yes, we issued command to generate empty skeleton script. Alembic auto-fills variables in this file that relevant to alembic management such as versions, libraries. there are two spots that we need to fill, and these are codes to upgrade and downgrade.

@lchen-2101
Copy link
Collaborator

gotcha, thanks for the explanation; so this is just a base without actual changes yet. I'll dig into the code more later, but in the meantime, can you get another ticket created that includes existing db tables? I think for this 1st revision you can just mark the scripts as already ran if the tables already exist, you can use this init.sql script as a point of reference.

@aharjati
Copy link
Contributor Author

init.sql

done. created follow-up ticket to move init.sql into alembic revision script: #37

db_revisions/env.py Outdated Show resolved Hide resolved
db_revisions/env.py Outdated Show resolved Hide resolved
Co-authored-by: lchen-2101 <[email protected]>
Copy link
Collaborator

@lchen-2101 lchen-2101 left a comment

Choose a reason for hiding this comment

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

LGTM

@lchen-2101 lchen-2101 merged commit 6891a4f into main Sep 28, 2023
3 checks passed
@lchen-2101 lchen-2101 deleted the features/18_add_alembic branch September 28, 2023 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Alembic for DB change management
2 participants