Skip to content

feat: implement PostgreSQL operation deparser for CREATE TABLE DDL generation #2094

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

Merged
merged 4 commits into from
Jun 20, 2025

Conversation

MH4GF
Copy link
Member

@MH4GF MH4GF commented Jun 19, 2025

Issue

  • resolve: N/A (internal enhancement)

Why is this change needed?

This change adds PostgreSQL operation deparser functionality to generate CREATE TABLE DDL statements from addTableOperation. This enables converting table creation operations into executable PostgreSQL DDL, supporting the database schema management workflow.

What would you like reviewers to focus on?

  • Implementation of postgresqlOperationDeparser and its test coverage
  • Integration with existing operation schema and type definitions
  • Import path refactoring from diff/constants to operation/constants

Testing Verification

  • Unit tests for postgresqlOperationDeparser with comprehensive test cases
  • Integration tests verify DDL generation from addTableOperation
  • All existing tests pass after import path refactoring

What was done

🤖 Generated by PR Agent at 2cb2249

• Implement PostgreSQL operation deparser for CREATE TABLE DDL generation
• Add comprehensive operation schema with table operation support
• Refactor constants from diff to operation directory
• Update all import paths across codebase

Detailed Changes

Relevant files
Enhancement
5 files
index.ts
Export postgresqlOperationDeparser function                           
+1/-0     
operationDeparser.ts
Implement PostgreSQL operation deparser logic                       
+49/-0   
type.ts
Add OperationDeparser type definition                                       
+2/-0     
operationsSchema.ts
Add addTableOperation schema and type guards                         
+26/-0   
index.ts
Export new operation deparser functionality                           
+5/-2     
Tests
1 files
operationDeparser.test.ts
Add comprehensive tests for operation deparser                     
+125/-0 
Configuration changes
1 files
constants.ts
Move PATH_PATTERNS constants from diff directory                 
[link]   
Miscellaneous
12 files
buildColumnCheckDiffItem.test.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnCommentDiffItem.test.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnDefaultDiffItem.test.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnDiffItem.test.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnNameDiffItem.test.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnNotNullDiffItem.test.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnPrimaryDiffItem.test.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnUniqueDiffItem.test.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnCheckDiffItem.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnCommentDiffItem.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnDefaultDiffItem.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
buildColumnDiffItem.ts
Update import path for PATH_PATTERNS                                         
+1/-1     
Additional files
25 files
buildColumnNameDiffItem.ts +1/-1     
buildColumnNotNullDiffItem.ts +1/-1     
buildColumnPrimaryDiffItem.ts +1/-1     
buildColumnUniqueDiffItem.ts +1/-1     
buildConstraintColumnNameDiffItem.ts +1/-1     
buildConstraintDeleteConstraintDiffItem.ts +1/-1     
buildConstraintDetailDiffItem.ts +1/-1     
buildConstraintDiffItem.ts +1/-1     
buildConstraintNameDiffItem.ts +1/-1     
buildConstraintTargetColumnNameDiffItem.ts +1/-1     
buildConstraintTargetTableNameDiffItem.ts +1/-1     
buildConstraintUpdateConstraintDiffItem.ts +1/-1     
buildIndexColumnsDiffItem.ts +1/-1     
buildIndexDiffItem.ts +1/-1     
buildIndexNameDiffItem.ts +1/-1     
buildIndexTypeDiffItem.ts +1/-1     
buildIndexUniqueDiffItem.ts +1/-1     
buildTableCommentDiffItem.test.ts +1/-1     
buildTableDiffItem.test.ts +1/-1     
buildTableNameDiffItem.test.ts +1/-1     
buildTableCommentDiffItem.ts +1/-1     
buildTableDiffItem.ts +1/-1     
buildTableNameDiffItem.ts +1/-1     
getChangeStatus.test.ts +1/-1     
getChangeStatus.ts +1/-1     

Additional Notes

This implementation focuses on CREATE TABLE operations. Future enhancements can extend support for other DDL operations like ALTER TABLE, DROP TABLE, etc.


Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • MH4GF and others added 3 commits June 19, 2025 15:15
    - Introduced addTableOperationSchema for adding table operations, including path validation and value schema.
    - Implemented isAddTableOperation type guard for runtime validation of add table operations.
    - Updated operationsSchema to include the new addTableOperationSchema, enhancing the overall schema capabilities.
    
    This change improves the flexibility of the operations schema by allowing the addition of tables with structured validation.
    - Added postgresqlOperationDeparser to handle CREATE TABLE statements from add table operations.
    - Introduced operation deparser tests to ensure correct DDL generation and error handling for unsupported operations.
    - Updated index exports to include postgresqlOperationDeparser and its associated types.
    
    This enhancement improves the capability to generate PostgreSQL DDL from structured operations, ensuring robust validation and error management.
    - Move PATH_PATTERNS constants from src/diff/constants.ts to src/operation/constants.ts
    - Update operationsSchema.ts to use PATH_PATTERNS.TABLE_BASE instead of hardcoded regex
    - Update all import statements across the codebase to use new constants location
    - Ensure consistent path pattern validation across the system
    
    🤖 Generated with [Claude Code](https://claude.ai/code)
    
    Co-Authored-By: Claude <[email protected]>
    Copy link

    changeset-bot bot commented Jun 19, 2025

    ⚠️ No Changeset found

    Latest commit: 1851127

    Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

    This PR includes no changesets

    When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

    Click here to learn what changesets are, and how to add one.

    Click here if you're a maintainer who wants to add a changeset to this PR

    Copy link

    vercel bot commented Jun 19, 2025

    The latest updates on your projects. Learn more about Vercel for Git ↗︎

    Name Status Preview Comments Updated (UTC)
    liam-app ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 19, 2025 9:55am
    liam-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 19, 2025 9:55am
    liam-erd-sample ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 19, 2025 9:55am
    liam-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 19, 2025 9:55am

    @MH4GF MH4GF changed the title convert create table ddl 2 feat: implement PostgreSQL operation deparser for CREATE TABLE DDL generation Jun 19, 2025
    Copy link
    Member Author

    Choose a reason for hiding this comment

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

    We originally prepared PATH_PATTERNS to implement UI diffs, but decided to move it to a different location because we wanted to use it elsewhere.

    Base automatically changed from convert-create-table-ddl to main June 19, 2025 09:37
    Comment on lines +10 to +17
    const addTableOperationSchema = v.object({
    op: v.literal('add'),
    path: v.custom<`/tables/${string}`>(
    isTablePath,
    'Path must match the pattern /tables/{tableName}',
    ),
    value: tableSchema,
    })
    Copy link
    Member Author

    Choose a reason for hiding this comment

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

    In the interim, I have decided to strictly define the schema for adding tables, possibly supporting many patterns such as columns, indexes, etc.We'll adjust the strictness once we understand how hard it will be.

    @MH4GF MH4GF marked this pull request as ready for review June 19, 2025 09:39
    @MH4GF MH4GF requested a review from a team as a code owner June 19, 2025 09:39
    @MH4GF MH4GF requested review from hoshinotsuyoshi, FunamaYukina, junkisai and NoritakaIkeda and removed request for a team June 19, 2025 09:39
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Unused Variable

    The tableName variable is extracted from the operation path but never used in the function. The generateCreateTableStatement function receives operation.value directly, making the path extraction redundant.

    const tableName = extractTableNameFromPath(operation.path)
    if (!tableName) {
      throw new Error(`Invalid table path: ${operation.path}`)
    }
    
    return generateCreateTableStatement(operation.value)
    Type Safety

    The isTablePath function uses a custom validator but the type assertion in addTableOperationSchema may not provide complete type safety. Consider using more specific validation or ensuring the path pattern validation is comprehensive.

    const isTablePath = (input: unknown): boolean => {
      if (typeof input !== 'string') return false
      return PATH_PATTERNS.TABLE_BASE.test(input)
    }
    
    const addTableOperationSchema = v.object({
      op: v.literal('add'),
      path: v.custom<`/tables/${string}`>(
        isTablePath,
        'Path must match the pattern /tables/{tableName}',
      ),
      value: tableSchema,
    })

    Copy link
    Contributor

    qodo-merge-for-open-source bot commented Jun 19, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Use existing path pattern constant

    The function should use the existing PATH_PATTERNS.TABLE_BASE constant instead
    of hardcoding the regex pattern. This ensures consistency with the path patterns
    defined in the constants file and reduces code duplication.

    frontend/packages/db-structure/src/deparser/postgresql/operationDeparser.ts [12-15]

     function extractTableNameFromPath(path: string): string | null {
    -  const match = path.match(/^\/tables\/([^/]+)/)
    +  const match = path.match(PATH_PATTERNS.TABLE_BASE)
       return match?.[1] || null
     }
    • Apply / Chat
    Suggestion importance[1-10]: 6

    __

    Why: The suggestion correctly identifies a hardcoded regex that can be replaced with the PATH_PATTERNS.TABLE_BASE constant introduced elsewhere in the PR. This improves code consistency and maintainability.

    Low
    • Update

    COMMENT ON COLUMN users.id IS 'User ID';
    COMMENT ON COLUMN users.email IS 'User email';"
    `)
    })
    Copy link
    Member Author

    @MH4GF MH4GF Jun 19, 2025

    Choose a reason for hiding this comment

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

    @NoritakaIkeda
    It still only supports CREATE TABLE, but using it this way allows us to create DDL from operation!
    The operation is the value in building_schema_versions.patch.Please let me know if you have any questions!

    ref: https://liam-erd-web.vercel.app/app/projects/284f1bca-2633-4a93-9567-7bc98ab298f7/ref/main/schema/frontend/internal-packages/db/schema/schema.sql?showMode=ALL_FIELDS&active=building_schema_versions

    Copy link
    Member

    @junkisai junkisai left a comment

    Choose a reason for hiding this comment

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

    That policy seems good!

    @MH4GF MH4GF added this pull request to the merge queue Jun 20, 2025
    Merged via the queue into main with commit 4c61298 Jun 20, 2025
    27 checks passed
    @MH4GF MH4GF deleted the convert-create-table-ddl-2 branch June 20, 2025 09:23
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants