Skip to content
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

Fix: Add created_at, created_at to workspace_schedule and workspaces_… #184

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Ashutosh619-sudo
Copy link
Contributor

@Ashutosh619-sudo Ashutosh619-sudo commented Dec 19, 2024

…user table

Description

Please add PR description here, add screenshots if needed

Clickup

Please add link here
https://app.clickup.com/1864988/v/l/6-901605343641-1

Summary by CodeRabbit

  • New Features

    • Introduced a new model for managing user associations with workspaces.
    • Added a created_at timestamp field for tracking record creation in the workspace user model.
  • Bug Fixes

    • Removed unnecessary timestamp fields from the workspace user model.
  • Documentation

    • Included help text for new fields to clarify their purpose.

Copy link

coderabbitai bot commented Dec 19, 2024

Walkthrough

This pull request introduces a new WorkspacesUser model to manage the many-to-many relationship between Workspaces and Users. The changes include creating a new intermediary model with fields like created_at and updated_at, and modifying the existing Workspace model to use this new model for user associations. Subsequent migrations adjust the database schema to support these changes, including the addition and later removal of timestamp fields, ensuring accurate tracking of workspace-user connections.

Changes

File Change Summary
apps/workspaces/models.py - Added WorkspacesUser model with id, workspace, and user fields, including a unique constraint on workspace and user.
apps/workspaces/migrations/0001_initial.py - Created initial migration for WorkspacesUser model and updated Workspace model to use WorkspacesUser for ManyToMany relationships.
apps/workspaces/migrations/0007_workspacesuser_created_at_workspacesuser_updated_at.py - Added created_at and updated_at timestamp fields to WorkspacesUser model.
apps/workspaces/migrations/0008_remove_workspacesuser_created_at_and_more.py - Removed created_at and updated_at fields from WorkspacesUser model.
apps/workspaces/migrations/0009_add_create_at_to_workspaces_user_sql.py - Added created_at column to workspaces_user table with a default value of the current timestamp.

Possibly related PRs

Suggested labels

size/XS

Poem

🐰 Workspace users, now with grace,
A model that finds its perfect place.
Timestamps dancing, relationships clear,
Migrations singing, connections dear.
A rabbit's code, both smart and light! 🚀


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f76605e and f6d281b.

📒 Files selected for processing (1)
  • apps/workspaces/models.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/workspaces/models.py

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the size/M Medium PR label Dec 19, 2024
Copy link

Tests Skipped Failures Errors Time
126 0 💤 0 ❌ 0 🔥 8.445s ⏱️

Copy link

Tests Skipped Failures Errors Time
126 0 💤 0 ❌ 0 🔥 8.432s ⏱️

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5abc8e8 and 3820aaa.

📒 Files selected for processing (3)
  • apps/workspaces/migrations/0001_initial.py (2 hunks)
  • apps/workspaces/migrations/0007_workspacesuser_created_at_workspacesuser_updated_at.py (1 hunks)
  • apps/workspaces/models.py (1 hunks)
🔇 Additional comments (2)
apps/workspaces/models.py (1)

51-51: LGTM: ManyToManyField through parameter is correctly configured

The through parameter is properly set to use the new WorkspacesUser model.

apps/workspaces/migrations/0007_workspacesuser_created_at_workspacesuser_updated_at.py (1)

13-22: Verify data integrity before applying migrations

Before applying these changes, verify:

  1. No orphaned records exist in the database that would violate the new PROTECT constraint
  2. All existing records have timestamp values to satisfy the non-null constraint

Comment on lines 33 to 43
class WorkspacesUser(models.Model):
id = models.BigAutoField(primary_key=True)
workspace = models.ForeignKey('Workspace', models.DO_NOTHING)
user = models.ForeignKey('users.User', models.DO_NOTHING)
created_at = models.DateTimeField(auto_now_add=True, null=True, help_text='Created at datetime')
updated_at = models.DateTimeField(auto_now=True, null=True, help_text='Updated at datetime')

class Meta:
db_table = 'workspaces_user'
unique_together = (('workspace', 'user'),)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add docstring and use PROTECT for foreign keys

The new WorkspacesUser model needs the following improvements:

  1. Add a docstring explaining the purpose of this intermediary model
  2. Use models.PROTECT instead of models.DO_NOTHING for foreign keys to maintain referential integrity and be consistent with other models in the codebase

Apply this diff:

 class WorkspacesUser(models.Model):
+    """
+    Intermediary model for many-to-many relationship between Workspace and User
+    """
     id = models.BigAutoField(primary_key=True)
-    workspace = models.ForeignKey('Workspace', models.DO_NOTHING)
-    user = models.ForeignKey('users.User', models.DO_NOTHING)
+    workspace = models.ForeignKey('Workspace', models.PROTECT)
+    user = models.ForeignKey('users.User', models.PROTECT)
     created_at = models.DateTimeField(auto_now_add=True, null=True, help_text='Created at datetime')
     updated_at = models.DateTimeField(auto_now=True, null=True, help_text='Updated at datetime')
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
class WorkspacesUser(models.Model):
id = models.BigAutoField(primary_key=True)
workspace = models.ForeignKey('Workspace', models.DO_NOTHING)
user = models.ForeignKey('users.User', models.DO_NOTHING)
created_at = models.DateTimeField(auto_now_add=True, null=True, help_text='Created at datetime')
updated_at = models.DateTimeField(auto_now=True, null=True, help_text='Updated at datetime')
class Meta:
db_table = 'workspaces_user'
unique_together = (('workspace', 'user'),)
class WorkspacesUser(models.Model):
"""
Intermediary model for many-to-many relationship between Workspace and User
"""
id = models.BigAutoField(primary_key=True)
workspace = models.ForeignKey('Workspace', models.PROTECT)
user = models.ForeignKey('users.User', models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True, null=True, help_text='Created at datetime')
updated_at = models.DateTimeField(auto_now=True, null=True, help_text='Updated at datetime')
class Meta:
db_table = 'workspaces_user'
unique_together = (('workspace', 'user'),)

