MCPcopy Create free account
hub / github.com/daodst/chat / Open

Function Open

roomserver/storage/postgres/storage.go:40–88  ·  view source on GitHub ↗

Open a postgres database.

(base *base.BaseDendrite, dbProperties *config.DatabaseOptions, cache caching.RoomServerCaches)

Source from the content-addressed store, hash-verified

38
39// Open a postgres database.
40func Open(base *base.BaseDendrite, dbProperties *config.DatabaseOptions, cache caching.RoomServerCaches) (*Database, error) {
41 var d Database
42 var err error
43 db, writer, err := base.DatabaseConnection(dbProperties, sqlutil.NewDummyWriter())
44 if err != nil {
45 return nil, fmt.Errorf("sqlutil.Open: %w", err)
46 }
47
48 // Create the tables.
49 if err = d.create(db); err != nil {
50 return nil, err
51 }
52
53 // Special case, since this migration uses several tables, so it needs to
54 // be sure that all tables are created first.
55 // TODO: Remove when we are sure we are not having goose artefacts in the db
56 // This forces an error, which indicates the migration is already applied, since the
57 // column event_nid was removed from the table
58 var eventNID int
59 err = db.QueryRow("SELECT event_nid FROM roomserver_state_block LIMIT 1;").Scan(&eventNID)
60 if err == nil {
61 m := sqlutil.NewMigrator(db)
62 m.AddMigrations(sqlutil.Migration{
63 Version: "roomserver: state blocks refactor",
64 Up: deltas.UpStateBlocksRefactor,
65 })
66 if err = m.Up(base.Context()); err != nil {
67 return nil, err
68 }
69 } else {
70 switch e := err.(type) {
71 case *pq.Error:
72 // ignore undefined_column (42703) errors, as this is expected at this point
73 if e.Code != "42703" {
74 return nil, err
75 }
76 default:
77 return nil, err
78 }
79 }
80
81 // Then prepare the statements. Now that the migrations have run, any columns referred
82 // to in the database code should now exist.
83 if err = d.prepare(db, writer, cache); err != nil {
84 return nil, err
85 }
86
87 return &d, nil
88}
89
90func (d *Database) create(db *sql.DB) error {
91 if err := CreateEventStateKeysTable(db); err != nil {

Callers

nothing calls this directly

Calls 6

createMethod · 0.95
AddMigrationsMethod · 0.95
UpMethod · 0.95
prepareMethod · 0.95
DatabaseConnectionMethod · 0.80
ContextMethod · 0.80

Tested by

no test coverage detected