(current, desired *Domain)
| 2592 | } |
| 2593 | |
| 2594 | func (g *Generator) generateAlterDomainDDLs(current, desired *Domain) ([]string, error) { |
| 2595 | var ddls []string |
| 2596 | domainName := g.escapeDomainName(current) |
| 2597 | |
| 2598 | if !g.areSameDefaultValue(current.defaultValue, desired.defaultValue, current.dataType) { |
| 2599 | if desired.defaultValue == nil { |
| 2600 | ddls = append(ddls, fmt.Sprintf("ALTER DOMAIN %s DROP DEFAULT", domainName)) |
| 2601 | } else { |
| 2602 | normalizedExpr := normalizeExpr(desired.defaultValue.expression, g.mode) |
| 2603 | exprStr := parser.String(normalizedExpr) |
| 2604 | ddls = append(ddls, fmt.Sprintf("ALTER DOMAIN %s SET DEFAULT %s", domainName, exprStr)) |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | if current.notNull != desired.notNull { |
| 2609 | if desired.notNull { |
| 2610 | ddls = append(ddls, fmt.Sprintf("ALTER DOMAIN %s SET NOT NULL", domainName)) |
| 2611 | } else { |
| 2612 | ddls = append(ddls, fmt.Sprintf("ALTER DOMAIN %s DROP NOT NULL", domainName)) |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | for _, currentConstraint := range current.constraints { |
| 2617 | if !g.findDomainConstraintByExpression(desired.constraints, currentConstraint.expression) { |
| 2618 | if currentConstraint.name != "" { |
| 2619 | ddls = append(ddls, fmt.Sprintf("ALTER DOMAIN %s DROP CONSTRAINT %s", |
| 2620 | domainName, currentConstraint.name)) |
| 2621 | } |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | for _, desiredConstraint := range desired.constraints { |
| 2626 | if !g.findDomainConstraintByExpression(current.constraints, desiredConstraint.expression) { |
| 2627 | exprStr := parser.String(desiredConstraint.expression) |
| 2628 | if desiredConstraint.name != "" { |
| 2629 | ddls = append(ddls, fmt.Sprintf("ALTER DOMAIN %s ADD CONSTRAINT %s CHECK (%s)", |
| 2630 | domainName, desiredConstraint.name, exprStr)) |
| 2631 | } else { |
| 2632 | ddls = append(ddls, fmt.Sprintf("ALTER DOMAIN %s ADD CHECK (%s)", |
| 2633 | domainName, exprStr)) |
| 2634 | } |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | // Note: We don't handle collation changes as PostgreSQL doesn't support changing collation via ALTER DOMAIN |
| 2639 | // The user would need to drop and recreate the domain to change collation |
| 2640 | |
| 2641 | return ddls, nil |
| 2642 | } |
| 2643 | |
| 2644 | func (g *Generator) findDomainConstraintByExpression(constraints []DomainConstraint, expression parser.Expr) bool { |
| 2645 | normalizedExpr := normalizeCheckExpr(expression, g.mode) |
no test coverage detected