Running migrations to generate the user table

Now, we will run many scripts to run migrations and generate the necessary table in the PostgreSQL database. Make sure you run the scripts in the terminal or the Command Prompt window in which you have activated the virtual environment and that you are located in the api folder.

Run the first script that populates the migration script with the detected changes in the models. In this case, it is the second time we populate the migration script, and therefore, the migration script will generate the new table that will persist our new User model: model:

python migrate.py db migrate

The following lines show the sample output generated after running the previous script. Your output will be different according to the base folder in which you have created the virtual environment.

INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.autogenerate.compare] Detected added table 'user'
INFO [alembic.ddl.postgresql] Detected sequence named 'message_id_seq' as owned by integer column 'message(id)', assuming SERIAL and omitting
Generating
  /Users/gaston/PythonREST/Flask02/api/migrations/versions/c8c45e615f6d_.py ... done

The output indicates that the api/migrations/versions/c8c45e615f6d_.py file includes the code to create the user tables. The following lines show the code for this file that was automatically generated based on the models. Notice that the file name will be different in your configuration. The code file for the sample is included in the restful_python_chapter_06_01 folder:

"""empty message 
 
Revision ID: c8c45e615f6d 
Revises: 417543056ac3 
Create Date: 2016-08-11 17:31:44.989313 
 
""" 
 
# revision identifiers, used by Alembic. 
revision = 'c8c45e615f6d' 
down_revision = '417543056ac3' 
 
from alembic import op 
import sqlalchemy as sa 
 
 
def upgrade(): 
    ### commands auto generated by Alembic - please adjust! ### 
    op.create_table('user', 
    sa.Column('id', sa.Integer(), nullable=False), 
    sa.Column('name', sa.String(length=50), nullable=False), 
    sa.Column('hashed_password', sa.String(length=120), nullable=False), 
    sa.Column('creation_date', sa.TIMESTAMP(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False), 
    sa.PrimaryKeyConstraint('id'), 
    sa.UniqueConstraint('name') 
    ) 
    ### end Alembic commands ### 
 
 
def downgrade(): 
    ### commands auto generated by Alembic - please adjust! ### 
    op.drop_table('user') 
    ### end Alembic commands ### 

The code defines two functions: upgrade and downgrade. The upgrade function runs the necessary code to create the user table by making calls to alembic.op.create_table. The downgrade function runs the necessary code to go back to the previous version.

Run the second script to upgrade the database:

python migrate.py db upgrade

The following lines show the sample output generated after running the previous script:

INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 417543056ac3 ->
    c8c45e615f6d, empty message

The previous script called the upgrade function defined in the automatically generated api/migrations/versions/c8c45e615f6d_.py script. Don't forget that the file name will be different in your configuration.

After we run the previous scripts, we can use the PostgreSQL command line or any other application that allows us to easily verify the contents of the PostreSQL database to check the new table that the migration generated. Run the following command to list the generated tables. In case the database name you are using is not named messages, make sure you use the appropriate database name:

psql --username=user_name --dbname=messages --command="dt"

The following lines show the output with all the generated table names. The migrations upgrade generate a new table named user.


                    List of relations


 Schema |      Name       | Type  |   Owner   


--------+-----------------+-------+-----------


 public | alembic_version | table | user_name


 public | category        | table | user_name


 public | message         | table | user_name


 public | user            | table | user_name

(4 rows)

SQLAlchemy generated the user table with its primary key, its unique constraint on the name field and the password field based on the information included in our User model.

The following command will allow you to check the contents of the user table after we compose and send HTTP requests to the RESTful API and create new users. The commands assume that you are running PostgreSQL on the same computer in which you are running the command:

psql --username=user_name --dbname=messages --command="SELECT * FROM
    public.user;"

Now, we can run the api/run.py script that launches Flask's development. Execute the following command in the api folder:

python run.py

After we execute the previous command, the development server will start listening at port 5000.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.74.66