Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
User Management backend - Base #26
User Management backend - Base #26
Changes from 14 commits
ee7e3ef
1b7aaf8
345daf4
1bf76b8
92482f1
5bde4d5
daf8484
82e7381
4a1e0ce
ba95634
aeac940
9580175
21dad4f
e605ac5
ced0fc6
24650eb
fa860bb
75f3a5c
54500cc
f6c312f
61e4b1c
7deba85
9937b84
922a891
2f2d231
8fb4f37
93122d9
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may not be a good idea to do this every time you start your app. Consider what would happen in production when your database contains production information and your app restarts from some reason...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
db.create_all() creates only tables that not exists in the db. If a table exists in the db it won't be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would be the process for updating the structure of an existing table, when you decide you need to add a field to a model class for example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, currently we have two choices that I can think of:
For more advanced solution we could use Flask-Migrate extension that handles SQLAlchemy database migrations for Flask applications using Alembic. It can emit ALTER statements to a database in order to change the structure of tables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Executing this kind of SQL manually during the setup stage means you get a new database every time you bring up the system - this might work out in development but not in staging or production.
The technique for managing database configuration over time with Git is called "database migrations" here is a post introducing this concept:
https://rollout.io/blog/database-migration/
The examples there are for Ruby-on-Rails but the same concepts apply to Flask and SQLAlchemy
Here is a DB migration framework you can use with SQLAlchemy:
https://alembic.sqlalchemy.org/en/latest/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we create the database manually ? But on the next vagrant up we'll need to create it again... That is why I added those lines to setup.sh so we wouldn't need to do it over and over again.
Doesn't it migrate only the tables schema without the data ? and if so on vagrant destroy all the data will be gone. How does it different from creating a new database on every vagrant up ?
Edit: I've added Flask-Migrate extension to the project that handles SQLAlchemy database migrations for Flask applications using Alembic. It super easy to use and it configures Alembic in the proper way to work with our Flask and SQLAlchemy application.
Regarding of the question I asked:
I solved this issue of the data being destroyed by saving it inside the vagrant synced folder.
I used vagrant triggers so just before vagrant destroy is executed I save the data and
after vagrant up is executed I restore the data.
But I still need the SQL commands inside setup.sh to be executed so the database will be created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I squashed some commits so this file is no longer reachable through this commit.
This file could be found here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what vagrant workflow you're aiming for, but I think the current solution looks too complex and may not be needed.
They way I think of it, your workflow should include several commands:
vagrant up
)vagrant halt
)vagrant up
when its not the first time you've ran it)vagrant destroy
)You can use different combinations of Vagrant commands and scripts to achieve the commands above, but you should have them available because they are all needed in a typical development workflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIR Postgeas has some command-line shortcuts for DB and user creation, could be cleaner to use those then inlined SQL.