protected override bool TryPrepareRow(bool dump, DbConnection connection, ColumnMapper columnMapper, TableInfo table, DataRowInfo row, out Dictionary <string, object> preparedRow) { if (row.TableName == "files_thirdparty_id_mapping") { //todo: think... preparedRow = new Dictionary <string, object>(); object folderId = null; var sboxId = Regex.Replace(row[1].ToString(), @"(?<=(?:sbox-|box-|dropbox-|spoint-|drive-|onedrive-))\d+", match => { folderId = columnMapper.GetMapping("files_thirdparty_account", "id", match.Value); return(Convert.ToString(folderId)); }, RegexOptions.Compiled); if (folderId == null) { return(false); } var hashBytes = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(sboxId)); var hashedId = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); preparedRow.Add("hash_id", hashedId); preparedRow.Add("id", sboxId); preparedRow.Add("tenant_id", columnMapper.GetTenantMapping()); columnMapper.SetMapping("files_thirdparty_id_mapping", "hash_id", row["hash_id"], hashedId); return(true); } return(base.TryPrepareRow(dump, connection, columnMapper, table, row, out preparedRow)); }
protected override bool TryPrepareRow(bool dump, DbConnection connection, ColumnMapper columnMapper, TableInfo table, DataRowInfo row, out Dictionary <string, object> preparedRow) { if (table.Name == "tenants_tenants" && string.IsNullOrEmpty(Convert.ToString(row["payment_id"]))) { var oldTenantID = Convert.ToInt32(row["id"]); columnMapper.SetMapping("tenants_tenants", "payment_id", row["payment_id"], Core.CoreContext.Configuration.GetKey(oldTenantID)); } return(base.TryPrepareRow(dump, connection, columnMapper, table, row, out preparedRow)); }
protected virtual bool TryPrepareValue(IDbConnection connection, ColumnMapper columnMapper, TableInfo table, string columnName, ref object value) { if (columnName.Equals(table.TenantColumn, StringComparison.OrdinalIgnoreCase)) { int tenantMapping = columnMapper.GetTenantMapping(); if (tenantMapping < 1) { return(false); } value = tenantMapping; return(true); } if (columnName.Equals(table.GuidIDColumn, StringComparison.OrdinalIgnoreCase)) { var oldValue = value; value = Guid.NewGuid().ToString("D"); columnMapper.SetMapping(table.Name, columnName, oldValue, value); return(true); } if (table.UserIDColumns.Any(x => columnName.Equals(x, StringComparison.OrdinalIgnoreCase))) { var strVal = Convert.ToString(value); string userMapping = columnMapper.GetUserMapping(strVal); if (userMapping == null) { return(Helpers.IsEmptyOrSystemUser(strVal)); } value = userMapping; return(true); } var mapping = columnMapper.GetMapping(table.Name, columnName, value); if (mapping != null) { value = mapping; } return(true); }
protected override bool TryPrepareValue(IDbConnection connection, ColumnMapper columnMapper, TableInfo table, string columnName, ref object value) { if (table.Name == "files_file" && columnName == "id") { //In `files_file` possible multiple rows with the same value of the column `id`. //If such row was not inserted before current iteration then we need to create value for the `id` first. var fileId = columnMapper.GetMapping(table.Name, columnName, value); if (fileId == null) { fileId = connection .CreateCommand("select max(id) from files_file;") .WithTimeout(120) .ExecuteScalar <int>() + 1; columnMapper.SetMapping(table.Name, columnName, value, fileId); } value = fileId; return(true); } return(base.TryPrepareValue(connection, columnMapper, table, columnName, ref value)); }
public void RunJob() { Tenant tenant = null; var tempFile = PathHelper.GetTempFileName(tempFolder); try { NotifyHelper.SendAboutRestoreStarted(TenantId, Notify); var storage = BackupStorageFactory.GetBackupStorage(StorageType, TenantId); storage.Download(StoragePath, tempFile); Percentage = 10; tenant = CoreContext.TenantManager.GetTenant(TenantId); tenant.SetStatus(TenantStatus.Restoring); CoreContext.TenantManager.SaveTenant(tenant); var columnMapper = new ColumnMapper(); columnMapper.SetMapping("tenants_tenants", "alias", tenant.TenantAlias, ((Guid)Id).ToString("N")); columnMapper.Commit(); var restoreTask = new RestorePortalTask(Log, TenantId, configPaths[currentRegion], tempFile, columnMapper); restoreTask.IgnoreTable("tenants_tariff"); restoreTask.ProgressChanged += (sender, args) => Percentage = (10d + 0.65 * args.Progress); restoreTask.RunJob(); CoreContext.TenantManager.RemoveTenant(tenant.TenantId); var restoredTenant = CoreContext.TenantManager.GetTenant(columnMapper.GetTenantMapping()); restoredTenant.SetStatus(TenantStatus.Active); restoredTenant.TenantAlias = tenant.TenantAlias; restoredTenant.PaymentId = string.Empty; if (string.IsNullOrEmpty(restoredTenant.MappedDomain) && !string.IsNullOrEmpty(tenant.MappedDomain)) { restoredTenant.MappedDomain = tenant.MappedDomain; } CoreContext.TenantManager.SaveTenant(restoredTenant); Percentage = 75; File.Delete(tempFile); // sleep until tenants cache expires Thread.Sleep(TimeSpan.FromMinutes(2)); Percentage = 100; NotifyHelper.SendAboutRestoreCompleted(restoredTenant.TenantId, Notify); } catch (Exception error) { Log.Error(error); Error = error; if (tenant != null) { tenant.SetStatus(TenantStatus.Active); CoreContext.TenantManager.SaveTenant(tenant); } } finally { if (File.Exists(tempFile)) { File.Delete(tempFile); } IsCompleted = true; } }
protected override bool TryPrepareValue(IDbConnection connection, ColumnMapper columnMapper, TableInfo table, string columnName, ref object value) { if (table.Name == "files_file" && columnName == "id") { //In `files_file` possible multiple rows with the same value of the column `id`. //If such row was not inserted before current iteration then we need to create value for the `id` first. var fileId = columnMapper.GetMapping(table.Name, columnName, value); if (fileId == null) { fileId = connection .CreateCommand("select max(id) from files_file;") .WithTimeout(120) .ExecuteScalar<int>() + 1; columnMapper.SetMapping(table.Name, columnName, value, fileId); } value = fileId; return true; } return base.TryPrepareValue(connection, columnMapper, table, columnName, ref value); }
protected override bool TryPrepareRow(IDbConnection connection, ColumnMapper columnMapper, TableInfo table, DataRowInfo row, out Dictionary<string, object> preparedRow) { if (row.TableName == "files_thirdparty_id_mapping") { //todo: think... preparedRow = new Dictionary<string, object>(); object folderId = null; var sboxId = Regex.Replace(row[1].ToString(), @"(?<=sbox-)\d+", match => { folderId = columnMapper.GetMapping("files_thirdparty_account", "id", match.Value); return Convert.ToString(folderId); }, RegexOptions.Compiled); if (folderId == null) return false; var hashBytes = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(sboxId)); var hashedId = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); preparedRow.Add("hash_id", hashedId); preparedRow.Add("id", sboxId); preparedRow.Add("tenant_id", columnMapper.GetTenantMapping()); columnMapper.SetMapping("files_thirdparty_id_mapping", "hash_id", row["hash_id"], hashedId); return true; } return base.TryPrepareRow(connection, columnMapper, table, row, out preparedRow); }
protected override bool TryPrepareRow(IDbConnection connection, ColumnMapper columnMapper, TableInfo table, DataRowInfo row, out Dictionary<string, object> preparedRow) { if (table.Name == "tenants_tenants" && string.IsNullOrEmpty(Convert.ToString(row["payment_id"]))) { var oldTenantID = Convert.ToInt32(row["id"]); columnMapper.SetMapping("tenants_tenants", "payment_id", row["payment_id"], Core.CoreContext.Configuration.GetKey(oldTenantID)); } return base.TryPrepareRow(connection, columnMapper, table, row, out preparedRow); }
public void RunJob() { Tenant tenant = null; var tempFile = PathHelper.GetTempFileName(TempFolder); try { NotifyHelper.SendAboutRestoreStarted(TenantId, Notify); var storage = BackupStorageFactory.GetBackupStorage(StorageType, TenantId, StorageParams); storage.Download(StoragePath, tempFile); if (!CoreContext.Configuration.Standalone) { var backupHash = GetBackupHash(tempFile); var repo = BackupStorageFactory.GetBackupRepository(); var record = repo.GetBackupRecord(backupHash, TenantId); if (record == null) { throw new Exception(BackupResource.BackupNotFound); } } Percentage = 10; tenant = CoreContext.TenantManager.GetTenant(TenantId); tenant.SetStatus(TenantStatus.Restoring); CoreContext.TenantManager.SaveTenant(tenant); var columnMapper = new ColumnMapper(); columnMapper.SetMapping("tenants_tenants", "alias", tenant.TenantAlias, ((Guid)Id).ToString("N")); columnMapper.Commit(); var restoreTask = new RestorePortalTask(Log, TenantId, configPaths[currentRegion], tempFile, columnMapper, upgradesPath); restoreTask.ProgressChanged += (sender, args) => Percentage = (10d + 0.65 * args.Progress); restoreTask.RunJob(); Tenant restoredTenant = null; if (restoreTask.Dump) { AscCache.OnClearCache(); if (Notify) { var tenants = CoreContext.TenantManager.GetTenants(); foreach (var t in tenants) { NotifyHelper.SendAboutRestoreCompleted(t.TenantId, Notify); } } } else { CoreContext.TenantManager.RemoveTenant(tenant.TenantId); restoredTenant = CoreContext.TenantManager.GetTenant(columnMapper.GetTenantMapping()); restoredTenant.SetStatus(TenantStatus.Active); restoredTenant.TenantAlias = tenant.TenantAlias; restoredTenant.PaymentId = string.Empty; if (string.IsNullOrEmpty(restoredTenant.MappedDomain) && !string.IsNullOrEmpty(tenant.MappedDomain)) { restoredTenant.MappedDomain = tenant.MappedDomain; } CoreContext.TenantManager.SaveTenant(restoredTenant); // sleep until tenants cache expires Thread.Sleep(TimeSpan.FromMinutes(2)); NotifyHelper.SendAboutRestoreCompleted(restoredTenant.TenantId, Notify); } Percentage = 75; File.Delete(tempFile); Percentage = 100; } catch (Exception error) { Log.Error(error); Error = error; if (tenant != null) { tenant.SetStatus(TenantStatus.Active); CoreContext.TenantManager.SaveTenant(tenant); } } finally { if (File.Exists(tempFile)) { File.Delete(tempFile); } IsCompleted = true; } }
protected virtual bool TryPrepareValue(IDbConnection connection, ColumnMapper columnMapper, TableInfo table, string columnName, ref object value) { if (columnName.Equals(table.TenantColumn, StringComparison.OrdinalIgnoreCase)) { int tenantMapping = columnMapper.GetTenantMapping(); if (tenantMapping < 1) return false; value = tenantMapping; return true; } if (columnName.Equals(table.GuidIDColumn, StringComparison.OrdinalIgnoreCase)) { var oldValue = value; value = Guid.NewGuid().ToString("D"); columnMapper.SetMapping(table.Name, columnName, oldValue, value); return true; } if (table.UserIDColumns.Any(x => columnName.Equals(x, StringComparison.OrdinalIgnoreCase))) { var strVal = Convert.ToString(value); string userMapping = columnMapper.GetUserMapping(strVal); if (userMapping == null) return Helpers.IsEmptyOrSystemUser(strVal); value = userMapping; return true; } var mapping = columnMapper.GetMapping(table.Name, columnName, value); if (mapping != null) value = mapping; return true; }