|
| 1 | +package migrations |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + migrate "github.com/xakep666/mongo-migrate" |
| 7 | + "go.mongodb.org/mongo-driver/bson" |
| 8 | + "go.mongodb.org/mongo-driver/mongo" |
| 9 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 10 | +) |
| 11 | + |
| 12 | +var migration_13 = migrate.Migration{ |
| 13 | + Version: 13, |
| 14 | + Up: func(db *mongo.Database) error { |
| 15 | + mod := mongo.IndexModel{ |
| 16 | + Keys: bson.D{{"uid", 1}}, |
| 17 | + Options: options.Index().SetName("uid").SetUnique(true), |
| 18 | + } |
| 19 | + _, err := db.Collection("devices").Indexes().CreateOne(context.TODO(), mod) |
| 20 | + if err != nil { |
| 21 | + return err |
| 22 | + } |
| 23 | + |
| 24 | + mod = mongo.IndexModel{ |
| 25 | + Keys: bson.D{{"last_seen", 1}}, |
| 26 | + Options: options.Index().SetName("last_seen").SetExpireAfterSeconds(30), |
| 27 | + } |
| 28 | + _, err = db.Collection("connected_devices").Indexes().CreateOne(context.TODO(), mod) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + |
| 33 | + mod = mongo.IndexModel{ |
| 34 | + Keys: bson.D{{"uid", 1}}, |
| 35 | + Options: options.Index().SetName("uid").SetUnique(false), |
| 36 | + } |
| 37 | + _, err = db.Collection("connected_devices").Indexes().CreateOne(context.TODO(), mod) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + mod = mongo.IndexModel{ |
| 43 | + Keys: bson.D{{"uid", 1}}, |
| 44 | + Options: options.Index().SetName("uid").SetUnique(true), |
| 45 | + } |
| 46 | + _, err = db.Collection("sessions").Indexes().CreateOne(context.TODO(), mod) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + mod = mongo.IndexModel{ |
| 52 | + Keys: bson.D{{"last_seen", 1}}, |
| 53 | + Options: options.Index().SetName("last_seen").SetExpireAfterSeconds(30), |
| 54 | + } |
| 55 | + _, err = db.Collection("active_sessions").Indexes().CreateOne(context.TODO(), mod) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + mod = mongo.IndexModel{ |
| 61 | + Keys: bson.D{{"uid", 1}}, |
| 62 | + Options: options.Index().SetName("uid").SetUnique(false), |
| 63 | + } |
| 64 | + _, err = db.Collection("active_sessions").Indexes().CreateOne(context.TODO(), mod) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + mod = mongo.IndexModel{ |
| 70 | + Keys: bson.D{{"username", 1}}, |
| 71 | + Options: options.Index().SetName("username").SetUnique(true), |
| 72 | + } |
| 73 | + _, err = db.Collection("users").Indexes().CreateOne(context.TODO(), mod) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + mod = mongo.IndexModel{ |
| 79 | + Keys: bson.D{{"tenant_id", 1}}, |
| 80 | + Options: options.Index().SetName("tenant_id").SetUnique(true), |
| 81 | + } |
| 82 | + _, err = db.Collection("users").Indexes().CreateOne(context.TODO(), mod) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + return nil |
| 87 | + }, |
| 88 | + Down: func(db *mongo.Database) error { |
| 89 | + if _, err := db.Collection("devices").Indexes().DropOne(context.TODO(), "uid"); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + if _, err := db.Collection("connected_devices").Indexes().DropOne(context.TODO(), "last_seen"); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + if _, err := db.Collection("connected_devices").Indexes().DropOne(context.TODO(), "uid"); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + if _, err := db.Collection("sessions").Indexes().DropOne(context.TODO(), "uid"); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + if _, err := db.Collection("active_sessions").Indexes().DropOne(context.TODO(), "last_seen"); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + if _, err := db.Collection("users").Indexes().DropOne(context.TODO(), "username"); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + _, err := db.Collection("users").Indexes().DropOne(context.TODO(), "tenant_id") |
| 108 | + return err |
| 109 | + }, |
| 110 | +} |
0 commit comments