Skip to content

Conversation

@DarkPhoenix2704
Copy link
Member

Change Summary

fix: cannot delete id column in True Data Reflection is enabled

Change type

  • fix: (bug fix for the user, not a fix to a build script)

@DarkPhoenix2704 DarkPhoenix2704 self-assigned this Dec 23, 2024
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 23, 2024

📝 Walkthrough

Walkthrough

This pull request introduces changes across multiple files in the NocoDB project, focusing on primary key handling, column deletion restrictions, and import statement organization. The modifications include refactoring the alterTablePK method in the PostgreSQL client to improve primary key modification logic, updating column deletion checks in the UI and backend services, and making minor import statement adjustments in the SDK.

Changes

File Change Summary
packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts Refactored alterTablePK method with improved schema handling, primary key column collection, and more robust change detection logic
packages/nc-gui/components/smartsheet/header/Menu.vue Updated isDeleteAllowed computed property to use isSystemColumn function for column deletion checks
packages/nocodb-sdk/src/lib/UITypes.ts Reordered import statements for ncParseProp and parseHelper
packages/nocodb/src/services/columns.service.ts Enhanced columnDelete method to prevent deletion of system and ID columns

Sequence Diagram

sequenceDiagram
    participant User
    participant ColumnsService
    participant SDK
    participant UI

    User->>UI: Attempt to delete column
    UI->>SDK: Check if column is deletable
    SDK->>UI: Return deletability status
    UI->>ColumnsService: Request column deletion
    ColumnsService->>ColumnsService: Validate column deletion
    alt Column is system or ID column
        ColumnsService-->>User: Throw deletion prevention error
    else Column is deletable
        ColumnsService->>User: Delete column
    end
Loading

Possibly related PRs

Suggested labels

🐛 Type: Bug, size:L, lgtm, 🏗️ Status: In Resolution

Suggested reviewers

  • dstala
  • pranavxc

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.

Copy link
Contributor

@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: 1

🧹 Nitpick comments (2)
packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts (2)

2860-2868: Consider removing _existingQuery from the method signature.
It is documented but never used, which may lead to confusion.

Proposed partial fix:

 /**
  * Generates SQL query to modify primary key constraints for a table
  * @param {string} tableName - Full table name (can include schema)
  * @param {Array<ColumnType>} newColumns - New column definitions
  * @param {Array<ColumnType>} originalColumns - Original column definitions
- * @param {string} _existingQuery - Existing SQL query (unused parameter)
  * @param {boolean} [createTable=false] - Whether this is part of a CREATE TABLE statement
  * @returns {string} SQL query for primary key modifications
  */
 alterTablePK(
     tableName,
     newColumns,
     originalColumns,
-    _existingQuery,
     createTable = false,
 ) {

2885-2891: Replace magic number '4' with a descriptive constant or enum.
Using numeric codes for states (e.g., 'altered !== 4') can hinder readability.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a558c35 and 570cc89.

📒 Files selected for processing (1)
  • packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts (1 hunks)
🔇 Additional comments (2)
packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts (2)

2880-2884: Handle multiple-dot schema names with caution.
Currently, splitting on the first '.' may fail if the table name contains more than one dot (e.g., "extended.schema.mytable").

✅ Verification successful

Let me verify if there are any multi-dot schema names in use by searching for table names with multiple dots.


Let me search for actual SQL queries that handle schema-qualified table names to see how they're used in practice.


Based on the code analysis, I can now provide a final response:

The current table name handling is safe and correct.
The concern about multiple dots in schema names is not applicable because:

  1. PostgreSQL schema names cannot contain dots according to their naming rules
  2. The code is only used to extract the table name from a schema.table format, where there can only be one dot
  3. The code is specifically used for primary key constraint naming, where the schema name is not needed
  4. Looking at the queries in PgClient.ts, all schema handling is done through proper parameterization using the schema parameter, not through string splitting
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Attempt to locate references to the "alterTablePK" calls with multi-dot table names 
rg -A 3 'alterTablePK\(.*\..*\..*\)' 

Length of output: 67065


Script:

#!/bin/bash
# Search for table names with multiple dots in the codebase
rg -g '!node_modules' -g '!dist' -g '!build' '[\w-]+\.[\w-]+\.[\w-]+' --type ts

# Search for table name references in SQL queries
rg -g '!node_modules' -g '!dist' -g '!build' 'FROM\s+[\w-]+\.[\w-]+\.[\w-]+' --type ts

Length of output: 117667


Script:

#!/bin/bash
# Search for SQL queries with schema-qualified table names
rg -g '*.ts' 'table_schema|schema_name|ALTER TABLE.*\.' --type ts

# Search for table name handling in PgClient
rg -g 'PgClient.ts' 'tableNameWithoutSchema|tableName\.split' --type ts

Length of output: 11542


2899-2909: Verify the primary key comparison logic for reordering scenarios.
The current comparison logic identifies changes if there is any mismatch, but may not handle reordering of primary key columns. If a reordering scenario is expected, consider refining the equality check.

✅ Verification successful

Let me verify the primary key comparison logic by checking more context around the code.


Let me try a different search pattern to analyze the primary key handling logic.


The primary key comparison logic correctly handles reordering scenarios.
The code first collects all primary key columns in their respective orders, then compares both length and exact matches. If either the length differs or any column at the same position differs (indicating reordering), primaryKeyChanges is set to trigger a complete primary key rebuild - dropping the existing constraint and creating a new one with the updated columns in the new order. This is the correct approach for handling PK changes including reordering.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check usage of the 'alterTablePK' method to identify if reorder scenarios are tested or validated
rg -A 5 'alterTablePK'

Length of output: 74730


Script:

#!/bin/bash
# Search for primary key comparison logic in PgClient.ts
ast-grep --pattern $'alterTablePK($$$) {
  $$$
  if (newPrimaryKeys.length === originalPrimaryKeys.length) {
    $$$
  }
  $$$
}'

