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

Function writePrimaryKey

backend/plugin/schema/pg/get_database_definition.go:2167–2210  ·  view source on GitHub ↗
(out io.Writer, schema string, table string, index *storepb.IndexMetadata)

Source from the content-addressed store, hash-verified

2165}
2166
2167func writePrimaryKey(out io.Writer, schema string, table string, index *storepb.IndexMetadata) error {
2168 if _, err := io.WriteString(out, `ALTER TABLE ONLY "`); err != nil {
2169 return err
2170 }
2171 if _, err := io.WriteString(out, schema); err != nil {
2172 return err
2173 }
2174 if _, err := io.WriteString(out, `"."`); err != nil {
2175 return err
2176 }
2177 if _, err := io.WriteString(out, table); err != nil {
2178 return err
2179 }
2180 if _, err := io.WriteString(out, `" ADD CONSTRAINT "`); err != nil {
2181 return err
2182 }
2183 if _, err := io.WriteString(out, index.Name); err != nil {
2184 return err
2185 }
2186 if _, err := io.WriteString(out, `" PRIMARY KEY (`); err != nil {
2187 return err
2188 }
2189 for i, expression := range index.Expressions {
2190 if i > 0 {
2191 if _, err := io.WriteString(out, ", "); err != nil {
2192 return err
2193 }
2194 }
2195 if _, err := io.WriteString(out, expression); err != nil {
2196 return err
2197 }
2198 }
2199 if _, err := io.WriteString(out, ");\n\n"); err != nil {
2200 return err
2201 }
2202
2203 if len(index.Comment) > 0 {
2204 if err := writeConstraintComment(out, schema, table, index); err != nil {
2205 return err
2206 }
2207 }
2208
2209 return nil
2210}
2211
2212func writeConstraintComment(out io.Writer, schema string, table string, index *storepb.IndexMetadata) error {
2213 if _, err := io.WriteString(out, `COMMENT ON CONSTRAINT "`); err != nil {

Callers 5

writeAlterTableDiffFunction · 0.85
writeCreateIndexDiffFunction · 0.85
writeTableFunction · 0.85
writePartitionPrimaryKeyFunction · 0.85

Calls 1

writeConstraintCommentFunction · 0.85

Tested by

no test coverage detected