MCPcopy Create free account
hub / github.com/bytebase/bytebase / writeTable

Function writeTable

backend/plugin/schema/pg/get_database_definition.go:1712–1845  ·  view source on GitHub ↗
(out io.Writer, schema string, table *storepb.TableMetadata, sequences []*storepb.SequenceMetadata)

Source from the content-addressed store, hash-verified

1710}
1711
1712func writeTable(out io.Writer, schema string, table *storepb.TableMetadata, sequences []*storepb.SequenceMetadata) error {
1713 if tableMissingColumnMetadata(table) {
1714 // Emit a bare CREATE TABLE so the table still exists on replay, and
1715 // skip constraints, indexes, partitions, and sequence ownership —
1716 // they all reference columns absent from the dump.
1717 if err := writeCreateTable(out, schema, table.Name, nil, nil, nil); err != nil {
1718 return err
1719 }
1720 if _, err := io.WriteString(out, ";\n\n"); err != nil {
1721 return err
1722 }
1723 if len(table.Comment) > 0 {
1724 return writeTableComment(out, schema, table)
1725 }
1726 return nil
1727 }
1728
1729 generationTypes, identitySequences, nonIdentitySequences := splitSequencesByIdentityOrNot(table, sequences)
1730
1731 if err := writeCreateTable(out, schema, table.Name, table.Columns, table.CheckConstraints, table.ExcludeConstraints); err != nil {
1732 return err
1733 }
1734
1735 if len(table.Partitions) > 0 {
1736 if err := writePartitionClause(out, table.Partitions[0]); err != nil {
1737 return err
1738 }
1739 }
1740
1741 if _, err := io.WriteString(out, ";\n\n"); err != nil {
1742 return err
1743 }
1744
1745 // Skip OWNED BY for a table with no columns; the clause would reference
1746 // a column absent from the dump.
1747 if !tableHasNoColumns(table) {
1748 for _, sequence := range nonIdentitySequences {
1749 if err := writeAlterSequenceOwnedBy(out, schema, sequence); err != nil {
1750 return err
1751 }
1752 }
1753 }
1754
1755 // Construct comments.
1756 if len(table.Comment) > 0 {
1757 if err := writeTableComment(out, schema, table); err != nil {
1758 return err
1759 }
1760 }
1761
1762 for _, column := range table.Columns {
1763 if len(column.Comment) > 0 {
1764 if err := writeColumnComment(out, schema, table.Name, column); err != nil {
1765 return err
1766 }
1767 }
1768 }
1769

Callers 3

GetDatabaseDefinitionFunction · 0.70
GetSchemaDefinitionFunction · 0.70
GetTableDefinitionFunction · 0.70

Calls 15

writeCreateTableFunction · 0.85
tableHasNoColumnsFunction · 0.85
writePartitionTableFunction · 0.85
writeAttachPartitionFunction · 0.85
writePrimaryKeyFunction · 0.85
writePartitionPrimaryKeyFunction · 0.85
writeUniqueKeyFunction · 0.85
writePartitionUniqueKeyFunction · 0.85

Tested by

no test coverage detected