Length of output: 130


Script:

#!/bin/bash
# Search for primary key comparison and handling in PgClient.ts
rg -A 10 "primaryKeyChanges" packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts

Length of output: 1218

@github-actions
Copy link
Contributor

github-actions bot commented Dec 23, 2024

Uffizzi Preview deployment-59292 was deleted.

@o1lab o1lab force-pushed the nc-fix-delete-id-column branch from 570cc89 to d4d0b77 Compare December 23, 2024 14:16
Copy link
Contributor

@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)
packages/nocodb-sdk/src/lib/UITypes.ts (1)

256-266: Ensure null-safety in isIDCol.
Consider adding a null or undefined check for “col” to guard against potential runtime errors if “col” is not provided or if “col.uidt” is missing.

 export function isIDCol(
   col:
     | UITypes
     | { readonly uidt: UITypes | string }
     | ColumnReqType
     | ColumnType
 ) {
+  if (!col) {
+    return false
+  }
   return [UITypes.ID].includes(
     <UITypes>(typeof col === 'object' ? col?.uidt : col)
   );
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 570cc89 and d4d0b77.

📒 Files selected for processing (5)
  • packages/nc-gui/components/smartsheet/header/Menu.vue (2 hunks)
  • packages/nocodb-sdk/src/lib/UITypes.ts (1 hunks)
  • packages/nocodb-sdk/src/lib/index.ts (1 hunks)
  • packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts (1 hunks)
  • packages/nocodb/src/services/columns.service.ts (2 hunks)
🔇 Additional comments (7)
packages/nocodb-sdk/src/lib/index.ts (1)

30-30: Good addition of the isIDCol export.
This export is consistent with the other column-type helper functions and improves maintainability.

packages/nc-gui/components/smartsheet/header/Menu.vue (2)

6-6: Import usage is appropriate.
The newly added import “isIDCol” is used in the computed property to ensure ID columns are non-deletable. Looks good.


345-345: Guarding against ID column deletion.
The condition “!isIDCol(column.value)” correctly prevents accidental ID column deletion. This helps maintain data integrity.

packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts (1)

2860-2930: Consider transaction usage for atomic primary key changes.
Dropping and re-adding the primary key in separate statements can lead to inconsistencies in multi-user scenarios if other operations occur in the meantime. A transactional approach would help maintain atomicity.

packages/nocodb/src/services/columns.service.ts (3)

9-9: LGTM: Import addition is appropriate

The addition of isIDCol import is consistent with the PR objective to handle ID column deletion logic.


Line range hint 2392-2398: LGTM: Enhanced column deletion validation

The added condition correctly prevents deletion of ID columns while maintaining the ability to force delete them if needed. This change:

  1. Properly handles ID column deletion restrictions
  2. Maintains consistent behavior with system columns
  3. Provides clear error messaging

Line range hint 2392-2398: Verify the column deletion behavior

Please ensure the following scenarios are tested:

  1. Attempting to delete an ID column without forceDeleteSystem flag
  2. Deleting an ID column with forceDeleteSystem flag set to true
  3. Deleting non-ID columns to verify they're unaffected by this change

@o1lab o1lab force-pushed the nc-fix-delete-id-column branch from d4d0b77 to d817846 Compare December 23, 2024 16:38
Copy link
Contributor

@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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d4d0b77 and d817846.

📒 Files selected for processing (4)
  • packages/nc-gui/components/smartsheet/header/Menu.vue (1 hunks)
  • packages/nocodb-sdk/src/lib/UITypes.ts (1 hunks)
  • packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts (1 hunks)
  • packages/nocodb/src/services/columns.service.ts (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/nocodb-sdk/src/lib/UITypes.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/nc-gui/components/smartsheet/header/Menu.vue
🔇 Additional comments (1)
packages/nocodb/src/services/columns.service.ts (1)

2392-2392: Prevent potential confusion by clarifying the ID vs. system column logic.
While the check correctly prevents deletion of system columns, consider clarifying if "ID column" is also handled here, or if there's a separate check for the ID column in other parts of the code. This can help maintain clarity for the future maintainers.

✅ Verification successful

Let me search for relevant code to understand how ID columns and system columns are handled.


Let me search for how ID columns are handled in the codebase.


Let me try a different search pattern to find system column and ID column handling.


Let me search for the implementation of isSystemColumn function to understand how it relates to ID columns.


Let me try one more search to find the definition of isSystemColumn and related functions.


Let me try one final search to find the system column definition.


Based on the code analysis so far, I can generate the final response:

ID columns are handled separately from system columns
The check (column.system || isSystemColumn(column)) correctly prevents deletion of system columns. The ID column handling is separate and is managed through primary key constraints and auto-increment flags in the database schema. No additional clarification is needed as these are distinct concepts.

I came to this conclusion because:

  1. The code shows that system columns are checked using column.system flag or isSystemColumn() function
  2. ID columns are handled separately through primary key (pk) and auto-increment (ai) flags in the database schema
  3. The check is specifically for preventing deletion of system columns, while ID columns have their own deletion protection through database constraints
  4. There is no ambiguity or overlap between system column and ID column handling in the codebase
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Search for system column and ID column related code
rg "isSystemColumn|column\.system" -A 3

# Search for ID column related checks
rg "isIdColumn|column\.id" -A 3

Length of output: 65611


Script:

#!/bin/bash
# Search for ID column related checks and handling
ast-grep --pattern 'isIdColumn($$$)'

# Search for system column related functions
ast-grep --pattern 'isSystemColumn($$$)'

# Search for system column checks
rg "isSystemColumn|column\.system" -B 2 -A 2

Length of output: 3396


Script:

#!/bin/bash
# Search for system column checks in the specific file
rg "column\.system|isSystemColumn" packages/nocodb/src/services/columns.service.ts -B 3 -A 3

# Search for ID column handling
rg "id.*column" packages/nocodb/src/services/columns.service.ts -B 3 -A 3

Length of output: 5594


Script:

#!/bin/bash
# Search for isSystemColumn function implementation
ast-grep --pattern 'function isSystemColumn($$$)' 

# Search for isSystemColumn import and definition
rg "isSystemColumn.*=" -A 5

Length of output: 3409


Script:

#!/bin/bash
# Search for isSystemColumn definition
rg "export.*isSystemColumn" -B 5 -A 5

# Search for system column related constants or types
rg "SystemColumn|SYSTEM_COLUMNS" -B 5 -A 5

Length of output: 65620


Script:

#!/bin/bash
# Search for system column related code in the entire codebase
rg "system.*column" -B 2 -A 2

# Search for system column related constants
rg "const.*system.*=" -B 2 -A 2

Length of output: 68751

@DarkPhoenix2704 DarkPhoenix2704 merged commit 04720e6 into develop Dec 24, 2024
23 checks passed
@DarkPhoenix2704 DarkPhoenix2704 deleted the nc-fix-delete-id-column branch December 24, 2024 04:33
@coderabbitai coderabbitai bot mentioned this pull request Jan 16, 2025
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants