Skip to content

Commit 1b5908f

Browse files
zeripathlafriks
authored andcommitted
Fix dropTableColumns sqlite implementation (#7710) (#7765)
* Fix dropTableColumns sqlite implementation * use droptables and its index dropping support in v78 and v85 * golang-ci fixes * Add migration from gitea 1.3.3 for sqlite which reveals the droptables bug - thus showing this works
1 parent 65a76b7 commit 1b5908f

File tree

4 files changed

+23
-97
lines changed

4 files changed

+23
-97
lines changed
Binary file not shown.

models/migrations/migrations.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,25 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
321321
return err
322322
}
323323
tableSQL := string(res[0]["sql"])
324+
325+
// Separate out the column definitions
324326
tableSQL = tableSQL[strings.Index(tableSQL, "("):]
327+
328+
// Remove the required columnNames
325329
for _, name := range columnNames {
326-
tableSQL = regexp.MustCompile(regexp.QuoteMeta("`"+name+"`")+"[^`,)]*[,)]").ReplaceAllString(tableSQL, "")
330+
tableSQL = regexp.MustCompile(regexp.QuoteMeta("`"+name+"`")+"[^`,)]*?[,)]").ReplaceAllString(tableSQL, "")
331+
}
332+
333+
// Ensure the query is ended properly
334+
tableSQL = strings.TrimSpace(tableSQL)
335+
if tableSQL[len(tableSQL)-1] != ')' {
336+
if tableSQL[len(tableSQL)-1] == ',' {
337+
tableSQL = tableSQL[:len(tableSQL)-1]
338+
}
339+
tableSQL += ")"
327340
}
328341

342+
// Find all the columns in the table
329343
columns := regexp.MustCompile("`([^`]*)`").FindAllString(tableSQL, -1)
330344

331345
tableSQL = fmt.Sprintf("CREATE TABLE `new_%s_new` ", tableName) + tableSQL

models/migrations/v78.go

+7-58
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
package migrations
66

77
import (
8-
"fmt"
9-
10-
"code.gitea.io/gitea/models"
11-
"code.gitea.io/gitea/modules/log"
12-
138
"github.com/go-xorm/xorm"
14-
"xorm.io/core"
159
)
1610

