Browse Source

upgrade to use testfixtures v3 (#11904)

* upgrade to use testfixtures v3

* simplify logic

* make vendor

* update per @lunny

* Update templates/repo/empty.tmpl

* Update templates/repo/empty.tmpl

Co-authored-by: Lauris BH <[email protected]>
pull/11916/head
techknowlogick GitHub 1 month ago
parent
commit
9e6a79bea9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
97 changed files with 8813 additions and 5463 deletions
  1. +0
    -3
      contrib/pr/checkout.go
  2. +3
    -5
      go.mod
  3. +11
    -4
      go.sum
  4. +0
    -16
      integrations/integration_test.go
  5. +35
    -5
      models/test_fixtures.go
  6. +1
    -2
      models/unit_tests.go
  7. +0
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/.editorconfig
  8. +0
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/.gitattributes
  9. +2
    -2
      vendor/github.com/go-testfixtures/testfixtures/v3/.gitignore
  10. +41
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/.goreleaser.yml
  11. +0
    -1
      vendor/github.com/go-testfixtures/testfixtures/v3/.sample.env
  12. +93
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/CHANGELOG.md
  13. +9
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/Dockerfile
  14. +0
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/LICENSE
  15. +483
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/README.md
  16. +59
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/Taskfile.yml
  17. +37
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/docker-compose.yml
  18. +165
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/dump.go
  19. +14
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/go.mod
  20. +26
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/go.sum
  21. +6
    -8
      vendor/github.com/go-testfixtures/testfixtures/v3/helper.go
  22. +0
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/json.go
  23. +16
    -19
      vendor/github.com/go-testfixtures/testfixtures/v3/mysql.go
  24. +35
    -28
      vendor/github.com/go-testfixtures/testfixtures/v3/postgresql.go
  25. +5
    -6
      vendor/github.com/go-testfixtures/testfixtures/v3/sqlite.go
  26. +41
    -23
      vendor/github.com/go-testfixtures/testfixtures/v3/sqlserver.go
  27. +599
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/testfixtures.go
  28. +43
    -0
      vendor/github.com/go-testfixtures/testfixtures/v3/time.go
  29. +4
    -2
      vendor/github.com/mattn/go-sqlite3/.travis.yml
  30. +53
    -4
      vendor/github.com/mattn/go-sqlite3/README.md
  31. +4
    -4
      vendor/github.com/mattn/go-sqlite3/backup.go
  32. +22
    -4
      vendor/github.com/mattn/go-sqlite3/callback.go
  33. +299
    -0
      vendor/github.com/mattn/go-sqlite3/convert.go
  34. +18
    -3
      vendor/github.com/mattn/go-sqlite3/error.go
  35. +5233
    -3958
      vendor/github.com/mattn/go-sqlite3/sqlite3-binding.c
  36. +73
    -8
      vendor/github.com/mattn/go-sqlite3/sqlite3-binding.h
  37. +98
    -56
      vendor/github.com/mattn/go-sqlite3/sqlite3.go
  38. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_context.go
  39. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go
  40. +2
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_libsqlite3.go
  41. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_load_extension.go
  42. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_load_extension_omit.go
  43. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_allow_uri_authority.go
  44. +2
    -2
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_app_armor.go
  45. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_foreign_keys.go
  46. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_fts5.go
  47. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_icu.go
  48. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_introspect.go
  49. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_json1.go
  50. +20
    -0
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_preupdate.go
  51. +112
    -0
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_preupdate_hook.go
  52. +21
    -0
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_preupdate_omit.go
  53. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_secure_delete.go
  54. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_secure_delete_fast.go
  55. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_stat4.go
  56. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_unlock_notify.go
  57. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_vacuum_full.go
  58. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_vacuum_incr.go
  59. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_opt_vtable.go
  60. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_other.go
  61. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_solaris.go
  62. +2
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_trace.go
  63. +5
    -0
      vendor/github.com/mattn/go-sqlite3/sqlite3_type.go
  64. +1
    -1
      vendor/github.com/mattn/go-sqlite3/sqlite3_windows.go
  65. +4
    -0
      vendor/github.com/mattn/go-sqlite3/sqlite3ext.h
  66. +5
    -0
      vendor/github.com/mattn/go-sqlite3/static_mock.go
  67. +4
    -3
      vendor/github.com/spf13/pflag/.travis.yml
  68. +2
    -2
      vendor/github.com/spf13/pflag/README.md
  69. +38
    -0
      vendor/github.com/spf13/pflag/bool_slice.go
  70. +2
    -2
      vendor/github.com/spf13/pflag/count.go
  71. +38
    -0
      vendor/github.com/spf13/pflag/duration_slice.go
  72. +14
    -2
      vendor/github.com/spf13/pflag/flag.go
  73. +174
    -0
      vendor/github.com/spf13/pflag/float32_slice.go
  74. +166
    -0
      vendor/github.com/spf13/pflag/float64_slice.go
  75. +3
    -0
      vendor/github.com/spf13/pflag/go.mod
  76. +0
    -0
      vendor/github.com/spf13/pflag/go.sum
  77. +174
    -0
      vendor/github.com/spf13/pflag/int32_slice.go
  78. +166
    -0
      vendor/github.com/spf13/pflag/int64_slice.go
  79. +30
    -0
      vendor/github.com/spf13/pflag/int_slice.go
  80. +39
    -1
      vendor/github.com/spf13/pflag/ip_slice.go
  81. +26
    -0
      vendor/github.com/spf13/pflag/string_array.go
  82. +18
    -4
      vendor/github.com/spf13/pflag/string_slice.go
  83. +149
    -0
      vendor/github.com/spf13/pflag/string_to_int64.go
  84. +42
    -0
      vendor/github.com/spf13/pflag/uint_slice.go
  85. +0
    -26
      vendor/gopkg.in/testfixtures.v2/.travis.yml
  86. +0
    -358
      vendor/gopkg.in/testfixtures.v2/README.md
  87. +0
    -64
      vendor/gopkg.in/testfixtures.v2/Taskfile.yml
  88. +0
    -51
      vendor/gopkg.in/testfixtures.v2/appveyor.yml
  89. +0
    -75
      vendor/gopkg.in/testfixtures.v2/deprecated.go
  90. +0
    -41
      vendor/gopkg.in/testfixtures.v2/errors.go
  91. +0
    -110
      vendor/gopkg.in/testfixtures.v2/generate.go
  92. +0
    -19
      vendor/gopkg.in/testfixtures.v2/options.go
  93. +0
    -171
      vendor/gopkg.in/testfixtures.v2/oracle.go
  94. +0
    -305
      vendor/gopkg.in/testfixtures.v2/testfixtures.go
  95. +0
    -34
      vendor/gopkg.in/testfixtures.v2/time.go
  96. +1
    -0
      vendor/gopkg.in/yaml.v2/apic.go
  97. +6
    -10
      vendor/modules.txt

+ 0
- 3
contrib/pr/checkout.go View File

@@ -37,7 +37,6 @@ import (
"github.com/go-git/go-git/v5/plumbing"
context2 "github.com/gorilla/context"
"github.com/unknwon/com"
"gopkg.in/testfixtures.v2"
"xorm.io/xorm"
)

@@ -96,14 +95,12 @@ func runPR() {
setting.Database.LogSQL = true
//x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")

var helper testfixtures.Helper = &testfixtures.SQLite{}
models.NewEngine(context.Background(), func(_ *xorm.Engine) error {
return nil
})
models.HasEngine = true
//x.ShowSQL(true)
err = models.InitFixtures(
helper,
path.Join(curDir, "models/fixtures/"),
)
if err != nil {


+ 3
- 5
go.mod View File

@@ -44,6 +44,7 @@ require (
github.com/go-redis/redis v6.15.2+incompatible
github.com/go-sql-driver/mysql v1.4.1
github.com/go-swagger/go-swagger v0.21.0
github.com/go-testfixtures/testfixtures/v3 v3.2.0
github.com/gobwas/glob v0.2.3
github.com/gogs/chardet v0.0.0-20191104214054-4b6791f73a28
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14
@@ -56,7 +57,6 @@ require (
github.com/issue9/identicon v1.0.1
github.com/jaytaylor/html2text v0.0.0-20160923191438-8fb95d837f7d
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/joho/godotenv v1.3.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20170619183022-cd60e84ee657
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
github.com/klauspost/compress v1.10.2
@@ -66,8 +66,7 @@ require (
github.com/mailru/easyjson v0.7.0 // indirect
github.com/markbates/goth v1.61.2
github.com/mattn/go-isatty v0.0.11
github.com/mattn/go-oci8 v0.0.0-20190320171441-14ba190cf52d // indirect
github.com/mattn/go-sqlite3 v1.11.0
github.com/mattn/go-sqlite3 v2.0.2+incompatible
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75
github.com/mgechev/dots v0.0.0-20190921121421-c36f7dcfbb81
github.com/mgechev/revive v1.0.2
@@ -115,8 +114,7 @@ require (
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/ini.v1 v1.52.0
gopkg.in/ldap.v3 v3.0.2
gopkg.in/testfixtures.v2 v2.5.0
gopkg.in/yaml.v2 v2.2.8
gopkg.in/yaml.v2 v2.3.0
mvdan.cc/xurls/v2 v2.1.0
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251
xorm.io/builder v0.3.7


+ 11
- 4
go.sum View File

@@ -149,6 +149,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
github.com/denisenkom/go-mssqldb v0.0.0-20190924004331-208c0a498538 h1:bpWCJ5MddHsv4Xtl3azkK89mZzd/vvut32mvAnKbyUA=
github.com/denisenkom/go-mssqldb v0.0.0-20190924004331-208c0a498538/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc h1:VRRKCwnzqk8QCaRC4os14xoKDdbHqqlJtJA0oc1ZAjg=
github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
@@ -278,6 +279,8 @@ github.com/go-swagger/go-swagger v0.21.0 h1:AX9mdfzp6eJtUe92nFrWmbK7ocRgkCDPJs0F
github.com/go-swagger/go-swagger v0.21.0/go.mod h1:tDb8PdDVFcaE8EPXkMOsuxpL3UEPiwu1UDZar9Z/1RY=
github.com/go-swagger/scan-repo-boundary v0.0.0-20180623220736-973b3573c013 h1:l9rI6sNaZgNC0LnF3MiE+qTmyBA/tZAg1rtyrGbUMK0=
github.com/go-swagger/scan-repo-boundary v0.0.0-20180623220736-973b3573c013/go.mod h1:b65mBPzqzZWxOZGxSWrqs4GInLIn+u99Q9q7p+GKni0=
github.com/go-testfixtures/testfixtures/v3 v3.2.0 h1:FGAW3z5UzmrZGjR/dZp1u3Tbld0SDmirLO4RrR5++7Q=
github.com/go-testfixtures/testfixtures/v3 v3.2.0/go.mod h1:RZctY24ixituGC73XlAV1gkCwYMVwiSwPm26MNlQIhE=
github.com/go-xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:9wScpmSP5A3Bk8V3XHWUcJmYTh+ZnlHVyc+A4oZYS3Y=
github.com/go-xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:56xuuqnHyryaerycW3BfssRdxQstACi0Epw/yC5E2xM=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
@@ -437,6 +440,7 @@ github.com/lestrrat-go/jwx v0.9.0/go.mod h1:iEoxlYfZjvoGpuWwxUz+eR5e6KTJGsaRcy/Y
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.7.0 h1:h93mCPfUSkaul3Ka/VG8uZdmW1uMHDGxzu0NWHuJmHY=
github.com/lib/pq v1.7.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96 h1:uNwtsDp7ci48vBTTxDuwcoTXz4lwtDTe7TjCQ0noaWY=
@@ -462,13 +466,13 @@ github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-oci8 v0.0.0-20190320171441-14ba190cf52d h1:m+dSK37rFf2fqppZhg15yI2IwC9BtucBiRwSDm9VL8g=
github.com/mattn/go-oci8 v0.0.0-20190320171441-14ba190cf52d/go.mod h1:/M9VLO+lUPmxvoOK2PfWRZ8mTtB4q1Hy9lEGijv9Nr8=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q=
github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v2.0.2+incompatible h1:qzw9c2GNT8UFrgWNDhCTqRqYUSmu/Dav/9Z58LGpk7U=
github.com/mattn/go-sqlite3 v2.0.2+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 h1:Pijfgr7ZuvX7QIQiEwLdRVr3RoMG+i0SbBO1Qu+7yVk=
@@ -610,6 +614,8 @@ github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmq
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
@@ -890,8 +896,6 @@ gopkg.in/ldap.v3 v3.0.2 h1:R6RBtabK6e1GO0eQKtkyOFbAHO73QesLzI2w2DZ6b9w=
gopkg.in/ldap.v3 v3.0.2/go.mod h1:oxD7NyBuxchC+SgJDE1Q5Od05eGt29SDQVBmV+HYbzw=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/square/go-jose.v2 v2.3.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/testfixtures.v2 v2.5.0 h1:N08B7l2GzFQenyYbzqthDnKAA+cmb17iAZhhFxr7JHw=
gopkg.in/testfixtures.v2 v2.5.0/go.mod h1:vyAq+MYCgNpR29qitQdLZhdbLFf4mR/2MFJRFoQZZ2M=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
@@ -902,8 +906,11 @@ gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=


+ 0
- 16
integrations/integration_test.go View File

@@ -36,7 +36,6 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
"github.com/unknwon/com"
"gopkg.in/testfixtures.v2"
)

var mac *macaron.Macaron
@@ -88,22 +87,7 @@ func TestMain(m *testing.M) {
}
}

var helper testfixtures.Helper
if setting.Database.UseMySQL {
helper = &testfixtures.MySQL{}
} else if setting.Database.UsePostgreSQL {
helper = &testfixtures.PostgreSQL{}
} else if setting.Database.UseSQLite3 {
helper = &testfixtures.SQLite{}
} else if setting.Database.UseMSSQL {
helper = &testfixtures.SQLServer{}
} else {
fmt.Println("Unsupported RDBMS for integration tests")
os.Exit(1)
}

err := models.InitFixtures(
helper,
path.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
)
if err != nil {


+ 35
- 5
models/test_fixtures.go View File

@@ -6,18 +6,48 @@ package models

import (
"fmt"
"os"
"time"

"gopkg.in/testfixtures.v2"
"github.com/go-testfixtures/testfixtures/v3"
"xorm.io/xorm/schemas"
)

var fixtures *testfixtures.Context
var fixtures *testfixtures.Loader

// InitFixtures initialize test fixtures for a test database
func InitFixtures(helper testfixtures.Helper, dir string) (err error) {
testfixtures.SkipDatabaseNameCheck(true)
fixtures, err = testfixtures.NewFolder(x.DB().DB, helper, dir)
func InitFixtures(dir string) (err error) {
testfiles := testfixtures.Directory(dir)
dialect := "unknown"
switch x.Dialect().URI().DBType {
case schemas.POSTGRES:
dialect = "postgres"
case schemas.MYSQL:
dialect = "mysql"
case schemas.MSSQL:
dialect = "mssql"
case schemas.SQLITE:
dialect = "sqlite3"
default:
fmt.Println("Unsupported RDBMS for integration tests")
os.Exit(1)
}
loaderOptions := []func(loader *testfixtures.Loader) error{
testfixtures.Database(x.DB().DB),
testfixtures.Dialect(dialect),
testfixtures.DangerousSkipTestDatabaseCheck(),
testfiles,
}

if x.Dialect().URI().DBType == schemas.POSTGRES {
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
}

fixtures, err = testfixtures.New(loaderOptions...)
if err != nil {
return err
}

return err
}



+ 1
- 2
models/unit_tests.go View File

@@ -19,7 +19,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/unknwon/com"
"gopkg.in/testfixtures.v2"
"xorm.io/xorm"
"xorm.io/xorm/names"
)
@@ -101,7 +100,7 @@ func CreateTestEngine(fixturesDir string) error {
x.ShowSQL(true)
}

return InitFixtures(&testfixtures.SQLite{}, fixturesDir)
return InitFixtures(fixturesDir)
}

func removeAllWithRetry(dir string) error {


vendor/gopkg.in/testfixtures.v2/.editorconfig → vendor/github.com/go-testfixtures/testfixtures/v3/.editorconfig View File


vendor/gopkg.in/testfixtures.v2/.gitattributes → vendor/github.com/go-testfixtures/testfixtures/v3/.gitattributes View File


vendor/gopkg.in/testfixtures.v2/.gitignore → vendor/github.com/go-testfixtures/testfixtures/v3/.gitignore View File

@@ -23,7 +23,7 @@ _testmain.go
*.test
*.prof

# SQLite databases
*.sqlite3

.env
/testfixtures
/dist

+ 41
- 0
vendor/github.com/go-testfixtures/testfixtures/v3/.goreleaser.yml View File

@@ -0,0 +1,41 @@
build:
binary: testfixtures
main: ./cmd/testfixtures
goos:
- windows
- darwin
- linux
goarch:
- 386
- amd64
ignore:
- goos: darwin
goarch: 386
flags:
- -tags=sqlite

archives:
- name_template: "{{.Binary}}_{{.Os}}_{{.Arch}}"
format_overrides:
- goos: windows
format: zip

release:
draft: true

snapshot:
name_template: "{{.Tag}}"

checksum:
name_template: "testfixtures_checksums.txt"

nfpms:
- vendor: testfixtures
homepage: https://github.com/go-testfixtures/testfixtures
maintainer: Andrey Nering <[email protected]>
description: Ruby on Rails like test fixtures for Go.
license: MIT
formats:
- deb
- rpm
file_name_template: "{{.ProjectName}}_{{.Os}}_{{.Arch}}"

vendor/gopkg.in/testfixtures.v2/.sample.env → vendor/github.com/go-testfixtures/testfixtures/v3/.sample.env View File

@@ -2,4 +2,3 @@ PG_CONN_STRING="user=postgres dbname=testfixtures_test sslmode=disable"
MYSQL_CONN_STRING="root:@/testfixtures_test?multiStatements=true"
SQLITE_CONN_STRING="testdb.sqlite3"
SQLSERVER_CONN_STRING="server=localhost\SQLExpress;database=testfixtures_test;user id=sa;password=sqlserver;encrypt=disable"
ORACLE_CONN_STRING="testfixtures/[email protected]/XE"

+ 93
- 0
vendor/github.com/go-testfixtures/testfixtures/v3/CHANGELOG.md View File

@@ -0,0 +1,93 @@
# Changelog

## v3.2.0 - 2020-05-10

- Add support for loading multiple files and directories
([#65](https://github.com/go-testfixtures/testfixtures/pull/65)).

## v3.1.2 - 2020-04-26

- Dump: Fix column order in generated YAML files
([#62](https://github.com/go-testfixtures/testfixtures/pull/62)).

## v3.1.1 - 2020-01-11

- testfixtures now work with both `mssql` and `sqlserver` drivers.
Note that [the `mssql` one is deprecated](https://github.com/denisenkom/go-mssqldb#deprecated),
though. So try to migrate to `sqlserver` once possible.

## v3.1.0 - 2020-01-09

- Using `sqlserver` driver instead of the deprecated `mssql`
([#58](https://github.com/go-testfixtures/testfixtures/pull/58)).

## v3.0.0 - 2019-12-26

### Breaking changes

- The import path changed from `gopkg.in/testfixtures.v2` to
`github.com/go-testfixtures/testfixtures/v3`.
- This package no longer support Oracle databases. This decision was
taken because too few people actually used this package with Oracle and it
was the most difficult to test (we didn't run on CI due the lack of an
official Docker image, etc).
- The public API was totally rewritten to be more flexible and ideomatic.
It now uses functional options. It differs from v2, but should be easy
enough to upgrade.
- Some deprecated APIs from v2 were removed as well.
- This now requires Go >= 1.13.

### New features

- We now have a CLI so you can easily use testfixtures to load a sample
database from fixtures if you want.
- Templating via [text/template](https://golang.org/pkg/text/template/)
is now available. This allows some fancier use cases like generating data
or specific columns dynamically.
- It's now possible to choose which time zone to use when parsing timestamps
from fixtures. The default is the same as before, whatever is set on
`time.Local`.
- Errors now use the new `%w` verb only available on Go >= 1.13.

### MISC

- Travis and AppVeyor are gone. We're using GitHub Actions exclusively now.
The whole suite is ran inside Docker (with help of Docker Compose), so it's
easy to run tests locally as well.

Check the new README for some examples!

## v2.6.0 - 2019-10-24

- Add support for TimescaleDB
([#53](https://github.com/go-testfixtures/testfixtures/pull/53)).

## v2.5.3 - 2018-12-15

- Fixes related to use of foreign key pragmas on MySQL (#43).

## v2.5.2 - 2018-11-25

- This library now supports [Go Modules](https://github.com/golang/go/wiki/Modules);
- Also allow `.yaml` (as an alternative to `.yml`) as the file extension (#42).

## v2.5.1 - 2018-11-04

- Allowing disabling reset of PostgreSQL sequences (#38).

## v2.5.0 - 2018-09-07

- Add public function DetectTestDatabase (#35, #36).

## v2.4.5 - 2018-07-07

- Fix for MySQL/MariaDB: ignoring views on operations that should be run only on tables (#33).

## v2.4.4 - 2018-07-02

- Fix for multiple schemas on Microsoft SQL Server (#29 and #30);
- Configuring AppVeyor CI to also test for Microsoft SQL Server.

---

Sorry, we don't have changelog for older releases 😢.

+ 9
- 0
vendor/github.com/go-testfixtures/testfixtures/v3/Dockerfile View File

@@ -0,0 +1,9 @@
FROM golang:1.14-alpine

RUN apk update
RUN apk add alpine-sdk

WORKDIR /testfixtures
COPY . .

RUN go mod download

vendor/gopkg.in/testfixtures.v2/LICENSE → vendor/github.com/go-testfixtures/testfixtures/v3/LICENSE View File


+ 483
- 0
vendor/github.com/go-testfixtures/testfixtures/v3/README.md View File

@@ -0,0 +1,483 @@
# testfixtures

[![GoDoc](https://godoc.org/github.com/go-testfixtures/testfixtures?status.svg)][doc]

> ***Warning***: this package will wipe the database data before loading the
fixtures! It is supposed to be used on a test database. Please, double check
if you are running it against the correct database.

> **TIP**: There are options not described in this README page. It's
> recommended that you also check [the documentation][doc].

Writing tests is hard, even more when you have to deal with an SQL database.
This package aims to make writing functional tests for web apps written in
Go easier.

Basically this package mimics the ["Ruby on Rails' way"][railstests] of writing tests
for database applications, where sample data is kept in fixtures files. Before
the execution of every test, the test database is cleaned and the fixture data
is loaded into the database.

The idea is running tests against a real database, instead of relying in mocks,
which is boring to setup and may lead to production bugs not being caught in
the tests.

## Installation

First, import it like this:

```go
import (
"github.com/go-testfixtures/testfixtures/v3"
)
```

## Usage

Create a folder for the fixture files. Each file should contain data for a
single table and have the name `<table_name>.yml`:

```
myapp/
myapp.go
myapp_test.go
...
fixtures/
posts.yml
comments.yml
tags.yml
posts_tags.yml
...
```

The file would look like this (it can have as many record you want):

```yml
# comments.yml
- id: 1
post_id: 1
content: A comment...
author_name: John Doe
author_email: [email protected]
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59

- id: 2
post_id: 2
content: Another comment...
author_name: John Doe
author_email: [email protected]
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59

# ...
```

An YAML object or array will be converted to JSON. It will be stored on a native
JSON type like JSONB on PostgreSQL or as a TEXT or VARCHAR column on other
databases.

```yml
- id: 1
post_attributes:
author: John Due
author_email: [email protected]
title: "..."
tags:
- programming
- go
- testing
post: "..."
```

If you need to write raw SQL, probably to call a function, prefix the value
of the column with `RAW=`:

```yml
- id: 1
uuid_column: RAW=uuid_generate_v4()
postgis_type_column: RAW=ST_GeomFromText('params...')
created_at: RAW=NOW()
updated_at: RAW=NOW()
```

Your tests would look like this:

```go
package myapp

import (
"database/sql"

_ "github.com/lib/pq"
"github.com/go-testfixtures/testfixtures/v3"
)

var (
db *sql.DB
fixtures *testfixtures.Loader
)

func TestMain(m *testing.M) {
var err error

// Open connection to the test database.
// Do NOT import fixtures in a production database!
// Existing data would be deleted.
db, err = sql.Open("postgres", "dbname=myapp_test")
if err != nil {
...
}

fixtures, err := testfixtures.New(
testfixtures.Database(db), // You database connection
testfixtures.Dialect("postgres"), // Available: "postgresql", "timescaledb", "mysql", "mariadb", "sqlite" and "sqlserver"
testfixtures.Directory("testdata/fixtures"), // the directory containing the YAML files
)
if err != nil {
...
}

os.Exit(m.Run())
}

func prepareTestDatabase() {
if err := fixtures.Load(); err != nil {
...
}
}

func TestX(t *testing.T) {
prepareTestDatabase()

// Your test here ...
}

func TestY(t *testing.T) {
prepareTestDatabase()

// Your test here ...
}

func TestZ(t *testing.T) {
prepareTestDatabase()

// Your test here ...
}
```

Alternatively, you can use the `Files` option, to specify which
files you want to load into the database:

```go
fixtures, err := testfixtures.New(
testfixtures.Database(db),
testfixtures.Dialect("postgres"),
testfixtures.Files(
"fixtures/orders.yml",
"fixtures/customers.yml",
),
)
if err != nil {
...
}

fixtures, err := testfixtures.NewFiles(db, &testfixtures.PostgreSQL{},
"fixtures/orders.yml",
"fixtures/customers.yml",
// add as many files you want
)
if err != nil {
...
}
```

With `Paths` option, you can specify the paths that fixtures will load
from. Path can be directory or file. If directory, we will search YAML files
in it.

```go
fixtures, err := testfixtures.New(
testfixtures.Database(db),
testfixtures.Dialect("postgres"),
testfixtures.Paths(
"fixtures/orders.yml",
"fixtures/customers.yml",
"common_fixtures/users"
),
)
if err != nil {
...
}
```

## Security check

In order to prevent you from accidentally wiping the wrong database, this
package will refuse to load fixtures if the database name (or database
filename for SQLite) doesn't contains "test". If you want to disable this
check, use:

```go
testfixtures.New(
...
testfixtures.DangerousSkipTestDatabaseCheck(),
)
```

## Sequences

For PostgreSQL, this package also resets all sequences to a high
number to prevent duplicated primary keys while running the tests.
The default is 10000, but you can change that with:

```go
testfixtures.New(
...
testfixtures.ResetSequencesTo(10000),
)
```

Or, if you want to skip the reset of sequences entirely:

```go
testfixtures.New(
...
testfixtures.SkipResetSequences(),
)
```

## Compatible databases

### PostgreSQL / TimescaleDB

This package has two approaches to disable foreign keys while importing fixtures
for PostgreSQL databases:

#### With `DISABLE TRIGGER`

This is the default approach. For that use:

```go
testfixtures.New(
...
testfixtures.Dialect("postgres"), // or "timescaledb"
)
```

With the above snippet this package will use `DISABLE TRIGGER` to temporarily
disabling foreign key constraints while loading fixtures. This work with any
version of PostgreSQL, but it is **required** to be connected in the database
as a SUPERUSER. You can make a PostgreSQL user a SUPERUSER with:

```sql
ALTER USER your_user SUPERUSER;
```

#### With `ALTER CONSTRAINT`

This approach don't require to be connected as a SUPERUSER, but only work with
PostgreSQL versions >= 9.4. Try this if you are getting foreign key violation
errors with the previous approach. It is as simple as using:

```go
testfixtures.New(
...
testfixtures.Dialect("postgres"),
testfixtures.UseAlterConstraint(),
)
```

Tested using the [github.com/lib/pq](https://github.com/lib/pq) driver.

### MySQL / MariaDB

Just make sure the connection string have
[the multistatement parameter](https://github.com/go-sql-driver/mysql#multistatements)
set to true, and use:

```go
testfixtures.New(
...
testfixtures.Dialect("mysql"), // or "mariadb"
)
```

Tested using the [github.com/go-sql-driver/mysql](https://github.com/go-sql-driver/mysql) driver.

### SQLite

SQLite is also supported. It is recommended to create foreign keys as
`DEFERRABLE` (the default) to prevent problems. See more
[on the SQLite documentation](https://www.sqlite.org/foreignkeys.html#fk_deferred).
(Foreign key constraints are no-op by default on SQLite, but enabling it is
recommended).

```go
testfixtures.New(
...
testfixtures.Dialect("sqlite"),
)
```

Tested using the [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) driver.

### Microsoft SQL Server

SQL Server support requires SQL Server >= 2008. Inserting on `IDENTITY` columns
are handled as well. Just make sure you are logged in with a user with
`ALTER TABLE` permission.

```go
testfixtures.New(
...
testfixtures.Dialect("sqlserver"),
)
```

Tested using the `mssql` and `sqlserver` drivers from the
[github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb) lib.

## Templating

Testfixtures supports templating, but it's disabled by default. Most people
won't need it, but it may be useful to dynamically generate data.

Enable it by doing:

```go
testfixtures.New(
...
testfixtures.Template(),

// the above options are optional
TemplateFuncs(...),
TemplateDelims("{{", "}}"),
TemplateOptions("missingkey=zero"),
TemplateData(...),
)
```

The YAML file could look like this:

```yaml
# It's possible generate values...
- id: {{sha256 "my-awesome-post}}
title: My Awesome Post
text: {{randomText}}

# ... or records
{{range $post := $.Posts}}
- id: {{$post.Id}}
title: {{$post.Title}}
text: {{$post.Text}}
{{end}}
```

## Generating fixtures for a existing database

The following code will generate a YAML file for each table of the database
into a given folder. It may be useful to boostrap a test scenario from a sample
database of your app.

```go
dumper, err := testfixtures.NewDumper(
testfixtures.DumpDatabase(db),
testfixtures.DumpDialect("postgres"), // or your database of choice
testfixtures.DumpDirectory("tmp/fixtures"),
textfixtures.DumpTables( // optional, will dump all table if not given
"posts",
"comments",
"tags",
)
)
if err != nil {
...
}
if err := dumper.Dump(); err != nil {
...
}
```

> This was intended to run in small sample databases. It will likely break
if run in a production/big database.

## Gotchas

### Parallel testing

This library doesn't yet support running tests in parallel! Running tests
in parallel can result in random data being present in the database, which
will likely cause tests to randomly/intermittently fail.

This is specially tricky since it's not immediately clear that `go test ./...`
run tests for each package in parallel. If more than one package use this
library, you can face this issue. Please, use `go test -p 1 ./...` or run tests
for each package in separated commands to fix this issue.

If you're looking into being able to run tests in parallel you can try using
testfixtures together with the [txdb][gotxdb] package, which allows wrapping
each test run in a transaction.

## CLI

We also have a CLI to load fixtures in a given database.
Grab it from the [releases page](https://github.com/go-testfixtures/testfixtures/releases)
and use it like:

```bash
testfixtures -d postgres -c "postgres://user:[email protected]/database" -D testdata/fixtures
```

The connection string changes for each database driver.

Use `--help` for all flags.

## Contributing

We recommend you to [install Task](https://taskfile.dev/#/installation) and
Docker before contributing to this package, since some stuff is automated
using these tools.

It's recommended to use Docker Compose to run tests, since it runs tests for
all supported databases once. To do that you just need to run:

```bash
task docker
```

But if you want to run tests locally, copy the `.sample.env` file as `.env`
and edit it according to your database setup. You'll need to create a database
(likely names `testfixtures_test`) before continuing. Then run the command
for the database you want to run tests against:

```bash
task test:pg # PostgreSQL
task test:mysql # MySQL
task test:sqlite # SQLite
task test:sqlserver # Microsoft SQL Server
```

GitHub Actions (CI) runs the same Docker setup available locally.

## Alternatives

If you don't think using fixtures is a good idea, you can try one of these
packages instead:

- [factory-go][factorygo]: Factory for Go. Inspired by Python's Factory Boy
and Ruby's Factory Girl
- [go-txdb (Single transaction SQL driver for Go)][gotxdb]: Use a single
database transaction for each functional test, so you can rollback to
previous state between tests to have the same database state in all tests
- [go-sqlmock][gosqlmock]: A mock for the sql.DB interface. This allow you to
unit test database code without having to connect to a real database
- [dbcleaner][dbcleaner] - Clean database for testing, inspired by
database_cleaner for Ruby

[doc]: https://pkg.go.dev/github.com/go-testfixtures/testfixtures/v3?tab=doc
[railstests]: http://guides.rubyonrails.org/testing.html#the-test-database
[gotxdb]: https://github.com/DATA-DOG/go-txdb
[gosqlmock]: https://github.com/DATA-DOG/go-sqlmock
[factorygo]: https://github.com/bluele/factory-go
[dbcleaner]: https://github.com/khaiql/dbcleaner

+ 59
- 0
vendor/github.com/go-testfixtures/testfixtures/v3/Taskfile.yml View File

@@ -0,0 +1,59 @@
# https://taskfile.org

version: '2'

tasks:
build:
cmds:
- go build -v -tags sqlite -o ./testfixtures{{exeExt}} ./cmd/testfixtures

test-cli:
cmds:
- ./testfixtures -d sqlite -c testdb.sqlite3 -D testdata/fixtures

test:pg:
desc: Test PostgreSQL
cmds:
- task: test-db
vars: {DATABASE: postgresql}

test:mysql:
desc: Test MySQL
cmds:
- task: test:db
vars: {DATABASE: mysql}

test:sqlite:
desc: Test SQLite
cmds:
- task: test-db
vars: {DATABASE: sqlite}

test:sqlserver:
desc: Test SQLServer
cmds:
- task: test-db
vars: {DATABASE: sqlserver}

test-db:
cmds:
- go test -v -tags {{.DATABASE}}

goreleaser:test:
desc: Tests release process without publishing
cmds:
- goreleaser --snapshot --rm-dist

docker:
cmds:
- task: docker:build
- task: docker:test

docker:build:
cmds:
- docker build -t testfixtures .

docker:test:
cmds:
- docker-compose down -v
- docker-compose run testfixtures go test -v -tags 'postgresql sqlite mysql sqlserver'

+ 37
- 0
vendor/github.com/go-testfixtures/testfixtures/v3/docker-compose.yml View File

@@ -0,0 +1,37 @@
version: '3'

services:
testfixtures:
image: testfixtures
depends_on:
- postgresql
- mysql
- sqlserver
environment:
PGPASSWORD: postgres
PG_CONN_STRING: host=postgresql user=postgres dbname=testfixtures_test port=5432 sslmode=disable

MYSQL_CONN_STRING: root:[email protected](mysql)/testfixtures_test?multiStatements=true

SQLITE_CONN_STRING: testfixtures_test.sqlite3

SQLSERVER_CONN_STRING: server=sqlserver;database=master;user id=sa;[email protected];encrypt=disable

postgresql:
image: postgres:12.1-alpine
environment:
POSTGRES_DB: testfixtures_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres

mysql:
image: mysql:8.0
environment:
MYSQL_DATABASE: testfixtures_test
MYSQL_ROOT_PASSWORD: mysql

sqlserver:
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
ACCEPT_EULA: 'Y'
SA_PASSWORD: [email protected]

+ 165
- 0
vendor/github.com/go-testfixtures/testfixtures/v3/dump.go View File

@@ -0,0 +1,165 @@
package testfixtures

import (
"database/sql"
"fmt"
"os"
"path/filepath"
"unicode/utf8"

"gopkg.in/yaml.v2"
)

// Dumper is resposible for dumping fixtures from the database into a
// directory.
type Dumper struct {
db *sql.DB
helper helper
dir string

tables []string
}

// NewDumper creates a new dumper with the given options.
//
// The "DumpDatabase", "DumpDialect" and "DumpDirectory" options are required.
func NewDumper(options ...func(*Dumper) error) (*Dumper, error) {
d := &Dumper{}

for _, option := range options {
if err := option(d); err != nil {
return nil, err
}
}

return d, nil
}

// DumpDatabase sets the database to be dumped.
func DumpDatabase(db *sql.DB) func(*Dumper) error {
return func(d *Dumper) error {
d.db = db
return nil
}
}

// DumpDialect informs Loader about which database dialect you're using.
//
// Possible options are "postgresql", "timescaledb", "mysql", "mariadb",
// "sqlite" and "sqlserver".
func DumpDialect(dialect string) func(*Dumper) error {
return func(d *Dumper) error {
h, err := helperForDialect(dialect)
if err != nil {
return err
}
d.helper = h
return nil
}
}

// DumpDirectory sets the directory where the fixtures files will be created.
func DumpDirectory(dir string) func(*Dumper) error {
return func(d *Dumper) error {
d.dir = dir
return nil
}
}

// DumpTables allows you to choose which tables you want to dump.
//
// If not informed, Dumper will dump all tables by default.
func DumpTables(tables ...string) func(*Dumper) error {
return func(d *Dumper) error {
d.tables = tables
return nil
}
}

// Dump dumps the databases as YAML fixtures.
func (d *Dumper) Dump() error {
tables := d.tables
if len(tables) == 0 {
var err error
tables, err = d.helper.tableNames(d.db)
if err != nil {
return err
}
}

for _, table := range tables {
if err := d.dumpTable(table); err != nil {
return err
}
}
return nil
}

func (d *Dumper) dumpTable(table string) error {
query := fmt.Sprintf("SELECT * FROM %s", d.helper.quoteKeyword(table))

stmt, err := d.db.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()

rows, err := stmt.Query()
if err != nil {
return err
}
defer rows.Close()

columns, err := rows.Columns()
if err != nil {
return err
}

fixtures := make([]yaml.MapSlice, 0, 10)
for rows.Next() {
entries := make([]interface{}, len(columns))
entryPtrs := make([]interface{}, len(entries))
for i := range entries {
entryPtrs[i] = &entries[i]
}
if err := rows.Scan(entryPtrs...); err != nil {
return err
}

entryMap := make([]yaml.MapItem, len(entries))
for i, column := range columns {
entryMap[i] = yaml.MapItem{
Key: column,
Value: convertValue(entries[i]),
}
}
fixtures = append(fixtures, entryMap)
}
if err = rows.Err(); err != nil {
return err
}

filePath := filepath.Join(d.dir, table+".yml")
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()

data, err := yaml.Marshal(fixtures)
if err != nil {
return err
}
_, err = f.Write(data)
return err
}

func convertValue(value interface{}) interface{} {
switch v := value.(type) {
case []byte:
if utf8.Valid(v) {
return string(v)
}
}
return value
}

+ 14
- 0
vendor/github.com/go-testfixtures/testfixtures/v3/go.mod View File

@@ -0,0 +1,14 @@
module github.com/go-testfixtures/testfixtures/v3

require (
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73
github.com/go-sql-driver/mysql v1.4.1
github.com/joho/godotenv v1.3.0
github.com/lib/pq v1.3.0
github.com/mattn/go-sqlite3 v2.0.2+incompatible
github.com/spf13/pflag v1.0.5
google.golang.org/appengine v1.3.0 // indirect
gopkg.in/yaml.v2 v2.2.7
)

go 1.13

+ 26
- 0
vendor/github.com/go-testfixtures/testfixtures/v3/go.sum View File

@@ -0,0 +1,26 @@
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73 h1:OGNva6WhsKst5OZf7eZOklDztV3hwtTHovdrLHV+MsA=
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v2.0.2+incompatible h1:qzw9c2GNT8UFrgWNDhCTqRqYUSmu/Dav/9Z58LGpk7U=
github.com/mattn/go-sqlite3 v2.0.2+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c h1:Vj5n4GlwjmQteupaxJ9+0FNOmBrHfq7vN4btdGoDZgI=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.3.0 h1:FBSsiFRMz3LBeXIomRnVzrQwSDj4ibvcRexLG0LZGQk=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

vendor/gopkg.in/testfixtures.v2/helper.go → vendor/github.com/go-testfixtures/testfixtures/v3/helper.go View File

@@ -8,13 +8,12 @@ import (
const (
paramTypeDollar = iota + 1
paramTypeQuestion
paramTypeColon
paramTypeAtSign
)

type loadFunction func(tx *sql.Tx) error

// Helper is the generic interface for the database helper
type Helper interface {
type helper interface {
init(*sql.DB) error
disableReferentialIntegrity(*sql.DB, loadFunction) error
paramType() int
@@ -43,11 +42,10 @@ type batchSplitter interface {
}

var (
_ Helper = &MySQL{}
_ Helper = &PostgreSQL{}
_ Helper = &SQLite{}
_ Helper = &Oracle{}
_ Helper = &SQLServer{}
_ helper = &mySQL{}
_ helper = &postgreSQL{}
_ helper = &sqlite{}
_ helper = &sqlserver{}
)

type baseHelper struct{}

vendor/gopkg.in/testfixtures.v2/json.go → vendor/github.com/go-testfixtures/testfixtures/v3/json.go View File


vendor/gopkg.in/testfixtures.v2/mysql.go → vendor/github.com/go-testfixtures/testfixtures/v3/mysql.go View File

@@ -5,14 +5,13 @@ import (
"fmt"
)

// MySQL is the MySQL helper for this package
type MySQL struct {
type mySQL struct {
baseHelper
tables []string
tablesChecksum map[string]int64
}

func (h *MySQL) init(db *sql.DB) error {
func (h *mySQL) init(db *sql.DB) error {
var err error
h.tables, err = h.tableNames(db)
if err != nil {
@@ -22,21 +21,21 @@ func (h *MySQL) init(db *sql.DB) error {
return nil
}

func (*MySQL) paramType() int {
func (*mySQL) paramType() int {
return paramTypeQuestion
}

func (*MySQL) quoteKeyword(str string) string {
func (*mySQL) quoteKeyword(str string) string {
return fmt.Sprintf("`%s`", str)
}

func (*MySQL) databaseName(q queryable) (string, error) {
func (*mySQL) databaseName(q queryable) (string, error) {
var dbName string
err := q.QueryRow("SELECT DATABASE()").Scan(&dbName)
return dbName, err
}

func (h *MySQL) tableNames(q queryable) ([]string, error) {
func (h *mySQL) tableNames(q queryable) ([]string, error) {
query := `
SELECT table_name
FROM information_schema.tables
@@ -69,14 +68,7 @@ func (h *MySQL) tableNames(q queryable) ([]string, error) {

}

func (h *MySQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
// re-enable after load
defer func() {
if _, err2 := db.Exec("SET FOREIGN_KEY_CHECKS = 1"); err2 != nil && err == nil {
err = err2
}
}()

func (h *mySQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
tx, err := db.Begin()
if err != nil {
return err
@@ -87,14 +79,19 @@ func (h *MySQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (er
return err
}

if err = loadFn(tx); err != nil {
err = loadFn(tx)
_, err2 := tx.Exec("SET FOREIGN_KEY_CHECKS = 1")
if err != nil {
return err
}
if err2 != nil {
return err2
}

return tx.Commit()
}

func (h *MySQL) isTableModified(q queryable, tableName string) (bool, error) {
func (h *mySQL) isTableModified(q queryable, tableName string) (bool, error) {
checksum, err := h.getChecksum(q, tableName)
if err != nil {
return true, err
@@ -105,7 +102,7 @@ func (h *MySQL) isTableModified(q queryable, tableName string) (bool, error) {
return oldChecksum == 0 || checksum != oldChecksum, nil
}

func (h *MySQL) afterLoad(q queryable) error {
func (h *mySQL) afterLoad(q queryable) error {
if h.tablesChecksum != nil {
return nil
}
@@ -121,7 +118,7 @@ func (h *MySQL) afterLoad(q queryable) error {
return nil
}

func (h *MySQL) getChecksum(q queryable, tableName string) (int64, error) {
func (h *mySQL) getChecksum(q queryable, tableName string) (int64, error) {
sql := fmt.Sprintf("CHECKSUM TABLE %s", h.quoteKeyword(tableName))
var (
table string

vendor/gopkg.in/testfixtures.v2/postgresql.go → vendor/github.com/go-testfixtures/testfixtures/v3/postgresql.go View File

@@ -6,15 +6,12 @@ import (
"strings"
)

// PostgreSQL is the PG helper for this package
type PostgreSQL struct {
type postgreSQL struct {
baseHelper

// UseAlterConstraint If true, the contraint disabling will do
// using ALTER CONTRAINT sintax, only allowed in PG >= 9.4.
// If false, the constraint disabling will use DISABLE TRIGGER ALL,
// which requires SUPERUSER privileges.
UseAlterConstraint bool
useAlterConstraint bool
skipResetSequences bool
resetSequencesTo int64

tables []string
sequences []string
@@ -27,7 +24,7 @@ type pgConstraint struct {
constraintName string
}

func (h *PostgreSQL) init(db *sql.DB) error {
func (h *postgreSQL) init(db *sql.DB) error {
var err error

h.tables, err = h.tableNames(db)
@@ -48,17 +45,17 @@ func (h *PostgreSQL) init(db *sql.DB) error {
return nil
}

func (*PostgreSQL) paramType() int {
func (*postgreSQL) paramType() int {
return paramTypeDollar
}

func (*PostgreSQL) databaseName(q queryable) (string, error) {
func (*postgreSQL) databaseName(q queryable) (string, error) {
var dbName string
err := q.QueryRow("SELECT current_database()").Scan(&dbName)
return dbName, err
}

func (h *PostgreSQL) tableNames(q queryable) ([]string, error) {
func (h *postgreSQL) tableNames(q queryable) ([]string, error) {
var tables []string

sql := `
@@ -67,7 +64,8 @@ func (h *PostgreSQL) tableNames(q queryable) ([]string, error) {
INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE pg_class.relkind = 'r'
AND pg_namespace.nspname NOT IN ('pg_catalog', 'information_schema')
AND pg_namespace.nspname NOT LIKE 'pg_toast%';
AND pg_namespace.nspname NOT LIKE 'pg_toast%'
AND pg_namespace.nspname NOT LIKE '\_timescaledb%';
`
rows, err := q.Query(sql)
if err != nil {
@@ -88,12 +86,13 @@ func (h *PostgreSQL) tableNames(q queryable) ([]string, error) {
return tables, nil
}

func (h *PostgreSQL) getSequences(q queryable) ([]string, error) {
func (h *postgreSQL) getSequences(q queryable) ([]string, error) {
const sql = `
SELECT pg_namespace.nspname || '.' || pg_class.relname AS sequence_name
FROM pg_class
INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE pg_class.relkind = 'S'
AND pg_namespace.nspname NOT LIKE '\_timescaledb%'
`

rows, err := q.Query(sql)
@@ -116,7 +115,7 @@ func (h *PostgreSQL) getSequences(q queryable) ([]string, error) {
return sequences, nil
}

func (*PostgreSQL) getNonDeferrableConstraints(q queryable) ([]pgConstraint, error) {
func (*postgreSQL) getNonDeferrableConstraints(q queryable) ([]pgConstraint, error) {
var constraints []pgConstraint

sql := `
@@ -124,6 +123,7 @@ func (*PostgreSQL) getNonDeferrableConstraints(q queryable) ([]pgConstraint, err
FROM information_schema.table_constraints
WHERE constraint_type = 'FOREIGN KEY'
AND is_deferrable = 'NO'
AND table_schema NOT LIKE '\_timescaledb%'
`
rows, err := q.Query(sql)
if err != nil {
@@ -144,7 +144,7 @@ func (*PostgreSQL) getNonDeferrableConstraints(q queryable) ([]pgConstraint, err
return constraints, nil
}

func (h *PostgreSQL) disableTriggers(db *sql.DB, loadFn loadFunction) (err error) {
func (h *postgreSQL) disableTriggers(db *sql.DB, loadFn loadFunction) (err error) {
defer func() {
// re-enable triggers after load
var sql string
@@ -177,7 +177,7 @@ func (h *PostgreSQL) disableTriggers(db *sql.DB, loadFn loadFunction) (err error
return tx.Commit()
}

func (h *PostgreSQL) makeConstraintsDeferrable(db *sql.DB, loadFn loadFunction) (err error) {
func (h *postgreSQL) makeConstraintsDeferrable(db *sql.DB, loadFn loadFunction) (err error) {
defer func() {
// ensure constraint being not deferrable again after load
var sql string
@@ -214,21 +214,28 @@ func (h *PostgreSQL) makeConstraintsDeferrable(db *sql.DB, loadFn loadFunction)
return tx.Commit()
}

func (h *PostgreSQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
func (h *postgreSQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
// ensure sequences being reset after load
defer func() {
if err2 := h.resetSequences(db); err2 != nil && err == nil {
err = err2
}
}()
if !h.skipResetSequences {
defer func() {
if err2 := h.resetSequences(db); err2 != nil && err == nil {
err = err2
}
}()
}

if h.UseAlterConstraint {
if h.useAlterConstraint {
return h.makeConstraintsDeferrable(db, loadFn)
}
return h.disableTriggers(db, loadFn)
}

func (h *PostgreSQL) resetSequences(db *sql.DB) error {
func (h *postgreSQL) resetSequences(db *sql.DB) error {
resetSequencesTo := h.resetSequencesTo
if resetSequencesTo == 0 {
resetSequencesTo = 10000
}

for _, sequence := range h.sequences {
_, err := db.Exec(fmt.Sprintf("SELECT SETVAL('%s', %d)", sequence, resetSequencesTo))
if err != nil {
@@ -238,7 +245,7 @@ func (h *PostgreSQL) resetSequences(db *sql.DB) error {
return nil
}

func (h *PostgreSQL) isTableModified(q queryable, tableName string) (bool, error) {
func (h *postgreSQL) isTableModified(q queryable, tableName string) (bool, error) {
checksum, err := h.getChecksum(q, tableName)
if err != nil {
return false, err
@@ -249,7 +256,7 @@ func (h *PostgreSQL) isTableModified(q queryable, tableName string) (bool, error
return oldChecksum == "" || checksum != oldChecksum, nil
}

func (h *PostgreSQL) afterLoad(q queryable) error {
func (h *postgreSQL) afterLoad(q queryable) error {
if h.tablesChecksum != nil {
return nil
}
@@ -265,7 +272,7 @@ func (h *PostgreSQL) afterLoad(q queryable) error {
return nil
}

func (h *PostgreSQL) getChecksum(q queryable, tableName string) (string, error) {
func (h *postgreSQL) getChecksum(q queryable, tableName string) (string, error) {
sqlStr := fmt.Sprintf(`
SELECT md5(CAST((array_agg(t.*)) AS TEXT))
FROM %s AS t
@@ -280,7 +287,7 @@ func (h *PostgreSQL) getChecksum(q queryable, tableName string) (string, error)
return checksum.String, nil
}

func (*PostgreSQL) quoteKeyword(s string) string {
func (*postgreSQL) quoteKeyword(s string) string {
parts := strings.Split(s, ".")
for i, p := range parts {
parts[i] = fmt.Sprintf(`"%s"`, p)

vendor/gopkg.in/testfixtures.v2/sqlite.go → vendor/github.com/go-testfixtures/testfixtures/v3/sqlite.go View File

@@ -5,16 +5,15 @@ import (
"path/filepath"
)

// SQLite is the SQLite Helper for this package
type SQLite struct {
type sqlite struct {
baseHelper
}

func (*SQLite) paramType() int {
func (*sqlite) paramType() int {
return paramTypeQuestion
}

func (*SQLite) databaseName(q queryable) (string, error) {
func (*sqlite) databaseName(q queryable) (string, error) {
var seq int
var main, dbName string
err := q.QueryRow("PRAGMA database_list").Scan(&seq, &main, &dbName)
@@ -25,7 +24,7 @@ func (*SQLite) databaseName(q queryable) (string, error) {
return dbName, nil
}

func (*SQLite) tableNames(q queryable) ([]string, error) {
func (*sqlite) tableNames(q queryable) ([]string, error) {
query := `
SELECT name
FROM sqlite_master
@@ -51,7 +50,7 @@ func (*SQLite) tableNames(q queryable) ([]string, error) {
return tables, nil
}

func (*SQLite) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
func (*sqlite) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
defer func() {
if _, err2 := db.Exec("PRAGMA defer_foreign_keys = OFF"); err2 != nil && err == nil {
err = err2

vendor/gopkg.in/testfixtures.v2/sqlserver.go → vendor/github.com/go-testfixtures/testfixtures/v3/sqlserver.go View File

@@ -6,17 +6,29 @@ import (
"strings"
)

// SQLServer is the helper for SQL Server for this package.
// SQL Server >= 2008 is required.
type SQLServer struct {
type sqlserver struct {
baseHelper

tables []string
paramTypeCache int
tables []string
}

func (h *SQLServer) init(db *sql.DB) error {
func (h *sqlserver) init(db *sql.DB) error {
var err error

// NOTE(@andreynering): The SQL Server lib (github.com/denisenkom/go-mssqldb)
// supports both the "?" style (when using the deprecated "mssql" driver)
// and the "@p1" style (when using the new "sqlserver" driver).
//
// Since we don't have a way to know which driver it's been used,
// this is a small hack to detect the allowed param style.
var v int
if err := db.QueryRow("SELECT ?", 1).Scan(&v); err == nil && v == 1 {
h.paramTypeCache = paramTypeQuestion
} else {
h.paramTypeCache = paramTypeAtSign
}

h.tables, err = h.tableNames(db)
if err != nil {
return err
@@ -25,11 +37,11 @@ func (h *SQLServer) init(db *sql.DB) error {
return nil
}

func (*SQLServer) paramType() int {
return paramTypeQuestion
func (h *sqlserver) paramType() int {
return h.paramTypeCache
}

func (*SQLServer) quoteKeyword(s string) string {
func (*sqlserver) quoteKeyword(s string) string {
parts := strings.Split(s, ".")
for i, p := range parts {
parts[i] = fmt.Sprintf(`[%s]`, p)
@@ -37,14 +49,14 @@ func (*SQLServer) quoteKeyword(s string) string {
return strings.Join(parts, ".")
}

func (*SQLServer) databaseName(q queryable) (string, error) {
func (*sqlserver) databaseName(q queryable) (string, error) {
var dbName string
err := q.QueryRow("SELECT DB_NAME()").Scan(&dbName)
return dbName, err
}

func (*SQLServer) tableNames(q queryable) ([]string, error) {
rows, err := q.Query("SELECT table_schema + '.' + table_name FROM information_schema.tables")
func (*sqlserver) tableNames(q queryable) ([]string, error) {
rows, err := q.Query("SELECT table_schema + '.' + table_name FROM information_schema.tables WHERE table_name <> 'spt_values'")
if err != nil {
return nil, err
}
@@ -64,36 +76,42 @@ func (*SQLServer) tableNames(q queryable) ([]string, error) {
return tables, nil
}

func (h *SQLServer) tableHasIdentityColumn(q queryable, tableName string) bool {
sql := `
func (h *sqlserver) tableHasIdentityColumn(q queryable, tableName string) (bool, error) {
sql := fmt.Sprintf(`
SELECT COUNT(*)
FROM SYS.IDENTITY_COLUMNS
WHERE OBJECT_ID = OBJECT_ID(?)
`
WHERE OBJECT_ID = OBJECT_ID('%s')
`, tableName)
var count int
q.QueryRow(sql, h.quoteKeyword(tableName)).Scan(&count)
return count > 0
if err := q.QueryRow(sql).Scan(&count); err != nil {
return false, err
}
return count > 0, nil

}

func (h *SQLServer) whileInsertOnTable(tx *sql.Tx, tableName string, fn func() error) (err error) {
if h.tableHasIdentityColumn(tx, tableName) {
func (h *sqlserver) whileInsertOnTable(tx *sql.Tx, tableName string, fn func() error) (err error) {
hasIdentityColumn, err := h.tableHasIdentityColumn(tx, tableName)
if err != nil {
return err
}
if hasIdentityColumn {
defer func() {
_, err2 := tx.Exec(fmt.Sprintf("SET IDENTITY_INSERT %s OFF", h.quoteKeyword(tableName)))
if err2 != nil && err == nil {
err = err2
err = fmt.Errorf("testfixtures: could not disable identity insert: %w", err2)
}
}()

_, err := tx.Exec(fmt.Sprintf("SET IDENTITY_INSERT %s ON", h.quoteKeyword(tableName)))
if err != nil {
return err
return fmt.Errorf("testfixtures: could not enable identity insert: %w", err)
}
}
return fn()
}

func (h *SQLServer) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
func (h *sqlserver) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
// ensure the triggers are re-enable after all
defer func() {
var sql string
@@ -130,6 +148,6 @@ func (h *SQLServer) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction)
// SQL Server because commands like a `CREATE SCHEMA...` and a `CREATE TABLE...`
// could not be executed in the same batch.
// See https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms175502(v=sql.105)#rules-for-using-batches
func (*SQLServer) splitter() []byte {
func (*sqlserver) splitter() []byte {
return []byte("GO\n")
}

+ 599
- 0
vendor/github.com/go-testfixtures/testfixtures/v3/testfixtures.go View File

@@ -0,0 +1,599 @@
package testfixtures // import "github.com/go-testfixtures/testfixtures/v3"

import (
"bytes"
"database/sql"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"
"time"

"gopkg.in/yaml.v2"
)

// Loader is the responsible to loading fixtures.
type Loader struct {
db *sql.DB
helper helper
fixturesFiles []*fixtureFile

skipTestDatabaseCheck bool
location *time.Location

template bool
templateFuncs template.FuncMap
templateLeftDelim string
templateRightDelim string
templateOptions []string
templateData interface{}
}

type fixtureFile struct {
path string
fileName string
content []byte
insertSQLs []insertSQL
}

type insertSQL struct {
sql string
params []interface{}
}

var (
testDatabaseRegexp = regexp.MustCompile("(?i)test")

errDatabaseIsRequired = fmt.Errorf("testfixtures: database is required")
errDialectIsRequired = fmt.Errorf("testfixtures: dialect is required")
)

// New instantiates a new Loader instance. The "Database" and "Driver"
// options are required.
func New(options ...func(*Loader) error) (*Loader, error) {
l := &Loader{
templateLeftDelim: "{{",
templateRightDelim: "}}",
templateOptions: []string{"missingkey=zero"},
}

for _, option := range options {
if err := option(l); err != nil {
return nil, err
}
}

if l.db == nil {
return nil, errDatabaseIsRequired
}
if l.helper == nil {
return nil, errDialectIsRequired
}

if err := l.helper.init(l.db); err != nil {
return nil, err
}
if err := l.buildInsertSQLs(); err != nil {
return nil, err
}

return l, nil
}

// Database sets an existing sql.DB instant to Loader.
func Database(db *sql.DB) func(*Loader) error {
return func(l *Loader) error {
l.db = db
return nil
}
}

// Dialect informs Loader about which database dialect you're using.
//
// Possible options are "postgresql", "timescaledb", "mysql", "mariadb",
// "sqlite" and "sqlserver".
func Dialect(dialect string) func(*Loader) error {
return func(l *Loader) error {
h, err := helperForDialect(dialect)
if err != nil {
return err
}
l.helper = h
return nil
}
}

func helperForDialect(dialect string) (helper, error) {
switch dialect {
case "postgres", "postgresql", "timescaledb":
return &postgreSQL{}, nil
case "mysql", "mariadb":
return &mySQL{}, nil
case "sqlite", "sqlite3":
return &sqlite{}, nil
case "mssql", "sqlserver":
return &sqlserver{}, nil
default:
return nil, fmt.Errorf(`testfixtures: unrecognized dialect "%s"`, dialect)
}
}

// UseAlterConstraint If true, the contraint disabling will do
// using ALTER CONTRAINT sintax, only allowed in PG >= 9.4.
// If false, the constraint disabling will use DISABLE TRIGGER ALL,
// which requires SUPERUSER privileges.
//
// Only valid for PostgreSQL. Returns an error otherwise.
func UseAlterConstraint() func(*Loader) error {
return func(l *Loader) error {
pgHelper, ok := l.helper.(*postgreSQL)
if !ok {
return fmt.Errorf("testfixtures: UseAlterConstraint is only valid for PostgreSQL databases")
}
pgHelper.useAlterConstraint = true
return nil
}
}

// SkipResetSequences prevents Loader from reseting sequences after loading
// fixtures.
//
// Only valid for PostgreSQL. Returns an error otherwise.
func SkipResetSequences() func(*Loader) error {
return func(l *Loader) error {
pgHelper, ok := l.helper.(*postgreSQL)
if !ok {
return fmt.Errorf("testfixtures: SkipResetSequences is only valid for PostgreSQL databases")
}
pgHelper.skipResetSequences = true
return nil
}
}

// ResetSequencesTo sets the value the sequences will be reset to.
//
// Defaults to 10000.
//
// Only valid for PostgreSQL. Returns an error otherwise.
func ResetSequencesTo(value int64) func(*Loader) error {
return func(l *Loader) error {
pgHelper, ok := l.helper.(*postgreSQL)
if !ok {
return fmt.Errorf("testfixtures: ResetSequencesTo is only valid for PostgreSQL databases")
}
pgHelper.resetSequencesTo = value
return nil
}
}

// DangerousSkipTestDatabaseCheck will make Loader not check if the database
// name contains "test". Use with caution!
func DangerousSkipTestDatabaseCheck() func(*Loader) error {
return func(l *Loader) error {
l.skipTestDatabaseCheck = true
return nil
}
}

// Directory informs Loader to load YAML files from a given directory.
func Directory(dir string) func(*Loader) error {
return func(l *Loader) error {
fixtures, err := l.fixturesFromDir(dir)
if err != nil {
return err
}
l.fixturesFiles = append(l.fixturesFiles, fixtures...)
return nil
}
}

// Files informs Loader to load a given set of YAML files.
func Files(files ...string) func(*Loader) error {
return func(l *Loader) error {
fixtures, err := l.fixturesFromFiles(files...)
if err != nil {
return err
}
l.fixturesFiles = append(l.fixturesFiles, fixtures...)
return nil
}
}

// Paths inform Loader to load a given set of YAML files and directories.
func Paths(paths ...string) func(*Loader) error {
return func(l *Loader) error {
fixtures, err := l.fixturesFromPaths(paths...)
if err != nil {
return err
}
l.fixturesFiles = append(l.fixturesFiles, fixtures...)
return nil
}
}

// Location makes Loader use the given location by default when parsing
// dates. If not given, by default it uses the value of time.Local.
func Location(location *time.Location) func(*Loader) error {
return func(l *Loader) error {
l.location = location
return nil
}
}

// Template makes loader process each YAML file as an template using the
// text/template package.
//
// For more information on how templates work in Go please read:
// https://golang.org/pkg/text/template/
//
// If not given the YAML files are parsed as is.
func Template() func(*Loader) error {
return func(l *Loader) error {
l.template = true
return nil
}
}

// TemplateFuncs allow choosing which functions will be available
// when processing templates.
//
// For more information see: https://golang.org/pkg/text/template/#Template.Funcs
func TemplateFuncs(funcs template.FuncMap) func(*Loader) error {
return func(l *Loader) error {
if !l.template {
return fmt.Errorf(`testfixtures: the Template() options is required in order to use the TemplateFuns() option`)
}

l.templateFuncs = funcs
return nil
}
}

// TemplateDelims allow choosing which delimiters will be used for templating.
// This defaults to "{{" and "}}".
//
// For more information see https://golang.org/pkg/text/template/#Template.Delims
func TemplateDelims(left, right string) func(*Loader) error {
return func(l *Loader) error {
if !l.template {
return fmt.Errorf(`testfixtures: the Template() options is required in order to use the TemplateDelims() option`)
}

l.templateLeftDelim = left
l.templateRightDelim = right
return nil
}
}

// TemplateOptions allows you to specific which text/template options will
// be enabled when processing templates.
//
// This defaults to "missingkey=zero". Check the available options here:
// https://golang.org/pkg/text/template/#Template.Option
func TemplateOptions(options ...string) func(*Loader) error {
return func(l *Loader) error {
if !l.template {
return fmt.Errorf(`testfixtures: the Template() options is required in order to use the TemplateOptions() option`)
}

l.templateOptions = options
return nil
}
}

// TemplateData allows you to specify which data will be available
// when processing templates. Data is accesible by prefixing it with a "."
// like {{.MyKey}}.
func TemplateData(data interface{}) func(*Loader) error {
return func(l *Loader) error {
if !l.template {
return fmt.Errorf(`testfixtures: the Template() options is required in order to use the TemplateData() option`)
}

l.templateData = data
return nil
}
}

// EnsureTestDatabase returns an error if the database name does not contains
// "test".
func (l *Loader) EnsureTestDatabase() error {
dbName, err := l.helper.databaseName(l.db)
if err != nil {
return err
}
if !testDatabaseRegexp.MatchString(dbName) {
return fmt.Errorf(`testfixtures: database "%s" does not appear to be a test database`, dbName)
}
return nil
}

// Load wipes and after load all fixtures in the database.
// if err := fixtures.Load(); err != nil {
// ...
// }
func (l *Loader) Load() error {
if !l.skipTestDatabaseCheck {
if err := l.EnsureTestDatabase(); err != nil {
return err
}
}

err := l.helper.disableReferentialIntegrity(l.db, func(tx *sql.Tx) error {
for _, file := range l.fixturesFiles {
modified, err := l.helper.isTableModified(tx, file.fileNameWithoutExtension())
if err != nil {
return err
}
if !modified {
continue
}
if err := file.delete(tx, l.helper); err != nil {
return err
}

err = l.helper.whileInsertOnTable(tx, file.fileNameWithoutExtension(), func() error {
for j, i := range file.insertSQLs {
if _, err := tx.Exec(i.sql, i.params...); err != nil {
return &InsertError{
Err: err,
File: file.fileName,
Index: j,
SQL: i.sql,
Params: i.params,
}
}
}
return nil
})
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
return l.helper.afterLoad(l.db)
}

// InsertError will be returned if any error happens on database while
// inserting the record.
type InsertError struct {
Err error
File string
Index int
SQL string
Params []interface{}
}

func (e *InsertError) Error() string {
return fmt.Sprintf(
"testfixtures: error inserting record: %v, on file: %s, index: %d, sql: %s, params: %v",
e.Err,
e.File,
e.Index,
e.SQL,
e.Params,
)
}

func (l *Loader) buildInsertSQLs() error {
for _, f := range l.fixturesFiles {
var records interface{}
if err := yaml.Unmarshal(f.content, &records); err != nil {
return fmt.Errorf("testfixtures: could not unmarshal YAML: %w", err)
}

switch records := records.(type) {
case []interface{}:
f.insertSQLs = make([]insertSQL, 0, len(records))

for _, record := range records {
recordMap, ok := record.(map[interface{}]interface{})
if !ok {
return fmt.Errorf("testfixtures: could not cast record: not a map[interface{}]interface{}")
}

sql, values, err := l.buildInsertSQL(f, recordMap)
if err != nil {
return err
}

f.insertSQLs = append(f.insertSQLs, insertSQL{sql, values})
}
case map[interface{}]interface{}:
f.insertSQLs = make([]insertSQL, 0, len(records))

for _, record := range records {
recordMap, ok := record.(map[interface{}]interface{})
if !ok {
return fmt.Errorf("testfixtures: could not cast record: not a map[interface{}]interface{}")
}

sql, values, err := l.buildInsertSQL(f, recordMap)
if err != nil {
return err
}

f.insertSQLs = append(f.insertSQLs, insertSQL{sql, values})
}
default:
return fmt.Errorf("testfixtures: fixture is not a slice or map")
}
}

return nil
}

func (f *fixtureFile) fileNameWithoutExtension() string {
return strings.Replace(f.fileName, filepath.Ext(f.fileName), "", 1)
}

func (f *fixtureFile) delete(tx *sql.Tx, h helper) error {
if _, err := tx.Exec(fmt.Sprintf("DELETE FROM %s", h.quoteKeyword(f.fileNameWithoutExtension()))); err != nil {
return fmt.Errorf(`testfixtures: could not clean table "%s": %w`, f.fileNameWithoutExtension(), err)
}
return nil
}

func (l *Loader) buildInsertSQL(f *fixtureFile, record map[interface{}]interface{}) (sqlStr string, values []interface{}, err error) {
var (