Skip to content

Comments

feat: GENERATED ALWAYS AS (…) STORED#43

Merged
tianzhou merged 1 commit intomainfrom
generated_column
Sep 25, 2025
Merged

feat: GENERATED ALWAYS AS (…) STORED#43
tianzhou merged 1 commit intomainfrom
generated_column

Conversation

@tianzhou
Copy link
Contributor

Fix #37

Copilot AI review requested due to automatic review settings September 25, 2025 07:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements support for PostgreSQL generated columns with the GENERATED ALWAYS AS (...) STORED syntax. The changes enable the schema management tool to properly detect, parse, and generate DDL for stored generated columns.

  • Adds detection of generated columns in database queries by checking the attgenerated attribute
  • Updates the column data structure to include generated expression and status fields
  • Implements proper DDL generation for ALTER TABLE ADD COLUMN statements with generated column syntax

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
testdata/diff/create_table/add_column_generated/* Test data files demonstrating generated column functionality
ir/queries/queries.sql.go Updates generated Go code to handle generated column fields from database queries
ir/queries/queries.sql Modifies SQL queries to extract generated column information from PostgreSQL system catalogs
ir/ir.go Adds GeneratedExpr and IsGenerated fields to the Column struct
ir/parser.go Implements parsing logic for GENERATED constraints and expression extraction
ir/inspector.go Updates column building logic to handle generated columns from database inspection
internal/diff/table.go Modifies DDL generation to properly format generated column syntax in ALTER statements

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +549 to +559
// Don't add DEFAULT for SERIAL columns, identity columns, or generated columns
if column.DefaultValue != nil && column.Identity == nil && !column.IsGenerated && !isSerialColumn(column) {
stmt += fmt.Sprintf(" DEFAULT %s", *column.DefaultValue)
}

// Don't add NOT NULL for identity columns or SERIAL columns as they are implicitly NOT NULL
// Also skip NOT NULL if we're adding PRIMARY KEY inline (PRIMARY KEY implies NOT NULL)
// For generated columns, include NOT NULL if explicitly specified (but before GENERATED clause)
if !column.IsNullable && column.Identity == nil && !isSerialColumn(column) && pkConstraint == nil {
stmt += " NOT NULL"
}
Copy link

Copilot AI Sep 25, 2025

Choose a reason for hiding this comment

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

[nitpick] The logic for determining when to add DEFAULT and NOT NULL clauses has become complex and duplicated. Consider extracting this into separate helper functions like shouldAddDefault(column) and shouldAddNotNull(column, pkConstraint) to improve readability and maintainability.

Copilot uses AI. Check for mistakes.
@tianzhou tianzhou merged commit b1096e6 into main Sep 25, 2025
2 checks passed
@tianzhou tianzhou deleted the generated_column branch October 23, 2025 09:15
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.

Incorrect migration for generated columns

1 participant