SchemaMigration and nop 4.30 - several questions?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
I have several questions about SchemaMigration and nop 4.30:

1. How to check if the database has a table?
Customer can remove the plugin folder (not unistall) and the database can have old tables
I want to remove old tables if a customer tries to install plugin again?

2. How to add the new column and remove old columns using Migration
I saw that I need to create the new NopMigration class and remove the attributes 'SkipMigrationOnUpdate'

    [SkipMigrationOnUpdate]
    [NopMigration("2020/06/18 09:27:23:6455432", "MyPlugin.NameOfPlugin base schema")]
    public class SchemaMigration : AutoReversingMigration


3. Can I use the SQL script in migration?
I saw examples from official site than I can use this command.
Execute.Script(sqlscript);
How to use it?

Thank you.
3 years ago
We recommend an approach through migrations for any changes at the DB level.
1. You can use the following properties inside Up() method:

    if(Schema.Table("{tableName}").Exists())
    {
    ...
    }


2. If you need to add/remove column use following properties:

//Edit existing column
Alter.Table("{tableName}")
    .AlterColumn("{columnName}")
    .AsString()
    .WithDefaultValue("{new value}");
//Add new column
Alter
    .Table("{tableName}")
    .AddColumn("{columnName}")
    .AsString()
    .WithDefaultValue("new value");
//Delete column
Delete     // this property is accessible from Migration class only
    .Column("{tableName}")
    .FromTable("{columnName}")

3.  Yes, you can. Just property Execute also is accessible from Migration class only:

Execute.Script("{pathToSqlScript}.sql");
// or
Execute.Sql("select * from {table}");
3 years ago
Thank you
3 years ago
Hello, I am new
How To use

   // this property is accessible from Migration class only
Delete  
    .Column("{tableName}")
    .FromTable("{columnName}")


in migration class nopcommerce 4.30

Thank you
3 years ago
khedr wrote:

How To use


You need to inherit your migration from Migration class instead of AutoReversingMigration
3 years ago

    [NopMigration("2020/06/23 09:57:00:2551771", "All New Migration")]
    public class AllNewMigration : Migration
    {
       public override void Down()
        {
           throw new System.NotImplementedException();
      }

        public override void Up()
        {
              Delete.Column("SomeNewProperty")
              .FromTable(nameof(BlogPost));
        }

    }



not working :(
3 years ago
khedr wrote:
not working :(

Hi, this code should work. Please create a new topic and describe the problem. Perhaps the community or we will help you.
3 years ago
So you have this code

[NopMigration("2020/06/23 09:57:00:2551771", "Plugin base schema")]
public class SchemaMigration: AutoReversingMigration
{
    public override void Down()
    {
        throw new System.NotImplementedException();
    }
    public override void Up()
    {
    // do some table stuff
     }
}


Is there some documentation that best details how this migration stuff works  ?
Can you explain "2020/06/23 09:57:00:2551771" what does this mean - it is a date and time - Is it when you defined this migration. Then later if you change the plugin and/or migration then you update the date and time or what is it used for ?

Why is the Down method not used when a plugin is unistalled ?
I mean it calls the Up method to install the tables when you install the pluign
then it calls the Up method to delete the tables when you uninstall the pluign
How does the Up method know what to do - is there some other definition to tell it what to do ?
Or is it as simple as
"Oh ! there are no tables in the database I better make them"
then
"Oh there are tables in the database I better delete them"

:)
3 years ago
The code from nop team wroks.
Change the class AutoReversingMigration
3 years ago
Thank you for every one here   the answer, good luck.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.