@@ -217,6 +217,66 @@ Please try to upgrade to a lower version (>= v0.6.0) first, then upgrade to curr
217
217
return nil
218
218
}
219
219
220
+ func dropTableColumns (x * xorm.Engine , tableName string , columnNames ... string ) (err error ) {
221
+ if tableName == "" || len (columnNames ) == 0 {
222
+ return nil
223
+ }
224
+
225
+ switch {
226
+ case setting .UseSQLite3 :
227
+ log .Warn ("Unable to drop columns in SQLite" )
228
+ case setting .UseMySQL , setting .UseTiDB , setting .UsePostgreSQL :
229
+ cols := ""
230
+ for _ , col := range columnNames {
231
+ if cols != "" {
232
+ cols += ", "
233
+ }
234
+ cols += "DROP COLUMN `" + col + "`"
235
+ }
236
+ if _ , err := x .Exec (fmt .Sprintf ("ALTER TABLE `%s` %s" , tableName , columnNames )); err != nil {
237
+ return fmt .Errorf ("Drop table `%s` columns %v: %v" , tableName , columnNames , err )
238
+ }
239
+ case setting .UseMSSQL :
240
+ sess := x .NewSession ()
241
+ defer sess .Close ()
242
+
243
+ if err = sess .Begin (); err != nil {
244
+ return err
245
+ }
246
+
247
+ cols := ""
248
+ for _ , col := range columnNames {
249
+ if cols != "" {
250
+ cols += ", "
251
+ }
252
+ cols += "`" + strings .ToLower (col ) + "`"
253
+ }
254
+ sql := fmt .Sprintf ("SELECT Name FROM SYS.DEFAULT_CONSTRAINTS WHERE PARENT_OBJECT_ID = OBJECT_ID('%[1]s') AND PARENT_COLUMN_ID IN (SELECT column_id FROM sys.columns WHERE lower(NAME) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))" ,
255
+ tableName , strings .Replace (cols , "`" , "'" , - 1 ))
256
+ constraints := make ([]string , 0 )
257
+ if err := sess .SQL (sql ).Find (& constraints ); err != nil {
258
+ sess .Rollback ()
259
+ return fmt .Errorf ("Find constraints: %v" , err )
260
+ }
261
+ for _ , constraint := range constraints {
262
+ if _ , err := sess .Exec (fmt .Sprintf ("ALTER TABLE `%s` DROP CONSTRAINT `%s`" , tableName , constraint )); err != nil {
263
+ sess .Rollback ()
264
+ return fmt .Errorf ("Drop table `%s` constraint `%s`: %v" , tableName , constraint , err )
265
+ }
266
+ }
267
+ if _ , err := sess .Exec (fmt .Sprintf ("ALTER TABLE `%s` DROP COLUMN %s" , tableName , cols )); err != nil {
268
+ sess .Rollback ()
269
+ return fmt .Errorf ("Drop table `%s` columns %v: %v" , tableName , columnNames , err )
270
+ }
271
+
272
+ return sess .Commit ()
273
+ default :
274
+ log .Fatal (4 , "Unrecognized DB" )
275
+ }
276
+
277
+ return nil
278
+ }
279
+
220
280
func fixLocaleFileLoadPanic (_ * xorm.Engine ) error {
221
281
cfg , err := ini .Load (setting .CustomConf )
222
282
if err != nil {
0 commit comments