go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/dbgen/unique_key.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package dbgen 9 10 import ( 11 "strings" 12 13 "go.charczuk.com/sdk/db" 14 ) 15 16 // UniqueKey generates an alter table statement that adds a unique key constriant to a table. 17 func UniqueKey(obj any, columns ...string) string { 18 for _, column := range columns { 19 mustHaveColumn(obj, column) 20 } 21 22 objTableName := db.TableName(obj) 23 columnLabels := strings.Join(columns, "_") 24 columnCSV := strings.Join(columns, ",") 25 return f(`ALTER TABLE {{ .objTableName }} ADD CONSTRAINT uk_{{ .objTableName }}_{{ .columnLabels }} UNIQUE ({{ .columnCSV }})`, v{ 26 "objTableName": objTableName, 27 "columnLabels": columnLabels, 28 "columnCSV": columnCSV, 29 }) 30 }