MCPcopy
hub / github.com/pocketbase/pocketbase / SyncRecordTableSchema

Method SyncRecordTableSchema

core/collection_record_table_sync.go:21–153  ·  view source on GitHub ↗

SyncRecordTableSchema compares the two provided collections and applies the necessary related record table changes. If oldCollection is null, then only newCollection is used to create the record table. This method is automatically invoked as part of a collection create/update/delete operation.

(newCollection *Collection, oldCollection *Collection)

Source from the content-addressed store, hash-verified

19//
20// This method is automatically invoked as part of a collection create/update/delete operation.
21func (app *BaseApp) SyncRecordTableSchema(newCollection *Collection, oldCollection *Collection) error {
22 if newCollection.IsView() {
23 return nil // nothing to sync since views don't have records table
24 }
25
26 txErr := app.RunInTransaction(func(txApp App) error {
27 // create
28 // -----------------------------------------------------------
29 if oldCollection == nil || !app.HasTable(oldCollection.Name) {
30 tableName := newCollection.Name
31
32 fields := newCollection.Fields
33
34 cols := make(map[string]string, len(fields))
35
36 // add fields definition
37 for _, field := range fields {
38 cols[field.GetName()] = field.ColumnType(app)
39 }
40
41 // create table
42 if _, err := txApp.DB().CreateTable(tableName, cols).Execute(); err != nil {
43 return err
44 }
45
46 return createCollectionIndexes(txApp, newCollection)
47 }
48
49 // update
50 // -----------------------------------------------------------
51 oldTableName := oldCollection.Name
52 newTableName := newCollection.Name
53 oldFields := oldCollection.Fields
54 newFields := newCollection.Fields
55
56 needTableRename := !strings.EqualFold(oldTableName, newTableName)
57
58 var needIndexesUpdate bool
59 if needTableRename ||
60 oldFields.String() != newFields.String() ||
61 oldCollection.Indexes.String() != newCollection.Indexes.String() {
62 needIndexesUpdate = true
63 }
64
65 if needIndexesUpdate {
66 // drop old indexes (if any)
67 if err := dropCollectionIndexes(txApp, oldCollection); err != nil {
68 return err
69 }
70 }
71
72 // check for renamed table
73 if needTableRename {
74 _, err := txApp.DB().RenameTable("{{"+oldTableName+"}}", "{{"+newTableName+"}}").Execute()
75 if err != nil {
76 return err
77 }
78 }

Callers

nothing calls this directly

Calls 15

RunInTransactionMethod · 0.95
HasTableMethod · 0.95
NonconcurrentDBMethod · 0.95
LoggerMethod · 0.95
PseudorandomStringFunction · 0.92
createCollectionIndexesFunction · 0.85
dropCollectionIndexesFunction · 0.85
IsViewMethod · 0.80
ExecuteMethod · 0.80
CreateTableMethod · 0.80
RenameTableMethod · 0.80

Tested by

no test coverage detected