Comment on lines +40 to +56
migrations.CreateModel(
name='WorkspacesUser',
fields=[
('id', models.BigAutoField(primary_key=True, serialize=False)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
('workspace', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='workspaces.workspace')),
],
options={
'db_table': 'workspaces_user',
'unique_together': {('workspace', 'user')},
},
),
migrations.AlterField(
model_name='workspace',
name='user',
field=models.ManyToManyField(help_text='Reference to users table', through='workspaces.WorkspacesUser', to=settings.AUTH_USER_MODEL),
),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Update migration to use PROTECT for foreign keys

After updating the model to use models.PROTECT, this migration needs to be regenerated to match the model changes.

Since this is the initial migration, you can:

  1. Delete all migration files
  2. Update the models.py as suggested
  3. Run python manage.py makemigrations to generate new migrations

Comment on lines +13 to +22
migrations.AddField(
model_name='workspacesuser',
name='created_at',
field=models.DateTimeField(auto_now_add=True, help_text='Created at datetime', null=True),
),
migrations.AddField(
model_name='workspacesuser',
name='updated_at',
field=models.DateTimeField(auto_now=True, help_text='Updated at datetime', null=True),
),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Consolidate migrations and make timestamps non-nullable

The timestamp fields should:

  1. Be part of the initial migration rather than a separate migration
  2. Be non-nullable to maintain consistency with other models in the codebase (see BaseModel, BaseForeignWorkspaceModel)

After updating the model in models.py, regenerate migrations with these changes:

 class WorkspacesUser(models.Model):
     id = models.BigAutoField(primary_key=True)
     workspace = models.ForeignKey('Workspace', models.PROTECT)
     user = models.ForeignKey('users.User', models.PROTECT)
-    created_at = models.DateTimeField(auto_now_add=True, null=True, help_text='Created at datetime')
-    updated_at = models.DateTimeField(auto_now=True, null=True, help_text='Updated at datetime')
+    created_at = models.DateTimeField(auto_now_add=True, help_text='Created at datetime')
+    updated_at = models.DateTimeField(auto_now=True, help_text='Updated at datetime')
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
migrations.AddField(
model_name='workspacesuser',
name='created_at',
field=models.DateTimeField(auto_now_add=True, help_text='Created at datetime', null=True),
),
migrations.AddField(
model_name='workspacesuser',
name='updated_at',
field=models.DateTimeField(auto_now=True, help_text='Updated at datetime', null=True),
),
migrations.AddField(
model_name='workspacesuser',
name='created_at',
field=models.DateTimeField(auto_now_add=True, help_text='Created at datetime'),
),
migrations.AddField(
model_name='workspacesuser',
name='updated_at',
field=models.DateTimeField(auto_now=True, help_text='Updated at datetime'),
),

Copy link
Contributor

@ashwin1111 ashwin1111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will review post netsuite PR - some ongoing discussion in slack

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
apps/workspaces/migrations/0009_add_create_at_to_workspaces_user_sql.py (1)

1-1: Consider revising the migration file name for consistency.

Currently, the file name says “add_create_at” while the column name is “created_at.” Renaming the file to ensure consistency (e.g., “0009_add_created_at_to_workspaces_user_sql.py”) will make it more intuitive.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3820aaa and 1198b23.

📒 Files selected for processing (3)
  • apps/workspaces/migrations/0008_remove_workspacesuser_created_at_and_more.py (1 hunks)
  • apps/workspaces/migrations/0009_add_create_at_to_workspaces_user_sql.py (1 hunks)
  • apps/workspaces/models.py (0 hunks)
💤 Files with no reviewable changes (1)
  • apps/workspaces/models.py
✅ Files skipped from review due to trivial changes (1)
  • apps/workspaces/migrations/0008_remove_workspacesuser_created_at_and_more.py
🔇 Additional comments (2)
apps/workspaces/migrations/0009_add_create_at_to_workspaces_user_sql.py (2)

18-21: Good inclusion of reverse SQL for rolling back the column.

The reverse SQL cleanly drops the column. This is consistent and will help maintain safe reverts if needed.


13-16: Ensure data correctness and confirm the default applies as intended.

This statement creates a non-nullable column with a default of NOW() for existing rows. Verify that this behavior is desired, because any pre-existing rows will automatically have the current time as their creation timestamp.

Copy link

Tests Skipped Failures Errors Time
126 0 💤 0 ❌ 0 🔥 8.192s ⏱️

Copy link

Tests Skipped Failures Errors Time
126 0 💤 0 ❌ 119 🔥 28.233s ⏱️

],
options={
'db_table': 'workspaces',
},
),
migrations.CreateModel(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls revert changes in this file

]

operations = [
migrations.RemoveField(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this as well - pls to remove

@@ -30,6 +30,16 @@ def get_default_onboarding_state():
return 'CONNECTION'


class WorkspacesUser(models.Model):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deploy deploy to staging environment size/M Medium PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants