Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

2.3 cake schema #759

Merged
merged 4 commits into from

3 participants

@dereuromark
Collaborator
  • corrected uncaught error with a missing table

  • removed the 5-level if clauses to a less complex construct (refactoring)

  • added option "models" for generate. The CakeSchema is already capable of it but the shell as of now could not provide the information for the schema to only export specific models. this is now also covered included test cases

see http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3101-console-schema-generate-improvements

@lorenzo
Owner

Looks good to me, let's see what the others have to say :)

@markstory
Owner

I think it look good too.

@markstory markstory merged commit 7dae124 into cakephp:2.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
11 lib/Cake/Console/Command/SchemaShell.php
@@ -96,6 +96,7 @@ public function startup() {
$name = $plugin;
}
}
+ $name = Inflector::classify($name);
$this->Schema = new CakeSchema(compact('name', 'path', 'file', 'connection', 'plugin'));
}
@@ -127,7 +128,9 @@ public function generate() {
$this->out(__d('cake_console', 'Generating Schema...'));
$options = array();
if ($this->params['force']) {
- $options = array('models' => false);
+ $options['models'] = false;
+ } elseif (!empty($this->params['models'])) {
+ $options['models'] = String::tokenize($this->params['models']);
}
$snapshot = false;
@@ -464,6 +467,10 @@ public function getOptionParser() {
'short' => 's',
'help' => __d('cake_console', 'Snapshot number to use/make.')
);
+ $models = array(
+ 'short' => 'm',
+ 'help' => __d('cake_console', 'Specify models as comma separated list.'),
+ );
$dry = array(
'help' => __d('cake_console', 'Perform a dry run on create and update commands. Queries will be output instead of run.'),
'boolean' => true
@@ -489,7 +496,7 @@ public function getOptionParser() {
))->addSubcommand('generate', array(
'help' => __d('cake_console', 'Reads from --connection and writes to --path. Generate snapshots with -s'),
'parser' => array(
- 'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'snapshot', 'force'),
+ 'options' => compact('plugin', 'path', 'file', 'name', 'connection', 'snapshot', 'force', 'models'),
'arguments' => array(
'snapshot' => array('help' => __d('cake_console', 'Generate a snapshot.'))
)
View
77 lib/Cake/Model/CakeSchema.php
@@ -254,44 +254,49 @@ public function read($options = array()) {
continue;
}
- $db = $Object->getDataSource();
- if (is_object($Object) && $Object->useTable !== false) {
- $fulltable = $table = $db->fullTableName($Object, false, false);
- if ($prefix && strpos($table, $prefix) !== 0) {
+ if (!is_object($Object) || $Object->useTable === false) {
+ continue;
+ }
+
+ $fulltable = $table = $db->fullTableName($Object, false, false);
+ if ($prefix && strpos($table, $prefix) !== 0) {
+ continue;
+ }
+ if (!in_array($fulltable, $currentTables)) {
+ continue;
+ }
+
+ $table = $this->_noPrefixTable($prefix, $table);
+
+ $key = array_search($fulltable, $currentTables);
+ if (empty($tables[$table])) {
+ $tables[$table] = $this->_columns($Object);
+ $tables[$table]['indexes'] = $db->index($Object);
+ $tables[$table]['tableParameters'] = $db->readTableParameters($fulltable);
+ unset($currentTables[$key]);
+ }
+ if (empty($Object->hasAndBelongsToMany)) {
+ continue;
+ }
+ foreach ($Object->hasAndBelongsToMany as $Assoc => $assocData) {
+ if (isset($assocData['with'])) {
+ $class = $assocData['with'];
+ }
+ if (!is_object($Object->$class)) {
continue;
}
- $table = $this->_noPrefixTable($prefix, $table);
-
- if (in_array($fulltable, $currentTables)) {
- $key = array_search($fulltable, $currentTables);
- if (empty($tables[$table])) {
- $tables[$table] = $this->_columns($Object);
- $tables[$table]['indexes'] = $db->index($Object);
- $tables[$table]['tableParameters'] = $db->readTableParameters($fulltable);
- unset($currentTables[$key]);
- }
- if (!empty($Object->hasAndBelongsToMany)) {
- foreach ($Object->hasAndBelongsToMany as $Assoc => $assocData) {
- if (isset($assocData['with'])) {
- $class = $assocData['with'];
- }
- if (is_object($Object->$class)) {
- $withTable = $db->fullTableName($Object->$class, false, false);
- if ($prefix && strpos($withTable, $prefix) !== 0) {
- continue;
- }
- if (in_array($withTable, $currentTables)) {
- $key = array_search($withTable, $currentTables);
- $noPrefixWith = $this->_noPrefixTable($prefix, $withTable);
-
- $tables[$noPrefixWith] = $this->_columns($Object->$class);
- $tables[$noPrefixWith]['indexes'] = $db->index($Object->$class);
- $tables[$noPrefixWith]['tableParameters'] = $db->readTableParameters($withTable);
- unset($currentTables[$key]);
- }
- }
- }
- }
+ $withTable = $db->fullTableName($Object->$class, false, false);
+ if ($prefix && strpos($withTable, $prefix) !== 0) {
+ continue;
+ }
+ if (in_array($withTable, $currentTables)) {
+ $key = array_search($withTable, $currentTables);
+ $noPrefixWith = $this->_noPrefixTable($prefix, $withTable);
+
+ $tables[$noPrefixWith] = $this->_columns($Object->$class);
+ $tables[$noPrefixWith]['indexes'] = $db->index($Object->$class);
+ $tables[$noPrefixWith]['tableParameters'] = $db->readTableParameters($withTable);
+ unset($currentTables[$key]);
}
}
}
View
64 lib/Cake/Test/Case/Console/Command/SchemaShellTest.php
@@ -363,6 +363,41 @@ public function testGenerateWithPlugins() {
}
/**
+ * test generate with specific models
+ *
+ * @return void
+ */
+ public function testGenerateModels() {
+ App::build(array(
+ 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
+ ), App::RESET);
+ CakePlugin::load('TestPlugin');
+
+ $this->db->cacheSources = false;
+ $this->Shell->params = array(
+ 'plugin' => 'TestPlugin',
+ 'connection' => 'test',
+ 'models' => 'TestPluginComment',
+ 'force' => false,
+ 'overwrite' => true
+ );
+ $this->Shell->startup();
+ $this->Shell->Schema->path = TMP . 'tests' . DS;
+
+ $this->Shell->generate();
+ $this->file = new File(TMP . 'tests' . DS . 'schema.php');
+ $contents = $this->file->read();
+
+ $this->assertRegExp('/class TestPluginSchema/', $contents);
+ $this->assertRegExp('/public \$test_plugin_comments/', $contents);
+ $this->assertNotRegExp('/public \$authors/', $contents);
+ $this->assertNotRegExp('/public \$auth_users/', $contents);
+ $this->assertNotRegExp('/public \$posts/', $contents);
+ CakePlugin::unload();
+ }
+
+
+/**
* Test schema run create with no table args.
*
* @return void
@@ -464,6 +499,35 @@ public function testPluginParam() {
}
/**
+ * test that underscored names also result in CamelCased class names
+ *
+ * @return void
+ */
+ public function testName() {
+ App::build(array(
+ 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
+ ));
+ CakePlugin::load('TestPlugin');
+ $this->Shell->params = array(
+ 'plugin' => 'TestPlugin',
+ 'connection' => 'test',
+ 'name' => 'custom_name',
+ 'force' => false,
+ 'overwrite' => true,
+ );
+ $this->Shell->startup();
+ if (file_exists($this->Shell->Schema->path . DS . 'custom_name.php')) {
+ unlink($this->Shell->Schema->path . DS . 'custom_name.php');
+ }
+ $this->Shell->generate();
+
+ $contents = file_get_contents($this->Shell->Schema->path . DS . 'custom_name.php');
+ $this->assertRegExp('/class CustomNameSchema/', $contents);
+ unlink($this->Shell->Schema->path . DS . 'custom_name.php');
+ CakePlugin::unload();
+ }
+
+/**
* test that using Plugin.name with write.
*
* @return void
Something went wrong with that request. Please try again.