-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisttable.go
145 lines (125 loc) · 3.85 KB
/
disttable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"database/sql"
"fmt"
"log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
func DistributeTable(db *sql.DB, coll *mongo.Collection) (err error) {
schema := coll.Database().Name()
collName := coll.Name()
// need to insert something to the collection to get it to exist
doc := struct {
ID primitive.ObjectID `bson:"_id"`
}{
ID: primitive.NewObjectID(),
}
_, err = coll.InsertOne(context.TODO(), doc)
if err != nil {
return err
}
// clear out the record created above since the collection now should exist
filter := bson.M{"_id": doc.ID}
_, err = coll.DeleteOne(context.TODO(), filter)
if err != nil {
log.Printf("%v; fail to delete record created for distribution; id: %v", err, doc.ID)
}
// get table and index names
var table, index string
qTmplSelectTableIndex := `
SELECT _jsonb->>'table'
, _jsonb->'indexes'->0->>'pgindex'
FROM %s._ferretdb_database_metadata
WHERE _jsonb->>'_id' = $1
`
err = db.QueryRow(fmt.Sprintf(qTmplSelectTableIndex, schema), collName).Scan(&table, &index)
if err != nil {
return err
}
log.Println("got table and index name", "table", table, "index", index)
qTmplDropIndex := `DROP INDEX %s.%s`
_, err = db.Exec(fmt.Sprintf(qTmplDropIndex, schema, index))
if err != nil {
return err
}
log.Println("old index dropped")
qTmplAlterTableName := `ALTER TABLE %s.%s RENAME TO %s_back`
_, err = db.Exec(fmt.Sprintf(qTmplAlterTableName, schema, table, table))
if err != nil {
return err
}
log.Println("table rename to ..._back")
qTmplAddIDColumn := `ALTER TABLE %s.%s_back ADD COLUMN _id TEXT`
_, err = db.Exec(fmt.Sprintf(qTmplAddIDColumn, schema, table))
if err != nil {
return err
}
log.Println("add _id field")
qTmplAddNewPK := `ALTER TABLE %s.%s_back ADD PRIMARY KEY (_id)`
_, err = db.Exec(fmt.Sprintf(qTmplAddNewPK, schema, table))
if err != nil {
return err
}
log.Println("add new PK to match with dist key")
qDistributeBackTable := `SELECT create_distributed_table($1, '_id')`
_, err = db.Exec(qDistributeBackTable, schema+"."+table+"_back")
if err != nil {
return err
}
log.Println("make ..._back table a distributed table")
qTmplCreateBasicView := `CREATE OR REPLACE VIEW %s.%s AS (SELECT * FROM %s.%s_back)`
_, err = db.Exec(fmt.Sprintf(qTmplCreateBasicView, schema, table, schema, table))
if err != nil {
return err
}
log.Println("create view with orignal table name")
qTmplCreateTriggerFunction := `
CREATE OR REPLACE FUNCTION %s.func_tg_insert__%s() RETURNS TRIGGER AS $view_insert_row$
BEGIN
INSERT INTO %s.%s_back (_id, _jsonb)
SELECT NEW._jsonb->>'_id', NEW._jsonb;
RETURN NULL;
END;
$view_insert_row$ LANGUAGE plpgsql;
`
_, err = db.Exec(fmt.Sprintf(qTmplCreateTriggerFunction, schema, table, schema, table))
if err != nil {
return err
}
qTmplCreateTrigger := `
CREATE OR REPLACE TRIGGER tg_insert__%s
INSTEAD OF INSERT ON %s.%s
FOR EACH ROW EXECUTE FUNCTION %s.func_tg_insert__%s();
`
_, err = db.Exec(fmt.Sprintf(qTmplCreateTrigger, table, schema, table, schema, table))
if err != nil {
return err
}
log.Println("create trigger to handle inserts")
return
}
func DropDistributeTable(db *sql.DB, coll *mongo.Collection) (err error) {
schema := coll.Database().Name()
collName := coll.Name()
var table string
qTmplSelectTableIndex := `
SELECT _jsonb->>'table'
FROM %s._ferretdb_database_metadata
WHERE _jsonb->>'_id' = $1
`
err = db.QueryRow(fmt.Sprintf(qTmplSelectTableIndex, schema), collName).Scan(&table)
if err != nil {
return err
}
log.Println("DropDistributeTable - got table name", "table", table)
qTmplDropTableCascade := `DROP TABLE %s.%s_back CASCADE`
_, err = db.Exec(fmt.Sprintf(qTmplDropTableCascade, schema, table))
if err != nil {
return err
}
log.Println("DropDistributeTable - drop table ..._back cascade")
return
}