diff options
author | guillep2k <18600385+guillep2k@users.noreply.github.com> | 2020-01-20 12:45:14 -0300 |
---|---|---|
committer | Antoine GIRARD <sapk@users.noreply.github.com> | 2020-01-20 16:45:14 +0100 |
commit | ad1b6d439fe0e0875e54227e0bc23a74411f490e (patch) | |
tree | 8e16e25a913d167ebb772ad2e0e92cbba9c56b66 /vendor/xorm.io | |
parent | 6d6f1d568ec36786b1020f4b43cbd872228c6633 (diff) | |
download | gitea-ad1b6d439fe0e0875e54227e0bc23a74411f490e.tar.gz gitea-ad1b6d439fe0e0875e54227e0bc23a74411f490e.zip |
Add support for database schema in PostgreSQL (#8819)
* Add support for database schema
* Require setting search_path for the db user
* Add schema setting to admin/config.tmpl
* Use a schema different from default for psql tests
* Update postgres scripts to use custom schema
* Update to xorm/core 0.7.3 and xorm/xorm c37aff9b3a
* Fix migration test
Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'vendor/xorm.io')
-rw-r--r-- | vendor/xorm.io/core/.drone.yml | 120 | ||||
-rw-r--r-- | vendor/xorm.io/core/README.md | 2 | ||||
-rw-r--r-- | vendor/xorm.io/core/column.go | 4 | ||||
-rw-r--r-- | vendor/xorm.io/core/index.go | 4 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/.drone.yml | 216 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/dialect_postgres.go | 6 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/session_insert.go | 94 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/session_update.go | 10 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/statement_exprparam.go | 10 |
9 files changed, 71 insertions, 395 deletions
diff --git a/vendor/xorm.io/core/.drone.yml b/vendor/xorm.io/core/.drone.yml index 4cb2fb4ae6..3c118d4c06 100644 --- a/vendor/xorm.io/core/.drone.yml +++ b/vendor/xorm.io/core/.drone.yml @@ -1,128 +1,8 @@ --- kind: pipeline -name: go1.10 - -platform: - os: linux - arch: amd64 - -clone: - disable: true - -workspace: - base: /go - path: src/xorm.io/core - -steps: -- name: git - pull: default - image: plugins/git:next - settings: - depth: 50 - tags: true - -- name: test - pull: default - image: golang:1.10 - commands: - - go get github.com/stretchr/testify/assert - - go get github.com/go-xorm/sqlfiddle - - go get github.com/go-sql-driver/mysql - - go get github.com/mattn/go-sqlite3 - - go vet - - "go test -v -race -coverprofile=coverage.txt -covermode=atomic -dbConn=\"root:@tcp(mysql:3306)/core_test?charset=utf8mb4\"" - when: - event: - - push - - tag - - pull_request - -services: -- name: mysql - pull: default - image: mysql:5.7 - environment: - MYSQL_ALLOW_EMPTY_PASSWORD: yes - MYSQL_DATABASE: core_test - when: - event: - - push - - tag - - pull_request - ---- -kind: pipeline -name: go1.11 - -platform: - os: linux - arch: amd64 - -clone: - disable: true - -workspace: - base: /go - path: src/xorm.io/core - -steps: -- name: git - pull: default - image: plugins/git:next - settings: - depth: 50 - tags: true - -- name: test - pull: default - image: golang:1.11 - commands: - - go vet - - "go test -v -race -coverprofile=coverage.txt -covermode=atomic -dbConn=\"root:@tcp(mysql:3306)/core_test?charset=utf8mb4\"" - environment: - GO111MODULE: "on" - GOPROXY: https://goproxy.cn - when: - event: - - push - - tag - - pull_request - -services: -- name: mysql - pull: default - image: mysql:5.7 - environment: - MYSQL_ALLOW_EMPTY_PASSWORD: yes - MYSQL_DATABASE: core_test - when: - event: - - push - - tag - - pull_request - ---- -kind: pipeline name: go1.12 -platform: - os: linux - arch: amd64 - -clone: - disable: true - -workspace: - base: /go - path: src/xorm.io/core - steps: -- name: git - pull: default - image: plugins/git:next - settings: - depth: 50 - tags: true - name: test pull: default diff --git a/vendor/xorm.io/core/README.md b/vendor/xorm.io/core/README.md index c2cedcae8c..54436b6893 100644 --- a/vendor/xorm.io/core/README.md +++ b/vendor/xorm.io/core/README.md @@ -1,7 +1,7 @@ Core is a lightweight wrapper of sql.DB. [![Build Status](https://drone.gitea.com/api/badges/xorm/core/status.svg)](https://drone.gitea.com/xorm/core) -[![](http://gocover.io/_badge/xorm.io/core)](http://gocover.io/xorm.io/core) +[![Test Coverage](https://gocover.io/_badge/xorm.io/core)](https://gocover.io/xorm.io/core) [![Go Report Card](https://goreportcard.com/badge/code.gitea.io/gitea)](https://goreportcard.com/report/xorm.io/core) # Open diff --git a/vendor/xorm.io/core/column.go b/vendor/xorm.io/core/column.go index b5906a9874..8f375db594 100644 --- a/vendor/xorm.io/core/column.go +++ b/vendor/xorm.io/core/column.go @@ -37,7 +37,7 @@ type Column struct { IsDeleted bool IsCascade bool IsVersion bool - DefaultIsEmpty bool + DefaultIsEmpty bool // false means column has no default set, but not default value is empty EnumOptions map[string]int SetOptions map[string]int DisableTimeZone bool @@ -65,7 +65,7 @@ func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable IsDeleted: false, IsCascade: false, IsVersion: false, - DefaultIsEmpty: false, + DefaultIsEmpty: true, // default should be no default EnumOptions: make(map[string]int), Comment: "", } diff --git a/vendor/xorm.io/core/index.go b/vendor/xorm.io/core/index.go index 2915428f26..129b543921 100644 --- a/vendor/xorm.io/core/index.go +++ b/vendor/xorm.io/core/index.go @@ -26,8 +26,8 @@ type Index struct { func (index *Index) XName(tableName string) string { if !strings.HasPrefix(index.Name, "UQE_") && !strings.HasPrefix(index.Name, "IDX_") { - tableName = strings.Replace(tableName, `"`, "", -1) - tableName = strings.Replace(tableName, `.`, "_", -1) + tableParts := strings.Split(strings.Replace(tableName, `"`, "", -1), ".") + tableName = tableParts[len(tableParts)-1] if index.Type == UniqueType { return fmt.Sprintf("UQE_%v_%v", tableName, index.Name) } diff --git a/vendor/xorm.io/xorm/.drone.yml b/vendor/xorm.io/xorm/.drone.yml index b2198e380a..e9dae78896 100644 --- a/vendor/xorm.io/xorm/.drone.yml +++ b/vendor/xorm.io/xorm/.drone.yml @@ -1,204 +1,14 @@ --- kind: pipeline -name: go1.10-test -workspace: - base: /go - path: src/gitea.com/xorm/xorm - -steps: -- name: build - pull: default - image: golang:1.10 - commands: - - go get -t -d -v - - go build -v - when: - event: - - push - - pull_request - -- name: test-sqlite - pull: default - image: golang:1.10 - depends_on: - - build - commands: - - "go test -v -race -db=\"sqlite3\" -conn_str=\"./test.db\" -coverprofile=coverage1-1.txt -covermode=atomic" - - "go test -v -race -db=\"sqlite3\" -conn_str=\"./test.db\" -cache=true -coverprofile=coverage1-2.txt -covermode=atomic" - when: - event: - - push - - pull_request - -- name: test-mysql - pull: default - image: golang:1.10 - depends_on: - - build - commands: - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(mysql)/xorm_test\" -coverprofile=coverage2-1.txt -covermode=atomic" - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(mysql)/xorm_test\" -cache=true -coverprofile=coverage2-2.txt -covermode=atomic" - when: - event: - - push - - pull_request - -- name: test-mysql-utf8mb4 - pull: default - image: golang:1.10 - depends_on: - - test-mysql - commands: - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(mysql)/xorm_test?charset=utf8mb4\" -coverprofile=coverage2.1-1.txt -covermode=atomic" - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(mysql)/xorm_test?charset=utf8mb4\" -cache=true -coverprofile=coverage2.1-2.txt -covermode=atomic" - when: - event: - - push - - pull_request - -- name: test-mymysql - pull: default - image: golang:1.10 - depends_on: - - test-mysql-utf8mb4 - commands: - - "go test -v -race -db=\"mymysql\" -conn_str=\"tcp:mysql:3306*xorm_test/root/\" -coverprofile=coverage3-1.txt -covermode=atomic" - - "go test -v -race -db=\"mymysql\" -conn_str=\"tcp:mysql:3306*xorm_test/root/\" -cache=true -coverprofile=coverage3-2.txt -covermode=atomic" - when: - event: - - push - - pull_request - -- name: test-postgres - pull: default - image: golang:1.10 - depends_on: - - build - commands: - - "go test -v -race -db=\"postgres\" -conn_str=\"postgres://postgres:@pgsql/xorm_test?sslmode=disable\" -coverprofile=coverage4-1.txt -covermode=atomic" - - "go test -v -race -db=\"postgres\" -conn_str=\"postgres://postgres:@pgsql/xorm_test?sslmode=disable\" -cache=true -coverprofile=coverage4-2.txt -covermode=atomic" - when: - event: - - push - - pull_request - -- name: test-postgres-schema - pull: default - image: golang:1.10 - depends_on: - - build - commands: - - "go test -v -race -db=\"postgres\" -conn_str=\"postgres://postgres:@pgsql/xorm_test?sslmode=disable\" -schema=xorm -coverprofile=coverage5-1.txt -covermode=atomic" - - "go test -v -race -db=\"postgres\" -conn_str=\"postgres://postgres:@pgsql/xorm_test?sslmode=disable\" -schema=xorm -cache=true -coverprofile=coverage5-2.txt -covermode=atomic" - when: - event: - - push - - pull_request - -- name: test-mssql - pull: default - image: golang:1.10 - depends_on: - - build - commands: - - "go test -v -race -db=\"mssql\" -conn_str=\"server=mssql;user id=sa;password=yourStrong(!)Password;database=xorm_test\" -coverprofile=coverage6-1.txt -covermode=atomic" - - "go test -v -race -db=\"mssql\" -conn_str=\"server=mssql;user id=sa;password=yourStrong(!)Password;database=xorm_test\" -cache=true -coverprofile=coverage6-2.txt -covermode=atomic" - when: - event: - - push - - pull_request - -- name: test-tidb - pull: default - image: golang:1.10 - depends_on: - - build - commands: - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(tidb:4000)/xorm_test\" -ignore_select_update=true -coverprofile=coverage7-1.txt -covermode=atomic" - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(tidb:4000)/xorm_test\" -ignore_select_update=true -cache=true -coverprofile=coverage7-2.txt -covermode=atomic" - when: - event: - - push - - pull_request - -- name: test-end - pull: default - image: golang:1.10 - depends_on: - - test-sqlite - - test-mysql - - test-mysql-utf8mb4 - - test-mymysql - - test-postgres - - test-postgres-schema - - test-mssql - - test-tidb - commands: - - echo "go1.10 build end" - when: - event: - - push - - pull_request - -services: -- name: mysql - pull: default - image: mysql:5.7 - environment: - MYSQL_ALLOW_EMPTY_PASSWORD: yes - MYSQL_DATABASE: xorm_test - when: - event: - - push - - tag - - pull_request - -- name: pgsql - pull: default - image: postgres:9.5 - environment: - POSTGRES_DB: xorm_test - POSTGRES_USER: postgres - when: - event: - - push - - tag - - pull_request - -- name: mssql - pull: default - image: microsoft/mssql-server-linux:latest - environment: - ACCEPT_EULA: Y - SA_PASSWORD: yourStrong(!)Password - MSSQL_PID: Developer - when: - event: - - push - - tag - - pull_request - -- name: tidb - pull: default - image: pingcap/tidb:v3.0.3 - when: - event: - - push - - tag - - pull_request - ---- -kind: pipeline -name: go1.13-test +name: testing steps: -- name: build +- name: test-vet pull: default - image: golang:1.13 + image: golang:1.12 environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" commands: - - go build -v - go vet when: event: @@ -207,7 +17,7 @@ steps: - name: test-sqlite pull: default - image: golang:1.13 + image: golang:1.12 environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" @@ -221,7 +31,7 @@ steps: - name: test-mysql pull: default - image: golang:1.13 + image: golang:1.12 environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" @@ -235,7 +45,7 @@ steps: - name: test-mysql-utf8mb4 pull: default - image: golang:1.13 + image: golang:1.12 depends_on: - test-mysql environment: @@ -251,7 +61,7 @@ steps: - name: test-mymysql pull: default - image: golang:1.13 + image: golang:1.12 depends_on: - test-mysql-utf8mb4 environment: @@ -267,7 +77,7 @@ steps: - name: test-postgres pull: default - image: golang:1.13 + image: golang:1.12 environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" @@ -281,7 +91,7 @@ steps: - name: test-postgres-schema pull: default - image: golang:1.13 + image: golang:1.12 environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" @@ -295,7 +105,7 @@ steps: - name: test-mssql pull: default - image: golang:1.13 + image: golang:1.12 environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" @@ -309,7 +119,7 @@ steps: - name: test-tidb pull: default - image: golang:1.13 + image: golang:1.12 environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" @@ -323,12 +133,12 @@ steps: - name: merge_coverage pull: default - image: golang:1.13 + image: golang:1.12 environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" depends_on: - - build + - test-vet - test-sqlite - test-mysql - test-mysql-utf8mb4 diff --git a/vendor/xorm.io/xorm/dialect_postgres.go b/vendor/xorm.io/xorm/dialect_postgres.go index ccef3086b2..ac6d4fe896 100644 --- a/vendor/xorm.io/xorm/dialect_postgres.go +++ b/vendor/xorm.io/xorm/dialect_postgres.go @@ -901,7 +901,7 @@ func (db *postgres) TableCheckSql(tableName string) (string, []interface{}) { } func (db *postgres) ModifyColumnSql(tableName string, col *core.Column) string { - if len(db.Schema) == 0 { + if len(db.Schema) == 0 || strings.Contains(tableName, ".") { return fmt.Sprintf("alter table %s ALTER COLUMN %s TYPE %s", tableName, col.Name, db.SqlType(col)) } @@ -913,8 +913,8 @@ func (db *postgres) DropIndexSql(tableName string, index *core.Index) string { quote := db.Quote idxName := index.Name - tableName = strings.Replace(tableName, `"`, "", -1) - tableName = strings.Replace(tableName, `.`, "_", -1) + tableParts := strings.Split(strings.Replace(tableName, `"`, "", -1), ".") + tableName = tableParts[len(tableParts)-1] if !strings.HasPrefix(idxName, "UQE_") && !strings.HasPrefix(idxName, "IDX_") { diff --git a/vendor/xorm.io/xorm/session_insert.go b/vendor/xorm.io/xorm/session_insert.go index 1e19ce7a4e..5f8f7e1ee8 100644 --- a/vendor/xorm.io/xorm/session_insert.go +++ b/vendor/xorm.io/xorm/session_insert.go @@ -729,66 +729,7 @@ func (session *Session) insertMapInterface(m map[string]interface{}) (int64, err args = append(args, m[colName]) } - w := builder.NewWriter() - if session.statement.cond.IsValid() { - if _, err := w.WriteString(fmt.Sprintf("INSERT INTO %s (", session.engine.Quote(tableName))); err != nil { - return 0, err - } - - if err := writeStrings(w, append(columns, exprs.colNames...), "`", "`"); err != nil { - return 0, err - } - - if _, err := w.WriteString(") SELECT "); err != nil { - return 0, err - } - - if err := session.statement.writeArgs(w, args); err != nil { - return 0, err - } - - if len(exprs.args) > 0 { - if _, err := w.WriteString(","); err != nil { - return 0, err - } - if err := exprs.writeArgs(w); err != nil { - return 0, err - } - } - - if _, err := w.WriteString(fmt.Sprintf(" FROM %s WHERE ", session.engine.Quote(tableName))); err != nil { - return 0, err - } - - if err := session.statement.cond.WriteTo(w); err != nil { - return 0, err - } - } else { - qm := strings.Repeat("?,", len(columns)) - qm = qm[:len(qm)-1] - - if _, err := w.WriteString(fmt.Sprintf("INSERT INTO %s (`%s`) VALUES (%s)", session.engine.Quote(tableName), strings.Join(columns, "`,`"), qm)); err != nil { - return 0, err - } - w.Append(args...) - } - - sql := w.String() - args = w.Args() - - if err := session.cacheInsert(tableName); err != nil { - return 0, err - } - - res, err := session.exec(sql, args...) - if err != nil { - return 0, err - } - affected, err := res.RowsAffected() - if err != nil { - return 0, err - } - return affected, nil + return session.insertMap(columns, args) } func (session *Session) insertMapString(m map[string]string) (int64, error) { @@ -808,6 +749,7 @@ func (session *Session) insertMapString(m map[string]string) (int64, error) { columns = append(columns, k) } } + sort.Strings(columns) var args = make([]interface{}, 0, len(m)) @@ -815,7 +757,18 @@ func (session *Session) insertMapString(m map[string]string) (int64, error) { args = append(args, m[colName]) } + return session.insertMap(columns, args) +} + +func (session *Session) insertMap(columns []string, args []interface{}) (int64, error) { + tableName := session.statement.TableName() + if len(tableName) <= 0 { + return 0, ErrTableNotFound + } + + exprs := session.statement.exprColumns w := builder.NewWriter() + // if insert where if session.statement.cond.IsValid() { if _, err := w.WriteString(fmt.Sprintf("INSERT INTO %s (", session.engine.Quote(tableName))); err != nil { return 0, err @@ -853,10 +806,29 @@ func (session *Session) insertMapString(m map[string]string) (int64, error) { qm := strings.Repeat("?,", len(columns)) qm = qm[:len(qm)-1] - if _, err := w.WriteString(fmt.Sprintf("INSERT INTO %s (`%s`) VALUES (%s)", session.engine.Quote(tableName), strings.Join(columns, "`,`"), qm)); err != nil { + if _, err := w.WriteString(fmt.Sprintf("INSERT INTO %s (", session.engine.Quote(tableName))); err != nil { return 0, err } + + if err := writeStrings(w, append(columns, exprs.colNames...), "`", "`"); err != nil { + return 0, err + } + if _, err := w.WriteString(fmt.Sprintf(") VALUES (%s", qm)); err != nil { + return 0, err + } + w.Append(args...) + if len(exprs.args) > 0 { + if _, err := w.WriteString(","); err != nil { + return 0, err + } + if err := exprs.writeArgs(w); err != nil { + return 0, err + } + } + if _, err := w.WriteString(")"); err != nil { + return 0, err + } } sql := w.String() diff --git a/vendor/xorm.io/xorm/session_update.go b/vendor/xorm.io/xorm/session_update.go index 231163e063..47ced66d19 100644 --- a/vendor/xorm.io/xorm/session_update.go +++ b/vendor/xorm.io/xorm/session_update.go @@ -239,14 +239,20 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 for i, colName := range exprColumns.colNames { switch tp := exprColumns.args[i].(type) { case string: - colNames = append(colNames, session.engine.Quote(colName)+" = "+tp) + if len(tp) == 0 { + tp = "''" + } + colNames = append(colNames, session.engine.Quote(colName)+"="+tp) case *builder.Builder: subQuery, subArgs, err := builder.ToSQL(tp) if err != nil { return 0, err } - colNames = append(colNames, session.engine.Quote(colName)+" = ("+subQuery+")") + colNames = append(colNames, session.engine.Quote(colName)+"=("+subQuery+")") args = append(args, subArgs...) + default: + colNames = append(colNames, session.engine.Quote(colName)+"=?") + args = append(args, exprColumns.args[i]) } } diff --git a/vendor/xorm.io/xorm/statement_exprparam.go b/vendor/xorm.io/xorm/statement_exprparam.go index 4da4f1ea12..fc62e36f1f 100644 --- a/vendor/xorm.io/xorm/statement_exprparam.go +++ b/vendor/xorm.io/xorm/statement_exprparam.go @@ -69,10 +69,18 @@ func (exprs *exprParams) writeArgs(w *builder.BytesWriter) error { if _, err := w.WriteString(")"); err != nil { return err } - default: + case string: + if arg == "" { + arg = "''" + } if _, err := w.WriteString(fmt.Sprintf("%v", arg)); err != nil { return err } + default: + if _, err := w.WriteString("?"); err != nil { + return err + } + w.Append(arg) } if i != len(exprs.args)-1 { if _, err := w.WriteString(","); err != nil { |