1711
func renameRepoIsBareToIsEmpty(x *xorm.Engine) error {
@@ -21,73 +15,28 @@ func renameRepoIsBareToIsEmpty(x *xorm.Engine) error {
2115
IsEmpty bool `xorm:"INDEX"`
2216
}
2317

24-
// First remove the index
2518
sess := x.NewSession()
2619
defer sess.Close()
2720
if err := sess.Begin(); err != nil {
2821
return err
2922
}
3023

31-
var err error
32-
if models.DbCfg.Type == core.POSTGRES || models.DbCfg.Type == core.SQLITE {
33-
_, err = sess.Exec("DROP INDEX IF EXISTS IDX_repository_is_bare")
34-
} else if models.DbCfg.Type == core.MSSQL {
35-
_, err = sess.Exec(`DECLARE @ConstraintName VARCHAR(256)
36-
DECLARE @SQL NVARCHAR(256)
37-
SELECT @ConstraintName = obj.name FROM sys.columns col LEFT OUTER JOIN sys.objects obj ON obj.object_id = col.default_object_id AND obj.type = 'D' WHERE col.object_id = OBJECT_ID('repository') AND obj.name IS NOT NULL AND col.name = 'is_bare'
38-
SET @SQL = N'ALTER TABLE [repository] DROP CONSTRAINT [' + @ConstraintName + N']'
39-
EXEC sp_executesql @SQL`)
40-
if err != nil {
41-
return err
42-
}
43-
} else if models.DbCfg.Type == core.MYSQL {
44-
indexes, err := sess.QueryString(`SHOW INDEX FROM repository WHERE KEY_NAME = 'IDX_repository_is_bare'`)
45-
if err != nil {
46-
return err
47-
}
48-
49-
if len(indexes) >= 1 {
50-
_, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository")
51-
if err != nil {
52-
return fmt.Errorf("Drop index failed: %v", err)
53-
}
54-
}
55-
} else {
56-
_, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository")
57-
}
58-
59-
if err != nil {
60-
return fmt.Errorf("Drop index failed: %v", err)
61-
}
62-
63-
if err = sess.Commit(); err != nil {
64-
return err
65-
}
66-
67-
if err := sess.Begin(); err != nil {
68-
return err
69-
}
70-
7124
if err := sess.Sync2(new(Repository)); err != nil {
7225
return err
7326
}
7427
if _, err := sess.Exec("UPDATE repository SET is_empty = is_bare;"); err != nil {
7528
return err
7629
}
77-
78-
if models.DbCfg.Type != core.SQLITE {
79-
_, err = sess.Exec("ALTER TABLE repository DROP COLUMN is_bare")
80-
if err != nil {
81-
return fmt.Errorf("Drop column failed: %v", err)
82-
}
30+
if err := sess.Commit(); err != nil {
31+
return err
8332
}
8433

85-
if err = sess.Commit(); err != nil {
34+
if err := sess.Begin(); err != nil {
8635
return err
8736
}
88-
89-
if models.DbCfg.Type == core.SQLITE {
90-
log.Warn("TABLE repository's COLUMN is_bare should be DROP but sqlite is not supported, you could manually do that.")
37+
if err := dropTableColumns(sess, "repository", "is_bare"); err != nil {
38+
return err
9139
}
92-
return nil
40+
41+
return sess.Commit()
9342
}

models/migrations/v85.go

+1-38
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88
"fmt"
99

1010
"github.com/go-xorm/xorm"
11-
"xorm.io/core"
1211

13-
"code.gitea.io/gitea/models"
1412
"code.gitea.io/gitea/modules/generate"
1513
"code.gitea.io/gitea/modules/log"
1614
"code.gitea.io/gitea/modules/util"
@@ -37,41 +35,6 @@ func hashAppToken(x *xorm.Engine) error {
3735
// First remove the index
3836
sess := x.NewSession()
3937
defer sess.Close()
40-
if err := sess.Begin(); err != nil {
41-
return err
42-
}
43-
44-
var err error
45-
if models.DbCfg.Type == core.POSTGRES || models.DbCfg.Type == core.SQLITE {
46-
_, err = sess.Exec("DROP INDEX IF EXISTS UQE_access_token_sha1")
47-
} else if models.DbCfg.Type == core.MSSQL {
48-
_, err = sess.Exec(`DECLARE @ConstraintName VARCHAR(256)
49-
DECLARE @SQL NVARCHAR(256)
50-
SELECT @ConstraintName = obj.name FROM sys.columns col LEFT OUTER JOIN sys.objects obj ON obj.object_id = col.default_object_id AND obj.type = 'D' WHERE col.object_id = OBJECT_ID('access_token') AND obj.name IS NOT NULL AND col.name = 'sha1'
51-
SET @SQL = N'ALTER TABLE [access_token] DROP CONSTRAINT [' + @ConstraintName + N']'
52-
EXEC sp_executesql @SQL`)
53-
} else if models.DbCfg.Type == core.MYSQL {
54-
indexes, err := sess.QueryString(`SHOW INDEX FROM access_token WHERE KEY_NAME = 'UQE_access_token_sha1'`)
55-
if err != nil {
56-
return err
57-
}
58-
59-
if len(indexes) >= 1 {
60-
_, err = sess.Exec("DROP INDEX UQE_access_token_sha1 ON access_token")
61-
if err != nil {
62-
return err
63-
}
64-
}
65-
} else {
66-
_, err = sess.Exec("DROP INDEX UQE_access_token_sha1 ON access_token")
67-
}
68-
if err != nil {
69-
return fmt.Errorf("Drop index failed: %v", err)
70-
}
71-
72-
if err = sess.Commit(); err != nil {
73-
return err
74-
}
7538

7639
if err := sess.Begin(); err != nil {
7740
return err
@@ -81,7 +44,7 @@ func hashAppToken(x *xorm.Engine) error {
8144
return fmt.Errorf("Sync2: %v", err)
8245
}
8346

84-
if err = sess.Commit(); err != nil {
47+
if err := sess.Commit(); err != nil {
8548
return err
8649
}
8750

0 commit comments

Comments
 (0)