Skip to content

Commit

Permalink
Merge pull request #298 from ONLYOFFICE/feature/wizard/migration
Browse files Browse the repository at this point in the history
feature/wizard-migration
  • Loading branch information
pavelbannov authored Oct 25, 2021
2 parents 2954296 + b3747a9 commit 3443ea2
Show file tree
Hide file tree
Showing 130 changed files with 5,850 additions and 5,224 deletions.
6 changes: 6 additions & 0 deletions ASC.Tests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASC.Core.Common.Tests", "co
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASC.Notify.Textile.Tests", "common\Tests\ASC.Notify.Textile.Tests\ASC.Notify.Textile.Tests.csproj", "{8FAD3D1B-3ADC-470C-9933-CAE1B95A8599}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASC.Web.Api.Tests", "common\Tests\ASC.Web.Api.Tests\ASC.Web.Api.Tests.csproj", "{14823E93-C749-4069-9E0A-95E63DE0B254}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -201,6 +203,10 @@ Global
{8FAD3D1B-3ADC-470C-9933-CAE1B95A8599}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FAD3D1B-3ADC-470C-9933-CAE1B95A8599}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FAD3D1B-3ADC-470C-9933-CAE1B95A8599}.Release|Any CPU.Build.0 = Release|Any CPU
{14823E93-C749-4069-9E0A-95E63DE0B254}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{14823E93-C749-4069-9E0A-95E63DE0B254}.Debug|Any CPU.Build.0 = Debug|Any CPU
{14823E93-C749-4069-9E0A-95E63DE0B254}.Release|Any CPU.ActiveCfg = Release|Any CPU
{14823E93-C749-4069-9E0A-95E63DE0B254}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 5 additions & 0 deletions common/ASC.Core.Common/ASC.Core.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.5.1.25" />
<PackageReference Include="AWSSDK.SimpleEmail" Version="3.5.0.27" />
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="Grpc.Tools" Version="2.32.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down Expand Up @@ -81,4 +82,8 @@
<Protobuf Include="protos\AzRecordCache.proto" />
<Protobuf Include="protos\QuotaCacheItem.proto" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\MySql\DbContextMySql\" />
<Folder Include="Migrations\MySql\NotifyDbContextMySql\" />
</ItemGroup>
</Project>
28 changes: 14 additions & 14 deletions common/ASC.Core.Common/Data/DbAzService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class DbAzService : IAzService
{
public Expression<Func<Acl, AzRecord>> FromAclToAzRecord { get; set; }

private CoreDbContext CoreDbContext { get => LazyCoreDbContext.Value; }
private Lazy<CoreDbContext> LazyCoreDbContext { get; set; }
private UserDbContext UserDbContext { get => LazyUserDbContext.Value; }
private Lazy<UserDbContext> LazyUserDbContext { get; set; }

public DbAzService(DbContextManager<CoreDbContext> dbContextManager)
public DbAzService(DbContextManager<UserDbContext> dbContextManager)
{
LazyCoreDbContext = new Lazy<CoreDbContext>(() => dbContextManager.Value);
LazyUserDbContext = new Lazy<UserDbContext>(() => dbContextManager.Value);
FromAclToAzRecord = r => new AzRecord
{
ActionId = r.Action,
Expand All @@ -60,13 +60,13 @@ public IEnumerable<AzRecord> GetAces(int tenant, DateTime from)
{
// row with tenant = -1 - common for all tenants, but equal row with tenant != -1 escape common row for the portal
var commonAces =
CoreDbContext.Acl
UserDbContext.Acl
.Where(r => r.Tenant == Tenant.DEFAULT_TENANT)
.Select(FromAclToAzRecord)
.ToDictionary(a => string.Concat(a.Tenant.ToString(), a.SubjectId.ToString(), a.ActionId.ToString(), a.ObjectId));

var tenantAces =
CoreDbContext.Acl
UserDbContext.Acl
.Where(r => r.Tenant == tenant)
.Select(FromAclToAzRecord)
.ToList();
Expand All @@ -92,7 +92,7 @@ public IEnumerable<AzRecord> GetAces(int tenant, DateTime from)
public AzRecord SaveAce(int tenant, AzRecord r)
{
r.Tenant = tenant;
using var tx = CoreDbContext.Database.BeginTransaction();
using var tx = UserDbContext.Database.BeginTransaction();
if (!ExistEscapeRecord(r))
{
InsertRecord(r);
Expand All @@ -110,7 +110,7 @@ public AzRecord SaveAce(int tenant, AzRecord r)
public void RemoveAce(int tenant, AzRecord r)
{
r.Tenant = tenant;
using var tx = CoreDbContext.Database.BeginTransaction();
using var tx = UserDbContext.Database.BeginTransaction();
if (ExistEscapeRecord(r))
{
// escape
Expand All @@ -126,7 +126,7 @@ public void RemoveAce(int tenant, AzRecord r)

private bool ExistEscapeRecord(AzRecord r)
{
var count = CoreDbContext.Acl
var count = UserDbContext.Acl
.Where(a => a.Tenant == Tenant.DEFAULT_TENANT)
.Where(a => a.Subject == r.SubjectId)
.Where(a => a.Action == r.ActionId)
Expand All @@ -139,7 +139,7 @@ private bool ExistEscapeRecord(AzRecord r)

private void DeleteRecord(AzRecord r)
{
var record = CoreDbContext.Acl
var record = UserDbContext.Acl
.Where(a => a.Tenant == r.Tenant)
.Where(a => a.Subject == r.SubjectId)
.Where(a => a.Action == r.ActionId)
Expand All @@ -149,8 +149,8 @@ private void DeleteRecord(AzRecord r)

if (record != null)
{
CoreDbContext.Acl.Remove(record);
CoreDbContext.SaveChanges();
UserDbContext.Acl.Remove(record);
UserDbContext.SaveChanges();
}
}

Expand All @@ -165,8 +165,8 @@ private void InsertRecord(AzRecord r)
Tenant = r.Tenant
};

CoreDbContext.AddOrUpdate(r => r.Acl, record);
CoreDbContext.SaveChanges();
UserDbContext.AddOrUpdate(r => r.Acl, record);
UserDbContext.SaveChanges();
}
}
}
3 changes: 2 additions & 1 deletion common/ASC.Core.Common/Data/DbSettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ public class DbSettingsManager
internal DbSettingsManagerCache DbSettingsManagerCache { get; set; }
internal AuthContext AuthContext { get; set; }
internal TenantManager TenantManager { get; set; }
internal WebstudioDbContext WebstudioDbContext { get => LazyWebstudioDbContext.Value; }
internal Lazy<WebstudioDbContext> LazyWebstudioDbContext { get; set; }
internal WebstudioDbContext WebstudioDbContext { get => LazyWebstudioDbContext.Value; }


public DbSettingsManager()
{
Expand Down
11 changes: 7 additions & 4 deletions common/ASC.Core.Common/Data/DbTenantService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public class DbTenantService : ITenantService
private MachinePseudoKeys MachinePseudoKeys { get; }
internal TenantDbContext TenantDbContext { get => LazyTenantDbContext.Value; }
internal Lazy<TenantDbContext> LazyTenantDbContext { get; set; }

internal UserDbContext UserDbContext { get => LazyUserDbContext.Value; }
internal Lazy<UserDbContext> LazyUserDbContext { get; set; }
private static Expression<Func<DbTenant, Tenant>> FromDbTenantToTenant { get; set; }
private static Expression<Func<TenantUserSecurity, Tenant>> FromTenantUserToTenant { get; set; }

Expand Down Expand Up @@ -122,10 +123,12 @@ public DbTenantService()

public DbTenantService(
DbContextManager<TenantDbContext> dbContextManager,
DbContextManager<UserDbContext> DbContextManager,
TenantDomainValidator tenantDomainValidator,
MachinePseudoKeys machinePseudoKeys)
{
LazyTenantDbContext = new Lazy<TenantDbContext>(() => dbContextManager.Value);
LazyUserDbContext = new Lazy<UserDbContext>(() => DbContextManager.Value);
TenantDomainValidator = tenantDomainValidator;
MachinePseudoKeys = machinePseudoKeys;
}
Expand Down Expand Up @@ -159,12 +162,12 @@ public IEnumerable<Tenant> GetTenants(string login, string passwordHash)

IQueryable<TenantUserSecurity> query() => TenantsQuery()
.Where(r => r.Status == TenantStatus.Active)
.Join(TenantDbContext.Users, r => r.Id, r => r.Tenant, (tenant, user) => new
.Join(UserDbContext.Users, r => r.Id, r => r.Tenant, (tenant, user) => new
{
tenant,
user
})
.Join(TenantDbContext.UserSecurity, r => r.user.Id, r => r.UserId, (tenantUser, security) => new TenantUserSecurity
.Join(UserDbContext.UserSecurity, r => r.user.Id, r => r.UserId, (tenantUser, security) => new TenantUserSecurity
{
DbTenant = tenantUser.tenant,
User = tenantUser.user,
Expand Down Expand Up @@ -214,7 +217,7 @@ IQueryable<TenantUserSecurity> query() => TenantsQuery()
//old password
var result = q.Select(FromTenantUserToTenant).ToList();

var usersQuery = TenantDbContext.Users
var usersQuery = UserDbContext.Users
.Where(r => r.Email == login)
.Where(r => r.Status == EmployeeStatus.Active)
.Where(r => !r.Removed)
Expand Down
23 changes: 18 additions & 5 deletions common/ASC.Core.Common/EF/Context/AuditTrailContext.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@

using System;
using System.Collections.Generic;

using ASC.Common;
using ASC.Core.Common.EF.Model;

using Microsoft.EntityFrameworkCore;

namespace ASC.Core.Common.EF.Context
{
{
public class MySqlAuditTrailContext : AuditTrailContext { }
public class PostgreSqlAuditTrailContext : AuditTrailContext { }
public class AuditTrailContext : BaseDbContext
{
public DbSet<User> User { get; set; }
public DbSet<LoginEvents> LoginEvents { get; set; }
public DbSet<AuditEvent> AuditEvents { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
ModelBuilderWrapper
.From(modelBuilder, Provider)
.AddUser()
.AddLoginEvents()
.AddAuditEvent()
.AddDbFunction();
}

protected override Dictionary<Provider, Func<BaseDbContext>> ProviderContext
{
get
{
return new Dictionary<Provider, Func<BaseDbContext>>()
{
{ Provider.MySql, () => new MySqlCoreDbContext() } ,
{ Provider.Postgre, () => new PostgreSqlCoreDbContext() } ,
};
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions common/ASC.Core.Common/EF/Context/CoreDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class CoreDbContext : BaseDbContext
{
public DbSet<DbTariff> Tariffs { get; set; }
public DbSet<DbButton> Buttons { get; set; }
public DbSet<Acl> Acl { get; set; }
public DbSet<DbQuota> Quotas { get; set; }
public DbSet<DbQuotaRow> QuotaRows { get; set; }
protected override Dictionary<Provider, Func<BaseDbContext>> ProviderContext
Expand All @@ -32,7 +31,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
ModelBuilderWrapper
.From(modelBuilder, Provider)
.AddAcl()
.AddDbButton()
.AddDbQuotaRow()
.AddDbQuota()
Expand Down
6 changes: 0 additions & 6 deletions common/ASC.Core.Common/EF/Context/MessagesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public class MySqlMessagesContext : MessagesContext { }
public class PostgreSqlMessagesContext : MessagesContext { }
public class MessagesContext : BaseDbContext
{
public DbSet<AuditEvent> AuditEvents { get; set; }
public DbSet<LoginEvents> LoginEvents { get; set; }
public DbSet<DbTenant> Tenants { get; set; }
public DbSet<DbWebstudioSettings> WebstudioSettings { get; set; }

protected override Dictionary<Provider, Func<BaseDbContext>> ProviderContext
{
Expand All @@ -33,9 +30,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
ModelBuilderWrapper
.From(modelBuilder, Provider)
.AddDbTenant()
.AddWebstudioSettings()
.AddAuditEvent()
.AddLoginEvents()
.AddDbFunction();
}
Expand Down
4 changes: 0 additions & 4 deletions common/ASC.Core.Common/EF/Context/TenantDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class PostgreSqlTenantDbContext : TenantDbContext { }
public class TenantDbContext : BaseDbContext
{
public DbSet<DbTenant> Tenants { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserSecurity> UserSecurity { get; set; }
public DbSet<DbTenantVersion> TenantVersion { get; set; }
public DbSet<DbTenantPartner> TenantPartner { get; set; }
public DbSet<DbTenantForbiden> TenantForbiden { get; set; }
Expand Down Expand Up @@ -42,10 +40,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
ModelBuilderWrapper
.From(modelBuilder, Provider)
.AddUser()
.AddDbTenant()
.AddCoreSettings()
.AddUserSecurity()
.AddDbTenantForbiden()
.AddTenantIpRestrictions()
.AddDbTenantPartner()
Expand Down
11 changes: 2 additions & 9 deletions common/ASC.Core.Common/EF/Context/UserDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@

namespace ASC.Core.Common.EF
{
public class MySqlUserDbContext : UserDbContext
{

}

public class PostgreUserDbContext : UserDbContext
{

}
public class MySqlUserDbContext : UserDbContext{}
public class PostgreUserDbContext : UserDbContext{}

public class UserDbContext : BaseDbContext
{
Expand Down
9 changes: 7 additions & 2 deletions common/ASC.Core.Common/EF/Model/DbWebstudioSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ public static ModelBuilderWrapper AddWebstudioSettings(this ModelBuilderWrapper
.Add(MySqlAddWebstudioSettings, Provider.MySql)
.Add(PgSqlAddWebstudioSettings, Provider.Postgre)
.HasData(
new DbWebstudioSettings { TenantId = 1, Id = Guid.Parse("9a925891-1f92-4ed7-b277-d6f649739f06"), UserId = Guid.Parse("00000000-0000-0000-0000-000000000000"), Data = "{'Completed':false}" }
);
new DbWebstudioSettings
{
TenantId = 1,
Id = Guid.Parse("9a925891-1f92-4ed7-b277-d6f649739f06"),
UserId = Guid.Parse("00000000-0000-0000-0000-000000000000"),
Data = "{\"Completed\":false}"
});
return modelBuilder;
}

Expand Down
7 changes: 1 addition & 6 deletions common/ASC.Core.Common/EF/Model/DbWebstudioUservisit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ public static ModelBuilderWrapper AddWebstudioUserVisit(this ModelBuilderWrapper
{
modelBuilder
.Add(MySqlAddWebstudioUserVisit, Provider.MySql)
.Add(PgSqlAddWebstudioUserVisit, Provider.Postgre)
.HasData(
new DbWebstudioUserVisit { TenantId = 1, VisitDate = DateTime.UtcNow, ProductId = Guid.Parse("00000000-0000-0000-0000-000000000000"), UserId = Guid.Parse("66faa6e4-f133-11ea-b126-00ffeec8b4ef"), VisitCount = 3, FirstVisitTime = DateTime.UtcNow, LastVisitTime = DateTime.UtcNow },
new DbWebstudioUserVisit { TenantId = 1, VisitDate = DateTime.UtcNow, ProductId = Guid.Parse("00000000-0000-0000-0000-000000000000"), UserId = Guid.Parse("66faa6e4-f133-11ea-b126-00ffeec8b4ef"), VisitCount = 2, FirstVisitTime = DateTime.UtcNow, LastVisitTime = DateTime.UtcNow },
new DbWebstudioUserVisit { TenantId = 1, VisitDate = DateTime.UtcNow, ProductId = Guid.Parse("e67be73d-f9ae-4ce1-8fec-1880cb518cb4"), UserId = Guid.Parse("66faa6e4-f133-11ea-b126-00ffeec8b4ef"), VisitCount = 1, FirstVisitTime = DateTime.UtcNow, LastVisitTime = DateTime.UtcNow }
);
.Add(PgSqlAddWebstudioUserVisit, Provider.Postgre);

return modelBuilder;
}
Expand Down
3 changes: 3 additions & 0 deletions common/ASC.Core.Common/EF/Model/Mail/GreyListingWhiteList.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

using Microsoft.EntityFrameworkCore;

namespace ASC.Core.Common.EF.Model.Mail
{
[Keyless]
public class GreyListingWhiteList
{
public string Comment { get; set; }
Expand Down
3 changes: 3 additions & 0 deletions common/ASC.Core.Common/EF/Model/Regions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

using Microsoft.EntityFrameworkCore;

namespace ASC.Core.Common.EF.Model
{
[Keyless]
public class Regions
{
public string Region { get; set; }
Expand Down
14 changes: 12 additions & 2 deletions common/ASC.Core.Common/EF/Model/Tenant/DbQuota.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,18 @@ public static ModelBuilderWrapper AddDbQuota(this ModelBuilderWrapper modelBuild
.Add(MySqlAddDbQuota, Provider.MySql)
.Add(PgSqlAddDbQuota, Provider.Postgre)
.HasData(
new DbQuota { Tenant = -1, Name = "default", Description = null, MaxFileSize = 102400, MaxTotalSize = 10995116277760, ActiveUsers = 10000, Features = "domain,audit,controlpanel,healthcheck,ldap,sso,whitelabel,branding,ssbranding,update,support,portals:10000,discencryption,privacyroom,restore", Price = decimal.Parse("0,00"), AvangateId = "0", Visible = false }
);
new DbQuota
{
Tenant = -1,
Name = "default",
Description = null,
MaxFileSize = 102400,
MaxTotalSize = 10995116277760,
ActiveUsers = 10000,
Features = "domain,audit,controlpanel,healthcheck,ldap,sso,whitelabel,branding,ssbranding,update,support,portals:10000,discencryption,privacyroom,restore",
Price = decimal.Parse("0,00"),
AvangateId = "0", Visible = false
});

return modelBuilder;
}
Expand Down
3 changes: 1 addition & 2 deletions common/ASC.Core.Common/EF/Model/User/UserSecurity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public static ModelBuilderWrapper AddUserSecurity(this ModelBuilderWrapper model
{
Tenant = 1,
UserId = Guid.Parse("66faa6e4-f133-11ea-b126-00ffeec8b4ef"),
PwdHash = "vLFfghR5tNV3K9DKhmwArV+SbjWAcgZZzIDTnJ0JgCo=",
PwdHashSha512 = "USubvPlB+ogq0Q1trcSupg==",
PwdHash = "jGl25bVBBBW96Qi9Te4V37Fnqchz/Eu4qB9vKrRIqRg=",
LastModified = DateTime.UtcNow
});

Expand Down
Loading

0 comments on commit 3443ea2

Please sign in to comment.