Phase 4 successfully removed all discussion-related database tables, foreign keys, and references while preserving the integrity of other application features.
Updated old migrations to remove references to the deleted channels app:
- Removed
import channels.models - Changed
channels.models.Base36IntegerField()tomodels.CharField(max_length=10)forpost_idandcomment_idfields
- Removed
import channels.models - Changed
channels.models.Base36IntegerField()tomodels.CharField(max_length=10)forpost_idfield
- Removed dependency on
("channels", "0026_channel_moderator_notifications") - Changed FK to channels from
models.ForeignKey(to="channels.Channel")tomodels.IntegerField()to avoid lazy reference issues
- Removed dependency on
("channels", "0024_channel_membership_config")
This migration performs the following database operations:
-
Dropped Foreign Key Constraint
- Removed FK constraint
notifications_notifi_channel_id_a9e02816_fk_channels_that referencedchannels_channeltable
- Removed FK constraint
-
Dropped Unique Constraints
- Removed constraint
unique_with_channel - Removed index
unique_without_channel
- Removed constraint
-
Removed channel_id Column
- Dropped
channel_idcolumn fromnotifications_notificationsettingstable using CASCADE
- Dropped
-
Added New Unique Constraint
- Created constraint
unique_user_notification_typeon(user_id, notification_type)without channel dependency
- Created constraint
-
Deleted Discussion Models
- Deleted
CommentEventmodel/table - Deleted
PostEventmodel/table
- Deleted
- Updated
NotificationSettings.Meta.constraints:- Removed
unique_with_channelconstraint - Removed
unique_without_channelconditional constraint - Added simpler
unique_user_notification_typeconstraint on["user", "notification_type"]
- Removed
This migration drops all remaining channels and channels_fields tables from the database:
Channels app tables dropped:
channels_spamcheckresultchannels_articlechannels_commentchannels_channelmembershipconfig_channelschannels_channelmembershipconfigchannels_channelgrouprolechannels_channelsubscriptionchannels_postchannels_linkmetachannels_channelinvitationchannels_channelchannels_subscriptionchannels_redditaccesstokenchannels_redditrefreshtoken
Channels_fields app tables dropped:
channels_fields_subfieldchannels_fields_fieldlistchannels_fields_fieldchannelgrouprolechannels_fields_fieldchannel
notifications_notificationsettings:
- Had channel_id column (FK to channels_channel)
- Had unique_with_channel constraint
- Had unique_without_channel conditional constraint
- Had FK constraint to channels_channel
Other tables:
- notifications_commentevent (existed)
- notifications_postevent (existed)
notifications_notificationsettings:
- No channel_id column
- Simple unique constraint on (user_id, notification_type)
- No FK constraint to channels
Deleted tables:
- notifications_commentevent (deleted)
- notifications_postevent (deleted)
- All 14 channels_* tables (deleted)
- All 4 channels_fields_* tables (deleted)
docker-compose exec web python manage.py check
# Result: System check identified no issues (0 silenced).docker-compose exec web python manage.py showmigrations notifications
# All migrations marked as [X] applied, including 0007_remove_discussion_modelsdocker-compose exec db psql -U postgres -d postgres -c "\dt notifications_*"
# Result: Only 2 tables remain:
# - notifications_emailnotification
# - notifications_notificationsettings
docker-compose exec db psql -U postgres -d postgres -c "SELECT tablename FROM pg_tables WHERE tablename LIKE 'channels_%';"
# Result: 0 rows (all channels tables deleted)moira_lists/migrations/0001_initial.py- Removed channels dependencynotifications/migrations/0004_add_comment_event.py- Replaced channels.models referencesnotifications/migrations/0005_moderator_post_notifications.py- Replaced channels.models referencesnotifications/migrations/0006_moderator_post_notifiacation_channel.py- Changed FK to IntegerFieldnotifications/models.py- Updated constraintsnotifications/migrations/0007_remove_discussion_models.py- NEW cleanup migrationnotifications/migrations/0008_drop_channels_tables.py- NEW drop all channels tables
-
Used RunSQL for column removal: Since the database column was named
channel_idbut Django expectedchannel, direct SQL was more reliable than Django's RemoveField operation. -
Cascade deletion: Used CASCADE when dropping the channel_id column to automatically clean up related constraints and indexes.
-
Fixed historical migrations: Rather than deleting old migrations (which could break existing deployments), updated them to replace channels-specific field types with standard Django fields.
-
IntegerField instead of removing FK: In migration 0006, changed the FK to an IntegerField to maintain migration history without requiring the channels app to be installed.
- ✅ Foreign key constraints to channels app
- ✅ channel_id column from NotificationSettings
- ✅ CommentEvent and PostEvent models/tables
- ✅ All discussion-related notification tracking
- ✅ EmailNotification model and functionality
- ✅ NotificationSettings model (without channel field)
- ✅ User notification preferences
- ✅ All non-discussion notification types
- ✅ Migration history integrity
According to the AI Agent Guide, Phase 5 is next:
- Remove remaining references in code
- Clean up search index
- Update documentation
- Comprehensive testing
- The database is now free of all channels/discussions foreign keys
- No orphaned data remains from discussion features
- All migrations can be run from scratch on a new database
- System check passes with no errors
Two commits were made:
feat: Phase 4 - Remove discussion database references and modelsfeat: Drop all channels database tables