Skip to content
Snippets Groups Projects
Commit a29f7c4d authored by Hayden Swanson's avatar Hayden Swanson
Browse files

updated readme and functionality to initialize the database

parent 41a4bcb1
No related tags found
No related merge requests found
......@@ -21,7 +21,7 @@ sudo apt install postgresql
```
For other operating systems see the [postgresql manual](https://www.postgresql.org/download/).
Log in as the `postgres` user.
Log in as the `postgres` user,
```
sudo -u postgres psql
......@@ -43,6 +43,7 @@ CREATE DATABASE ETL_database;
```
If you then run `\list` you should see the database name in the list. If you do you can now logout by just typing `quit`.
Note, the case matters on the database name, whatever it is here is what will be used in the database URI below.
### App Setup
......@@ -71,7 +72,7 @@ touch env_personal.py
For documentation purposes the following is contained the `env_personal.py` but for fresh setup purposes only `SQLALCHEMY_DATABASE_URI` is needed.
```
SQLALCHEMY_DATABASE_URI = "postgresql:postgres:mynewpassword@localhost:5432/ETL_database
SQLALCHEMY_DATABASE_URI = "postgresql://postgres:mynewpassword@localhost:5432/etl_database
#used for remote upload
WEBDAV_LOGIN = ...
WEBDAV_PASSWORD = ...
......@@ -79,30 +80,62 @@ WEBDAV_PASSWORD = ...
EMAIL_ADDRESS = ...
EMAIL_APP_PASSWORD = ...
```
### Testing Connection and Seed Data
Alright, lets see if you have a connection to the database by added the base data to start running the app.
```
python etldb.py add-base --init_db
```
Voila, your database should now have data. You will also probably want to be able to sign in when you fire up the server so you should create the root user,
```
python etldb.py add-root
```
### Running the Flask App
This can simply be done by running this line which also puts it in debug mode (this allows you to make code changes and the server will restart with the necessary changes),
```
python flask --app mtd_etl_server run --debug
```
If this complains and returns this `* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.` you can try this line to fire up the server,
```
python -m flask run
```
### Database Migrations
If you decide a schema needs to be changed in the database you need to have database migrations set up. An easy method to do this is by running
```
flask db upgrade
```
Or if it is complaining about the import `python -m flask db upgrade`.
- configure the Config
- instantiate with seed data using CLI
- run flask app
This will create the needed database to track migrations. You just have to make sure upon any schema, changes pulled from git, (as decided by the file `db_models.py`) that you run flask db upgrade so that the migration scripts can run and catch your database up to date.
If you make your own changes to `db_models.py` you need to generate a migration script,
```
flask db migrate -m "added mycolumn to tests"
```
review it carefully such that you can upgrade or downgrade it seemlessly. Then once satisfied,
```
flask db upgrade
```
As before if it is complaining about imports you need `python -m` in front of the flask command.
## Application Architecture
If you wish to understand how the web app works I will be explaining all the core areas of the application here. Again, it is assumed you have the knowledge from this [tutorial](https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world). Like in the tutorial, I have my app/application folder with the housed blueprints that logically organize different core areas of the web application.
To avoid confusion by "application" I mean all code inside this repository. So the tracked python package ETLup, Command line interfaces, the application folder , migrations, etc...
### Database Models and Schema
### ETLup
### Flask Blueprints
#### Parts
#### Construction
#### Shipments
#### API
#### Admin
#### Auth
#### Tables
#### Errors
### Command Line Interfaces
### Migrations
#### Database Models
The entire schema is defined in `application/db_models.py` by using [SQLAlchemy](https://docs.sqlalchemy.org/en/20/). Any updates to the schema by editing the `db_models.py` requires a database migration. See the database migrations set up in the installation for details.
#### ETLup
This is a standalone python package. You will not be able to publish but you can push changes that can be published. It is inteaded to help with the uploads to the database.
### Blueprints
The Flask app uses blueprints to organize different sections. Each subdirectory within the main application directory represents a blueprint, helping to separate and manage unrelated areas of the web app.
## Hosting with CERN Services
......
......@@ -154,9 +154,13 @@ def insert_db():
pass
@insert_db.command(help="Adds all the data from 'BASE_DB_DATA.json', does not work if it is partially uploaded.")
def add_base():
@click.option('--init_db', is_flag=True, help="Will also create all the tables for the database, used for fresh installation")
def add_base(init_db):
if init_db:
db.create_all()
add_base_db_data()
click.echo("Added base data for app")
db.session.commit()
@insert_db.command(help="Adds the root user to the database.")
def add_root():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment