Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSSQL, Error: sync database struct error: mssql: Invalid object name 'SYS.INDEXES'. #13615

Closed
2 of 7 tasks
harkirat777 opened this issue Nov 18, 2020 · 3 comments · Fixed by #16627
Closed
2 of 7 tasks
Labels
type/bug type/upstream This is an issue in one of Gitea's dependencies and should be reported there

Comments

@harkirat777
Copy link

  • Gitea version (or commit ref): 1.12.6 (same error on 1.13.0-rc2)
  • Git version: 2.15.1
  • Operating system: Microsoft Server 2012 R2
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL v2017
    • SQLite
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
    • Not relevant
  • Log gist:

Description

I am trying to install gitea 1.12.6 on SQL Server 2017. I have database and user using the following query:

CREATE DATABASE giteadb
ALTER DATABASE giteadb COLLATE SQL_Latin1_General_CP1_CS_AS

USE master
CREATE LOGIN giteauser WITH PASSWORD=N'giteapass', DEFAULT_DATABASE=giteadb, CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
ALTER AUTHORIZATION ON DATABASE::giteadb TO giteauser

When I click 'Install Gitea' from localhost:3000, a bunch of tables are generated in the database but then I get the below error.

...

Log

2020/11/18 09:31:03 ...dules/setting/log.go:279:newLogService() [I] Gitea Log Mode: File(File:info)
2020/11/18 09:31:03 ...les/setting/cache.go:70:newCacheService() [I] Cache Service Enabled
2020/11/18 09:31:03 ...les/setting/cache.go:81:newCacheService() [I] Last Commit Cache Service Enabled
2020/11/18 09:31:03 ...s/setting/session.go:63:newSessionService() [I] Session Service Enabled
2020/11/18 09:31:03 routers/init.go:63:initDBEngine() [I] Beginning ORM engine initialization.
2020/11/18 09:31:03 routers/init.go:70:initDBEngine() [I] ORM engine initialization attempt #1/10...
2020/11/18 09:31:03 ...rm/session_schema.go:25:Ping() [I] PING DATABASE mssql
2020/11/18 09:31:03 ...eue/queue_wrapped.go:75:setInternal() [W] [Attempt: 1] Failed to create queue: level for notification-service cfg: queue.LevelQueueConfiguration{ByteFIFOQueueConfiguration:queue.ByteFIFOQueueConfiguration{WorkerPoolConfiguration:queue.WorkerPoolConfiguration{QueueLength:20, BatchLength:20, BlockTimeout:1000000000, BoostTimeout:300000000000, BoostWorkers:5, MaxWorkers:6}, Workers:1, Name:"notification-service-level"}, DataDir:"D:\\gitea\\var\\lib\\gitea\\data\\queues\\notification-service"} error: The process cannot access the file because it is being used by another process.
2020/11/18 09:31:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] select * from sysobjects where id = object_id(N'version') and OBJECTPROPERTY(id, N'IsUserTable') = 1 [] - 3.9997ms
2020/11/18 09:31:03 ...m.io/xorm/core/db.go:277:ExecContext() [I] [SQL] IF NOT EXISTS (SELECT [name] FROM sys.tables WHERE [name] = 'version' ) CREATE TABLE [version] ([id] BIGINT PRIMARY KEY IDENTITY NOT NULL, [version] BIGINT NULL); [] - 12.0016ms
2020/11/18 09:31:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "COLUMN_NAME" = ? [version id] - 32.998ms
2020/11/18 09:31:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "COLUMN_NAME" = ? [version version] - 14.0031ms
2020/11/18 09:31:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] SELECT TOP 1 [id], [version] FROM [version] WHERE [id]=? [1] - 997.5µs
2020/11/18 09:31:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] INSERT INTO [version] ([version]) OUTPUT Inserted.id VALUES (?) [141] - 2.9998ms
2020/11/18 09:31:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] select name from sysobjects where xtype ='U' [] - 2.0005ms
2020/11/18 09:31:03 ...eue/queue_wrapped.go:75:setInternal() [W] [Attempt: 2] Failed to create queue: level for notification-service cfg: queue.LevelQueueConfiguration{ByteFIFOQueueConfiguration:queue.ByteFIFOQueueConfiguration{WorkerPoolConfiguration:queue.WorkerPoolConfiguration{QueueLength:20, BatchLength:20, BlockTimeout:1000000000, BoostTimeout:300000000000, BoostWorkers:5, MaxWorkers:6}, Workers:1, Name:"notification-service-level"}, DataDir:"D:\\gitea\\var\\lib\\gitea\\data\\queues\\notification-service"} error: The process cannot access the file because it is being used by another process.
2020/11/18 09:31:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] select a.name as name, b.name as ctype,a.max_length,a.precision,a.scale,a.is_nullable as nullable,
			  "default_is_null" = (CASE WHEN c.text is null THEN 1 ELSE 0 END),
		      replace(replace(isnull(c.text,''),'(',''),')','') as vdefault,
			  ISNULL(p.is_primary_key, 0), a.is_identity as is_identity
	          from sys.columns a 
			  left join sys.types b on a.user_type_id=b.user_type_id
	          left join sys.syscomments c on a.default_object_id=c.id
			  LEFT OUTER JOIN (SELECT i.object_id, ic.column_id, i.is_primary_key
				FROM sys.indexes i
			  LEFT JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
				WHERE i.is_primary_key = 1
			) as p on p.object_id = a.object_id AND p.column_id = a.column_id
	          where a.object_id=object_id('user') [] - 88.9988ms
