Nop 4.4 dev branch: Nop.Data.Migrations.UpgradeTo440 DataMigration bug

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 года назад
step to reproduce:
1. install clean with sample data
2. will get NullReferenceException
3. redo install with same database(do not delete database)
4. will get second NullReferenceException
5. redo install the third time with same database(do not delete database)
6. installation success this time, but AdminRole does not have these 2 permissions because of the error:
-"Public store. Access MiniProfiler results"
-"Admin area. Manage Multi-factor Authentication Methods"

error in these 2 blocks since InstallationService.cs haven't run yet to insert the adminRole:

            // new permission
            if (!_dataProvider.GetTable<PermissionRecord>().Any(pr => string.Compare(pr.SystemName, "AccessProfiling", true) == 0))
            {
                var profilingPermission = _dataProvider.InsertEntity(
                    new PermissionRecord
                    {
                        Name = "Public store. Access MiniProfiler results",
                        SystemName = "AccessProfiling",
                        Category = "PublicStore"
                    }
                );

                //add it to the Admin role by default
                var adminRole = _dataProvider
                    .GetTable<CustomerRole>()
                    .FirstOrDefault(x => x.IsSystemRole && x.SystemName == NopCustomerDefaults.AdministratorsRoleName);

                _dataProvider.InsertEntity(
                    new PermissionRecordCustomerRoleMapping
                    {
                        CustomerRoleId = adminRole.Id,
                        PermissionRecordId = profilingPermission.Id
                    }
                );
            }


            //<MFA #475>
            if (!_dataProvider.GetTable<PermissionRecord>().Any(pr => string.Compare(pr.SystemName, "ManageMultifactorAuthenticationMethods", true) == 0))
            {
                var multiFactorAuthenticationPermission = _dataProvider.InsertEntity(
                    new PermissionRecord
                    {
                        Name = "Admin area. Manage Multi-factor Authentication Methods",
                        SystemName = "ManageMultifactorAuthenticationMethods",
                        Category = "Configuration"
                    }
                );

                //add it to the Admin role by default
                var adminRole = _dataProvider
                    .GetTable<CustomerRole>()
                    .FirstOrDefault(x => x.IsSystemRole && x.SystemName == NopCustomerDefaults.AdministratorsRoleName);

                _dataProvider.InsertEntity(
                    new PermissionRecordCustomerRoleMapping
                    {
                        CustomerRoleId = adminRole.Id,
                        PermissionRecordId = multiFactorAuthenticationPermission.Id
                    }
                );
            }
2 года назад
kaide wrote:
step to reproduce:
1. install clean with sample data
2. will get NullReferenceException


I cannot reproduce this error, installation is completed as it should. Please provide more details about it.

kaide wrote:
6. installation success this time, but AdminRole does not have these 2 permissions because of the error:
-"Public store. Access MiniProfiler results"
-"Admin area. Manage Multi-factor Authentication Methods"


Configuration of these permissions is already included in the installation process. Make sure you are trying to connect to the correct db.
2 года назад
Hi,

I had a same problem like that:
1- Added a AutoReversingMigration class :
namespace Nop.Data.Migrations.Indexes
{
    [NopMigration("2021/10/05 01:01:00:0000000")]
    public class AddSampleTableNameIX : AutoReversingMigration
    {
        public override void Up()
        {
            Create.Index("IX_SampleTable_Name").OnTable(nameof(SampleTable))
                .OnColumn(nameof(SampleTable.Name)).Ascending()
                .WithOptions().Unique();
        }
    }
}

2- during install application when dataProvider.InitializeDatabase calls (before insert any data to db) then DataMigration.up method calls and get a null refrence error when Insert PermissionRecordCustomerRoleMapping because of has no CustomerRole for Administrators Role yet.
    [NopMigration("2020-06-10 00:00:00", "4.40.0", UpdateMigrationType.Data)]
    [SkipMigrationOnInstall]
    public class DataMigration : Migration
   {  ... }  
3- I found that it happened when New AutoReversingMigration classes have datetime greater than DataMigration class datetime, so when I increase DataMigration date to something like ''2050-06-10 00:00:00" it solved and  no more calls DataMigration.Up method when InitializeDatabase.

Logically it seems "Data Migration" should never calls during InitializeDatabase,
so please can you help me to know why this happened? and what is the right way to solve this problem?
2 года назад
Hi, Armoonia.

The essence of your problem is described by a #5580 ticket. This problem has been solved (will be available in the 4.50). You can see the solution in this commit.

Armoonia wrote:
Hi,
....
Logically it seems "Data Migration" should never calls during InitializeDatabase,
so please can you help me to know why this happened? and what is the right way to solve this problem?
2 года назад
Maybe this is off topic but another thing with Migration of the Database v4.3 to v4.4 is what permissions are required by SQL.  I was getting this error:

"The specified schema name “dbo” either does not exist or you do not have permission to use it"
I am not an SQL Expert and after reading a few pages I saw the need for "Alter on Schema" I did that and still got error, I turned on a few more Alter permission and got a different error.
Eventually I turned on every Alter Permission (along with every other permission)

Then it worked - I did not have time to go back and check which one it was.  Maybe if I was an SQL Expert I would have know what permission to turn on or what other configuration was required.

Anyway don’t know if there is something you can do programmatically or it will always be the case that you need to play with the permissions in SQL ?
2 года назад
Hi Yinda, maybe it is because of your sql login you specified during installation and it has not required permission (Windows Authentication or SQL Server Authentication Credentials),  if you want to programmatically add permission to db' your sql connection login must has admin permission, so you need at least one admin user with sysadmin Server Role. you must setup sql user permissions before install nop (or any other applications connect to database).
2 года назад
I was not using a login I was using Windows Integrated.

Maybe its just me or the way I have things setup, but I have never added an "Alter XXX" permission to the IIS APPPOOL\Websitename User before. So it was something new which took me a while to work out.

I was just saying that it would be good if the system told you what permissions you needed to have.

Or better still it was smart enough to be able to do it somehow programically via the install.
But maybe as you suggest that in itself requires a permission that the install does not normally have :)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.