public function resetDb($plugin)
{
$path = BcUtil::getSchemaPath($plugin);
if (!$path) {
return true;
}
$db = ConnectionManager::getDataSource('default');
$db->cacheSources = false;
$listSources = $db->listSources();
$prefix = $db->config['prefix'];
$Folder = new Folder($path);
$files = $Folder->read(true, true);
if (empty($files[1])) {
return true;
}
$tmpdir = TMP . 'schemas' . DS;
$result = true;
foreach ($files[1] as $file) {
$oldSchemaPath = '';
if (preg_match('/^(.*?)\\.php$/', $file, $matches)) {
$type = 'drop';
$table = $matches[1];
$schemaPath = $tmpdir;
if (preg_match('/^create_(.*?)\\.php$/', $file, $matches)) {
$type = 'drop';
$table = $matches[1];
if (!in_array($prefix . $table, $listSources)) {
continue;
}
copy($path . DS . $file, $tmpdir . $table . '.php');
} elseif (preg_match('/^alter_(.*?)\\.php$/', $file, $matches)) {
$type = 'alter';
$table = $matches[1];
if (!in_array($prefix . $table, $listSources)) {
continue;
}
$corePlugins = implode('|', Configure::read('BcApp.corePlugins'));
if (preg_match('/^(' . $corePlugins . ')/', Inflector::camelize($table), $matches)) {
$pluginName = $matches[1];
}
$File = new File($path . DS . $file);
$data = $File->read();
$data = preg_replace('/class\\s+' . Inflector::camelize($table) . 'Schema/', 'class Alter' . Inflector::camelize($table) . 'Schema', $data);
$oldSchemaPath = $tmpdir . $file;
$File = new File($oldSchemaPath);
$File->write($data);
$schemaPath = BcUtil::getSchemaPath($pluginName) . DS;
} elseif (preg_match('/^drop_(.*?)\\.php$/', $file, $matches)) {
$type = 'create';
$table = $matches[1];
if (in_array($prefix . $table, $listSources)) {
continue;
}
copy($path . DS . $file, $tmpdir . $table . '.php');
} else {
if (!in_array($prefix . $table, $listSources)) {
continue;
}
copy($path . DS . $file, $tmpdir . $table . '.php');
}
if (!$db->loadSchema(array('type' => $type, 'path' => $schemaPath, 'file' => $table . '.php', 'dropField' => true, 'oldSchemaPath' => $oldSchemaPath))) {
$result = false;
}
@unlink($tmpdir . $table . '.php');
if (file_exists($oldSchemaPath)) {
unlink($oldSchemaPath);
}
}
}
return $result;
}