[BulkInsert] Multi-tenant support

[BulkInsert] Multi-tenant support

  
Forge Component
(6)
Published on 2015-11-24 by João Pedro Abreu
6 votes
Published on 2015-11-24 by João Pedro Abreu

When running bulk insert, I noticed tenant_id is not populated. To fix this I added the column to the SQL statement:

private void MySqlBulkInsert(IOSList recordList, string ssTableName, string ssColumnsToIgnore, Dictionary<string, FieldHolder> recordFields)
        {
            string[] columnsToIgnoreArray = ssColumnsToIgnore.Split(',');
            ArrayList columnsToIgnoreHash = new ArrayList(columnsToIgnoreArray);

            DatabaseProvider dbp = DatabaseAccess.ForRunningApplication();
            MySqlConnection con = (MySqlConnection)dbp.GetRequestTransaction().GetConnection().GetDriverConnection();

            #region Create MySQL Command

            MySqlCommand sqlCommand = con.CreateCommand();
            sqlCommand.CommandType = CommandType.Text;
           
            string sql = "insert into " + ssTableName + " (";
            int k = 0;
            foreach (FieldHolder field in recordFields.Values)
            {
                if (k++ > 0) sql += ",";
                sql += field.name(true).ToUpper();
            }

            // TJI Add tenant id
            sql += ", tenant_id";

            sql += ") values (";
            k = 0;
            foreach (string fieldName in recordFields.Keys)
            {
                if (k++ > 0) sql += ",";
                sql += "@" + fieldName;
            }
           
            // TJI Add tenant id
            sql += string.Format(", {0}", AppInfo.GetAppInfo().Tenant.Id);
...

...

...


Br,

Toni

Hi Tony.

Did you tried to debug and checked if the tenant identifier is filled in each record?


No, but I did check that the tenant id is populated for all rows in the table. After the change no new rows with tenant id 0 get inserted.

Hi Toni!

Did the entity you were inserting have its tenant_id exposed?

I don't see any reason it would be populated if it was exposed.


That being said, I see why we would want to populate that field, even if it wasn't exposed, but to do that automatically, the extension would need to do some introspection into the application's metadata, or the database table's metadata, which I would like to avoid.


Hi,

Perhaps I was a not clear in my original post. The rows in the above code excerpt marked with tags //TJI will solve the issue.

// TJI Add tenant id
sql += ", tenant_id";

// TJI Add tenant id
sql += string.Format(", {0}", AppInfo.GetAppInfo().Tenant.Id);

Toni Juvani wrote:

Hi,

Perhaps I was a not clear in my original post. The rows in the above code excerpt marked with tags //TJI will solve the issue.

// TJI Add tenant id
sql += ", tenant_id";

// TJI Add tenant id
sql += string.Format(", {0}", AppInfo.GetAppInfo().Tenant.Id);

I understood your post and I'm glad that solutions solves the issue for you.

The questions I'm posting are intended to figure out a way to solve that issue without making changes to the component that will prevent it from working when the entity is not multi-tenant.

Thanks!

Ok thanks, now I understand. The entity does not have its "Show Tenant Identifier" checkbox checked.