Paving the way for Python 3.8
Posted on Wed 08 July 2020 in conveyor
Currently Conveyor will install Python 3.6 when you create a server as 3.6 is the version that is used with Ubuntu 18. However, it would be great to have the ability to install more recent versions of Python and be able to use different versions depending on the needs of an application.
To do this we can add a database column to the Site table that will track the version of Python we are going to use. That is easy enough but we also want to make sure all of the current sites on Conveyor default to version 3.6.
This can be done within the migration by using version 3.6 as the default for any sites that do not currently have a python version.
Here are the important details:
"""
Make sure the correct libraries are imported, in this case we need to import
table from sqlalchemy.sql
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import table
"""
Define the table and any columns that will be changed by the data migration
"""
site = table(
'site',
sa.Column('python_version', sa.String()),
)
"""
Run the migration only on the python_version column that is empty
"""
op.execute(
site
.update()
.where(site.c.python_version.is_(None))
.values({'python_version': "3.6"})
)
Here is what the full migration will look like
"""Add python_version to site
Revision ID: 6f73958d5e4d
Revises: 7385fac597f4
Create Date: 2020-07-08 11:18:23.102634
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import table
# revision identifiers, used by Alembic.
revision = '6e83958d5e4c'
down_revision = '7385fab497f5'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('site',
sa.Column('python_version', sa.String(), nullable=True))
site = table(
'site',
sa.Column('python_version', sa.String()),
)
op.execute(
site
.update()
.where(site.c.python_version.is_(None))
.values({'python_version': "3.6"})
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('site', 'python_version')
# ### end Alembic commands ###