https://github.com/appcypher/pgseeder
A Seeder for Postgres Databases
https://github.com/appcypher/pgseeder
Last synced: about 1 year ago
JSON representation
A Seeder for Postgres Databases
- Host: GitHub
- URL: https://github.com/appcypher/pgseeder
- Owner: appcypher
- License: apache-2.0
- Created: 2021-04-27T13:42:38.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-07-02T12:49:50.000Z (almost 5 years ago)
- Last Synced: 2025-03-05T15:03:24.020Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 33.2 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
PGSEEDER
A seeder for adding and removing seeds from a Postgres database.
## đ INSTALLATION
- Clone pgseeder repo and cd into the created folder
```sh
git clone https://github.com/gigamono/pgseeder
```
```sh
cd pgseeder
```
- Build the binary.
```sh
go build cmd/pgseeder.go
```
- Add the binary to system path and run `pgseeder -h` command
## đ USAGE
### CREATING SEED FILES
- Pgseeder expects `.sql` filenames to be ordered based on their dependency.
The ordering format has to be a prefix and an integer.
For example:
```
01_users.sql
```
```
20210702_users.sql
```
- You can use Pgseeder to create a timestamped `.sql` file.
```sh
pgseeder --create "users"
```
This generates a timestamped `.sql` file similar to this: `20210620150637_users.sql`
> âšī¸ Ordering allows Pgseeder to avoid foreign key constraint errors when adding or removing all seeds.
### ADDING SEEDS TO THE DATABASE
- To add seeds from a `20210620150637_users.sql` file, you run following command:
```sh
pgseeder -c "postgres://postgres@localhost:5432/resourcedb?sslmode=disable" --add users
```
The `-c` is needed to connect to the database. It takes postgres connection string.
- You can also run all the `.sql` files in the current directory with the `--add-all` flag
```sh
pgseeder -c "postgres://postgres@localhost:5432/resourcedb?sslmode=disable" --add-all
```
- You can specify a directory where the `.sql` files live with `-d` flag
```sh
pgseeder -c "postgres://postgres@localhost:5432/resourcedb?sslmode=disable" --add-all -d "./seeds"
```
> âšī¸ To support removing seeds later, Pgseeder expects the `.sql` filename to correspond to seeded table's name.
> â If multiple tables are seeded in a `.sql` file, Pgseeder will only recognise the one that matches the filename.
### REMOVING SEEDS FROM THE DATABASE
- Here is how to remove seeds from `users` table.
```sh
pgseeder -c "postgres://postgres@localhost:5432/resourcedb?sslmode=disable" --remove users
```
- To remove all seeds.
```sh
pgseeder -c "postgres://postgres@localhost:5432/resourcedb?sslmode=disable" --remove-all
```
### SEED KEYS
- By default Pgseeder stores the `id` (if there is one) of the seed which it uses later to remove it.
- Sometimes the primary key is not `id`. You can specify the primary key with `-k` flag.
```sh
pgseeder -c "postgres://postgres@localhost:5432/resourcedb?sslmode=disable" --add users -k "user_id"
```
- And to uniquely identify seeds using composite keys, you separate them by colons, commas or spaces.
```sh
pgseeder -c "postgres://postgres@localhost:5432/resourcedb?sslmode=disable" --add x_memberships -k "user_id, workspace_id"
```
## đ CURRENT LIMITATIONS
- Only supports `.sql` files.
- Does not support seeding multiple tables in a single `.sql` file.