go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/dbmodel/migrations.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package dbmodel 9 10 import ( 11 "go.charczuk.com/sdk/apputil" 12 "go.charczuk.com/sdk/db/dbgen" 13 "go.charczuk.com/sdk/db/migration" 14 ) 15 16 // Migrations returns the migration suite to bootstrap the database. 17 func Migrations(opts ...migration.SuiteOption) *migration.Suite { 18 return migration.NewWithGroups( 19 append( 20 initialSchema(), 21 migration.NewGroupWithStep( 22 migration.ColumnNotExists("node", "display_height"), 23 migration.Exec("ALTER TABLE node ADD COLUMN display_height double precision"), 24 ), 25 migration.NewGroupWithStep( 26 migration.ColumnNotExists("node", "display_width"), 27 migration.Exec("ALTER TABLE node ADD COLUMN display_width double precision"), 28 ), 29 migration.NewGroupWithStep( 30 migration.ColumnNotExists("graph", "updated_utc"), 31 migration.Exec("ALTER TABLE graph ADD COLUMN updated_utc timestamp"), 32 ), 33 migration.NewGroupWithStep( 34 migration.ColumnExists("graph", "is_active"), 35 migration.Exec("ALTER TABLE graph DROP COLUMN is_active"), 36 ), 37 )..., 38 ) 39 } 40 41 func initialSchema() []*migration.Group { 42 return append( 43 apputil.MigrationGroups(), // this includes e.g. `User` and `Session` 44 migration.NewGroupWithAction( 45 dbgen.TableFrom(Graph{}, 46 dbgen.ForeignKey(Graph{}, "user_id", apputil.User{}, "id"), 47 ), 48 ), 49 migration.NewGroupWithAction( 50 dbgen.TableFrom(GraphLogs{}, 51 dbgen.ForeignKey(GraphLogs{}, "graph_id", Graph{}, "id"), 52 dbgen.ForeignKey(GraphLogs{}, "user_id", apputil.User{}, "id"), 53 ), 54 ), 55 migration.NewGroupWithAction( 56 dbgen.TableFrom(Node{}, 57 dbgen.ForeignKey(Node{}, "user_id", apputil.User{}, "id"), 58 ), 59 ), 60 migration.NewGroupWithAction( 61 dbgen.TableFrom(GraphRecomputeHeap{}, 62 dbgen.ForeignKey(GraphRecomputeHeap{}, "graph_id", Graph{}, "id"), 63 dbgen.ForeignKey(GraphRecomputeHeap{}, "node_id", Node{}, "id"), 64 dbgen.ForeignKey(GraphRecomputeHeap{}, "user_id", apputil.User{}, "id"), 65 ), 66 ), 67 migration.NewGroupWithAction( 68 dbgen.TableFrom(NodeValue{}, 69 dbgen.ForeignKey(NodeValue{}, "user_id", apputil.User{}, "id"), 70 dbgen.ForeignKey(NodeValue{}, "node_id", Node{}, "id"), 71 ), 72 ), 73 migration.NewGroupWithAction( 74 dbgen.TableFrom(Edge{}, 75 dbgen.ForeignKey(Edge{}, "graph_id", Graph{}, "id"), 76 dbgen.ForeignKey(Edge{}, "user_id", apputil.User{}, "id"), 77 dbgen.ForeignKey(Edge{}, "parent_id", Node{}, "id"), 78 dbgen.ForeignKey(Edge{}, "child_id", Node{}, "id"), 79 ), 80 ), 81 ) 82 }