Alter table without using migrations

Topics: Writing modules
Oct 7, 2014 at 11:02 PM
Hi, is there a way to use the SchemaBuilder without having to write it in a migrations file and compile it?

The main goal is create tables and modify them in a dynamic way.

I know i can reuse the Orchard.ContentType code, but i dont want to use fields.

Oct 8, 2014 at 4:51 AM
Did you try mimicking the code in the DataMigrationManager class where it instantiates the SchemaBuilder class?
It looks like the following ought to work:
// _interpreter is constructor-injected and is of type _IDataMigrationInterpreter_.
var schemaBuilder = new SchemaBuilder(_interpreter, featurePrefix: "MyModuleName", formatPrefix: s => s.Replace(".", "_") + "_");
Oct 8, 2014 at 8:54 PM

It worked very good! i also injected IContentDefinitionManager in order to use AlterTypeDefinition and AlterPartDefinition and it also worked..

I think I will have to query that tables using CreateSqlQuery from ISessionLocator interface. I already tested it and it works. Is there a better way to do it?
Oct 8, 2014 at 9:38 PM
Edited Oct 8, 2014 at 9:39 PM
Great. Working with ISessionLocator directly is fine, although I would recommend working with HQL instead of with SQL if you can - this abstraction enables you to switch database engines more easily if you ever need to.
Another interface that might interest you is the IUpgradeService found in the Upgrade module, which looks like this:
public interface IUpgradeService : IDependency {
    void CopyTable(string fromTableName, string toTableName, string[] ignoreColumns);
    void ExecuteReader(string sqlStatement, Action<IDataReader, IDbConnection> action);
    string GetPrefixedTableName(string tableName);
The GetPrefixedTableName might be useful to determine a table name, taking into account any specified table prefix as configured during setup. At the very least, the UpgradeService implementation shows a nice example of working with ISession directly.

Also be sure to check out these excellent posts:
Oct 9, 2014 at 4:30 PM
Thx too much Skywalker!!