README.md (2014B)
1 # Migrations 2 3 ## How do I write a migration file? 4 5 [See here](https://bun.uptrace.dev/guide/migrations.html#migration-names) 6 7 As a template, take one of the existing migration files and modify it, or use the below code snippet: 8 9 ```go 10 /* 11 GoToSocial 12 Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org 13 14 This program is free software: you can redistribute it and/or modify 15 it under the terms of the GNU Affero General Public License as published by 16 the Free Software Foundation, either version 3 of the License, or 17 (at your option) any later version. 18 19 This program is distributed in the hope that it will be useful, 20 but WITHOUT ANY WARRANTY; without even the implied warranty of 21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 GNU Affero General Public License for more details. 23 24 You should have received a copy of the GNU Affero General Public License 25 along with this program. If not, see <http://www.gnu.org/licenses/>. 26 */ 27 28 package migrations 29 30 import ( 31 "context" 32 33 "github.com/uptrace/bun" 34 ) 35 36 func init() { 37 up := func(ctx context.Context, db *bun.DB) error { 38 return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { 39 // your logic here 40 return nil 41 }) 42 } 43 44 down := func(ctx context.Context, db *bun.DB) error { 45 return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { 46 // your logic here 47 return nil 48 }) 49 } 50 51 if err := Migrations.Register(up, down); err != nil { 52 panic(err) 53 } 54 } 55 ``` 56 57 ## File format 58 59 Bun requires a very specific format: 14 digits, then letters or underscores. 60 61 You can use the following bash command on your branch to generate a suitable migration filename. 62 63 ```bash 64 echo "$(date --utc +%Y%m%d%H%M%S | head -c 14)_$(git rev-parse --abbrev-ref HEAD).go" 65 ``` 66 67 ## Rules of thumb 68 69 1. **DON'T DROP TABLES**!!!!!!!! 70 2. Don't make something `NOT NULL` if it's likely to already contain `null` fields.