You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Readme.md
+91Lines changed: 91 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,3 +54,94 @@ go test ./...
54
54
```bash
55
55
go test -v -cover ./...
56
56
```
57
+
58
+
```markdown
59
+
# Database Migrations
60
+
61
+
The DefiFundr backend uses [goose](https://github.com/pressly/goose) for managing database migrations. Migrations are stored in the `migrations` directory and are written in SQL.
62
+
63
+
## Setting Up Migrations
64
+
65
+
1. Install goose:
66
+
```bash
67
+
go install github.com/pressly/goose/v3/cmd/goose@latest
68
+
```
69
+
70
+
2. Make sure you have PostgreSQL running and accessible with the connection details specified in your environment variables or `.env` file.
71
+
72
+
## Migration Commands
73
+
74
+
We provide helper scripts to manage migrations:
75
+
76
+
*`./scripts/migrate.sh` - Apply all pending migrations
77
+
*`./scripts/migrate_create.sh <migration_name>` - Create a new migration file
78
+
*`./scripts/migrate_down.sh` - Roll back the last migration
79
+
*`./scripts/migrate_status.sh` - Check the status of all migrations
80
+
*`./scripts/migrate_reset.sh` - Roll back all migrations and apply them again (use with caution!)
81
+
82
+
### Creating a New Migration
83
+
84
+
To create a new migration:
85
+
86
+
```bash
87
+
./scripts/migrate_create.sh add_new_feature
88
+
```
89
+
90
+
This will create a new file in the `migrations` directory with a timestamp prefix, like `20230809123456_add_new_feature.sql`.
91
+
92
+
Edit this file to add your SQL commands. Each migration file should have two sections:
93
+
94
+
```sql
95
+
-- +goose Up
96
+
-- SQL in this section is executed when the migration is applied
97
+
CREATETABLEexample (
98
+
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
99
+
name VARCHAR(255) NOT NULL
100
+
);
101
+
102
+
-- +goose Down
103
+
-- SQL in this section is executed when the migration is rolled back
104
+
DROPTABLE example;
105
+
```
106
+
107
+
### Running Migrations
108
+
109
+
To apply all pending migrations:
110
+
111
+
```bash
112
+
./scripts/migrate.sh
113
+
```
114
+
115
+
### Database Connection
116
+
117
+
By default, the migration scripts will use the `DATABASE_URL` environment variable. If this is not set, they will fall back to `postgres://postgres:postgres@localhost:5432/defifundr?sslmode=disable`.
118
+
119
+
You can set the environment variable before running the script:
Or you can update the default value in the scripts.
126
+
127
+
## Migration Best Practices
128
+
129
+
1.**Always include a Down migration**: This ensures you can roll back if something goes wrong.
130
+
2.**Keep migrations small and focused**: It's better to have multiple small migrations than one large one.
131
+
3.**Test migrations before deploying**: Run migrations on a test database to ensure they work correctly.
132
+
4.**Version control migrations**: All migrations should be committed to the repository.
133
+
5.**Never modify an existing migration file**: Once a migration has been applied to any environment, create a new migration instead of modifying the existing one.
134
+
135
+
## Troubleshooting
136
+
137
+
If you encounter issues with migrations:
138
+
139
+
1. Check the migration status with `./scripts/migrate_status.sh`
140
+
2. Ensure your database connection details are correct
141
+
3. Look for syntax errors in your SQL statements
142
+
4. Check if you have the necessary permissions on the database
143
+
144
+
For more complex issues, you might need to manually fix the goose migration table (`goose_db_version`).
145
+
```
146
+
147
+
These scripts and documentation provide a comprehensive system for managing your database migrations using goose. Make sure to make the scripts executable after creating them (`chmod +x scripts/*.sh`).
0 commit comments