Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit 6bc9b24

Browse files
Merge pull request #6 from Storytel/readme
chore: update readme
2 parents 57845d2 + c8bf828 commit 6bc9b24

File tree

1 file changed

+105
-3
lines changed

1 file changed

+105
-3
lines changed

README.md

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,109 @@
11
# go-mysql-seed
22

33
Utility for seeding a MySQL database with Go.
4-
Useful for testing
4+
Useful during testing.
55

6-
## Coding
7-
[Getting started with Golang at Storytel](https://storytel.atlassian.net/wiki/spaces/TECH/pages/224690179/Golang)
6+
This package supports 2 types of MySQL seeding, both requires a path to a `.sql` file.
7+
8+
1. Seeding via `MySQL Command Line Tool`:
9+
Initialize a import using your locally installed MySQL CLT.
10+
11+
2. Seeding via `*sql.DB` struct: Start an import of the specified file using a `*sql.DB` struct.
12+
13+
The `.sql` file should contain one or more valid MySQL statements. To be able to execute multiple statements at a time, each statement must end with a `;`. If you are using option `2` above the SQL connection must use `multiStatements=true` to be able to run more than one statement at a time.
14+
15+
## Installation
16+
17+
Install the package with:
18+
19+
```
20+
go get github.com/Storytel/go-mysql-seed
21+
```
22+
23+
Once installed import it to your code:
24+
25+
```
26+
import mysqlseed "github.com/Storytel/go-mysql-seed"
27+
```
28+
29+
## Examples
30+
31+
This package is especially useful for testing. `go-mysql-seed` allows you to seed new database instances.
32+
33+
**Note:** The path to the `.sql` file is recommended to be absolute. The following snippet can be modified and used if you are uncertain.
34+
35+
```
36+
fmt.Sprintf("%s/src/github.com/Storytel/go-example-service/%s.sql", filepath.ToSlash(os.Getenv("GOPATH")), seedFileName)
37+
```
38+
39+
Below is a typical and simple example using `go-docker-initiator` in a test using the `*sql.DB` struct.
40+
41+
```
42+
package example_test
43+
44+
import (
45+
"log"
46+
"testing"
47+
48+
mysqlseed "github.com/Storytel/go-mysql-seed"
49+
"github.com/stretchr/testify/assert"
50+
)
51+
52+
func TestDatabaseIntegration(t *testing.T) {
53+
// Establish a database connection to the exposed environment variables
54+
db, err := InitAndCreateDatabase()
55+
assert.NoError(t, err)
56+
defer db.Close()
57+
58+
// Run DB seeds
59+
err = mysqlseed.ApplySeedWithDB(db, "/path/to/my-seed-file.sql")
60+
assert.NoError(t, err)
61+
62+
// Setup your service and inject the database
63+
exampleService := ExampleService{
64+
Db: db,
65+
}
66+
67+
// Test your integration
68+
_, err = exmapleService.Create()
69+
assert.NoError(t, err)
70+
}
71+
```
72+
73+
You can also apply a seed with the `MySQL Command Line Tool`.
74+
75+
```
76+
package example_test
77+
78+
import (
79+
"log"
80+
"testing"
81+
82+
mysqlseed "github.com/Storytel/go-mysql-seed"
83+
"github.com/stretchr/testify/assert"
84+
)
85+
86+
func TestDatabaseIntegration(t *testing.T) {
87+
// Establish a database connection to the exposed environment variables
88+
db, err := InitAndCreateDatabase()
89+
assert.NoError(t, err)
90+
defer db.Close()
91+
92+
// Run DB seeds
93+
err = mysqlseed.ApplySeedWithCmd("127.0.0.1:3306", "root", "password", "testdb", "/path/to/my-seed-file.sql")
94+
assert.NoError(t, err)
95+
96+
// Setup your service and inject the database
97+
exampleService := ExampleService{
98+
Db: db,
99+
}
100+
101+
// Test your integration
102+
_, err = exmapleService.Create()
103+
assert.NoError(t, err)
104+
}
105+
```
106+
107+
## Storytel Go
108+
109+
https://github.com/Storytel/go-docker-initiator - Utility for starting docker containers from Go.

0 commit comments

Comments
 (0)