2020/11/18 09:31:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] SELECT
	IXS.NAME                    AS  [INDEX_NAME],
	C.NAME                      AS  [COLUMN_NAME],
	IXS.is_unique AS [IS_UNIQUE]
	FROM SYS.INDEXES IXS
	INNER JOIN SYS.INDEX_COLUMNS   IXCS
	ON IXS.OBJECT_ID=IXCS.OBJECT_ID  AND IXS.INDEX_ID = IXCS.INDEX_ID
	INNER   JOIN SYS.COLUMNS C  ON IXS.OBJECT_ID=C.OBJECT_ID
	AND IXCS.COLUMN_ID=C.COLUMN_ID
	WHERE IXS.TYPE_DESC='NONCLUSTERED' and OBJECT_NAME(IXS.OBJECT_ID) =?
	 [user] - 1.0002ms
2020/11/18 09:31:03 routers/init.go:76:initDBEngine() [E] ORM engine initialization attempt #1/10 failed. Error: sync database struct error: mssql: Invalid object name 'SYS.INDEXES'.
2020/11/18 09:31:03 routers/init.go:77:initDBEngine() [I] Backing off for 3 seconds
2020/11/18 09:31:03 ...eue/queue_wrapped.go:75:setInternal() [W] [Attempt: 3] Failed to create queue: level for notification-service cfg: queue.LevelQueueConfiguration{ByteFIFOQueueConfiguration:queue.ByteFIFOQueueConfiguration{WorkerPoolConfiguration:queue.WorkerPoolConfiguration{QueueLength:20, BatchLength:20, BlockTimeout:1000000000, BoostTimeout:300000000000, BoostWorkers:5, MaxWorkers:6}, Workers:1, Name:"notification-service-level"}, DataDir:"D:\\gitea\\var\\lib\\gitea\\data\\queues\\notification-service"} error: The process cannot access the file because it is being used by another process.
@zeripath
Copy link
Contributor

zeripath commented Aug 5, 2021

Ah

I think this might be due to the capitalisation.

WHY MSSQL WHY?

zeripath added a commit to zeripath/gitea that referenced this issue Aug 6, 2021
This fixes two problems with MSSQL:

* `ALTER TABLE DROP ... IF EXISTS ...` is only supported in SQL Server >16.

The `IF EXISTS` here is a belt-and-braces and does not need to be present. Therefore
can be dropped. Also stop attempting to drop the indexes as constraints as they're indexes!

* System tables like: `sys.indexes` should be lowercase not uppercase because of collation issues.

Fix go-gitea#13615

Signed-off-by: Andrew Thornton <[email protected]>
lafriks pushed a commit that referenced this issue Aug 8, 2021
This fixes two problems with MSSQL:

* `ALTER TABLE DROP ... IF EXISTS ...` is only supported in SQL Server >16.

The `IF EXISTS` here is a belt-and-braces and does not need to be present. Therefore
can be dropped. Also stop attempting to drop the indexes as constraints as they're indexes!

* System tables like: `sys.indexes` should be lowercase not uppercase because of collation issues.

Fix #13615

Signed-off-by: Andrew Thornton <[email protected]>

Co-authored-by: Lunny Xiao <[email protected]>
@go-gitea go-gitea locked and limited conversation to collaborators Oct 19, 2021
@wxiaoguang wxiaoguang reopened this Dec 31, 2023
@go-gitea go-gitea unlocked this conversation Dec 31, 2023
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Dec 31, 2023

@lunny this is a XORM bug.

When you use "CS_AS" collation for the MSSQL database, you should use "SELECT * FROM sys.indexes" but not "SYS.INDEXES"

image

@lunny lunny added type/bug type/upstream This is an issue in one of Gitea's dependencies and should be reported there labels Dec 31, 2023
@lunny
Copy link
Member

lunny commented Feb 28, 2024

Fixed by https://gitea.com/xorm/xorm/pulls/2389 and merged into Gitea.

@lunny lunny closed this as completed Feb 28, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type/bug type/upstream This is an issue in one of Gitea's dependencies and should be reported there
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants