@@ -36,6 +36,58 @@ func (m Migrator) GetTables() (tableList []string, err error) {
36
36
return tableList , m .DB .Raw ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_CATALOG = ?" , m .CurrentDatabase ()).Scan (& tableList ).Error
37
37
}
38
38
39
+ func (m Migrator ) CreateTable (values ... interface {}) (err error ) {
40
+ if err = m .Migrator .CreateTable (values ... ); err != nil {
41
+ return
42
+ }
43
+ for _ , value := range m .ReorderModels (values , false ) {
44
+ if err = m .RunWithValue (value , func (stmt * gorm.Statement ) (err error ) {
45
+ if stmt .Schema == nil {
46
+ return
47
+ }
48
+ for _ , fieldName := range stmt .Schema .DBNames {
49
+ field := stmt .Schema .FieldsByDBName [fieldName ]
50
+ if field .Comment == "" {
51
+ continue
52
+ }
53
+ if err = m .setColumnComment (stmt , field , true ); err != nil {
54
+ return
55
+ }
56
+ }
57
+ return
58
+ }); err != nil {
59
+ return
60
+ }
61
+ }
62
+ return
63
+ }
64
+
65
+ func (m Migrator ) setColumnComment (stmt * gorm.Statement , field * schema.Field , add bool ) error {
66
+ schemaName := m .getTableSchemaName (stmt .Schema )
67
+ // add field comment
68
+ if add {
69
+ return m .DB .Exec (
70
+ "EXEC sp_addextendedproperty 'MS_Description', ?, 'SCHEMA', ?, 'TABLE', ?, 'COLUMN', ?" ,
71
+ field .Comment , schemaName , stmt .Table , field .DBName ,
72
+ ).Error
73
+ }
74
+ // update field comment
75
+ return m .DB .Exec (
76
+ "EXEC sp_updateextendedproperty 'MS_Description', ?, 'SCHEMA', ?, 'TABLE', ?, 'COLUMN', ?" ,
77
+ field .Comment , schemaName , stmt .Table , field .DBName ,
78
+ ).Error
79
+ }
80
+
81
+ func (m Migrator ) getTableSchemaName (schema * schema.Schema ) string {
82
+ // return the schema name if it is explicitly provided in the table name
83
+ // otherwise return default schema name
84
+ schemaName := getTableSchemaName (schema )
85
+ if schemaName == "" {
86
+ schemaName = m .DefaultSchema ()
87
+ }
88
+ return schemaName
89
+ }
90
+
39
91
func getTableSchemaName (schema * schema.Schema ) string {
40
92
// return the schema name if it is explicitly provided in the table name
41
93
// otherwise return a sql wildcard -> use any table_schema
@@ -141,6 +193,26 @@ func (m Migrator) RenameTable(oldName, newName interface{}) error {
141
193
).Error
142
194
}
143
195
196
+ func (m Migrator ) AddColumn (value interface {}, name string ) error {
197
+ if err := m .Migrator .AddColumn (value , name ); err != nil {
198
+ return err
199
+ }
200
+
201
+ return m .RunWithValue (value , func (stmt * gorm.Statement ) (err error ) {
202
+ if stmt .Schema != nil {
203
+ if field := stmt .Schema .LookUpField (name ); field != nil {
204
+ if field .Comment == "" {
205
+ return
206
+ }
207
+ if err = m .setColumnComment (stmt , field , true ); err != nil {
208
+ return
209
+ }
210
+ }
211
+ }
212
+ return
213
+ })
214
+ }
215
+
144
216
func (m Migrator ) HasColumn (value interface {}, field string ) bool {
145
217
var count int64
146
218
m .RunWithValue (value , func (stmt * gorm.Statement ) error {
@@ -200,6 +272,39 @@ func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error
200
272
})
201
273
}
202
274
275
+ func (m Migrator ) GetColumnComment (stmt * gorm.Statement , fieldDBName string ) (description string ) {
276
+ queryTx := m .DB
277
+ if m .DB .DryRun {
278
+ queryTx = m .DB .Session (& gorm.Session {})
279
+ queryTx .DryRun = false
280
+ }
281
+ var comment sql.NullString
282
+ queryTx .Raw ("SELECT value FROM ?.sys.fn_listextendedproperty('MS_Description', 'SCHEMA', ?, 'TABLE', ?, 'COLUMN', ?)" ,
283
+ gorm .Expr (m .CurrentDatabase ()), m .getTableSchemaName (stmt .Schema ), stmt .Table , fieldDBName ).Scan (& comment )
284
+ if comment .Valid {
285
+ description = comment .String
286
+ }
287
+ return
288
+ }
289
+
290
+ func (m Migrator ) MigrateColumn (value interface {}, field * schema.Field , columnType gorm.ColumnType ) error {
291
+ if err := m .Migrator .MigrateColumn (value , field , columnType ); err != nil {
292
+ return err
293
+ }
294
+
295
+ return m .RunWithValue (value , func (stmt * gorm.Statement ) (err error ) {
296
+ description := m .GetColumnComment (stmt , field .DBName )
297
+ if field .Comment != description {
298
+ if description == "" {
299
+ err = m .setColumnComment (stmt , field , true )
300
+ } else {
301
+ err = m .setColumnComment (stmt , field , false )
302
+ }
303
+ }
304
+ return
305
+ })
306
+ }
307
+
203
308
var defaultValueTrimRegexp = regexp .MustCompile ("^\\ ('?([^']*)'?\\ )$" )
204
309
205
310
// ColumnTypes return columnTypes []gorm.ColumnType and execErr error
0 commit comments