|
| 1 | +package migrations |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "git.handmade.network/hmn/hmn/src/migration/types" |
| 8 | + "github.com/jackc/pgx/v5" |
| 9 | +) |
| 10 | + |
| 11 | +func init() { |
| 12 | + registerMigration(DeleteImageFile{}) |
| 13 | +} |
| 14 | + |
| 15 | +type DeleteImageFile struct{} |
| 16 | + |
| 17 | +func (m DeleteImageFile) Version() types.MigrationVersion { |
| 18 | + return types.MigrationVersion(time.Date(2026, 7, 25, 2, 48, 1, 0, time.UTC)) |
| 19 | +} |
| 20 | + |
| 21 | +func (m DeleteImageFile) Name() string { |
| 22 | + return "DeleteImageFile" |
| 23 | +} |
| 24 | + |
| 25 | +func (m DeleteImageFile) Description() string { |
| 26 | + return "Removes the imagefile table, replacing all uses with assets IDs" |
| 27 | +} |
| 28 | + |
| 29 | +func (m DeleteImageFile) Up(ctx context.Context, tx pgx.Tx) error { |
| 30 | + // NOTE(ben): Only project screenshots and podcast art uses image files. |
| 31 | + _, err := tx.Exec(ctx, |
| 32 | + ` |
| 33 | + ALTER TABLE project_screenshot |
| 34 | + ADD COLUMN asset_id UUID REFERENCES asset (id) ON DELETE CASCADE; |
| 35 | + ALTER TABLE podcast |
| 36 | + ADD COLUMN image_asset UUID REFERENCES asset (id) ON DELETE SET NULL; |
| 37 | +
|
| 38 | + UPDATE project_screenshot |
| 39 | + SET asset_id = image_file.asset_id |
| 40 | + FROM image_file |
| 41 | + WHERE project_screenshot.imagefile_id = image_file.id; |
| 42 | +
|
| 43 | + UPDATE podcast |
| 44 | + SET image_asset = image_file.asset_id |
| 45 | + FROM image_file |
| 46 | + WHERE podcast.image_id = image_file.id; |
| 47 | + `, |
| 48 | + ) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + _, err = tx.Exec(ctx, ` |
| 54 | + ALTER TABLE project_screenshot |
| 55 | + DROP COLUMN imagefile_id; |
| 56 | + ALTER TABLE podcast |
| 57 | + DROP COLUMN image_id; |
| 58 | + DROP TABLE image_file; |
| 59 | + `) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func (m DeleteImageFile) Down(ctx context.Context, tx pgx.Tx) error { |
| 68 | + _, err := tx.Exec(ctx, ` |
| 69 | + ALTER TABLE project_screenshot DROP COLUMN asset_id; |
| 70 | + ALTER TABLE podcast DROP COLUMN image_asset; |
| 71 | +
|
| 72 | + CREATE TABLE image_file ( |
| 73 | + id INTEGER NOT NULL PRIMARY KEY, |
| 74 | + file VARCHAR(255) NOT NULL, |
| 75 | + size INTEGER NOT NULL, |
| 76 | + sha1sum VARCHAR(40) NOT NULL, |
| 77 | + protected BOOLEAN NOT NULL, |
| 78 | + height INTEGER NOT NULL, |
| 79 | + width INTEGER NOT NULL, |
| 80 | + asset_id UUID REFERENCES asset (id) ON DELETE SET NULL |
| 81 | + ); |
| 82 | + CREATE SEQUENCE image_file_id_seq OWNED BY image_file.id; |
| 83 | + ALTER TABLE image_file ALTER COLUMN id SET DEFAULT nextval('image_file_id_seq'); |
| 84 | +
|
| 85 | + ALTER TABLE project_screenshot |
| 86 | + ADD COLUMN imagefile_id INTEGER REFERENCES image_file (id); |
| 87 | + ALTER TABLE podcast |
| 88 | + ADD COLUMN image_id INTEGER REFERENCES image_file (id); |
| 89 | +
|
| 90 | + CREATE INDEX ON project_screenshot (imagefile_id); |
| 91 | + ALTER TABLE project_screenshot |
| 92 | + ADD CONSTRAINT project_screenshot_project_id_imagefile_id_uniq UNIQUE (project_id, imagefile_id); |
| 93 | +
|
| 94 | + CREATE INDEX ON podcast (image_id); |
| 95 | + `) |
| 96 | + return err |
| 97 | +} |
0 commit comments