-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor SlackMessage.channel_id
(CHAR
field) to SlackMessage.channel
(foreign key relationship)
#5292
Merged
joeyorlando
merged 27 commits into
dev
from
jorlando/refactor-slack-messages-channel-relationship
Nov 26, 2024
+741
−369
Merged
refactor SlackMessage.channel_id
(CHAR
field) to SlackMessage.channel
(foreign key relationship)
#5292
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
b3681e8
WIP
joeyorlando f22d7a0
wip
joeyorlando e7bf5de
Merge branch 'dev' into jorlando/refactor-slack-messages-channel-rela…
joeyorlando a94137b
wip
joeyorlando 7f36f36
wip
joeyorlando bde7b00
wip
joeyorlando be7efee
make migration compatible w/ sqlite and postgres
joeyorlando e1aefe6
wip
joeyorlando 5e456e6
wip
joeyorlando 2877cef
wip
joeyorlando 9780c5c
wip
joeyorlando ca5f27f
remove `print` statements
joeyorlando 39844d7
wip
joeyorlando 330281b
update tests
joeyorlando a716ba9
Merge branch 'dev' into jorlando/refactor-slack-messages-channel-rela…
joeyorlando a6f192e
remove `pprint` imports
joeyorlando 0ee632d
Merge branch 'jorlando/refactor-slack-messages-channel-relationship' …
joeyorlando 536b1bc
PR comment
joeyorlando efa8f92
dual-write + read from old column
joeyorlando 99d9621
final tweaks to migration approach
joeyorlando c2ab44d
wip
joeyorlando 7f5d4cc
wip
joeyorlando 2d2adc3
tests + linting
joeyorlando 7ba3cde
tests + linting
joeyorlando e6a5d1e
wip
joeyorlando 6fb318a
update batch migrator script to use `id` for ordering instead of `cre…
joeyorlando 7515122
linting
joeyorlando File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
engine/apps/slack/migrations/0006_rename_channel_id_slackmessage__channel_id_and_more.py
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. main change |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Generated by Django 4.2.16 on 2024-11-22 12:36 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django_migration_linter as linter | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('slack', '0005_slackteamidentity__unified_slack_app_installed'), | ||
] | ||
|
||
operations = [ | ||
linter.IgnoreMigration(), | ||
migrations.RenameField( | ||
model_name='slackmessage', | ||
old_name='channel_id', | ||
new_name='_channel_id', | ||
), | ||
migrations.AddField( | ||
model_name='slackmessage', | ||
name='channel', | ||
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='slack_messages', to='slack.slackchannel'), | ||
), | ||
] |
47 changes: 47 additions & 0 deletions
47
engine/apps/slack/migrations/0007_migrate_slackmessage_channel_id.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Generated by Django 4.2.16 on 2024-11-01 10:58 | ||
import logging | ||
|
||
from django.db import migrations | ||
import django_migration_linter as linter | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def populate_slack_channel(apps, schema_editor): | ||
SlackMessage = apps.get_model("slack", "SlackMessage") | ||
SlackChannel = apps.get_model("slack", "SlackChannel") | ||
|
||
logger.info("Starting migration to populate channel field.") | ||
|
||
sql = f""" | ||
UPDATE | ||
{SlackMessage._meta.db_table} AS sm | ||
JOIN {SlackChannel._meta.db_table} AS sc | ||
ON sc.slack_id = sm._channel_id | ||
AND sc.slack_team_identity_id = sm.slack_team_identity | ||
SET | ||
sm.channel_id = sc.id | ||
WHERE | ||
sm._channel_id IS NOT NULL | ||
AND sm.slack_team_identity IS NOT NULL; | ||
""" | ||
|
||
with schema_editor.connection.cursor() as cursor: | ||
cursor.execute(sql) | ||
updated_rows = cursor.rowcount # Number of rows updated | ||
|
||
logger.info(f"Bulk updated {updated_rows} SlackMessages with their Slack channel.") | ||
logger.info("Finished migration to populate channel field.") | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('slack', '0006_rename_channel_id_slackmessage__channel_id_and_more'), | ||
] | ||
|
||
operations = [ | ||
# simply setting this new field is okay, we are not deleting the value of _channel_id | ||
# therefore, no need to revert it | ||
linter.IgnoreMigration(), | ||
migrations.RunPython(populate_slack_channel, migrations.RunPython.noop), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added for this