/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { Craft::log('Renaming `social_accounts` table to `social_login_accounts`', LogLevel::Info, true); MigrationHelper::renameTable('social_accounts', 'social_login_accounts'); Craft::log('Done renaming `social_accounts` table to `social_login_accounts`', LogLevel::Info, true); return true; }
/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { // Rename the table to charges MigrationHelper::renameTable('charge_stripe', 'charges'); // Make it an element type MigrationHelper::makeElemental('charges', 'Charge'); return true; }
/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { Craft::log('Interchanging social_users and social_accounts table names', LogLevel::Info, true); MigrationHelper::renameTable('social_users', 'social_accounts_temp'); MigrationHelper::renameTable('social_accounts', 'social_users'); MigrationHelper::renameTable('social_accounts_temp', 'social_accounts'); Craft::log('Done interchanging social_users and social_accounts table names', LogLevel::Info, true); return true; }
/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { if (!craft()->db->tableExists('tagsets')) { // Create the tagsets table craft()->db->createCommand()->createTable('tagsets', array('name' => array('maxLength' => 100, 'column' => ColumnType::Varchar, 'null' => false), 'handle' => array('maxLength' => 45, 'column' => ColumnType::Char, 'null' => false), 'fieldLayoutId' => array('column' => ColumnType::Int, 'length' => 10, 'unsigned' => false))); $this->createIndex('tagsets', 'name', true); $this->createIndex('tagsets', 'handle', true); // Create the Default tag group $this->insert('tagsets', array('name' => 'Default', 'handle' => 'default')); $tagSetId = craft()->db->getLastInsertID(); // Rename the entrytags table MigrationHelper::renameTable('entrytags', 'tags'); // Convert the tags to elements MigrationHelper::makeElemental('tags', 'Tag'); // Make some tweaks on the tags table $this->alterColumn('tags', 'name', array('column' => ColumnType::Varchar, 'null' => false)); $this->dropColumn('tags', 'count'); $this->addColumnBefore('tags', 'setId', array('column' => ColumnType::Int, 'null' => false), 'name'); $this->dropIndex('tags', 'name', true); // Place all current tags into the Default group $this->update('tags', array('setId' => $tagSetId)); $this->createIndex('tags', 'setId, name', true); $this->addForeignKey('tags', 'setId', 'tagsets', 'id', 'CASCADE', null); // Create a new field group $this->insert('fieldgroups', array('name' => 'Tags (Auto-created)')); $groupId = craft()->db->getLastInsertID(); // Create a new Tags field // Find a unique handle for ($i = 0; true; $i++) { $handle = 'tags' . ($i != 0 ? "-{$i}" : ''); $totalFields = craft()->db->createCommand()->from('fields')->where(array('handle' => $handle))->count('id'); if ($totalFields == 0) { break; } } $this->insert('fields', array('groupId' => $groupId, 'name' => 'Tags', 'handle' => $handle, 'type' => 'Tags', 'settings' => JsonHelper::encode(array('source' => 'tagset:' . $tagSetId)))); $fieldId = craft()->db->getLastInsertID(); // Migrate entrytags_enrtries data into relations $tagRelations = craft()->db->createCommand()->select('entryId, tagId, dateCreated, dateUpdated, uid')->from('entrytags_entries')->queryAll(false); foreach ($tagRelations as &$relation) { array_unshift($relation, $fieldId); } $this->insertAll('relations', array('fieldId', 'parentId', 'childId', 'dateCreated', 'dateUpdated', 'uid'), $tagRelations, false); // Update the search indexes $this->update('searchindex', array('attribute' => 'field', 'fieldId' => $fieldId), array('attribute' => 'tags'), array(), false); // Drop the old entrytags_entries table $this->dropTable('entrytags_entries'); } else { Craft::log('Tried to add the `tagsets` table, but it already exists.', LogLevel::Warning); } return true; }
/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { MigrationHelper::refresh(); $addFkBack = false; if (craft()->db->tableExists('tagsets')) { // A couple people have had failed updates that resulted in tagsets *and* taggroups tables lying around // causing a MySQL error if trying to rename the tagsets table // so let's make sure it's gone first. if (craft()->db->tableExists('taggroups')) { MigrationHelper::dropForeignKeyIfExists('taggroups', array('fieldLayoutId')); if (craft()->db->columnExists('tags', 'groupId')) { MigrationHelper::dropForeignKeyIfExists('tags', array('groupId')); MigrationHelper::renameColumn('tags', 'groupId', 'setId'); $addFkBack = true; } $this->dropTable('taggroups'); // ...and refresh the schema cache craft()->db->getSchema()->refresh(); } Craft::log('Renaming the tagsets table to taggroups.', LogLevel::Info, true); MigrationHelper::renameTable('tagsets', 'taggroups'); } if (craft()->db->columnExists('tags', 'setId')) { Craft::log('Renaming the tags.setId column to groupId.', LogLevel::Info, true); MigrationHelper::renameColumn('tags', 'setId', 'groupId'); } if ($addFkBack) { $this->addForeignKey('tags', 'groupId', 'taggroups', 'id', null, 'CASCADE'); } Craft::log('Updating the Tags fields\' settings.', LogLevel::Info, true); $fields = craft()->db->createCommand()->select('id, settings')->from('fields')->where('type="Tags"')->queryAll(); foreach ($fields as $field) { $settings = JsonHelper::decode($field['settings']); if (isset($settings['source']) && strncmp($settings['source'], 'tagset:', 7) === 0) { $settings['source'] = 'taggroup:' . substr($settings['source'], 7); $this->update('fields', array('settings' => JsonHelper::encode($settings)), array('id' => $field['id'])); } } return true; }
/** * Saves a Matrix field's settings. * * @param MatrixSettingsModel $settings The settings model. * @param bool $validate Whether the settings should be validated before being saved. * * @throws \Exception * @return bool Whether the settings saved successfully. */ public function saveSettings(MatrixSettingsModel $settings, $validate = true) { if (!$validate || $this->validateFieldSettings($settings)) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { $matrixField = $settings->getField(); // Create the content table first since the block type fields will need it $oldContentTable = $this->getContentTableName($matrixField, true); $newContentTable = $this->getContentTableName($matrixField); // Do we need to create/rename the content table? if (!craft()->db->tableExists($newContentTable)) { if ($oldContentTable && craft()->db->tableExists($oldContentTable)) { MigrationHelper::renameTable($oldContentTable, $newContentTable); } else { $this->_createContentTable($newContentTable); } } // Delete the old block types first, in case there's a handle conflict with one of the new ones $oldBlockTypes = $this->getBlockTypesByFieldId($matrixField->id); $oldBlockTypesById = array(); foreach ($oldBlockTypes as $blockType) { $oldBlockTypesById[$blockType->id] = $blockType; } foreach ($settings->getBlockTypes() as $blockType) { if (!$blockType->isNew()) { unset($oldBlockTypesById[$blockType->id]); } } foreach ($oldBlockTypesById as $blockType) { $this->deleteBlockType($blockType); } // Save the new ones $sortOrder = 0; $originalContentTable = craft()->content->contentTable; craft()->content->contentTable = $newContentTable; foreach ($settings->getBlockTypes() as $blockType) { $sortOrder++; $blockType->fieldId = $matrixField->id; $blockType->sortOrder = $sortOrder; $this->saveBlockType($blockType, false); } craft()->content->contentTable = $originalContentTable; if ($transaction !== null) { $transaction->commit(); } // Update our cache of this field's block types $this->_blockTypesByFieldId[$settings->getField()->id] = $settings->getBlockTypes(); return true; } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } } else { return false; } }
/** * @param SproutForms_FormModel $form * * @throws \Exception * @return bool */ public function saveForm(SproutForms_FormModel $form) { $formRecord = new SproutForms_FormRecord(); $isNewForm = true; if ($form->id && !$form->saveAsNew) { $formRecord = SproutForms_FormRecord::model()->findById($form->id); if (!$formRecord) { throw new Exception(Craft::t('No form exists with the ID “{id}”', array('id' => $form->id))); } $oldForm = SproutForms_FormModel::populateModel($formRecord); $isNewForm = false; $hasLayout = count($form->getFieldLayout()->getFields()) > 0; // Add the oldHandle to our model so we can determine if we // need to rename the content table $form->oldHandle = $formRecord->getOldHandle(); } // Create our new Form Record $formRecord->name = $form->name; $formRecord->handle = $form->handle; $formRecord->titleFormat = $form->titleFormat ? $form->titleFormat : "{dateCreated|date('D, d M Y H:i:s')}"; $formRecord->displaySectionTitles = $form->displaySectionTitles; $formRecord->groupId = $form->groupId; $formRecord->redirectUri = $form->redirectUri; $formRecord->submitAction = $form->submitAction; $formRecord->submitButtonText = $form->submitButtonText; $formRecord->notificationEnabled = $form->notificationEnabled; $formRecord->notificationRecipients = $form->notificationRecipients; $formRecord->notificationSubject = $form->notificationSubject; $formRecord->notificationSenderName = $form->notificationSenderName; $formRecord->notificationSenderEmail = $form->notificationSenderEmail; $formRecord->notificationReplyToEmail = $form->notificationReplyToEmail; $formRecord->validate(); $form->addErrors($formRecord->getErrors()); if ($form->saveAsNew) { $form->name = $formRecord->name; $form->handle = $formRecord->handle; } if (!$form->hasErrors()) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { // Set the field context craft()->content->fieldContext = $form->getFieldContext(); craft()->content->contentTable = $form->getContentTable(); if ($isNewForm) { $fieldLayout = $form->getFieldLayout(); // Save the field layout craft()->fields->saveLayout($fieldLayout); // Assign our new layout id info to our form model and records $form->fieldLayoutId = $fieldLayout->id; $form->setFieldLayout($fieldLayout); $formRecord->fieldLayoutId = $fieldLayout->id; } else { // If we have a layout use it, otherwise // since this is an existing form, grab the oldForm layout if ($hasLayout) { // Delete our previous record craft()->fields->deleteLayoutById($oldForm->fieldLayoutId); $fieldLayout = $form->getFieldLayout(); // Save the field layout craft()->fields->saveLayout($fieldLayout); // Assign our new layout id info to our // form model and records $form->fieldLayoutId = $fieldLayout->id; $form->setFieldLayout($fieldLayout); $formRecord->fieldLayoutId = $fieldLayout->id; } else { // We don't have a field layout right now $form->fieldLayoutId = NULL; } } // Create the content table first since the form will need it $oldContentTable = $this->getContentTableName($form, true); $newContentTable = $this->getContentTableName($form); // Do we need to create/rename the content table? if (!craft()->db->tableExists($newContentTable)) { if ($oldContentTable && craft()->db->tableExists($oldContentTable)) { MigrationHelper::renameTable($oldContentTable, $newContentTable); } else { $this->_createContentTable($newContentTable); } } if (craft()->elements->saveElement($form)) { // Create the new fields if ($form->saveAsNew) { // Duplicate the fields in the newContent Table also set the fields in the craft fields table $newFields = array(); foreach ($form->getFields() as $key => $value) { $field = new FieldModel(); $field->name = $value->name; $field->handle = $value->handle; $field->instructions = $value->instructions; $field->required = $value->required; $field->translatable = (bool) $value->translatable; $field->type = $value->type; if (isset($value->settings)) { $field->settings = $value->settings; } craft()->content->fieldContext = $form->getFieldContext(); craft()->content->contentTable = $form->getContentTable(); craft()->fields->saveField($field); array_push($newFields, $field); SproutFormsPlugin::log('Saved field as new ' . $field->id); } // Update fieldId on layoutfields table $fieldLayout = $form->getFieldLayout(); $fieldLayoutIds = FieldLayoutFieldRecord::model()->findAll("layoutId = {$fieldLayout->id}"); foreach ($fieldLayoutIds as $key => $layout) { SproutFormsPlugin::log('Updated field layout ' . $layout->id); $model = FieldLayoutFieldRecord::model()->findByPk($layout->id); $model->fieldId = $newFields[$key]->id; $model->save(); } } // Now that we have an element ID, save it on the other stuff if ($isNewForm) { $formRecord->id = $form->id; } // Save our Form Settings $formRecord->save(false); if ($transaction !== null) { $transaction->commit(); } return true; } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } } }