├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── fixtures ├── test_fixtures1.yml └── test_fixtures2.yml ├── go.mod ├── go.sum ├── gometalinter.json ├── load.go ├── load_postgres_test.go ├── load_sqlite_test.go ├── load_test.go ├── package.go ├── row.go └── row_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | coverage.txt 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: go 3 | 4 | go: 5 | - 1.13.x 6 | 7 | env: 8 | - GO111MODULE=on 9 | 10 | services: 11 | - docker 12 | 13 | before_script: 14 | - createuser --createdb go_fixtures 15 | 16 | script: 17 | - make test-with-coverage 18 | 19 | after_success: 20 | - bash <(curl -s https://codecov.io/bash) 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License, version 2.0 2 | 3 | 1. Definitions 4 | 5 | 1.1. “Contributor” 6 | 7 | means each individual or legal entity that creates, contributes to the 8 | creation of, or owns Covered Software. 9 | 10 | 1.2. “Contributor Version” 11 | 12 | means the combination of the Contributions of others (if any) used by a 13 | Contributor and that particular Contributor’s Contribution. 14 | 15 | 1.3. “Contribution” 16 | 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. “Covered Software” 20 | 21 | means Source Code Form to which the initial Contributor has attached the 22 | notice in Exhibit A, the Executable Form of such Source Code Form, and 23 | Modifications of such Source Code Form, in each case including portions 24 | thereof. 25 | 26 | 1.5. “Incompatible With Secondary Licenses” 27 | means 28 | 29 | a. that the initial Contributor has attached the notice described in 30 | Exhibit B to the Covered Software; or 31 | 32 | b. that the Covered Software was made available under the terms of version 33 | 1.1 or earlier of the License, but not also under the terms of a 34 | Secondary License. 35 | 36 | 1.6. “Executable Form” 37 | 38 | means any form of the work other than Source Code Form. 39 | 40 | 1.7. “Larger Work” 41 | 42 | means a work that combines Covered Software with other material, in a separate 43 | file or files, that is not Covered Software. 44 | 45 | 1.8. “License” 46 | 47 | means this document. 48 | 49 | 1.9. “Licensable” 50 | 51 | means having the right to grant, to the maximum extent possible, whether at the 52 | time of the initial grant or subsequently, any and all of the rights conveyed by 53 | this License. 54 | 55 | 1.10. “Modifications” 56 | 57 | means any of the following: 58 | 59 | a. any file in Source Code Form that results from an addition to, deletion 60 | from, or modification of the contents of Covered Software; or 61 | 62 | b. any new file in Source Code Form that contains any Covered Software. 63 | 64 | 1.11. “Patent Claims” of a Contributor 65 | 66 | means any patent claim(s), including without limitation, method, process, 67 | and apparatus claims, in any patent Licensable by such Contributor that 68 | would be infringed, but for the grant of the License, by the making, 69 | using, selling, offering for sale, having made, import, or transfer of 70 | either its Contributions or its Contributor Version. 71 | 72 | 1.12. “Secondary License” 73 | 74 | means either the GNU General Public License, Version 2.0, the GNU Lesser 75 | General Public License, Version 2.1, the GNU Affero General Public 76 | License, Version 3.0, or any later versions of those licenses. 77 | 78 | 1.13. “Source Code Form” 79 | 80 | means the form of the work preferred for making modifications. 81 | 82 | 1.14. “You” (or “Your”) 83 | 84 | means an individual or a legal entity exercising rights under this 85 | License. For legal entities, “You” includes any entity that controls, is 86 | controlled by, or is under common control with You. For purposes of this 87 | definition, “control” means (a) the power, direct or indirect, to cause 88 | the direction or management of such entity, whether by contract or 89 | otherwise, or (b) ownership of more than fifty percent (50%) of the 90 | outstanding shares or beneficial ownership of such entity. 91 | 92 | 93 | 2. License Grants and Conditions 94 | 95 | 2.1. Grants 96 | 97 | Each Contributor hereby grants You a world-wide, royalty-free, 98 | non-exclusive license: 99 | 100 | a. under intellectual property rights (other than patent or trademark) 101 | Licensable by such Contributor to use, reproduce, make available, 102 | modify, display, perform, distribute, and otherwise exploit its 103 | Contributions, either on an unmodified basis, with Modifications, or as 104 | part of a Larger Work; and 105 | 106 | b. under Patent Claims of such Contributor to make, use, sell, offer for 107 | sale, have made, import, and otherwise transfer either its Contributions 108 | or its Contributor Version. 109 | 110 | 2.2. Effective Date 111 | 112 | The licenses granted in Section 2.1 with respect to any Contribution become 113 | effective for each Contribution on the date the Contributor first distributes 114 | such Contribution. 115 | 116 | 2.3. Limitations on Grant Scope 117 | 118 | The licenses granted in this Section 2 are the only rights granted under this 119 | License. No additional rights or licenses will be implied from the distribution 120 | or licensing of Covered Software under this License. Notwithstanding Section 121 | 2.1(b) above, no patent license is granted by a Contributor: 122 | 123 | a. for any code that a Contributor has removed from Covered Software; or 124 | 125 | b. for infringements caused by: (i) Your and any other third party’s 126 | modifications of Covered Software, or (ii) the combination of its 127 | Contributions with other software (except as part of its Contributor 128 | Version); or 129 | 130 | c. under Patent Claims infringed by Covered Software in the absence of its 131 | Contributions. 132 | 133 | This License does not grant any rights in the trademarks, service marks, or 134 | logos of any Contributor (except as may be necessary to comply with the 135 | notice requirements in Section 3.4). 136 | 137 | 2.4. Subsequent Licenses 138 | 139 | No Contributor makes additional grants as a result of Your choice to 140 | distribute the Covered Software under a subsequent version of this License 141 | (see Section 10.2) or under the terms of a Secondary License (if permitted 142 | under the terms of Section 3.3). 143 | 144 | 2.5. Representation 145 | 146 | Each Contributor represents that the Contributor believes its Contributions 147 | are its original creation(s) or it has sufficient rights to grant the 148 | rights to its Contributions conveyed by this License. 149 | 150 | 2.6. Fair Use 151 | 152 | This License is not intended to limit any rights You have under applicable 153 | copyright doctrines of fair use, fair dealing, or other equivalents. 154 | 155 | 2.7. Conditions 156 | 157 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in 158 | Section 2.1. 159 | 160 | 161 | 3. Responsibilities 162 | 163 | 3.1. Distribution of Source Form 164 | 165 | All distribution of Covered Software in Source Code Form, including any 166 | Modifications that You create or to which You contribute, must be under the 167 | terms of this License. You must inform recipients that the Source Code Form 168 | of the Covered Software is governed by the terms of this License, and how 169 | they can obtain a copy of this License. You may not attempt to alter or 170 | restrict the recipients’ rights in the Source Code Form. 171 | 172 | 3.2. Distribution of Executable Form 173 | 174 | If You distribute Covered Software in Executable Form then: 175 | 176 | a. such Covered Software must also be made available in Source Code Form, 177 | as described in Section 3.1, and You must inform recipients of the 178 | Executable Form how they can obtain a copy of such Source Code Form by 179 | reasonable means in a timely manner, at a charge no more than the cost 180 | of distribution to the recipient; and 181 | 182 | b. You may distribute such Executable Form under the terms of this License, 183 | or sublicense it under different terms, provided that the license for 184 | the Executable Form does not attempt to limit or alter the recipients’ 185 | rights in the Source Code Form under this License. 186 | 187 | 3.3. Distribution of a Larger Work 188 | 189 | You may create and distribute a Larger Work under terms of Your choice, 190 | provided that You also comply with the requirements of this License for the 191 | Covered Software. If the Larger Work is a combination of Covered Software 192 | with a work governed by one or more Secondary Licenses, and the Covered 193 | Software is not Incompatible With Secondary Licenses, this License permits 194 | You to additionally distribute such Covered Software under the terms of 195 | such Secondary License(s), so that the recipient of the Larger Work may, at 196 | their option, further distribute the Covered Software under the terms of 197 | either this License or such Secondary License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices (including 202 | copyright notices, patent notices, disclaimers of warranty, or limitations 203 | of liability) contained within the Source Code Form of the Covered 204 | Software, except that You may alter any license notices to the extent 205 | required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on behalf 212 | of any Contributor. You must make it absolutely clear that any such 213 | warranty, support, indemnity, or liability obligation is offered by You 214 | alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | 222 | If it is impossible for You to comply with any of the terms of this License 223 | with respect to some or all of the Covered Software due to statute, judicial 224 | order, or regulation then You must: (a) comply with the terms of this License 225 | to the maximum extent possible; and (b) describe the limitations and the code 226 | they affect. Such description must be placed in a text file included with all 227 | distributions of the Covered Software under this License. Except to the 228 | extent prohibited by statute or regulation, such description must be 229 | sufficiently detailed for a recipient of ordinary skill to be able to 230 | understand it. 231 | 232 | 5. Termination 233 | 234 | 5.1. The rights granted under this License will terminate automatically if You 235 | fail to comply with any of its terms. However, if You become compliant, 236 | then the rights granted under this License from a particular Contributor 237 | are reinstated (a) provisionally, unless and until such Contributor 238 | explicitly and finally terminates Your grants, and (b) on an ongoing basis, 239 | if such Contributor fails to notify You of the non-compliance by some 240 | reasonable means prior to 60 days after You have come back into compliance. 241 | Moreover, Your grants from a particular Contributor are reinstated on an 242 | ongoing basis if such Contributor notifies You of the non-compliance by 243 | some reasonable means, this is the first time You have received notice of 244 | non-compliance with this License from such Contributor, and You become 245 | compliant prior to 30 days after Your receipt of the notice. 246 | 247 | 5.2. If You initiate litigation against any entity by asserting a patent 248 | infringement claim (excluding declaratory judgment actions, counter-claims, 249 | and cross-claims) alleging that a Contributor Version directly or 250 | indirectly infringes any patent, then the rights granted to You by any and 251 | all Contributors for the Covered Software under Section 2.1 of this License 252 | shall terminate. 253 | 254 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user 255 | license agreements (excluding distributors and resellers) which have been 256 | validly granted by You or Your distributors under this License prior to 257 | termination shall survive termination. 258 | 259 | 6. Disclaimer of Warranty 260 | 261 | Covered Software is provided under this License on an “as is” basis, without 262 | warranty of any kind, either expressed, implied, or statutory, including, 263 | without limitation, warranties that the Covered Software is free of defects, 264 | merchantable, fit for a particular purpose or non-infringing. The entire 265 | risk as to the quality and performance of the Covered Software is with You. 266 | Should any Covered Software prove defective in any respect, You (not any 267 | Contributor) assume the cost of any necessary servicing, repair, or 268 | correction. This disclaimer of warranty constitutes an essential part of this 269 | License. No use of any Covered Software is authorized under this License 270 | except under this disclaimer. 271 | 272 | 7. Limitation of Liability 273 | 274 | Under no circumstances and under no legal theory, whether tort (including 275 | negligence), contract, or otherwise, shall any Contributor, or anyone who 276 | distributes Covered Software as permitted above, be liable to You for any 277 | direct, indirect, special, incidental, or consequential damages of any 278 | character including, without limitation, damages for lost profits, loss of 279 | goodwill, work stoppage, computer failure or malfunction, or any and all 280 | other commercial damages or losses, even if such party shall have been 281 | informed of the possibility of such damages. This limitation of liability 282 | shall not apply to liability for death or personal injury resulting from such 283 | party’s negligence to the extent applicable law prohibits such limitation. 284 | Some jurisdictions do not allow the exclusion or limitation of incidental or 285 | consequential damages, so this exclusion and limitation may not apply to You. 286 | 287 | 8. Litigation 288 | 289 | Any litigation relating to this License may be brought only in the courts of 290 | a jurisdiction where the defendant maintains its principal place of business 291 | and such litigation shall be governed by laws of that jurisdiction, without 292 | reference to its conflict-of-law provisions. Nothing in this Section shall 293 | prevent a party’s ability to bring cross-claims or counter-claims. 294 | 295 | 9. Miscellaneous 296 | 297 | This License represents the complete agreement concerning the subject matter 298 | hereof. If any provision of this License is held to be unenforceable, such 299 | provision shall be reformed only to the extent necessary to make it 300 | enforceable. Any law or regulation which provides that the language of a 301 | contract shall be construed against the drafter shall not be used to construe 302 | this License against a Contributor. 303 | 304 | 305 | 10. Versions of the License 306 | 307 | 10.1. New Versions 308 | 309 | Mozilla Foundation is the license steward. Except as provided in Section 310 | 10.3, no one other than the license steward has the right to modify or 311 | publish new versions of this License. Each version will be given a 312 | distinguishing version number. 313 | 314 | 10.2. Effect of New Versions 315 | 316 | You may distribute the Covered Software under the terms of the version of 317 | the License under which You originally received the Covered Software, or 318 | under the terms of any subsequent version published by the license 319 | steward. 320 | 321 | 10.3. Modified Versions 322 | 323 | If you create software not governed by this License, and you want to 324 | create a new license for such software, you may create and use a modified 325 | version of this License if you rename the license and remove any 326 | references to the name of the license steward (except to note that such 327 | modified license differs from this License). 328 | 329 | 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses 330 | If You choose to distribute Source Code Form that is Incompatible With 331 | Secondary Licenses under the terms of this version of the License, the 332 | notice described in Exhibit B of this License must be attached. 333 | 334 | Exhibit A - Source Code Form License Notice 335 | 336 | This Source Code Form is subject to the 337 | terms of the Mozilla Public License, v. 338 | 2.0. If a copy of the MPL was not 339 | distributed with this file, You can 340 | obtain one at 341 | http://mozilla.org/MPL/2.0/. 342 | 343 | If it is not possible or desirable to put the notice in a particular file, then 344 | You may include the notice in a location (such as a LICENSE file in a relevant 345 | directory) where a recipient would be likely to look for such a notice. 346 | 347 | You may add additional accurate notices of copyright ownership. 348 | 349 | Exhibit B - “Incompatible With Secondary Licenses” Notice 350 | 351 | This Source Code Form is “Incompatible 352 | With Secondary Licenses”, as defined by 353 | the Mozilla Public License, v. 2.0. 354 | 355 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: fmt lint golint test test-with-coverage 2 | # TODO: When Go 1.9 is released vendor folder should be ignored automatically 3 | PACKAGES=`go list ./... | grep -v vendor | grep -v mocks` 4 | 5 | fmt: 6 | for pkg in ${PACKAGES}; do \ 7 | go fmt $$pkg; \ 8 | done; 9 | 10 | lint: 11 | gometalinter --tests --disable-all --deadline=120s -E vet -E gofmt -E misspell -E ineffassign -E goimports -E deadcode ./... 12 | 13 | golint: 14 | for pkg in ${PACKAGES}; do \ 15 | golint -set_exit_status $$pkg || GOLINT_FAILED=1; \ 16 | done; \ 17 | [ -z "$$GOLINT_FAILED" ] 18 | 19 | test: 20 | TEST_FAILED= ; \ 21 | for pkg in ${PACKAGES}; do \ 22 | go test $$pkg || TEST_FAILED=1; \ 23 | done; \ 24 | [ -z "$$TEST_FAILED" ] 25 | 26 | test-with-coverage: 27 | echo "" > coverage.out 28 | echo "mode: set" > coverage-all.out 29 | TEST_FAILED= ; \ 30 | for pkg in ${PACKAGES}; do \ 31 | go test -coverprofile=coverage.out -covermode=set $$pkg || TEST_FAILED=1; \ 32 | tail -n +2 coverage.out >> coverage-all.out; \ 33 | done; \ 34 | [ -z "$$TEST_FAILED" ] 35 | #go tool cover -html=coverage-all.out 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## go-fixtures 2 | 3 | Django style fixtures for Golang's excellent built-in `database/sql` library. Currently only `YAML` fixtures are supported. 4 | 5 | [![Travis Status for RichardKnop/go-fixtures](https://travis-ci.org/RichardKnop/go-fixtures.svg?branch=master&label=linux+build)](https://travis-ci.org/RichardKnop/go-fixtures) 6 | [![godoc for RichardKnop/go-fixtures](https://godoc.org/github.com/nathany/looper?status.svg)](http://godoc.org/github.com/RichardKnop/go-fixtures) 7 | [![codecov for RichardKnop/go-fixtures](https://codecov.io/gh/RichardKnop/go-fixtures/branch/master/graph/badge.svg)](https://codecov.io/gh/RichardKnop/go-fixtures) 8 | 9 | --- 10 | 11 | 12 | There are two reserved values you can use for `datetime` fields: 13 | 14 | * `ON_INSERT_NOW()` will only be used when a row is being inserted 15 | * `ON_UPDATE_NOW()` will only be used when a row is being updated 16 | 17 | Example YAML fixture: 18 | 19 | ```yaml 20 | --- 21 | 22 | - table: 'some_table' 23 | pk: 24 | id: 1 25 | fields: 26 | string_field: 'foobar' 27 | boolean_field: true 28 | created_at: 'ON_INSERT_NOW()' 29 | updated_at: 'ON_UPDATE_NOW()' 30 | 31 | - table: 'other_table' 32 | pk: 33 | id: 2 34 | fields: 35 | int_field: 123 36 | boolean_field: false 37 | created_at: 'ON_INSERT_NOW()' 38 | updated_at: 'ON_UPDATE_NOW()' 39 | 40 | - table: 'join_table' 41 | pk: 42 | some_id: 1 43 | other_id: 2 44 | ``` 45 | 46 | Example integration for your project: 47 | 48 | ```go 49 | package main 50 | 51 | import ( 52 | "database/sql" 53 | "io/ioutil" 54 | "log" 55 | 56 | "github.com/RichardKnop/go-fixtures" 57 | "github.com/urfave/cli" 58 | // Drivers 59 | _ "github.com/lib/pq" 60 | ) 61 | 62 | var ( 63 | cliApp *cli.App 64 | ) 65 | 66 | func init() { 67 | cliApp = cli.NewApp() 68 | cliApp.Name = "your-project" 69 | cliApp.Usage = "Project's usage" 70 | cliApp.Author = "Your Name" 71 | cliApp.Email = "your@email" 72 | cliApp.Version = "0.0.0" 73 | } 74 | 75 | func main() { 76 | db, err := sql.Connect("postgres", "user=foo dbname=bar sslmode=disable") 77 | if err != nil { 78 | log.Fatal(err) 79 | } 80 | defer db.Close() 81 | 82 | cliApp.Commands = []cli.Command{ 83 | { 84 | Name: "loaddata", 85 | Usage: "load data from fixture", 86 | Action: func(c *cli.Context) error { 87 | data, err := ioutil.ReadFile(c.Args().First()) 88 | if err != nil { 89 | return err 90 | } 91 | 92 | if err := fixtures.Load(data, db, "postgres"); err != nil { 93 | return err 94 | } 95 | }, 96 | }, 97 | { 98 | Name: "runserver", 99 | Usage: "run web server", 100 | Action: func(c *cli.Context) error { 101 | // Run your web server here 102 | return nil 103 | }, 104 | }, 105 | } 106 | 107 | cliApp.Run(os.Args) 108 | } 109 | ``` 110 | -------------------------------------------------------------------------------- /fixtures/test_fixtures1.yml: -------------------------------------------------------------------------------- 1 | - table: 'some_table' 2 | pk: 3 | id: 1 4 | fields: 5 | string_field: 'foobar' 6 | boolean_field: true 7 | created_at: 'ON_INSERT_NOW()' 8 | updated_at: 'ON_UPDATE_NOW()' -------------------------------------------------------------------------------- /fixtures/test_fixtures2.yml: -------------------------------------------------------------------------------- 1 | - table: 'other_table' 2 | pk: 3 | id: 2 4 | fields: 5 | int_field: 123 6 | boolean_field: false 7 | created_at: 'ON_INSERT_NOW()' 8 | updated_at: 'ON_UPDATE_NOW()' 9 | 10 | - table: 'join_table' 11 | pk: 12 | some_id: 1 13 | other_id: 2 14 | 15 | - table: 'string_key_table' 16 | pk: 17 | id: 'new_id' 18 | fields: 19 | created_at: 'ON_INSERT_NOW()' 20 | updated_at: 'ON_UPDATE_NOW()' -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/RichardKnop/go-fixtures 2 | 3 | require ( 4 | github.com/davecgh/go-spew v1.1.1 // indirect 5 | github.com/kr/pretty v0.1.0 // indirect 6 | github.com/lib/pq v1.0.0 7 | github.com/mattn/go-sqlite3 v1.10.0 8 | github.com/pmezard/go-difflib v1.0.0 // indirect 9 | github.com/stretchr/testify v1.2.2 10 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect 11 | gopkg.in/yaml.v2 v2.2.1 12 | ) 13 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 4 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 5 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 6 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 7 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 8 | github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= 9 | github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= 10 | github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o= 11 | github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= 12 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 13 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 14 | github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= 15 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 16 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 17 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= 18 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 19 | gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= 20 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 21 | -------------------------------------------------------------------------------- /gometalinter.json: -------------------------------------------------------------------------------- 1 | { 2 | "Linters": 3 | { 4 | "vet": 5 | { 6 | "Command": "go tool vet" 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /load.go: -------------------------------------------------------------------------------- 1 | package fixtures 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "io/ioutil" 7 | "strings" 8 | 9 | "gopkg.in/yaml.v2" 10 | ) 11 | 12 | // NewProcessingError ... 13 | func NewProcessingError(row int, cause error) error { 14 | return fmt.Errorf("Error loading row %d: %s", row, cause.Error()) 15 | } 16 | 17 | // NewFileError ... 18 | func NewFileError(filename string, cause error) error { 19 | return fmt.Errorf("Error loading file %s: %s", filename, cause.Error()) 20 | } 21 | 22 | // Load processes a YAML fixture and inserts/updates the database accordingly 23 | func Load(data []byte, db *sql.DB, driver string) error { 24 | // Unmarshal the YAML data into a []Row slice 25 | var rows []Row 26 | if err := yaml.Unmarshal(data, &rows); err != nil { 27 | return err 28 | } 29 | 30 | // Begin a transaction 31 | tx, err := db.Begin() 32 | if err != nil { 33 | return err 34 | } 35 | 36 | // Iterate over rows define in the fixture 37 | for i, row := range rows { 38 | // Load internat struct variables 39 | row.Init() 40 | 41 | // Run a SELECT query to find out if we need to insert or UPDATE 42 | selectQuery := fmt.Sprintf( 43 | `SELECT COUNT(*) FROM "%s" WHERE %s`, 44 | row.Table, 45 | row.GetWhere(driver, 0), 46 | ) 47 | if driver == "mysql" { 48 | selectQuery = strings.Replace(selectQuery, "\"", "", -1) 49 | } 50 | var count int 51 | err = tx.QueryRow(selectQuery, row.GetPKValues()...).Scan(&count) 52 | if err != nil { 53 | tx.Rollback() // rollback the transaction 54 | return NewProcessingError(i+1, err) 55 | } 56 | 57 | if count == 0 { 58 | // Primary key not found, let's run an INSERT query 59 | insertQuery := fmt.Sprintf( 60 | `INSERT INTO "%s"(%s) VALUES(%s)`, 61 | row.Table, 62 | strings.Join(row.GetInsertColumns(), ", "), 63 | strings.Join(row.GetInsertPlaceholders(driver), ", "), 64 | ) 65 | if driver == "mysql" { 66 | insertQuery = strings.Replace(insertQuery, "\"", "", -1) 67 | } 68 | _, err := tx.Exec(insertQuery, row.GetInsertValues()...) 69 | if err != nil { 70 | tx.Rollback() // rollback the transaction 71 | return NewProcessingError(i+1, err) 72 | } 73 | if driver == postgresDriver && row.GetInsertColumns()[0] == "\"id\"" { 74 | err = fixPostgresPKSequence(tx, row.Table, "id") 75 | if err != nil { 76 | tx.Rollback() 77 | return NewProcessingError(i+1, err) 78 | } 79 | } 80 | } else { 81 | // Primary key found, let's run UPDATE query 82 | updateQuery := fmt.Sprintf( 83 | `UPDATE "%s" SET %s WHERE %s`, 84 | row.Table, 85 | strings.Join(row.GetUpdatePlaceholders(driver), ", "), 86 | row.GetWhere(driver, row.GetUpdateValuesLength()), 87 | ) 88 | if driver == "mysql" { 89 | updateQuery = strings.Replace(updateQuery, "\"", "", -1) 90 | } 91 | values := append(row.GetUpdateValues(), row.GetPKValues()...) 92 | _, err := tx.Exec(updateQuery, values...) 93 | if err != nil { 94 | tx.Rollback() // rollback the transaction 95 | return NewProcessingError(i+1, err) 96 | } 97 | if driver == postgresDriver && row.GetUpdateColumns()[0] == "\"id\"" { 98 | err = fixPostgresPKSequence(tx, row.Table, "id") 99 | if err != nil { 100 | tx.Rollback() 101 | return NewProcessingError(i+1, err) 102 | } 103 | } 104 | } 105 | } 106 | 107 | // Commit the transaction 108 | if err := tx.Commit(); err != nil { 109 | tx.Rollback() // rollback the transaction 110 | return err 111 | } 112 | 113 | return nil 114 | } 115 | 116 | // LoadFile ... 117 | func LoadFile(filename string, db *sql.DB, driver string) error { 118 | // Read fixture data from the file 119 | data, err := ioutil.ReadFile(filename) 120 | if err != nil { 121 | return NewFileError(filename, err) 122 | } 123 | 124 | // Insert the fixture data 125 | return Load(data, db, driver) 126 | } 127 | 128 | // LoadFiles ... 129 | func LoadFiles(filenames []string, db *sql.DB, driver string) error { 130 | for _, filename := range filenames { 131 | if err := LoadFile(filename, db, driver); err != nil { 132 | return err 133 | } 134 | } 135 | return nil 136 | } 137 | 138 | // fixPostgresPKSequence 139 | func fixPostgresPKSequence(tx *sql.Tx, table string, column string) error { 140 | // Query for the qualified sequence name 141 | var seqName *string 142 | err := tx.QueryRow(` 143 | SELECT pg_get_serial_sequence($1, $2) 144 | `, table, column).Scan(&seqName) 145 | 146 | if err != nil { 147 | return err 148 | } 149 | 150 | if seqName == nil { 151 | // No sequence to fix 152 | return nil 153 | } 154 | 155 | // Set the sequence 156 | _, err = tx.Exec(fmt.Sprintf(` 157 | SELECT pg_catalog.setval($1, (SELECT MAX("%s") FROM "%s")) 158 | `, column, table), *seqName) 159 | 160 | return err 161 | } 162 | -------------------------------------------------------------------------------- /load_postgres_test.go: -------------------------------------------------------------------------------- 1 | package fixtures_test 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "log" 7 | "os/exec" 8 | "testing" 9 | "time" 10 | 11 | "github.com/RichardKnop/go-fixtures" 12 | "github.com/stretchr/testify/assert" 13 | // Driver 14 | _ "github.com/lib/pq" 15 | ) 16 | 17 | const testPostgresDbUser = "go_fixtures" 18 | 19 | func TestLoadWorksWithValidDataPostgres(t *testing.T) { 20 | t.Parallel() 21 | 22 | var ( 23 | db *sql.DB 24 | err error 25 | ) 26 | 27 | // Connect to a test Postgres db 28 | db, err = rebuildDatabasePostgres(testPostgresDbUser, "go_fixtures_test_load") 29 | if err != nil { 30 | log.Fatal(err) 31 | } 32 | defer db.Close() 33 | 34 | // Create a test schema 35 | _, err = db.Exec(testSchemaPostgres) 36 | if err != nil { 37 | log.Fatal(err) 38 | } 39 | 40 | // Let's load the fixture, since the database is empty, this should run inserts 41 | err = fixtures.Load([]byte(testData), db, "postgres") 42 | 43 | // Error should be nil 44 | assert.Nil(t, err) 45 | 46 | var ( 47 | count int 48 | rows *sql.Rows 49 | id int 50 | stringField string 51 | booleanField bool 52 | intField int 53 | createdAt *time.Time 54 | updatedAt *time.Time 55 | someID int 56 | otherID int 57 | ) 58 | 59 | // Check row counts 60 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 61 | assert.Equal(t, 1, count) 62 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 63 | assert.Equal(t, 1, count) 64 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 65 | assert.Equal(t, 1, count) 66 | 67 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 68 | assert.Equal(t, 1, count) 69 | 70 | // Check correct data has been loaded into some_table 71 | rows, err = db.Query("SELECT id, string_field, boolean_field, " + 72 | "created_at, updated_at FROM some_table") 73 | if err != nil { 74 | log.Fatal(err) 75 | } 76 | 77 | defer rows.Close() 78 | for rows.Next() { 79 | if err := rows.Scan( 80 | &id, 81 | &stringField, 82 | &booleanField, 83 | &createdAt, 84 | &updatedAt, 85 | ); err != nil { 86 | log.Fatal(err) 87 | } 88 | 89 | if err := rows.Err(); err != nil { 90 | log.Fatal(err) 91 | } 92 | 93 | assert.Equal(t, 1, id) 94 | assert.Equal(t, "foobar", stringField) 95 | assert.Equal(t, true, booleanField) 96 | assert.NotNil(t, createdAt) 97 | assert.Nil(t, updatedAt) 98 | } 99 | 100 | // Check correct data has been loaded into other_table 101 | rows, err = db.Query("SELECT id, int_field, boolean_field, " + 102 | "created_at, updated_at FROM other_table") 103 | if err != nil { 104 | log.Fatal(err) 105 | } 106 | 107 | defer rows.Close() 108 | for rows.Next() { 109 | if err := rows.Scan( 110 | &id, 111 | &intField, 112 | &booleanField, 113 | &createdAt, 114 | &updatedAt, 115 | ); err != nil { 116 | log.Fatal(err) 117 | } 118 | 119 | if err := rows.Err(); err != nil { 120 | log.Fatal(err) 121 | } 122 | 123 | assert.Equal(t, 2, id) 124 | assert.Equal(t, 123, intField) 125 | assert.Equal(t, false, booleanField) 126 | assert.NotNil(t, createdAt) 127 | assert.Nil(t, updatedAt) 128 | } 129 | 130 | // Check correct data has been loaded into join_table 131 | rows, err = db.Query("SELECT some_id, other_id FROM join_table") 132 | if err != nil { 133 | log.Fatal(err) 134 | } 135 | 136 | defer rows.Close() 137 | for rows.Next() { 138 | if err := rows.Scan( 139 | &someID, 140 | &otherID, 141 | ); err != nil { 142 | log.Fatal(err) 143 | } 144 | 145 | if err := rows.Err(); err != nil { 146 | log.Fatal(err) 147 | } 148 | 149 | assert.Equal(t, 1, someID) 150 | assert.Equal(t, 2, otherID) 151 | } 152 | 153 | // Let's reload the fixture, this should run updates 154 | err = fixtures.Load([]byte(testData), db, "postgres") 155 | 156 | // Error should be nil 157 | assert.Nil(t, err) 158 | 159 | // Check row counts, should be unchanged 160 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 161 | assert.Equal(t, 1, count) 162 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 163 | assert.Equal(t, 1, count) 164 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 165 | assert.Equal(t, 1, count) 166 | 167 | // Check correct data has been loaded into some_table 168 | rows, err = db.Query("SELECT id, string_field, boolean_field, " + 169 | "created_at, updated_at FROM some_table") 170 | if err != nil { 171 | log.Fatal(err) 172 | } 173 | 174 | defer rows.Close() 175 | for rows.Next() { 176 | if err := rows.Scan( 177 | &id, 178 | &stringField, 179 | &booleanField, 180 | &createdAt, 181 | &updatedAt, 182 | ); err != nil { 183 | log.Fatal(err) 184 | } 185 | 186 | if err := rows.Err(); err != nil { 187 | log.Fatal(err) 188 | } 189 | 190 | assert.Equal(t, 1, id) 191 | assert.Equal(t, "foobar", stringField) 192 | assert.Equal(t, true, booleanField) 193 | assert.NotNil(t, createdAt) 194 | assert.NotNil(t, updatedAt) 195 | } 196 | 197 | // Check correct data has been loaded into other_table 198 | rows, err = db.Query("SELECT id, int_field, boolean_field, " + 199 | "created_at, updated_at FROM other_table") 200 | if err != nil { 201 | log.Fatal(err) 202 | } 203 | 204 | defer rows.Close() 205 | for rows.Next() { 206 | if err := rows.Scan( 207 | &id, 208 | &intField, 209 | &booleanField, 210 | &createdAt, 211 | &updatedAt, 212 | ); err != nil { 213 | log.Fatal(err) 214 | } 215 | 216 | if err := rows.Err(); err != nil { 217 | log.Fatal(err) 218 | } 219 | 220 | assert.Equal(t, 2, id) 221 | assert.Equal(t, 123, intField) 222 | assert.Equal(t, false, booleanField) 223 | assert.NotNil(t, createdAt) 224 | assert.NotNil(t, updatedAt) 225 | } 226 | 227 | // Check correct data has been loaded into join_table 228 | rows, err = db.Query("SELECT some_id, other_id FROM join_table") 229 | if err != nil { 230 | log.Fatal(err) 231 | } 232 | 233 | defer rows.Close() 234 | for rows.Next() { 235 | if err := rows.Scan( 236 | &someID, 237 | &otherID, 238 | ); err != nil { 239 | log.Fatal(err) 240 | } 241 | 242 | if err := rows.Err(); err != nil { 243 | log.Fatal(err) 244 | } 245 | 246 | assert.Equal(t, 1, someID) 247 | assert.Equal(t, 2, otherID) 248 | } 249 | } 250 | 251 | func TestLoadFileWorksWithValidFilePostgres(t *testing.T) { 252 | t.Parallel() 253 | 254 | var ( 255 | db *sql.DB 256 | err error 257 | ) 258 | 259 | // Connect to a test Postgres db 260 | db, err = rebuildDatabasePostgres(testPostgresDbUser, "go_fixtures_test_load_file") 261 | if err != nil { 262 | log.Fatal(err) 263 | } 264 | defer db.Close() 265 | 266 | // Create a test schema 267 | _, err = db.Exec(testSchemaPostgres) 268 | if err != nil { 269 | log.Fatal(err) 270 | } 271 | 272 | var count int 273 | // Check row counts to show no data 274 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 275 | assert.Equal(t, 0, count) 276 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 277 | assert.Equal(t, 0, count) 278 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 279 | assert.Equal(t, 0, count) 280 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 281 | assert.Equal(t, 0, count) 282 | 283 | // Let's load the fixture, since the database is empty, this should run inserts 284 | err = fixtures.LoadFile(fixtureFile, db, "postgres") 285 | 286 | // Error should be nil 287 | assert.Nil(t, err) 288 | 289 | var ( 290 | rows *sql.Rows 291 | id int 292 | stringField string 293 | booleanField bool 294 | createdAt *time.Time 295 | updatedAt *time.Time 296 | ) 297 | 298 | // Check row counts 299 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 300 | assert.Equal(t, 1, count) 301 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 302 | assert.Equal(t, 0, count) 303 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 304 | assert.Equal(t, 0, count) 305 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 306 | assert.Equal(t, 0, count) 307 | 308 | // Check correct data has been loaded into some_table 309 | rows, err = db.Query("SELECT id, string_field, boolean_field, " + 310 | "created_at, updated_at FROM some_table") 311 | if err != nil { 312 | log.Fatal(err) 313 | } 314 | 315 | defer rows.Close() 316 | for rows.Next() { 317 | if err := rows.Scan( 318 | &id, 319 | &stringField, 320 | &booleanField, 321 | &createdAt, 322 | &updatedAt, 323 | ); err != nil { 324 | log.Fatal(err) 325 | } 326 | 327 | if err := rows.Err(); err != nil { 328 | log.Fatal(err) 329 | } 330 | 331 | assert.Equal(t, 1, id) 332 | assert.Equal(t, "foobar", stringField) 333 | assert.Equal(t, true, booleanField) 334 | assert.NotNil(t, createdAt) 335 | assert.Nil(t, updatedAt) 336 | } 337 | 338 | // Let's reload the fixture, this should run updates 339 | err = fixtures.LoadFile(fixtureFile, db, "postgres") 340 | 341 | // Error should be nil 342 | assert.Nil(t, err) 343 | 344 | // Check row counts, should be unchanged 345 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 346 | assert.Equal(t, 1, count) 347 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 348 | assert.Equal(t, 0, count) 349 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 350 | assert.Equal(t, 0, count) 351 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 352 | assert.Equal(t, 0, count) 353 | } 354 | 355 | func TestLoadFileFailsWithMissingFilePostgres(t *testing.T) { 356 | t.Parallel() 357 | 358 | var ( 359 | db *sql.DB 360 | err error 361 | ) 362 | 363 | // Connect to a test Postgres db 364 | db, err = rebuildDatabasePostgres(testPostgresDbUser, "go_fixtures_test_load_file_missing_file") 365 | if err != nil { 366 | log.Fatal(err) 367 | } 368 | defer db.Close() 369 | 370 | // Create a test schema 371 | _, err = db.Exec(testSchemaPostgres) 372 | if err != nil { 373 | log.Fatal(err) 374 | } 375 | 376 | // Let's load the fixture, since the database is empty, this should run inserts 377 | err = fixtures.LoadFile("bad_filename.yml", db, "postgres") 378 | 379 | // Error should be nil 380 | assert.EqualError(t, err, "Error loading file bad_filename.yml: open bad_filename.yml: no such file or directory") 381 | } 382 | 383 | func TestLoadFilesWorksWithValidFilesPostgres(t *testing.T) { 384 | t.Parallel() 385 | 386 | var ( 387 | db *sql.DB 388 | err error 389 | ) 390 | 391 | // Connect to a test Postgres db 392 | db, err = rebuildDatabasePostgres(testPostgresDbUser, "go_fixtures_test_load_files") 393 | if err != nil { 394 | log.Fatal(err) 395 | } 396 | defer db.Close() 397 | 398 | // Create a test schema 399 | _, err = db.Exec(testSchemaPostgres) 400 | if err != nil { 401 | log.Fatal(err) 402 | } 403 | 404 | var count int 405 | 406 | // Check rows are empty first 407 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 408 | assert.Equal(t, 0, count) 409 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 410 | assert.Equal(t, 0, count) 411 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 412 | assert.Equal(t, 0, count) 413 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 414 | assert.Equal(t, 0, count) 415 | 416 | // Let's load the fixture, since the database is empty, this should run inserts 417 | err = fixtures.LoadFiles(fixtureFiles, db, "postgres") 418 | 419 | // Error should be nil 420 | assert.Nil(t, err) 421 | 422 | // Check row counts 423 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 424 | assert.Equal(t, 1, count) 425 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 426 | assert.Equal(t, 1, count) 427 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 428 | assert.Equal(t, 1, count) 429 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 430 | assert.Equal(t, 1, count) 431 | 432 | // Let's reload the fixtures, this should run updates 433 | err = fixtures.LoadFiles(fixtureFiles, db, "postgres") 434 | 435 | // Error should be nil 436 | assert.Nil(t, err) 437 | 438 | // Check row counts, should be unchanged 439 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 440 | assert.Equal(t, 1, count) 441 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 442 | assert.Equal(t, 1, count) 443 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 444 | assert.Equal(t, 1, count) 445 | } 446 | 447 | func TestLoadFilesFailsWithABadFilePostgres(t *testing.T) { 448 | t.Parallel() 449 | 450 | var ( 451 | db *sql.DB 452 | err error 453 | ) 454 | 455 | // Connect to a test Postgres db 456 | db, err = rebuildDatabasePostgres(testPostgresDbUser, "go_fixtures_test_load_files_bad_file") 457 | if err != nil { 458 | log.Fatal(err) 459 | } 460 | defer db.Close() 461 | 462 | // Create a test schema 463 | _, err = db.Exec(testSchemaPostgres) 464 | if err != nil { 465 | log.Fatal(err) 466 | } 467 | 468 | var count int 469 | 470 | // Check rows are empty first 471 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 472 | assert.Equal(t, 0, count) 473 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 474 | assert.Equal(t, 0, count) 475 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 476 | assert.Equal(t, 0, count) 477 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 478 | assert.Equal(t, 0, count) 479 | 480 | var badList = []string{ 481 | fixtureFile, 482 | "bad_file", 483 | } 484 | 485 | // Let's load the fixture, since the database is empty, this should run inserts 486 | err = fixtures.LoadFiles(badList, db, "postgres") 487 | 488 | // Error should be nil 489 | assert.EqualError(t, err, "Error loading file bad_file: open bad_file: no such file or directory") 490 | } 491 | 492 | // rebuildDatabase attempts to delete an existing Postgres 493 | // database and rebuild it, returning a pointer to it 494 | func rebuildDatabasePostgres(dbUser, dbName string) (*sql.DB, error) { 495 | 496 | dropPostgresDB(dbUser, dbName) 497 | 498 | if err := createPostgresDB(dbUser, dbName); err != nil { 499 | return nil, err 500 | } 501 | 502 | return openPostgresDB(dbUser, dbName) 503 | } 504 | 505 | func openPostgresDB(dbUser, dbName string) (*sql.DB, error) { 506 | // Init a new postgres test database connection 507 | return sql.Open("postgres", 508 | fmt.Sprintf( 509 | "sslmode=disable host=localhost port=5432 user=%s password='' dbname=%s", 510 | dbUser, 511 | dbName, 512 | ), 513 | ) 514 | } 515 | 516 | func createPostgresDB(dbUser, dbName string) error { 517 | // Create a new test database 518 | createDbCmd := fmt.Sprintf("createdb -U %s %s", dbUser, dbName) 519 | log.Println(createDbCmd) 520 | out, err := exec.Command("sh", "-c", createDbCmd).Output() 521 | if err != nil { 522 | log.Printf("%v", string(out)) 523 | return err 524 | } 525 | return nil 526 | } 527 | 528 | func dropPostgresDB(dbUser, dbName string) { 529 | // Delete the current database if it exists 530 | dropDbCmd := fmt.Sprintf("dropdb --if-exists -U %s %s", dbUser, dbName) 531 | fmt.Println(dropDbCmd) 532 | exec.Command("sh", "-c", dropDbCmd).Output() 533 | } 534 | -------------------------------------------------------------------------------- /load_sqlite_test.go: -------------------------------------------------------------------------------- 1 | package fixtures_test 2 | 3 | import ( 4 | "database/sql" 5 | "log" 6 | "os" 7 | "testing" 8 | "time" 9 | 10 | "github.com/RichardKnop/go-fixtures" 11 | "github.com/stretchr/testify/assert" 12 | // Driver 13 | _ "github.com/mattn/go-sqlite3" 14 | ) 15 | 16 | func TestLoadWorksWithValidDataSQLite(t *testing.T) { 17 | t.Parallel() 18 | 19 | var testSQLiteDb = "/tmp/fixtures_testdb_load.sqlite" 20 | 21 | // Delete the test database 22 | os.Remove(testSQLiteDb) 23 | 24 | var ( 25 | db *sql.DB 26 | err error 27 | ) 28 | 29 | // Connect to an in-memory SQLite database 30 | db, err = sql.Open("sqlite3", testSQLiteDb) 31 | if err != nil { 32 | log.Fatal(err) 33 | } 34 | defer db.Close() 35 | 36 | // Create a test schema 37 | _, err = db.Exec(testSchemaSQLite) 38 | if err != nil { 39 | log.Fatal(err) 40 | } 41 | 42 | // Let's load the fixture, since the database is empty, this should run inserts 43 | err = fixtures.Load([]byte(testData), db, "sqlite") 44 | 45 | // Error should be nil 46 | assert.Nil(t, err) 47 | 48 | var ( 49 | count int 50 | rows *sql.Rows 51 | id int 52 | stringField string 53 | booleanField bool 54 | intField int 55 | createdAt *time.Time 56 | updatedAt *time.Time 57 | someID int 58 | otherID int 59 | ) 60 | 61 | // Check row counts 62 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 63 | assert.Equal(t, 1, count) 64 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 65 | assert.Equal(t, 1, count) 66 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 67 | assert.Equal(t, 1, count) 68 | 69 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 70 | assert.Equal(t, 1, count) 71 | 72 | // Check correct data has been loaded into some_table 73 | rows, err = db.Query("SELECT id, string_field, boolean_field, " + 74 | "created_at, updated_at FROM some_table") 75 | if err != nil { 76 | log.Fatal(err) 77 | } 78 | 79 | defer rows.Close() 80 | for rows.Next() { 81 | if err := rows.Scan( 82 | &id, 83 | &stringField, 84 | &booleanField, 85 | &createdAt, 86 | &updatedAt, 87 | ); err != nil { 88 | log.Fatal(err) 89 | } 90 | 91 | if err := rows.Err(); err != nil { 92 | log.Fatal(err) 93 | } 94 | 95 | assert.Equal(t, 1, id) 96 | assert.Equal(t, "foobar", stringField) 97 | assert.Equal(t, true, booleanField) 98 | assert.NotNil(t, createdAt) 99 | assert.Nil(t, updatedAt) 100 | } 101 | 102 | // Check correct data has been loaded into other_table 103 | rows, err = db.Query("SELECT id, int_field, boolean_field, " + 104 | "created_at, updated_at FROM other_table") 105 | if err != nil { 106 | log.Fatal(err) 107 | } 108 | 109 | defer rows.Close() 110 | for rows.Next() { 111 | if err := rows.Scan( 112 | &id, 113 | &intField, 114 | &booleanField, 115 | &createdAt, 116 | &updatedAt, 117 | ); err != nil { 118 | log.Fatal(err) 119 | } 120 | 121 | if err := rows.Err(); err != nil { 122 | log.Fatal(err) 123 | } 124 | 125 | assert.Equal(t, 2, id) 126 | assert.Equal(t, 123, intField) 127 | assert.Equal(t, false, booleanField) 128 | assert.NotNil(t, createdAt) 129 | assert.Nil(t, updatedAt) 130 | } 131 | 132 | // Check correct data has been loaded into join_table 133 | rows, err = db.Query("SELECT some_id, other_id FROM join_table") 134 | if err != nil { 135 | log.Fatal(err) 136 | } 137 | 138 | defer rows.Close() 139 | for rows.Next() { 140 | if err := rows.Scan( 141 | &someID, 142 | &otherID, 143 | ); err != nil { 144 | log.Fatal(err) 145 | } 146 | 147 | if err := rows.Err(); err != nil { 148 | log.Fatal(err) 149 | } 150 | 151 | assert.Equal(t, 1, someID) 152 | assert.Equal(t, 2, otherID) 153 | } 154 | 155 | // Let's reload the fixture, this should run updates 156 | err = fixtures.Load([]byte(testData), db, "sqlite") 157 | 158 | // Error should be nil 159 | assert.Nil(t, err) 160 | 161 | // Check row counts, should be unchanged 162 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 163 | assert.Equal(t, 1, count) 164 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 165 | assert.Equal(t, 1, count) 166 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 167 | assert.Equal(t, 1, count) 168 | 169 | // Check correct data has been loaded into some_table 170 | rows, err = db.Query("SELECT id, string_field, boolean_field, " + 171 | "created_at, updated_at FROM some_table") 172 | if err != nil { 173 | log.Fatal(err) 174 | } 175 | 176 | defer rows.Close() 177 | for rows.Next() { 178 | if err := rows.Scan( 179 | &id, 180 | &stringField, 181 | &booleanField, 182 | &createdAt, 183 | &updatedAt, 184 | ); err != nil { 185 | log.Fatal(err) 186 | } 187 | 188 | if err := rows.Err(); err != nil { 189 | log.Fatal(err) 190 | } 191 | 192 | assert.Equal(t, 1, id) 193 | assert.Equal(t, "foobar", stringField) 194 | assert.Equal(t, true, booleanField) 195 | assert.NotNil(t, createdAt) 196 | assert.NotNil(t, updatedAt) 197 | } 198 | 199 | // Check correct data has been loaded into other_table 200 | rows, err = db.Query("SELECT id, int_field, boolean_field, " + 201 | "created_at, updated_at FROM other_table") 202 | if err != nil { 203 | log.Fatal(err) 204 | } 205 | 206 | defer rows.Close() 207 | for rows.Next() { 208 | if err := rows.Scan( 209 | &id, 210 | &intField, 211 | &booleanField, 212 | &createdAt, 213 | &updatedAt, 214 | ); err != nil { 215 | log.Fatal(err) 216 | } 217 | 218 | if err := rows.Err(); err != nil { 219 | log.Fatal(err) 220 | } 221 | 222 | assert.Equal(t, 2, id) 223 | assert.Equal(t, 123, intField) 224 | assert.Equal(t, false, booleanField) 225 | assert.NotNil(t, createdAt) 226 | assert.NotNil(t, updatedAt) 227 | } 228 | 229 | // Check correct data has been loaded into join_table 230 | rows, err = db.Query("SELECT some_id, other_id FROM join_table") 231 | if err != nil { 232 | log.Fatal(err) 233 | } 234 | 235 | defer rows.Close() 236 | for rows.Next() { 237 | if err := rows.Scan( 238 | &someID, 239 | &otherID, 240 | ); err != nil { 241 | log.Fatal(err) 242 | } 243 | 244 | if err := rows.Err(); err != nil { 245 | log.Fatal(err) 246 | } 247 | 248 | assert.Equal(t, 1, someID) 249 | assert.Equal(t, 2, otherID) 250 | } 251 | } 252 | 253 | func TestLoadFileWorksWithValidFileSQLite(t *testing.T) { 254 | t.Parallel() 255 | 256 | var testSQLiteDb = "/tmp/fixtures_testdb_load_file.sqlite" 257 | 258 | // Delete the test database 259 | os.Remove(testSQLiteDb) 260 | 261 | var ( 262 | db *sql.DB 263 | err error 264 | ) 265 | 266 | // Connect to an in-memory SQLite database 267 | db, err = sql.Open("sqlite3", testSQLiteDb) 268 | if err != nil { 269 | log.Fatal(err) 270 | } 271 | defer db.Close() 272 | 273 | // Create a test schema 274 | _, err = db.Exec(testSchemaSQLite) 275 | if err != nil { 276 | log.Fatal(err) 277 | } 278 | 279 | var count int 280 | // Check row counts to show no data 281 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 282 | assert.Equal(t, 0, count) 283 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 284 | assert.Equal(t, 0, count) 285 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 286 | assert.Equal(t, 0, count) 287 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 288 | assert.Equal(t, 0, count) 289 | 290 | // Let's load the fixture, since the database is empty, this should run inserts 291 | err = fixtures.LoadFile(fixtureFile, db, "sqlite") 292 | 293 | // Error should be nil 294 | assert.Nil(t, err) 295 | 296 | var ( 297 | rows *sql.Rows 298 | id int 299 | stringField string 300 | booleanField bool 301 | createdAt *time.Time 302 | updatedAt *time.Time 303 | ) 304 | 305 | // Check row counts 306 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 307 | assert.Equal(t, 1, count) 308 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 309 | assert.Equal(t, 0, count) 310 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 311 | assert.Equal(t, 0, count) 312 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 313 | assert.Equal(t, 0, count) 314 | 315 | // Check correct data has been loaded into some_table 316 | rows, err = db.Query("SELECT id, string_field, boolean_field, " + 317 | "created_at, updated_at FROM some_table") 318 | if err != nil { 319 | log.Fatal(err) 320 | } 321 | 322 | defer rows.Close() 323 | for rows.Next() { 324 | if err := rows.Scan( 325 | &id, 326 | &stringField, 327 | &booleanField, 328 | &createdAt, 329 | &updatedAt, 330 | ); err != nil { 331 | log.Fatal(err) 332 | } 333 | 334 | if err := rows.Err(); err != nil { 335 | log.Fatal(err) 336 | } 337 | 338 | assert.Equal(t, 1, id) 339 | assert.Equal(t, "foobar", stringField) 340 | assert.Equal(t, true, booleanField) 341 | assert.NotNil(t, createdAt) 342 | assert.Nil(t, updatedAt) 343 | } 344 | 345 | // Let's reload the fixture, this should run updates 346 | err = fixtures.LoadFile(fixtureFile, db, "sqlite") 347 | 348 | // Error should be nil 349 | assert.Nil(t, err) 350 | 351 | // Check row counts, should be unchanged 352 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 353 | assert.Equal(t, 1, count) 354 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 355 | assert.Equal(t, 0, count) 356 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 357 | assert.Equal(t, 0, count) 358 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 359 | assert.Equal(t, 0, count) 360 | } 361 | 362 | func TestLoadFileFailsWithMissingFileSQLite(t *testing.T) { 363 | t.Parallel() 364 | 365 | var testSQLiteDb = "/tmp/fixtures_testdb_load_file_missing_file.sqlite" 366 | 367 | // Delete the test database 368 | os.Remove(testSQLiteDb) 369 | 370 | var ( 371 | db *sql.DB 372 | err error 373 | ) 374 | 375 | // Connect to an in-memory SQLite database 376 | db, err = sql.Open("sqlite3", testSQLiteDb) 377 | if err != nil { 378 | log.Fatal(err) 379 | } 380 | defer db.Close() 381 | 382 | // Create a test schema 383 | _, err = db.Exec(testSchemaSQLite) 384 | if err != nil { 385 | log.Fatal(err) 386 | } 387 | 388 | // Let's load the fixture, since the database is empty, this should run inserts 389 | err = fixtures.LoadFile("bad_filename.yml", db, "sqlite") 390 | 391 | // Error should be nil 392 | assert.EqualError(t, err, "Error loading file bad_filename.yml: open bad_filename.yml: no such file or directory") 393 | } 394 | 395 | func TestLoadFilesWorksWithValidFilesSQLite(t *testing.T) { 396 | t.Parallel() 397 | 398 | var testSQLiteDb = "/tmp/fixtures_testdb_load_files.sqlite" 399 | 400 | // Delete the test database 401 | os.Remove(testSQLiteDb) 402 | 403 | var ( 404 | db *sql.DB 405 | err error 406 | ) 407 | 408 | // Connect to an in-memory SQLite database 409 | db, err = sql.Open("sqlite3", testSQLiteDb) 410 | if err != nil { 411 | log.Fatal(err) 412 | } 413 | defer db.Close() 414 | 415 | // Create a test schema 416 | _, err = db.Exec(testSchemaSQLite) 417 | if err != nil { 418 | log.Fatal(err) 419 | } 420 | 421 | var count int 422 | 423 | // Check rows are empty first 424 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 425 | assert.Equal(t, 0, count) 426 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 427 | assert.Equal(t, 0, count) 428 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 429 | assert.Equal(t, 0, count) 430 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 431 | assert.Equal(t, 0, count) 432 | 433 | // Let's load the fixture, since the database is empty, this should run inserts 434 | err = fixtures.LoadFiles(fixtureFiles, db, "sqlite") 435 | 436 | // Error should be nil 437 | assert.Nil(t, err) 438 | 439 | // Check row counts 440 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 441 | assert.Equal(t, 1, count) 442 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 443 | assert.Equal(t, 1, count) 444 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 445 | assert.Equal(t, 1, count) 446 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 447 | assert.Equal(t, 1, count) 448 | 449 | // Let's reload the fixtures, this should run updates 450 | err = fixtures.LoadFiles(fixtureFiles, db, "sqlite") 451 | 452 | // Error should be nil 453 | assert.Nil(t, err) 454 | 455 | // Check row counts, should be unchanged 456 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 457 | assert.Equal(t, 1, count) 458 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 459 | assert.Equal(t, 1, count) 460 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 461 | assert.Equal(t, 1, count) 462 | } 463 | 464 | func TestLoadFilesFailsWithABadFileSQLite(t *testing.T) { 465 | t.Parallel() 466 | 467 | var testSQLiteDb = "/tmp/fixtures_testdb_load_files_bad_file.sqlite" 468 | 469 | // Delete the test database 470 | os.Remove(testSQLiteDb) 471 | 472 | var ( 473 | db *sql.DB 474 | err error 475 | ) 476 | 477 | // Connect to an in-memory SQLite database 478 | db, err = sql.Open("sqlite3", testSQLiteDb) 479 | if err != nil { 480 | log.Fatal(err) 481 | } 482 | defer db.Close() 483 | 484 | // Create a test schema 485 | _, err = db.Exec(testSchemaSQLite) 486 | if err != nil { 487 | log.Fatal(err) 488 | } 489 | 490 | var count int 491 | 492 | // Check rows are empty first 493 | db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count) 494 | assert.Equal(t, 0, count) 495 | db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count) 496 | assert.Equal(t, 0, count) 497 | db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count) 498 | assert.Equal(t, 0, count) 499 | db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count) 500 | assert.Equal(t, 0, count) 501 | 502 | var badList = []string{ 503 | fixtureFile, 504 | "bad_file", 505 | } 506 | 507 | // Let's load the fixture, since the database is empty, this should run inserts 508 | err = fixtures.LoadFiles(badList, db, "sqlite") 509 | 510 | // Error should be nil 511 | assert.EqualError(t, err, "Error loading file bad_file: open bad_file: no such file or directory") 512 | } 513 | -------------------------------------------------------------------------------- /load_test.go: -------------------------------------------------------------------------------- 1 | package fixtures_test 2 | 3 | var testSchemaSQLite = ` 4 | CREATE TABLE some_table( 5 | id INT PRIMARY KEY NOT NULL, 6 | string_field VARCHAR(50) NOT NULL, 7 | boolean_field BOOL NOT NULL, 8 | created_at DATETIME, 9 | updated_at DATETIME 10 | ); 11 | 12 | CREATE TABLE other_table( 13 | id INT PRIMARY KEY NOT NULL, 14 | int_field INT NOT NULL, 15 | boolean_field BOOL NOT NULL, 16 | created_at DATETIME, 17 | updated_at DATETIME 18 | ); 19 | 20 | CREATE TABLE join_table( 21 | some_id INT NOT NULL, 22 | other_id INT NOT NULL, 23 | PRIMARY KEY(some_id, other_id) 24 | ); 25 | 26 | CREATE TABLE string_key_table( 27 | id VARCHAR(50) PRIMARY KEY NOT NULL, 28 | created_at DATETIME, 29 | updated_at DATETIME 30 | ); 31 | ` 32 | 33 | var testSchemaPostgres = ` 34 | CREATE TABLE some_table( 35 | id INT PRIMARY KEY NOT NULL, 36 | string_field VARCHAR(50) NOT NULL, 37 | boolean_field BOOL NOT NULL, 38 | created_at TIMESTAMP WITH TIME ZONE, 39 | updated_at TIMESTAMP WITH TIME ZONE 40 | ); 41 | 42 | CREATE TABLE other_table( 43 | id INT PRIMARY KEY NOT NULL, 44 | int_field INT NOT NULL, 45 | boolean_field BOOL NOT NULL, 46 | created_at TIMESTAMP WITH TIME ZONE, 47 | updated_at TIMESTAMP WITH TIME ZONE 48 | ); 49 | 50 | CREATE TABLE join_table( 51 | some_id INT NOT NULL, 52 | other_id INT NOT NULL, 53 | PRIMARY KEY(some_id, other_id) 54 | ); 55 | 56 | CREATE TABLE string_key_table( 57 | id VARCHAR(50) PRIMARY KEY NOT NULL, 58 | created_at TIMESTAMP WITH TIME ZONE, 59 | updated_at TIMESTAMP WITH TIME ZONE 60 | ); 61 | ` 62 | 63 | var testData = ` 64 | --- 65 | - table: 'some_table' 66 | pk: 67 | id: 1 68 | fields: 69 | string_field: 'foobar' 70 | boolean_field: true 71 | created_at: 'ON_INSERT_NOW()' 72 | updated_at: 'ON_UPDATE_NOW()' 73 | - table: 'other_table' 74 | pk: 75 | id: 2 76 | fields: 77 | int_field: 123 78 | boolean_field: false 79 | created_at: 'ON_INSERT_NOW()' 80 | updated_at: 'ON_UPDATE_NOW()' 81 | - table: 'join_table' 82 | pk: 83 | some_id: 1 84 | other_id: 2 85 | - table: 'string_key_table' 86 | pk: 87 | id: 'new_id' 88 | fields: 89 | created_at: 'ON_INSERT_NOW()' 90 | updated_at: 'ON_UPDATE_NOW()' 91 | ` 92 | 93 | var ( 94 | fixtureFile = "fixtures/test_fixtures1.yml" 95 | fixtureFiles = []string{ 96 | "fixtures/test_fixtures1.yml", 97 | "fixtures/test_fixtures2.yml", 98 | } 99 | ) 100 | -------------------------------------------------------------------------------- /package.go: -------------------------------------------------------------------------------- 1 | // Package fixtures implements Django like fixtures for Postgres and MySQL 2 | package fixtures 3 | -------------------------------------------------------------------------------- /row.go: -------------------------------------------------------------------------------- 1 | package fixtures 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | "strings" 7 | "time" 8 | ) 9 | 10 | const ( 11 | onInsertNow = "ON_INSERT_NOW()" 12 | onUpdateNow = "ON_UPDATE_NOW()" 13 | postgresDriver = "postgres" 14 | ) 15 | 16 | // Row represents a single database row 17 | type Row struct { 18 | Table string 19 | PK map[string]interface{} 20 | Fields map[string]interface{} 21 | insertColumnLength int 22 | updateColumnLength int 23 | pkColumns []string 24 | pkValues []interface{} 25 | rawPkValues []interface{} 26 | insertColumns []string 27 | updateColumns []string 28 | insertValues []interface{} 29 | updateValues []interface{} 30 | rawInsertValues []interface{} 31 | rawUpdateValues []interface{} 32 | } 33 | 34 | // Init loads internal struct variables 35 | func (row *Row) Init() { 36 | // Initial values 37 | row.insertColumnLength = len(row.PK) + len(row.Fields) 38 | row.updateColumnLength = len(row.PK) + len(row.Fields) 39 | row.pkColumns = make([]string, 0) 40 | row.pkValues = make([]interface{}, 0) 41 | row.insertColumns = make([]string, 0) 42 | row.updateColumns = make([]string, 0) 43 | row.insertValues = make([]interface{}, 0) 44 | row.updateValues = make([]interface{}, 0) 45 | row.rawInsertValues = make([]interface{}, 0) 46 | row.rawUpdateValues = make([]interface{}, 0) 47 | // Get and sort map keys 48 | var i int 49 | pkKeys := make([]string, len(row.PK)) 50 | i = 0 51 | for pkKey := range row.PK { 52 | pkKeys[i] = pkKey 53 | i++ 54 | } 55 | sort.Strings(pkKeys) 56 | fieldKeys := make([]string, len(row.Fields)) 57 | i = 0 58 | for fieldKey := range row.Fields { 59 | fieldKeys[i] = fieldKey 60 | i++ 61 | } 62 | sort.Strings(fieldKeys) 63 | 64 | // Primary keys 65 | for _, pkKey := range pkKeys { 66 | row.pkColumns = append(row.pkColumns, pkKey) 67 | row.appendValue("pk", row.PK[pkKey]) 68 | row.insertColumns = append(row.insertColumns, pkKey) 69 | row.updateColumns = append(row.updateColumns, pkKey) 70 | row.appendValue("insert", row.PK[pkKey]) 71 | row.appendValue("update", row.PK[pkKey]) 72 | } 73 | 74 | // Rest of the fields 75 | for _, fieldKey := range fieldKeys { 76 | sv, ok := row.Fields[fieldKey].(string) 77 | if ok && sv == onInsertNow { 78 | row.insertColumns = append(row.insertColumns, fieldKey) 79 | row.appendValue("insert", time.Now()) 80 | row.updateColumnLength-- 81 | continue 82 | } 83 | if ok && sv == onUpdateNow { 84 | row.updateColumns = append(row.updateColumns, fieldKey) 85 | row.appendValue("update", time.Now()) 86 | row.insertColumnLength-- 87 | continue 88 | } 89 | row.insertColumns = append(row.insertColumns, fieldKey) 90 | row.updateColumns = append(row.updateColumns, fieldKey) 91 | row.appendValue("insert", row.Fields[fieldKey]) 92 | row.appendValue("update", row.Fields[fieldKey]) 93 | } 94 | } 95 | 96 | // GetInsertColumnsLength returns number of columns for INSERT query 97 | func (row *Row) GetInsertColumnsLength() int { 98 | return row.insertColumnLength 99 | } 100 | 101 | // GetInsertValuesLength returns number of values for INSERT query 102 | func (row *Row) GetInsertValuesLength() int { 103 | return len(row.insertValues) 104 | } 105 | 106 | // GetUpdateColumnsLength returns number of columns for UDPATE query 107 | func (row *Row) GetUpdateColumnsLength() int { 108 | return row.updateColumnLength 109 | } 110 | 111 | // GetUpdateValuesLength returns number of values for UDPATE query 112 | func (row *Row) GetUpdateValuesLength() int { 113 | return len(row.updateValues) 114 | } 115 | 116 | // GetInsertColumns returns a slice of column names for INSERT query 117 | func (row *Row) GetInsertColumns() []string { 118 | escapedColumns := make([]string, len(row.insertColumns)) 119 | for i, insertColumn := range row.insertColumns { 120 | escapedColumns[i] = fmt.Sprintf("\"%s\"", insertColumn) 121 | } 122 | return escapedColumns 123 | } 124 | 125 | // GetUpdateColumns returns a slice of column names for UPDATE query 126 | func (row *Row) GetUpdateColumns() []string { 127 | escapedColumns := make([]string, len(row.updateColumns)) 128 | for i, updateColumn := range row.updateColumns { 129 | escapedColumns[i] = fmt.Sprintf("\"%s\"", updateColumn) 130 | } 131 | return escapedColumns 132 | } 133 | 134 | // GetInsertValues returns a slice of values for INSERT query 135 | func (row *Row) GetInsertValues() []interface{} { 136 | return row.insertValues 137 | } 138 | 139 | // GetUpdateValues returns a slice of values for UPDATE query 140 | func (row *Row) GetUpdateValues() []interface{} { 141 | return row.updateValues 142 | } 143 | 144 | // GetInsertPlaceholders returns a slice of placeholders for INSERT query 145 | func (row *Row) GetInsertPlaceholders(driver string) []string { 146 | placeholders := make([]string, row.GetInsertColumnsLength()) 147 | for i, j := 0, 0; i < row.GetInsertColumnsLength(); i++ { 148 | val := row.rawInsertValues[i] 149 | switch v := val.(type) { 150 | case string: 151 | if strings.HasPrefix(v, "RAW=") { 152 | placeholders[i] = strings.TrimPrefix(v, "RAW=") 153 | continue 154 | } 155 | } 156 | if driver == postgresDriver { 157 | placeholders[i] = fmt.Sprintf("$%d", j+1) 158 | j++ 159 | } else { 160 | placeholders[i] = "?" 161 | } 162 | } 163 | return placeholders 164 | } 165 | 166 | // GetUpdatePlaceholders returns a slice of placeholders for UPDATE query 167 | func (row *Row) GetUpdatePlaceholders(driver string) []string { 168 | placeholders := make([]string, row.GetUpdateColumnsLength()) 169 | j := 0 170 | for i, c := range row.GetUpdateColumns() { 171 | val := row.rawUpdateValues[i] 172 | switch v := val.(type) { 173 | case string: 174 | if strings.HasPrefix(v, "RAW=") { 175 | placeholders[i] = fmt.Sprintf("%s = %s", c, strings.TrimPrefix(v, "RAW=")) 176 | continue 177 | } 178 | } 179 | if driver == postgresDriver { 180 | placeholders[i] = fmt.Sprintf("%s = $%d", c, j+1) 181 | j++ 182 | } else { 183 | placeholders[i] = fmt.Sprintf("%s = ?", c) 184 | } 185 | } 186 | return placeholders 187 | } 188 | 189 | // GetWhere returns a where condition based on primary key with placeholders 190 | func (row *Row) GetWhere(driver string, i int) string { 191 | wheres := make([]string, len(row.PK)) 192 | start, j := i, i 193 | for _, c := range row.pkColumns { 194 | val := row.rawPkValues[i-start] 195 | i++ 196 | switch v := val.(type) { 197 | case string: 198 | if strings.HasPrefix(v, "RAW=") { 199 | wheres[i-1-start] = fmt.Sprintf("%s = %s", c, strings.TrimPrefix(v, "RAW=")) 200 | 201 | continue 202 | } 203 | } 204 | if driver == postgresDriver { 205 | wheres[i-1-start] = fmt.Sprintf("%s = $%d", c, j+1) 206 | j++ 207 | } else { 208 | wheres[i-1-start] = fmt.Sprintf("%s = ?", c) 209 | } 210 | 211 | } 212 | return strings.Join(wheres, " AND ") 213 | } 214 | 215 | // GetPKValues returns a slice of primary key values 216 | func (row *Row) GetPKValues() []interface{} { 217 | return row.pkValues 218 | } 219 | 220 | func (row *Row) appendValue(queryType string, val interface{}) { 221 | sv, ok := val.(string) 222 | if !ok || !strings.HasPrefix(sv, "RAW=") { 223 | switch queryType { 224 | case "insert": 225 | row.insertValues = append(row.insertValues, val) 226 | case "update": 227 | row.updateValues = append(row.updateValues, val) 228 | case "pk": 229 | row.pkValues = append(row.pkValues, val) 230 | } 231 | } 232 | switch queryType { 233 | case "insert": 234 | row.rawInsertValues = append(row.rawInsertValues, val) 235 | case "update": 236 | row.rawUpdateValues = append(row.rawUpdateValues, val) 237 | case "pk": 238 | row.rawPkValues = append(row.rawPkValues, val) 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /row_test.go: -------------------------------------------------------------------------------- 1 | package fixtures_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/RichardKnop/go-fixtures" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func TestRow(t *testing.T) { 11 | t.Parallel() 12 | 13 | // Create a test Row instance 14 | row := &fixtures.Row{ 15 | Table: "some_table", 16 | PK: map[string]interface{}{ 17 | "some_id": interface{}(1), 18 | "other_id": interface{}(2), 19 | }, 20 | Fields: map[string]interface{}{ 21 | "string_field": interface{}("foobar"), 22 | "boolean_field": interface{}(true), 23 | "created_at": interface{}("ON_INSERT_NOW()"), 24 | "updated_at": interface{}("ON_UPDATE_NOW()"), 25 | }, 26 | } 27 | 28 | // Run the init method to populate internal variables 29 | row.Init() 30 | 31 | var ( 32 | expectedString string 33 | expectedStrings []string 34 | expectedInterfaces []interface{} 35 | ) 36 | 37 | // Test insert and update column lengths 38 | assert.Equal(t, 5, row.GetInsertColumnsLength()) 39 | assert.Equal(t, 5, row.GetUpdateColumnsLength()) 40 | 41 | // Test insert and update columns 42 | expectedStrings = []string{"\"other_id\"", "\"some_id\"", 43 | "\"boolean_field\"", "\"created_at\"", "\"string_field\""} 44 | assert.Equal(t, expectedStrings, row.GetInsertColumns()) 45 | expectedStrings = []string{"\"other_id\"", "\"some_id\"", 46 | "\"boolean_field\"", "\"string_field\"", "\"updated_at\""} 47 | assert.Equal(t, expectedStrings, row.GetUpdateColumns()) 48 | 49 | // Test postgres placeholders ($1, $2 and so on) 50 | expectedStrings = []string{"$1", "$2", "$3", "$4", "$5"} 51 | assert.Equal(t, expectedStrings, row.GetInsertPlaceholders("postgres")) 52 | expectedStrings = []string{"\"other_id\" = $1", "\"some_id\" = $2", 53 | "\"boolean_field\" = $3", "\"string_field\" = $4", "\"updated_at\" = $5"} 54 | assert.Equal(t, expectedStrings, row.GetUpdatePlaceholders("postgres")) 55 | 56 | // Test non postgres placeholders (?) 57 | expectedStrings = []string{"?", "?", "?", "?", "?"} 58 | assert.Equal(t, expectedStrings, row.GetInsertPlaceholders("sqlite")) 59 | expectedStrings = []string{"\"other_id\" = ?", "\"some_id\" = ?", 60 | "\"boolean_field\" = ?", "\"string_field\" = ?", "\"updated_at\" = ?"} 61 | assert.Equal(t, expectedStrings, row.GetUpdatePlaceholders("sqlite")) 62 | 63 | // Test where clause 64 | expectedString = "other_id = $3 AND some_id = $4" 65 | assert.Equal(t, expectedString, row.GetWhere("postgres", 2)) 66 | 67 | // Test primary key values 68 | expectedInterfaces = []interface{}{interface{}(2), interface{}(1)} 69 | assert.Equal(t, expectedInterfaces, row.GetPKValues()) 70 | } 71 | --------------------------------------------------------------------------------