/// <summary> /// Generates data to test a RJOIN operation with a one to one relationship /// connecting to a composition of two other entities /// </summary> /// <returns></returns> public static RequiredDataContainer OneToOneComputedEntity() { Entity Person = new Entity("Person"); Person.AddAttribute("personId", true); Person.AddAttributes("name"); Entity Car = new Entity("Car"); Car.AddAttribute("carId", true); Car.AddAttributes("model", "year"); Entity Garage = new Entity("Garage"); Garage.AddAttribute("garageId", true); Garage.AddAttributes("name"); Relationship Drives = new Relationship("Drives"); Drives.AddRelationshipEnd(new RelationshipEnd(Person)); Drives.AddRelationshipEnd(new RelationshipEnd(Car)); Relationship Repaired = new Relationship("Repaired"); Repaired.AddRelationshipEnd(new RelationshipEnd(Car)); Repaired.AddRelationshipEnd(new RelationshipEnd(Garage)); Repaired.AddAttribute("repairedId", true); Repaired.AddAttributes("repaired"); ERModel Model = new ERModel("ERModel", new List <BaseERElement> { Person, Car, Garage, Drives, Repaired }); // Mongo Schema MongoDBCollection PersonCol = new MongoDBCollection("Person"); PersonCol.AddAttributes("_id", "name", "carId"); MongoDBCollection CarCol = new MongoDBCollection("Car"); CarCol.AddAttributes("_id", "model", "year"); MongoDBCollection GarageCol = new MongoDBCollection("Garage"); GarageCol.AddAttributes("_id", "name"); MongoDBCollection RepairedCol = new MongoDBCollection("Repaired"); RepairedCol.AddAttributes("_id", "carId", "garageId", "repaired"); MongoSchema Schema = new MongoSchema("Schema", new List <MongoDBCollection> { PersonCol, CarCol, GarageCol, RepairedCol }); // Map Rules MapRule PersonRule = new MapRule(Person, PersonCol); PersonRule.AddRule("personId", "_id"); PersonRule.AddRule("name", "name"); MapRule CarRule = new MapRule(Car, CarCol); CarRule.AddRule("carId", "_id"); CarRule.AddRule("model", "model"); CarRule.AddRule("year", "year"); MapRule GarageRule = new MapRule(Garage, GarageCol); GarageRule.AddRule("garageId", "_id"); GarageRule.AddRule("name", "name"); MapRule RepairedRule = new MapRule(Repaired, RepairedCol); RepairedRule.AddRule("repairedId", "_id"); RepairedRule.AddRule("repaired", "repaired"); MapRule CarPersonRule = new MapRule(Car, PersonCol, false); CarPersonRule.AddRule("carId", "carId"); MapRule CarRepairedRule = new MapRule(Car, RepairedCol, false); CarRepairedRule.AddRule("carId", "carId"); MapRule GarageRepairedRule = new MapRule(Garage, RepairedCol, false); GarageRepairedRule.AddRule("garageId", "garageId"); ModelMapping Map = new ModelMapping("Map", new List <MapRule> { PersonRule, CarRule, GarageRule, RepairedRule, CarPersonRule, CarRepairedRule, GarageRepairedRule }); return(new RequiredDataContainer(Model, Schema, Map)); }
/// <summary> /// Generates data to test a RJOIN operation with a one to one relationship /// connecting to a composition of three other entities /// </summary> /// <returns></returns> public static RequiredDataContainer OneToOneComputedEntityMultiple2() { Entity Person = new Entity("Person"); Person.AddAttributes("personId", "name", "carId", "insuranceId"); Entity Car = new Entity("Car"); Car.AddAttributes("carId", "model", "year"); Entity Garage = new Entity("Garage"); Garage.AddAttributes("garageId", "name"); Entity Supplier = new Entity("Supplier"); Supplier.AddAttributes("supplierId", "name"); Entity Insurance = new Entity("Insurance"); Insurance.AddAttributes("insuranceId", "name", "value"); Relationship Drives = new Relationship("Drives"); Drives.AddRelationshipEnd(new RelationshipEnd(Person)); Drives.AddRelationshipEnd(new RelationshipEnd(Car)); Relationship Repaired = new Relationship("Repaired"); Repaired.AddAttributes("repairedId", "carId", "garageId", "supplierId", "repaired"); Repaired.AddRelationshipEnd(new RelationshipEnd(Car)); Repaired.AddRelationshipEnd(new RelationshipEnd(Garage)); Repaired.AddRelationshipEnd(new RelationshipEnd(Supplier));; Relationship HasInsurance = new Relationship("HasInsurance"); HasInsurance.AddRelationshipEnd(new RelationshipEnd(Person)); HasInsurance.AddRelationshipEnd(new RelationshipEnd(Insurance)); ERModel Model = new ERModel("ERModel", new List <BaseERElement> { Person, Car, Garage, Drives, Repaired, Supplier, Insurance, HasInsurance }); // Mongo Schema MongoDBCollection PersonCol = new MongoDBCollection("Person"); PersonCol.AddAttributes("_id", "name", "carId", "insuranceId"); MongoDBCollection CarCol = new MongoDBCollection("Car"); CarCol.AddAttributes("_id", "model", "year"); MongoDBCollection GarageCol = new MongoDBCollection("Garage"); GarageCol.AddAttributes("_id", "name"); MongoDBCollection SupplierCol = new MongoDBCollection("Supplier"); SupplierCol.AddAttributes("_id", "name"); MongoDBCollection RepairedCol = new MongoDBCollection("Repaired"); RepairedCol.AddAttributes("_id", "carId", "garageId", "supplierId", "repaired"); MongoDBCollection InsuranceCol = new MongoDBCollection("Insurance"); InsuranceCol.AddAttributes("_id", "name", "value"); MongoSchema Schema = new MongoSchema("Schema", new List <MongoDBCollection> { PersonCol, CarCol, GarageCol, RepairedCol, SupplierCol, InsuranceCol }); // Map Rules MapRule PersonRule = new MapRule(Person, PersonCol); PersonRule.AddRule("personId", "_id"); PersonRule.AddRule("name", "name"); PersonRule.AddRule("carId", "carId"); PersonRule.AddRule("insuranceId", "insuranceId"); MapRule CarRule = new MapRule(Car, CarCol); CarRule.AddRule("carId", "_id"); CarRule.AddRule("model", "model"); CarRule.AddRule("year", "year"); MapRule GarageRule = new MapRule(Garage, GarageCol); GarageRule.AddRule("garageId", "_id"); GarageRule.AddRule("name", "name"); MapRule SupplierRule = new MapRule(Supplier, SupplierCol); SupplierRule.AddRule("supplierId", "_id"); SupplierRule.AddRule("name", "name"); MapRule RepairedRule = new MapRule(Repaired, RepairedCol); RepairedRule.AddRule("repairedId", "_id"); RepairedRule.AddRule("carId", "carId"); RepairedRule.AddRule("garageId", "garageId"); RepairedRule.AddRule("supplierId", "supplierId"); RepairedRule.AddRule("repaired", "repaired"); MapRule InsuranceRule = new MapRule(Insurance, InsuranceCol); InsuranceRule.AddRule("insuranceId", "_id"); InsuranceRule.AddRule("name", "name"); InsuranceRule.AddRule("value", "value"); ModelMapping Map = new ModelMapping("Map", new List <MapRule> { PersonRule, CarRule, GarageRule, RepairedRule, SupplierRule, InsuranceRule }); return(new RequiredDataContainer(Model, Schema, Map)); }
/// <summary> /// Generates data to test a many to many relationship join with multiple entities /// and relationship attributes /// </summary> /// <returns></returns> public static RequiredDataContainer ManyToManyRelationshipAttributeMultipleEntities() { Entity Person = new Entity("Person"); Person.AddAttribute("personId"); Person.AddAttribute("name"); Entity Car = new Entity("Car"); Car.AddAttribute("carId"); Car.AddAttribute("name"); Car.AddAttribute("year"); Entity InsCompany = new Entity("InsCompany"); InsCompany.AddAttribute("companyId"); InsCompany.AddAttribute("name"); Relationship Insurance = new Relationship("Insurance"); Insurance.AddAttribute("insuranceId"); Insurance.AddAttribute("personId"); Insurance.AddAttribute("carId"); Insurance.AddAttribute("companyId"); Insurance.AddAttribute("insuranceValue"); Insurance.AddAttribute("aRandomValue"); Insurance.AddRelationshipEnd(new RelationshipEnd(Person)); Insurance.AddRelationshipEnd(new RelationshipEnd(Car)); Insurance.AddRelationshipEnd(new RelationshipEnd(InsCompany)); ERModel Model = new ERModel("PersonCar", new List <BaseERElement> { Person, Car, Insurance, InsCompany }); // MongoDB Schema MongoDBCollection PersonCol = new MongoDBCollection("Person"); PersonCol.DocumentSchema.AddAttribute("_id"); PersonCol.DocumentSchema.AddAttribute("name"); MongoDBCollection CarCol = new MongoDBCollection("Car"); CarCol.DocumentSchema.AddAttribute("_id"); CarCol.DocumentSchema.AddAttribute("name"); CarCol.DocumentSchema.AddAttribute("year"); MongoDBCollection InsuranceCol = new MongoDBCollection("Insurance"); InsuranceCol.DocumentSchema.AddAttribute("_id"); InsuranceCol.DocumentSchema.AddAttribute("personId"); InsuranceCol.DocumentSchema.AddAttribute("carId"); InsuranceCol.DocumentSchema.AddAttribute("companyId"); InsuranceCol.DocumentSchema.AddAttribute("insuranceValue"); InsuranceCol.DocumentSchema.AddAttribute("aRandomValue"); MongoDBCollection InsCompanyCol = new MongoDBCollection("InsCompany"); InsCompanyCol.DocumentSchema.AddAttribute("_id"); InsCompanyCol.DocumentSchema.AddAttribute("name"); MongoSchema DBSchema = new MongoSchema("PersonCarSchema", new List <MongoDBCollection> { PersonCol, CarCol, InsuranceCol, InsCompanyCol }); // Map MapRule PersonRule = new MapRule(Person, PersonCol); PersonRule.AddRule("personId", "_id"); PersonRule.AddRule("name", "name"); MapRule CarRule = new MapRule(Car, CarCol); CarRule.AddRule("carId", "_id"); CarRule.AddRule("name", "name"); CarRule.AddRule("year", "year"); MapRule InsuranceRule = new MapRule(Insurance, InsuranceCol); InsuranceRule.AddRule("insuranceId", "_id"); InsuranceRule.AddRule("personId", "personId"); InsuranceRule.AddRule("carId", "carId"); InsuranceRule.AddRule("companyId", "companyId"); InsuranceRule.AddRule("insuranceValue", "insuranceValue"); InsuranceRule.AddRule("aRandomValue", "aRandomValue"); MapRule InsCompanyRule = new MapRule(InsCompany, InsCompanyCol); InsCompanyRule.AddRule("companyId", "_id"); InsCompanyRule.AddRule("name", "name"); ModelMapping Map = new ModelMapping("PersonCarMap", new List <MapRule> { PersonRule, CarRule, InsuranceRule, InsCompanyRule }); return(new RequiredDataContainer(Model, DBSchema, Map)); }
/// <summary> /// Generate the name of tha tbackup collection based on the original collection name and schema. /// </summary> public static string GetBackupCollectionName(string collectionName, MongoSchema schema, MongoStorageOptions storageOptions) { return($@"{collectionName}.{(int)schema}.{storageOptions.MigrationOptions.BackupPostfix}"); }
/// <summary> /// Backs up each hangfire.mongo collection before executing migration /// </summary> /// <param name="storageOptions"></param> /// <param name="database"></param> /// <param name="fromSchema"></param> /// <param name="toSchema"></param> /// <exception cref="InvalidOperationException"></exception> public override void Backup(MongoStorageOptions storageOptions, IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { var existingCollectionNames = MongoMigrationUtils.ExistingHangfireCollectionNames(database, fromSchema, storageOptions).ToList(); var backupCollectionNames = existingCollectionNames .ToDictionary(k => k, v => MongoMigrationUtils.GetBackupCollectionName(v, fromSchema, storageOptions)); // Let's double check that we have not backed up before. var existingBackupCollectionName = existingCollectionNames.FirstOrDefault(n => backupCollectionNames.ContainsValue(n)); if (existingBackupCollectionName != null) { throw new InvalidOperationException( $"{Environment.NewLine}{existingBackupCollectionName} already exists. Cannot perform backup." + $"{Environment.NewLine}Cannot overwrite existing backups. Please resolve this manually (e.g. by droping collection)." + $"{Environment.NewLine}Please see https://github.com/sergeyzwezdin/Hangfire.Mongo#migration for further information."); } // Now do the actual backup foreach (var collection in backupCollectionNames) { BackupCollection(database, collection.Key, collection.Value); } }
protected virtual void BackupStrategyNone(IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { }
/// <summary> /// Generates required data for a OneToMany relationship without embedded documents /// And with relationship attribute /// </summary> /// <returns></returns> public static RequiredDataContainer OneToManyRelationshipAttributes() { // Create ER Stuff Entity Person = new Entity("Person"); Person.AddAttribute("personId"); Person.AddAttribute("name"); Entity Car = new Entity("Car"); Car.AddAttribute("carId"); Car.AddAttribute("name"); Car.AddAttribute("year"); Car.AddAttribute("driverId"); Relationship Drives = new Relationship("Drives"); Drives.AddAttribute("drivesFor"); Drives.AddRelationshipEnd(new RelationshipEnd(Person)); Drives.AddRelationshipEnd(new RelationshipEnd(Car)); ERModel Model = new ERModel("PersonCarModel", new List <BaseERElement> { Person, Car, Drives }); // Create MongoDB schema MongoDBCollection PersonCollection = new MongoDBCollection("Person"); PersonCollection.DocumentSchema.AddAttribute("_id"); PersonCollection.DocumentSchema.AddAttribute("name"); MongoDBCollection CarCollection = new MongoDBCollection("Car"); CarCollection.DocumentSchema.AddAttribute("_id"); CarCollection.DocumentSchema.AddAttribute("name"); CarCollection.DocumentSchema.AddAttribute("year"); CarCollection.DocumentSchema.AddAttribute("driverId"); CarCollection.DocumentSchema.AddAttribute("drivesFor"); MongoSchema Schema = new MongoSchema("PersonCarSchema", new List <MongoDBCollection> { PersonCollection, CarCollection }); // Create Map MapRule PersonRule = new MapRule(Model.FindByName("Person"), Schema.FindByName("Person")); PersonRule.AddRule("personId", "_id"); PersonRule.AddRule("name", "name"); MapRule CarRule = new MapRule(Model.FindByName("Car"), Schema.FindByName("Car")); CarRule.AddRule("carId", "_id"); CarRule.AddRule("name", "name"); CarRule.AddRule("year", "year"); CarRule.AddRule("driverId", "driverId"); MapRule DrivesRule = new MapRule(Model.FindByName("Drives"), Schema.FindByName("Car")); DrivesRule.AddRule("drivesFor", "drivesFor"); ModelMapping Map = new ModelMapping("PersonCarMap", new List <MapRule> { PersonRule, CarRule, DrivesRule }); return(new RequiredDataContainer(Model, Schema, Map)); }
/// <summary> /// Execute migration from "None" /// </summary> /// <param name="database"></param> /// <param name="fromSchema"></param> /// <param name="toSchema"></param> protected override void ExecuteMigration(IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { base.ExecuteMigration(database, MongoSchema.None, toSchema); }
/// <summary> /// drop all hangfire collections /// </summary> /// <param name="database"></param> /// <param name="fromSchema"></param> /// <param name="toSchema"></param> protected override void ExecuteStrategy(IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { foreach (var collectionName in MongoMigrationUtils.ExistingHangfireCollectionNames(database, fromSchema, StorageOptions)) { database.DropCollection(collectionName); } }
protected virtual void Migrate(MongoSchema fromSchema, MongoSchema toSchema) { var migrationRunner = new MongoMigrationRunner(_dbContext, _storageOptions); migrationRunner.Execute(fromSchema, toSchema); }
/// <summary> /// Maps entities to collection and return all data (ERModel, MongoSchema and ModelMapping) /// </summary> /// <returns></returns> public static DataContainer MapEntitiesToCollections() { // Create schema MongoDBCollection UserCol = new MongoDBCollection("User"); UserCol.AddAttributes("_id", "user_name", "user_email", "user_access", "user_newsletter"); MongoDBCollection ProductCol = new MongoDBCollection("Product"); ProductCol.AddAttributes("_id", "product_title", "product_description", "product_short_description", "product_url", "product_category_id", "product_store_id", "product_user_id", "product_published", "product_image"); MongoDBCollection CategoryCol = new MongoDBCollection("Category"); CategoryCol.AddAttributes("_id", "category_name"); MongoDBCollection StoreCol = new MongoDBCollection("Store"); StoreCol.AddAttributes("_id", "store_name", "store_logo"); MongoSchema Schema = new MongoSchema("CMSSchema", new List <MongoDBCollection>() { UserCol, ProductCol, StoreCol, CategoryCol }); // Retrieve ER Model ERModel Model = GetERModel(); // Build mapping Entity User = (Entity)Model.FindByName("User"); MapRule UserRules = new MapRule(User, UserCol); UserRules.AddRule("user_id", "_id"); UserRules.AddRule("user_name", "user_name"); UserRules.AddRule("user_email", "user_email"); UserRules.AddRule("user_access", "user_access"); UserRules.AddRule("user_newsletter", "user_newsletter"); Entity Product = (Entity)Model.FindByName("Product"); MapRule ProductRules = new MapRule(Product, ProductCol); ProductRules.AddRule("product_id", "_id"); ProductRules.AddRule("product_title", "product_title"); ProductRules.AddRule("product_description", "product_description"); ProductRules.AddRule("product_short_description", "product_short_description"); ProductRules.AddRule("product_url", "product_url"); ProductRules.AddRule("product_published", "product_published"); ProductRules.AddRule("product_image", "product_image"); Entity Category = (Entity)Model.FindByName("Category"); MapRule CategoryRules = new MapRule(Category, CategoryCol); CategoryRules.AddRule("category_id", "_id"); CategoryRules.AddRule("category_name", "category_name"); Entity Store = (Entity)Model.FindByName("Store"); MapRule StoreRules = new MapRule(Store, StoreCol); StoreRules.AddRule("store_id", "_id"); StoreRules.AddRule("store_name", "store_name"); StoreRules.AddRule("store_logo", "store_logo"); MapRule UserProductRule = new MapRule(User, ProductCol, false); UserProductRule.AddRule("user_id", "product_user_id"); MapRule StoreProductRule = new MapRule(Store, ProductCol, false); StoreProductRule.AddRule("store_id", "product_store_id"); MapRule CategoryProductRule = new MapRule(Category, ProductCol, false); CategoryProductRule.AddRule("category_id", "product_category_id"); ModelMapping Map = new ModelMapping("CMSMap11", new List <MapRule>() { UserRules, ProductRules, CategoryRules, StoreRules, UserProductRule, StoreProductRule, CategoryProductRule }); return(new DataContainer(Model, Schema, Map)); }
public static DataContainer CreateDataContainer() { // ER MODEL Entity Person = new Entity("Person"); Person.AddAttribute("id", true); Person.AddAttributes("name", "surname", "salary"); Entity Car = new Entity("Car"); Car.AddAttribute("id", true); Car.AddAttributes("reg_no"); Entity InsuranceCompany = new Entity("InsuranceCompany"); InsuranceCompany.AddAttribute("id", true); InsuranceCompany.AddAttributes("name"); Entity Garage = new Entity("Garage"); Garage.AddAttribute("id", true); Garage.AddAttributes("name", "phone"); Relationship Insurance = new Relationship("Insurance"); Insurance.AddAttributes("contract"); Insurance.AddRelationshipEnd(new RelationshipEnd(Person)); Insurance.AddRelationshipEnd(new RelationshipEnd(Car)); Insurance.AddRelationshipEnd(new RelationshipEnd(InsuranceCompany)); Relationship Drives = new Relationship("Drives"); Drives.AddRelationshipEnd(new RelationshipEnd(Person)); Drives.AddRelationshipEnd(new RelationshipEnd(Car)); Relationship Repaired = new Relationship("Repaired"); Repaired.AddAttributes("date"); Repaired.AddRelationshipEnd(new RelationshipEnd(Car)); Repaired.AddRelationshipEnd(new RelationshipEnd(Garage)); ERModel Model = new ERModel("SampleModel", new List <BaseERElement>() { Person, Car, InsuranceCompany, Garage, Insurance, Drives, Repaired }); // SCHEMA MongoDBCollection PersonCol = new MongoDBCollection("PersonCollection"); PersonCol.AddAttributes("_id", "fName", "fSurname", "fSalary"); MongoDBCollection CarCol = new MongoDBCollection("CarCollection"); CarCol.AddAttributes("_id", "fReg_no"); MongoDBCollection InsuranceCompanyCol = new MongoDBCollection("InsuranceCompanyCollection"); InsuranceCompanyCol.AddAttributes("_id", "fName"); MongoDBCollection GarageCol = new MongoDBCollection("GarageCollection"); GarageCol.AddAttributes("_id", "fName", "fPhone"); MongoDBCollection InsuranceCol = new MongoDBCollection("InsuranceCollection"); InsuranceCol.AddAttributes("contract"); MongoDBCollection DrivesCol = new MongoDBCollection("DrivesCollection"); DrivesCol.AddAttributes("fPersonId", "fCarId"); MongoDBCollection RepairedCol = new MongoDBCollection("RepairedCollection"); RepairedCol.AddAttributes("fCarId", "fGarageId", "fDate"); MongoSchema Schema = new MongoSchema("SampleSchema", new List <MongoDBCollection>() { PersonCol, CarCol, InsuranceCompanyCol, GarageCol, InsuranceCol, DrivesCol, RepairedCol }); // Mapping MapRule PersonRule = new MapRule(Person, PersonCol); PersonRule.AddRule("id", "_id"); PersonRule.AddRule("name", "fName"); PersonRule.AddRule("surname", "fSurname"); PersonRule.AddRule("salary", "fSalary"); MapRule CarRule = new MapRule(Car, CarCol); CarRule.AddRule("id", "_id"); CarRule.AddRule("reg_no", "fReg_no"); MapRule InsCompanyRule = new MapRule(InsuranceCompany, InsuranceCompanyCol); InsCompanyRule.AddRule("id", "_id"); InsCompanyRule.AddRule("name", "fName"); MapRule GarageRule = new MapRule(Garage, GarageCol); GarageRule.AddRule("id", "_id"); GarageRule.AddRule("name", "fName"); GarageRule.AddRule("phone", "fPhone"); MapRule InsuranceRule = new MapRule(Insurance, InsuranceCol); InsuranceRule.AddRule("contract", "contract"); MapRule PersonInsuranceRule = new MapRule(Person, InsuranceCol, false); PersonInsuranceRule.AddRule("id", "fPersonId"); MapRule CarInsuranceRule = new MapRule(Car, InsuranceCol, false); CarInsuranceRule.AddRule("id", "fCarId"); MapRule InsCompanyInsuranceRule = new MapRule(InsuranceCompany, InsuranceCol, false); InsCompanyInsuranceRule.AddRule("id", "fInsCoId"); MapRule DrivesRule = new MapRule(Drives, DrivesCol); MapRule PersonDrivesRule = new MapRule(Person, DrivesCol, false); PersonDrivesRule.AddRule("id", "fPersonId"); MapRule CarDrivesRule = new MapRule(Car, DrivesCol, false); CarDrivesRule.AddRule("id", "fCarId"); MapRule RepairedRule = new MapRule(Repaired, RepairedCol); RepairedRule.AddRule("date", "fDate"); MapRule CarRepairedRule = new MapRule(Car, RepairedCol, false); CarRepairedRule.AddRule("id", "fCarId"); MapRule GarageRepairedRule = new MapRule(Garage, RepairedCol, false); GarageRepairedRule.AddRule("id", "fGarageId"); ModelMapping Mapping = new ModelMapping("SampleMapping", new List <MapRule>() { PersonRule, CarRule, InsCompanyRule, GarageRule, InsuranceRule, PersonInsuranceRule, CarInsuranceRule, InsCompanyInsuranceRule, DrivesRule, PersonDrivesRule, CarDrivesRule, RepairedRule, CarRepairedRule, GarageRepairedRule }); return(new DataContainer(Model, Schema, Mapping)); }
protected override void ExecuteStrategy(IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { // nothing to do }
/// <summary> /// Generates required data to test a One to One relationship /// joining multiple entities with relationship attribute and multiple root attributes /// </summary> /// <returns></returns> public static RequiredDataContainer OneToOneRelationshipMultipleRootAttributes() { // Create ER Stuff Entity Person = new Entity("Person"); Person.AddAttribute("personId", true); Person.AddAttribute("name"); Person.AddAttribute("insuranceId"); Entity Car = new Entity("Car"); Car.AddAttribute("name"); Car.AddAttribute("year"); Car.AddAttribute("engine"); Car.AddAttribute("fuel"); Entity Insurance = new Entity("Insurance"); Insurance.AddAttribute("insuranceId", true); Insurance.AddAttribute("name"); Relationship HasInsurance = new Relationship("HasInsurance"); HasInsurance.AddAttribute("insuranceValue"); HasInsurance.AddRelationshipEnd(new RelationshipEnd(Person)); HasInsurance.AddRelationshipEnd(new RelationshipEnd(Car)); HasInsurance.AddRelationshipEnd(new RelationshipEnd(Insurance)); ERModel Model = new ERModel("PersonCarModel", new List <BaseERElement> { Person, Car, HasInsurance, Insurance }); // Create MongoDB schema MongoDBCollection PersonCollection = new MongoDBCollection("Person"); PersonCollection.DocumentSchema.AddAttribute("_id"); PersonCollection.DocumentSchema.AddAttribute("name"); PersonCollection.DocumentSchema.AddAttribute("car.name"); PersonCollection.DocumentSchema.AddAttribute("car.year"); PersonCollection.DocumentSchema.AddAttribute("carDetails.engine"); PersonCollection.DocumentSchema.AddAttribute("CarDetails.fuel"); PersonCollection.DocumentSchema.AddAttribute("insuranceId"); PersonCollection.DocumentSchema.AddAttribute("insuranceValue"); MongoDBCollection InsuranceCollection = new MongoDBCollection("Insurance"); InsuranceCollection.DocumentSchema.AddAttribute("_id"); InsuranceCollection.DocumentSchema.AddAttribute("name"); MongoSchema Schema = new MongoSchema("PersonCarSchema", new List <MongoDBCollection> { PersonCollection, InsuranceCollection }); // Create Map MapRule PersonRule = new MapRule(Model.FindByName("Person"), Schema.FindByName("Person")); PersonRule.AddRule("personId", "_id"); PersonRule.AddRule("name", "name"); PersonRule.AddRule("carId", "carId"); PersonRule.AddRule("insuranceId", "insuranceId"); MapRule CarRule = new MapRule(Model.FindByName("Car"), Schema.FindByName("Person"), false); CarRule.AddRule("name", "car.name"); CarRule.AddRule("year", "car.year"); CarRule.AddRule("engine", "carDetails.engine"); CarRule.AddRule("fuel", "carDetails.fuel"); MapRule InsuranceRule = new MapRule(Model.FindByName("Insurance"), Schema.FindByName("Insurance")); InsuranceRule.AddRule("insuranceId", "_id"); InsuranceRule.AddRule("name", "name"); MapRule InsurancePersonRule = new MapRule(Model.FindByName("Insurance"), Schema.FindByName("Person"), false); InsurancePersonRule.AddRule("insuranceId", "insuranceId"); MapRule RelationshipRule = new MapRule(Model.FindByName("HasInsurance"), Schema.FindByName("Person"), false); RelationshipRule.AddRule("insuranceValue", "insuranceValue"); ModelMapping Map = new ModelMapping("PersonCarMap", new List <MapRule> { PersonRule, CarRule, InsuranceRule, RelationshipRule, InsurancePersonRule }); return(new RequiredDataContainer(Model, Schema, Map)); }
/// <summary> /// Generate the name of tha tbackup collection based on the original collection name and schema. /// </summary> protected string GetBackupCollectionName(string collectionName, MongoSchema schema) { return($@"{collectionName}.{(int)schema}.{_storageOptions.MigrationOptions.BackupPostfix}"); }
internal static IList <string> CollectionNames(this MongoSchema schema, string prefix) { switch (schema) { case MongoSchema.None: return(new[] { "_identifiers", // A bug prevented the use of prefix prefix + ".aggregatedcounter", prefix + ".counter", prefix + ".hash", prefix + ".job", prefix + ".jobParameter", prefix + ".jobQueue", prefix + ".list", prefix + ".locks", prefix + ".server", prefix + ".set", prefix + ".state", }); case MongoSchema.Version04: return(new[] { "_identifiers", // A bug prevented the use of prefix prefix + ".aggregatedcounter", prefix + ".counter", prefix + ".hash", prefix + ".job", prefix + ".jobParameter", prefix + ".jobQueue", prefix + ".list", prefix + ".locks", prefix + ".schema", prefix + ".server", prefix + ".set", prefix + ".state", }); case MongoSchema.Version05: return(new[] { "_identifiers", // A bug prevented the use of prefix prefix + ".aggregatedcounter", prefix + ".counter", prefix + ".hash", prefix + ".job", prefix + ".jobParameter", prefix + ".jobQueue", prefix + ".list", prefix + ".locks", prefix + ".schema", prefix + ".server", prefix + ".set", prefix + ".state", }); case MongoSchema.Version06: return(new[] { prefix + ".job", prefix + ".jobQueue", prefix + ".locks", prefix + ".schema", prefix + ".server", prefix + ".statedata" }); case MongoSchema.Version07: case MongoSchema.Version08: return(new[] { prefix + ".job", prefix + ".jobQueue", prefix + ".locks", prefix + ".schema", prefix + ".server", prefix + ".stateData" }); case MongoSchema.Version09: case MongoSchema.Version10: case MongoSchema.Version11: case MongoSchema.Version12: return(new[] { prefix + ".job", prefix + ".jobQueue", prefix + ".locks", prefix + ".schema", prefix + ".server", prefix + ".signal", prefix + ".stateData" }); case MongoSchema.Version13: case MongoSchema.Version14: return(new[] { prefix + ".jobGraph", prefix + ".locks", prefix + ".schema", prefix + ".server", prefix + ".signal" }); case MongoSchema.Version15: case MongoSchema.Version16: return(new[] { prefix + ".jobGraph", prefix + ".locks", prefix + ".schema", prefix + ".server" }); case MongoSchema.Version17: return(new[] { prefix + ".jobGraph", prefix + ".locks", prefix + ".schema", prefix + ".server", prefix + ".notifications" }); case MongoSchema.Version18: return(new[] { prefix + ".jobGraph", prefix + ".locks", prefix + ".schema", prefix + ".server", prefix + ".notifications" }); default: throw new ArgumentException($@"Unknown schema: '{schema}'", nameof(schema)); } }
protected virtual void Migrate(MongoSchema fromSchema, MongoSchema toSchema) { _migrationRunner.Execute(fromSchema, toSchema); }
protected override void Migrate(MongoSchema fromSchema, MongoSchema toSchema) { base.Execute(MongoSchema.None, toSchema); }
protected virtual void BackupStrategyDatabase(IMongoClient client, IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { var databaseName = database.DatabaseNamespace.DatabaseName; var backupDatabaseName = GetBackupDatabaseName(databaseName, fromSchema); var db = client.GetDatabase(DatabaseNamespace.Admin.DatabaseName); var doc = new BsonDocument(new Dictionary <string, object> { { "copydb", 1 }, //{ "fromhost", "localhost" }, { "fromdb", databaseName }, { "todb", backupDatabaseName } }); db.RunCommand(new BsonDocumentCommand <BsonDocument>(doc)); }
protected override void BackupStrategyNone(IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { DropHangfireCollections(database, fromSchema); }
/// <summary> /// Find hangfire collection namespaces by reflecting over properties on database. /// </summary> public static IEnumerable <string> ExistingHangfireCollectionNames(IMongoDatabase database, MongoSchema schema, MongoStorageOptions storageOptions) { var existingCollectionNames = ExistingDatabaseCollectionNames(database).ToList(); return(schema.CollectionNames(storageOptions.Prefix).Where(c => existingCollectionNames.Contains(c))); }
protected override void BackupStrategyDatabase(IMongoClient client, IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { base.BackupStrategyDatabase(client, database, fromSchema, toSchema); // Now the database has been copied, // drop the hangfire collections. DropHangfireCollections(database, fromSchema); }
/// <summary> /// Generate the name of that backup collection based on the original collection name and schema. /// </summary> public static string GetBackupDatabaseName(string databaseName, MongoSchema schema, MongoStorageOptions storageOptions) { return($@"{databaseName}-{(int)schema}-{storageOptions.MigrationOptions.BackupPostfix}"); }
protected override void ExecuteStrategy(IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { var assemblyName = GetType().GetTypeInfo().Assembly.GetName(); throw new InvalidOperationException( $"{Environment.NewLine}{assemblyName.Name} version: {assemblyName.Version}, introduces a new schema version that requires migration." + $"{Environment.NewLine}You can choose a migration strategy by setting the {nameof(MongoStorageOptions)}.{nameof(MongoStorageOptions.MigrationOptions)} property." + $"{Environment.NewLine}Please see https://github.com/sergeyzwezdin/Hangfire.Mongo#migration for further information."); }
/// <summary> /// Initializes a new RequiredDataContainer instance /// </summary> /// <param name="EntityRelationshipModel"></param> /// <param name="MongoDBSchema"></param> /// <param name="ERMongoMapping"></param> public RequiredDataContainer(ERModel EntityRelationshipModel, MongoSchema MongoDBSchema, ModelMapping ERMongoMapping) { this.EntityRelationshipModel = EntityRelationshipModel; this.MongoDBSchema = MongoDBSchema; this.ERMongoMapping = ERMongoMapping; }
/// <summary> /// Find hangfire collection namespaces by reflecting over properties on database. /// </summary> protected IEnumerable <string> ExistingHangfireCollectionNames(MongoSchema schema) { var existingCollectionNames = ExistingDatabaseCollectionNames().ToList(); return(schema.CollectionNames(_storageOptions.Prefix).Where(c => existingCollectionNames.Contains(c))); }
/// <summary> /// Generate data to test a computed entity from a many to many relationship /// </summary> /// <returns></returns> public static RequiredDataContainer ManyToManyComputedEntity2() { Entity Person = new Entity("Person"); Person.AddAttributes("personId", "name"); Entity Car = new Entity("Car"); Car.AddAttributes("carId", "model", "year", "manufacturerId"); Entity Garage = new Entity("Garage"); Garage.AddAttributes("garageId", "name"); Entity Supplier = new Entity("Supplier"); Supplier.AddAttributes("supplierId", "name"); Entity Insurance = new Entity("Insurance"); Insurance.AddAttributes("insuranceId", "name", "value"); Entity Manufacturer = new Entity("Manufacturer"); Manufacturer.AddAttributes("manufacturerId", "name"); Relationship Owns = new Relationship("Owns"); Owns.AddAttributes("ownsId", "personId", "carId", "insuranceId"); Owns.AddRelationshipEnd(new RelationshipEnd(Person)); Owns.AddRelationshipEnd(new RelationshipEnd(Car)); Owns.AddRelationshipEnd(new RelationshipEnd(Insurance)); Relationship Repaired = new Relationship("Repaired"); Repaired.AddAttributes("repairedId", "carId", "garageId", "supplierId", "repaired"); Repaired.AddRelationshipEnd(new RelationshipEnd(Car)); Repaired.AddRelationshipEnd(new RelationshipEnd(Garage)); Repaired.AddRelationshipEnd(new RelationshipEnd(Supplier)); Relationship ManufacturedBy = new Relationship("ManufacturedBy"); ManufacturedBy.AddRelationshipEnd(new RelationshipEnd(Car)); ManufacturedBy.AddRelationshipEnd(new RelationshipEnd(Manufacturer)); ERModel Model = new ERModel("ERModel", new List <BaseERElement> { Person, Car, Garage, Repaired, Supplier, Insurance, Owns, Manufacturer, ManufacturedBy }); // Mongo Schema MongoDBCollection PersonCol = new MongoDBCollection("Person"); PersonCol.AddAttributes("_id", "name"); MongoDBCollection CarCol = new MongoDBCollection("Car"); CarCol.AddAttributes("_id", "model", "year", "manufacturerId"); MongoDBCollection GarageCol = new MongoDBCollection("Garage"); GarageCol.AddAttributes("_id", "name"); MongoDBCollection SupplierCol = new MongoDBCollection("Supplier"); SupplierCol.AddAttributes("_id", "name"); MongoDBCollection RepairedCol = new MongoDBCollection("Repaired"); RepairedCol.AddAttributes("_id", "carId", "garageId", "supplierId", "repaired"); MongoDBCollection InsuranceCol = new MongoDBCollection("Insurance"); InsuranceCol.AddAttributes("_id", "name", "value"); MongoDBCollection OwnsCol = new MongoDBCollection("Owns"); OwnsCol.AddAttributes("_id", "personId", "carId", "insuranceId"); MongoDBCollection ManufacturerCol = new MongoDBCollection("Manufacturer"); ManufacturerCol.AddAttributes("_id", "name"); MongoSchema Schema = new MongoSchema("Schema", new List <MongoDBCollection> { PersonCol, CarCol, GarageCol, RepairedCol, SupplierCol, InsuranceCol, OwnsCol, ManufacturerCol }); // Map Rules MapRule PersonRule = new MapRule(Person, PersonCol); PersonRule.AddRule("personId", "_id"); PersonRule.AddRule("name", "name"); MapRule CarRule = new MapRule(Car, CarCol); CarRule.AddRule("carId", "_id"); CarRule.AddRule("model", "model"); CarRule.AddRule("year", "year"); CarRule.AddRule("manufacturerId", "manufacturerId"); MapRule GarageRule = new MapRule(Garage, GarageCol); GarageRule.AddRule("garageId", "_id"); GarageRule.AddRule("name", "name"); MapRule SupplierRule = new MapRule(Supplier, SupplierCol); SupplierRule.AddRule("supplierId", "_id"); SupplierRule.AddRule("name", "name"); MapRule RepairedRule = new MapRule(Repaired, RepairedCol); RepairedRule.AddRule("repairedId", "_id"); RepairedRule.AddRule("carId", "carId"); RepairedRule.AddRule("garageId", "garageId"); RepairedRule.AddRule("supplierId", "supplierId"); RepairedRule.AddRule("repaired", "repaired"); MapRule InsuranceRule = new MapRule(Insurance, InsuranceCol); InsuranceRule.AddRule("insuranceId", "_id"); InsuranceRule.AddRule("name", "name"); InsuranceRule.AddRule("value", "value"); MapRule OwnsRule = new MapRule(Owns, OwnsCol); OwnsRule.AddRule("ownsId", "_id"); OwnsRule.AddRule("personId", "personId"); OwnsRule.AddRule("carId", "carId"); OwnsRule.AddRule("insuranceId", "insuranceId"); MapRule ManufacturerRule = new MapRule(Manufacturer, ManufacturerCol); ManufacturerRule.AddRule("manufacturerId", "_id"); ManufacturerRule.AddRule("name", "name"); ModelMapping Map = new ModelMapping("Map", new List <MapRule> { PersonRule, CarRule, GarageRule, RepairedRule, SupplierRule, InsuranceRule, OwnsRule, ManufacturerRule }); return(new RequiredDataContainer(Model, Schema, Map)); }
/// <summary> /// Generate the name of tha tbackup collection based on the original collection name and schema. /// </summary> protected string GetBackupDatabaseName(string databaseName, MongoSchema schema) { return($@"{databaseName}-{(int)schema}-{_storageOptions.MigrationOptions.BackupPostfix}"); }
public static RequiredDataContainer MapEntitiesToCollectionsStoreDuplicated() { // Create Schema MongoDBCollection ProductCol = new MongoDBCollection("Product"); ProductCol.AddAttributes("_id", "Title", "Description", "CategoryID", "UserID", "store._id", "store.name"); MongoDBCollection UserCol = new MongoDBCollection("User"); UserCol.AddAttributes("_id", "UserName", "UserEmail"); MongoDBCollection CategoryCol = new MongoDBCollection("Category"); CategoryCol.AddAttributes("_id", "CategoryName"); MongoDBCollection StoreCol = new MongoDBCollection("Store"); StoreCol.AddAttributes("_id", "StoreName"); MongoSchema Schema = new MongoSchema("CMSSchema", new List <MongoDBCollection>() { ProductCol, UserCol, CategoryCol, StoreCol }); // Retrieve ER Model ERModel Model = GetERModel(); // Build Mapping Entity User = (Entity)Model.FindByName("User"); MapRule UserRulesMain = new MapRule(User, UserCol); UserRulesMain.AddRule("UserID", "_id"); UserRulesMain.AddRule("UserName", "UserName"); UserRulesMain.AddRule("UserEmail", "UserEmail"); Entity Product = (Entity)Model.FindByName("Product"); MapRule ProductRules = new MapRule(Product, ProductCol); ProductRules.AddRule("ProductID", "_id"); ProductRules.AddRule("Title", "Title"); ProductRules.AddRule("Description", "Description"); Entity Category = (Entity)Model.FindByName("Category"); MapRule CategoryRulesMain = new MapRule(Category, CategoryCol); CategoryRulesMain.AddRule("CategoryID", "_id"); CategoryRulesMain.AddRule("CategoryName", "CategoryName"); Entity Store = (Entity)Model.FindByName("Store"); MapRule StoreRules = new MapRule(Store, ProductCol, false); StoreRules.AddRule("StoreID", "store._id"); StoreRules.AddRule("StoreName", "store.name"); MapRule StoreRulesMain = new MapRule(Store, StoreCol); StoreRulesMain.AddRule("StoreID", "_id"); StoreRulesMain.AddRule("StoreName", "StoreName"); MapRule UserProductRule = new MapRule(User, ProductCol, false); UserProductRule.AddRule("UserID", "UserID"); MapRule CategoryProductRule = new MapRule(Category, ProductCol, false); CategoryProductRule.AddRule("CategoryID", "CategoryID"); ModelMapping Map = new ModelMapping("CMSStoreDuplicates", new List <MapRule>() { UserRulesMain, ProductRules, CategoryRulesMain, StoreRules, UserProductRule, CategoryProductRule }); return(new RequiredDataContainer(Model, Schema, Map)); }
/// <summary> /// Executes backup routine /// </summary> public virtual void Backup(MongoStorageOptions storageOptions, IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { }