@@ -48,7 +48,7 @@ func (m Migrator) CreateTable(values ...interface{}) (err error) {
48
48
}
49
49
for _ , fieldName := range stmt .Schema .DBNames {
50
50
field := stmt .Schema .FieldsByDBName [fieldName ]
51
- if field . Comment == "" {
51
+ if _ , ok := field . TagSettings [ "COMMENT" ]; ! ok {
52
52
continue
53
53
}
54
54
if err = m .setColumnComment (stmt , field , true ); err != nil {
@@ -65,17 +65,18 @@ func (m Migrator) CreateTable(values ...interface{}) (err error) {
65
65
66
66
func (m Migrator ) setColumnComment (stmt * gorm.Statement , field * schema.Field , add bool ) error {
67
67
schemaName := m .getTableSchemaName (stmt .Schema )
68
+ commentExpr := gorm .Expr (strings .ReplaceAll (field .Comment , "'" , "''" ))
68
69
// add field comment
69
70
if add {
70
71
return m .DB .Exec (
71
- "EXEC sp_addextendedproperty 'MS_Description', ? , 'SCHEMA', ?, 'TABLE', ?, 'COLUMN', ?" ,
72
- field . Comment , schemaName , stmt .Table , field .DBName ,
72
+ "EXEC sp_addextendedproperty 'MS_Description', N'?' , 'SCHEMA', ?, 'TABLE', ?, 'COLUMN', ?" ,
73
+ commentExpr , schemaName , stmt .Table , field .DBName ,
73
74
).Error
74
75
}
75
76
// update field comment
76
77
return m .DB .Exec (
77
- "EXEC sp_updateextendedproperty 'MS_Description', ? , 'SCHEMA', ?, 'TABLE', ?, 'COLUMN', ?" ,
78
- field . Comment , schemaName , stmt .Table , field .DBName ,
78
+ "EXEC sp_updateextendedproperty 'MS_Description', N'?' , 'SCHEMA', ?, 'TABLE', ?, 'COLUMN', ?" ,
79
+ commentExpr , schemaName , stmt .Table , field .DBName ,
79
80
).Error
80
81
}
81
82
@@ -121,7 +122,7 @@ func getFullQualifiedTableName(stmt *gorm.Statement) string {
121
122
122
123
func (m Migrator ) HasTable (value interface {}) bool {
123
124
var count int
124
- m .RunWithValue (value , func (stmt * gorm.Statement ) error {
125
+ _ = m .RunWithValue (value , func (stmt * gorm.Statement ) error {
125
126
schemaName := getTableSchemaName (stmt .Schema )
126
127
if schemaName == "" {
127
128
schemaName = "%"
@@ -202,7 +203,7 @@ func (m Migrator) AddColumn(value interface{}, name string) error {
202
203
return m .RunWithValue (value , func (stmt * gorm.Statement ) (err error ) {
203
204
if stmt .Schema != nil {
204
205
if field := stmt .Schema .LookUpField (name ); field != nil {
205
- if field . Comment == "" {
206
+ if _ , ok := field . TagSettings [ "COMMENT" ]; ! ok {
206
207
return
207
208
}
208
209
if err = m .setColumnComment (stmt , field , true ); err != nil {
@@ -216,7 +217,7 @@ func (m Migrator) AddColumn(value interface{}, name string) error {
216
217
217
218
func (m Migrator ) HasColumn (value interface {}, field string ) bool {
218
219
var count int64
219
- m .RunWithValue (value , func (stmt * gorm.Statement ) error {
220
+ _ = m .RunWithValue (value , func (stmt * gorm.Statement ) error {
220
221
currentDatabase := m .DB .Migrator ().CurrentDatabase ()
221
222
name := field
222
223
if stmt .Schema != nil {
@@ -273,17 +274,13 @@ func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error
273
274
})
274
275
}
275
276
276
- func (m Migrator ) GetColumnComment (stmt * gorm.Statement , fieldDBName string ) (description string ) {
277
+ func (m Migrator ) GetColumnComment (stmt * gorm.Statement , fieldDBName string ) (comment sql. NullString ) {
277
278
queryTx := m .DB .Session (& gorm.Session {Logger : m .DB .Logger .LogMode (logger .Warn )})
278
279
if m .DB .DryRun {
279
280
queryTx .DryRun = false
280
281
}
281
- var comment sql.NullString
282
282
queryTx .Raw ("SELECT value FROM ?.sys.fn_listextendedproperty('MS_Description', 'SCHEMA', ?, 'TABLE', ?, 'COLUMN', ?)" ,
283
283
gorm .Expr (m .CurrentDatabase ()), m .getTableSchemaName (stmt .Schema ), stmt .Table , fieldDBName ).Scan (& comment )
284
- if comment .Valid {
285
- description = comment .String
286
- }
287
284
return
288
285
}
289
286
@@ -293,12 +290,12 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
293
290
}
294
291
295
292
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 {
293
+ comment := m .GetColumnComment (stmt , field .DBName )
294
+ if field .Comment != comment .String {
295
+ if comment .Valid {
301
296
err = m .setColumnComment (stmt , field , false )
297
+ } else {
298
+ err = m .setColumnComment (stmt , field , true )
302
299
}
303
300
}
304
301
return
@@ -317,7 +314,7 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
317
314
}
318
315
319
316
rawColumnTypes , _ := rows .ColumnTypes ()
320
- rows .Close ()
317
+ _ = rows .Close ()
321
318
322
319
{
323
320
_ , schemaName , tableName := splitFullQualifiedName (stmt .Table )
@@ -394,7 +391,7 @@ WHERE TABLE_CATALOG = ? AND TABLE_NAME = ?`)
394
391
columnTypes = append (columnTypes , column )
395
392
}
396
393
397
- columns .Close ()
394
+ _ = columns .Close ()
398
395
}
399
396
400
397
{
@@ -415,7 +412,7 @@ WHERE TABLE_CATALOG = ? AND TABLE_NAME = ?`)
415
412
416
413
for columnTypeRows .Next () {
417
414
var name , columnType string
418
- columnTypeRows .Scan (& name , & columnType )
415
+ _ = columnTypeRows .Scan (& name , & columnType )
419
416
for idx , c := range columnTypes {
420
417
mc := c .(migrator.ColumnType )
421
418
if mc .NameValue .String == name {
@@ -431,7 +428,7 @@ WHERE TABLE_CATALOG = ? AND TABLE_NAME = ?`)
431
428
}
432
429
}
433
430
434
- columnTypeRows .Close ()
431
+ _ = columnTypeRows .Close ()
435
432
}
436
433
437
434
return
@@ -473,7 +470,7 @@ func (m Migrator) CreateIndex(value interface{}, name string) error {
473
470
474
471
func (m Migrator ) HasIndex (value interface {}, name string ) bool {
475
472
var count int
476
- m .RunWithValue (value , func (stmt * gorm.Statement ) error {
473
+ _ = m .RunWithValue (value , func (stmt * gorm.Statement ) error {
477
474
if stmt .Schema != nil {
478
475
if idx := stmt .Schema .LookIndex (name ); idx != nil {
479
476
name = idx .Name
@@ -538,34 +535,34 @@ func (m Migrator) GetIndexes(value interface{}) ([]gorm.Index, error) {
538
535
539
536
func (m Migrator ) HasConstraint (value interface {}, name string ) bool {
540
537
var count int64
541
- m .RunWithValue (value , func (stmt * gorm.Statement ) error {
538
+ _ = m .RunWithValue (value , func (stmt * gorm.Statement ) error {
542
539
constraint , table := m .GuessConstraintInterfaceAndTable (stmt , name )
543
540
if constraint != nil {
544
541
name = constraint .GetName ()
545
542
}
546
543
547
- tableCatalog , schema , tableName := splitFullQualifiedName (table )
544
+ tableCatalog , tableSchema , tableName := splitFullQualifiedName (table )
548
545
if tableCatalog == "" {
549
546
tableCatalog = m .CurrentDatabase ()
550
547
}
551
- if schema == "" {
552
- schema = "%"
548
+ if tableSchema == "" {
549
+ tableSchema = "%"
553
550
}
554
551
555
552
return m .DB .Raw (
556
553
`SELECT count(*) FROM sys.foreign_keys as F inner join sys.tables as T on F.parent_object_id=T.object_id inner join INFORMATION_SCHEMA.TABLES as I on I.TABLE_NAME = T.name WHERE F.name = ? AND I.TABLE_NAME = ? AND I.TABLE_SCHEMA like ? AND I.TABLE_CATALOG = ?;` ,
557
- name , tableName , schema , tableCatalog ,
554
+ name , tableName , tableSchema , tableCatalog ,
558
555
).Row ().Scan (& count )
559
556
})
560
557
return count > 0
561
558
}
562
559
563
560
func (m Migrator ) CurrentDatabase () (name string ) {
564
- m .DB .Raw ("SELECT DB_NAME() AS [Current Database]" ).Row ().Scan (& name )
561
+ _ = m .DB .Raw ("SELECT DB_NAME() AS [Current Database]" ).Row ().Scan (& name )
565
562
return
566
563
}
567
564
568
565
func (m Migrator ) DefaultSchema () (name string ) {
569
- m .DB .Raw ("SELECT SCHEMA_NAME() AS [Default Schema]" ).Row ().Scan (& name )
566
+ _ = m .DB .Raw ("SELECT SCHEMA_NAME() AS [Default Schema]" ).Row ().Scan (& name )
570
567
return
571
568
}
0 commit comments