# HG changeset patch # User Mads Kiilerich # Date 2019-11-23 02:27:19 # Node ID 46681ae8669365acd9fb32894b79025fbeafc900 # Parent d426fe1311967fdd7aa1917e8e14aa2df73ef727 db: introduce migration step after 93834966ae01 dropped non-nullable inherit_default_permissions The database migration step was lazily and naively skipped ... but that turns out to be a problem when new users are added. In the database, the original column 'inherit_default_permissions' was marked as non-nullable without default value. In the Kallithea code after commit 93834966ae01, the column 'inherit_default_permissions' was no longer known, and thus not given a value when new users are added. As a result, the database complained: IntegrityError: (psycopg2.errors.NotNullViolation) null value in column "inherit_default_permissions" violates not-null constraint Fix that now by adding an appropriate db migration step to actually remove the columns. Use meta reflection to check if columns exist before running the upgrade step. The upgrade step only has to be run if it is an old database - not if it has been created after the schema changes were introduced. For the downgrade step, make sure to set a default value for non-nullable columns. diff --git a/kallithea/alembic/versions/151b4a4e8c48_db_migration_step_after_93834966ae01_.py b/kallithea/alembic/versions/151b4a4e8c48_db_migration_step_after_93834966ae01_.py new file mode 100644 --- /dev/null +++ b/kallithea/alembic/versions/151b4a4e8c48_db_migration_step_after_93834966ae01_.py @@ -0,0 +1,51 @@ +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""db: migration step after 93834966ae01 dropped non-nullable inherit_default_permissions + +Revision ID: 151b4a4e8c48 +Revises: b74907136bc1 +Create Date: 2019-11-23 01:37:42.963119 + +""" + +# The following opaque hexadecimal identifiers ("revisions") are used +# by Alembic to track this migration script and its relations to others. +revision = '151b4a4e8c48' +down_revision = 'b74907136bc1' +branch_labels = None +depends_on = None + +import sqlalchemy as sa +from alembic import op + + +def upgrade(): + meta = sa.MetaData() + meta.reflect(bind=op.get_bind()) + + if 'inherit_default_permissions' in meta.tables['users'].columns: + with op.batch_alter_table('users', schema=None) as batch_op: + batch_op.drop_column('inherit_default_permissions') + + if 'users_group_inherit_default_permissions' in meta.tables['users_groups'].columns: + with op.batch_alter_table('users_groups', schema=None) as batch_op: + batch_op.drop_column('users_group_inherit_default_permissions') + + +def downgrade(): + with op.batch_alter_table('users_groups', schema=None) as batch_op: + batch_op.add_column(sa.Column('users_group_inherit_default_permissions', sa.BOOLEAN(), nullable=False, default=True)) + + with op.batch_alter_table('users', schema=None) as batch_op: + batch_op.add_column(sa.Column('inherit_default_permissions', sa.BOOLEAN(), nullable=False, default=True))