FOSSASIA‘s Open Event Server uses alembic migration files to handle all database operations and updations. From creating tables to updating tables and database, all works with help of the migration files.
However, many a times we tend to miss out that automatically generated migration files mainly drops and adds columns rather than just changing them. One example of this would be:
def upgrade(): ### commands auto generated by Alembic - please adjust! ### op.add_column('session', sa.Column('submission_date', sa.DateTime(), nullable=True)) op.drop_column('session', 'date_of_submission')
Here, the idea was to change the has_session_speakers(string) to is_session_speakers_enabled (boolean), which resulted in the whole dropping of the column and creation of a new boolean column. We realize that, on doing so we have the whole data under has_session_speakers lost.
How to solve that? Here are two ways we can follow up:
When update is as simple as changing the column names, then we can use this. As discussed above, usually if we migrate directly after changing a column in our model, then the automatic migration created would drop the old column and create a new column with the changes. But on doing this in the production will cause huge loss of data which we don’t want. Suppose we want to just change the name of the column of start_time to starts_at. We don’t want the entire column to be dropped. So an alternative to this is using op.alter_column. The two main necessary parameters of the op.alter_column is the table name and the column which you are willing to alter. The other parameters include the new changes. Some of the commonly used parameters are:
- nullable – Optional: specify True or False to alter the column’s nullability.
- new_column_name – Optional; specify a string name here to indicate the new name within a column rename operation.
- type_ – Optional: a TypeEngine type object to specify a change to the column’s type. For SQLAlchemy types that also indicate a constraint (i.e. Boolean, Enum), the constraint is also generated.
- autoincrement – Optional: set the AUTO_INCREMENT flag of the column; currently understood by the MySQL dialect.
- existing_type– Optional: a TypeEngine type object to specify the previous type. This is required for all column alter operations that don’t otherwise specify a new type, as well as for when nullability is being changed on a column.
So, for example, if you want to change a column name from “start_time” to “starts_at” in events table you would write:
op.alter_column(‘events’, ‘start_time’, new_column_name=’starts_at’)
def upgrade(): ### commands auto generated by Alembic - please adjust! ### op.alter_column('sessions_version', 'end_time', new_column_name='ends_at') op.alter_column('sessions_version', 'start_time', new_column_name='starts_at') op.alter_column('events_version', 'end_time', new_column_name='ends_at') op.alter_column('events_version', 'start_time', new_column_name='starts_at')
Here, session_version and events_version are the tables name altering columns start_time to starts_at and end_time to ends_at with the op_alter_column parameter new_column_name.
Now with alter_column, most of the alteration in the column name or constraints or types is achievable. But there can be a separate scenario for changing the column properties. Suppose I change a table with column “aspect_ratio” which was a string column and had values “on” and “off” and want to convert the type to Boolean – True/False. Just changing the column type using alte_column() function won’t work since we need to also modify the whole data. So, sometimes we need to execute raw SQL commands. To do that, we can use the op.execute() function.
The way it is done:
def upgrade(): ### commands auto generated by Alembic - please adjust! ### op.execute("ALTER TABLE image_sizes ALTER full_aspect TYPE boolean USING CASE full_aspect WHEN 'on' THEN TRUE ELSE FALSE END", execution_options=None) op.execute("ALTER TABLE image_sizes ALTER icon_aspect TYPE boolean USING CASE icon_aspect WHEN 'on' THEN TRUE ELSE FALSE END", execution_options=None) op.execute("ALTER TABLE image_sizes ALTER thumbnail_aspect TYPE boolean USING CASE thumbnail_aspect WHEN 'on' THEN TRUE ELSE FALSE END"execution_options=None)
For a little more advanced use of op.execute() command will be:
op.alter_column('events', 'type', new_column_name='event_type_id') op.alter_column('events_version', 'type', new_column_name='event_type_id') op.execute('INSERT INTO event_types(name, slug) SELECT DISTINCT event_type_id, lower(replace(regexp_replace(event_type_id, \'& |,\', \'\', \'g\'), \' \', \'-\')) FROM events where not exists (SELECT 1 FROM event_types where event_types.name=events.event_type_id) and event_type_id is not null;') op.execute('UPDATE events SET event_type_id = (SELECT id FROM event_types WHERE event_types.name=events.event_type_id)') op.execute('ALTER TABLE events ALTER COLUMN event_type_id TYPE integer USING event_type_id::integer')
In this example:
- op.alter_column() renames the column type to event_type_id of events table
- op.execute() does the following:
- Inserts into column name of event_types table the value of event_type_idN (which previously contained the name of the event_type) from events table, and
- Inserts into slug column of event_types table the value of event_type_id where all letters are changed to lowercase; “& ” and “,” to “”; and spaces to “-”.
- Checks whether a type with that name already exists so as to disallow any duplicate entries in the event_types table.
- Checks whether the event_type_id is null because name of event_types table cannot be null.
You can learn more on Alembic migrations here: http://alembic.zzzcomputing.com/en/latest/ops.html
You must log in to post a comment.