From 026696b87ac3db1cd2c06dcabe8a2d279a2e48ff Mon Sep 17 00:00:00 2001 From: zeripath Date: Mon, 5 Aug 2019 22:49:49 +0100 Subject: [PATCH] Fix dropTableColumns sqlite implementation (#7710) * 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 --- .../gitea-v1.3.3.sqlite3.sql.gz | Bin 0 -> 6555 bytes models/migrations/migrations.go | 16 ++++- models/migrations/v78.go | 67 +++--------------- models/migrations/v85.go | 39 +--------- 4 files changed, 24 insertions(+), 98 deletions(-) create mode 100644 integrations/migration-test/gitea-v1.3.3.sqlite3.sql.gz diff --git a/integrations/migration-test/gitea-v1.3.3.sqlite3.sql.gz b/integrations/migration-test/gitea-v1.3.3.sqlite3.sql.gz new file mode 100644 index 0000000000000000000000000000000000000000..8375018d9fb37cc88398fff90393ca91cac3b908 GIT binary patch literal 6555 zcmV;M8D!=kiwFquLPK2u19Nd~X>?^RF)lMMGcI#+YyjOoYm?hHlHcSUWQ zy^^}Bnz<&UiO_S64D6Q6sJ;QA<)D&!+O<4-W#wqwx?&cG>nzV*uUgZZsM| zcLVy%>x*ArT@2P`l^1vW`Mdm~{;!)CFMjyu$ID-?t_QcTFRp*R`04iQ=K62l6ysC?G|90^0+hX;0aCLoq`OD?&!Cziqy}Ee)kHMcW|1r3Db9-}j zjlf=AUf&L`Z_wX2FJBG?SO$g5zx&~vtLtAcU*F==n?c)V@VASXZ!UlRHX4rN?^GI& zbzb!%U|Zhj)qJ1r^0$NA%ip9rxO9EoZY2O(obR$?tG9la=Z86|n;)v;eb(eMuc*l( ztLyuf4!SMxiv7HKIOrg>sxFV!QmkxY2R~oFxOnsOb`bKWI=u$G%d8>eAvV3w7j@An z@Kv^7)pAjtZ5z2{S>=f1)%>_GK4_gC4=Z!!HmjRCrQKX#<%e?qzNm{uu`QYhtSA{&E6mR_+Zvis zQO(QhF54IXR;d*!#e^QJa)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0tt;Y;Z-!& z@s`lQ!zSW#TVWYtcPA9Y#;g5BkMEJ%1dr<7$XNCags83aK+-O8AQD>VMj~(gwfn%< zt$&w>&?&Zj+tlT_R4!98!vWl}EV=3Eu0q1Q0|jNt4-+s_FC@wpOanR-b%3elw=g@& z$u~32wz>n=r;CikJy}FLG}EFGeucu{WRbWsn$l0xw^A&IWt zN^LUHf39CcA$3DRj5vSN=y~d}!)`1Ix1TesLkYXIB$RF|BBOM(?tw&!6)I>Pu|g3M zNlgWj7=_MrsN~5drQTcjSsem-sg{%Xt?RUy0Ht8j65;(3N&KjIe5{0=``LxO)%5O8 zwTWpMvSe&S=vqRmM8Hj`eNZW>-Y`PB(J5%VNn^%p&#+K(vsE`6n7l0t;O5n= 1 { - _, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository") - if err != nil { - return fmt.Errorf("Drop index failed: %v", err) - } - } - } else { - _, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository") - } - - if err != nil { - return fmt.Errorf("Drop index failed: %v", err) - } - - if err = sess.Commit(); err != nil { - return err - } - - if err := sess.Begin(); err != nil { - return err - } - if err := sess.Sync2(new(Repository)); err != nil { return err } if _, err := sess.Exec("UPDATE repository SET is_empty = is_bare;"); err != nil { return err } - - if models.DbCfg.Type != core.SQLITE { - _, err = sess.Exec("ALTER TABLE repository DROP COLUMN is_bare") - if err != nil { - return fmt.Errorf("Drop column failed: %v", err) - } - } - - if err = sess.Commit(); err != nil { + if err := sess.Commit(); err != nil { return err } - if models.DbCfg.Type == core.SQLITE { - log.Warn("TABLE repository's COLUMN is_bare should be DROP but sqlite is not supported, you could manually do that.") + if err := sess.Begin(); err != nil { + return err } - return nil + if err := dropTableColumns(sess, "repository", "is_bare"); err != nil { + return err + } + + return sess.Commit() } diff --git a/models/migrations/v85.go b/models/migrations/v85.go index b8d0ee544..157c06d7b 100644 --- a/models/migrations/v85.go +++ b/models/migrations/v85.go @@ -8,9 +8,7 @@ import ( "fmt" "github.com/go-xorm/xorm" - "xorm.io/core" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/generate" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/util" @@ -37,41 +35,6 @@ func hashAppToken(x *xorm.Engine) error { // First remove the index sess := x.NewSession() defer sess.Close() - if err := sess.Begin(); err != nil { - return err - } - - var err error - if models.DbCfg.Type == core.POSTGRES || models.DbCfg.Type == core.SQLITE { - _, err = sess.Exec("DROP INDEX IF EXISTS UQE_access_token_sha1") - } else if models.DbCfg.Type == core.MSSQL { - _, err = sess.Exec(`DECLARE @ConstraintName VARCHAR(256) - DECLARE @SQL NVARCHAR(256) - 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' - SET @SQL = N'ALTER TABLE [access_token] DROP CONSTRAINT [' + @ConstraintName + N']' - EXEC sp_executesql @SQL`) - } else if models.DbCfg.Type == core.MYSQL { - indexes, err := sess.QueryString(`SHOW INDEX FROM access_token WHERE KEY_NAME = 'UQE_access_token_sha1'`) - if err != nil { - return err - } - - if len(indexes) >= 1 { - _, err = sess.Exec("DROP INDEX UQE_access_token_sha1 ON access_token") - if err != nil { - return err - } - } - } else { - _, err = sess.Exec("DROP INDEX UQE_access_token_sha1 ON access_token") - } - if err != nil { - return fmt.Errorf("Drop index failed: %v", err) - } - - if err = sess.Commit(); err != nil { - return err - } if err := sess.Begin(); err != nil { return err @@ -81,7 +44,7 @@ func hashAppToken(x *xorm.Engine) error { return fmt.Errorf("Sync2: %v", err) } - if err = sess.Commit(); err != nil { + if err := sess.Commit(); err != nil { return err }