Permalink
Browse files

#103 Converting from static to non-static no longer results in delete…

…d rows
  • Loading branch information...
1 parent 92fb265 commit 47dd79296e8837c420a219d8a0e1cd4774f10ff3 @iarna committed Apr 17, 2012
Showing with 100 additions and 62 deletions.
  1. +7 −4 Modyllic/Diff.php
  2. +4 −0 Modyllic/Generator/NativeSQL.php
  3. +86 −55 Modyllic/Generator/SQL.php
  4. +3 −3 Modyllic/Schema.php
View
@@ -46,9 +46,12 @@ private function calculate_changeset() {
}
// If our metadata table doesn't yet exist, create it
- if ( ! $this->from->sqlmeta_exists and $this->changeset->sqlmeta_exists ) {
+ if ( ! $this->from->sqlmeta_exists and $this->to->sqlmeta_exists ) {
$this->changeset->sqlmeta_exists = TRUE;
-
+ }
+ // If we no longer need a metadata table, we'll drop it
+ if ( $this->from->sqlmeta_exists and ! $this->to->sqlmeta_exists ) {
+ $this->changeset->sqlmeta_exists = FALSE;
}
# Find completely new tables
@@ -371,7 +374,7 @@ class Modyllic_Changeset {
public $remove;
public $update;
public $schema;
- public $sqlmeta_exists = FALSE;
+ public $sqlmeta_exists;
function __construct() {
$this->add = array(
@@ -519,7 +522,7 @@ function has_changes() {
count($this->add['views' ]) + count($this->update['views' ]) + count($this->remove['views' ]) +
count($this->add['triggers']) + count($this->update['triggers']) + count($this->remove['triggers'])
);
- return ($changed != 0 or $this->sqlmeta_exists or isset($this->schema->charset) or isset($this->schema->collate));
+ return ($changed != 0 or isset($this->sqlmeta_exists) or isset($this->schema->charset) or isset($this->schema->collate));
}
}
@@ -11,6 +11,10 @@
require_once "Modyllic/Schema.php";
class Modyllic_Generator_NativeSQL extends Modyllic_Generator_SQL {
+ function sqlmeta_exists($schema) {
+ return false;
+ }
+
// We include weak constraints as well as regular ones
function ignore_index( $index ) {
return FALSE;
View
@@ -15,12 +15,38 @@ class Modyllic_Generator_SQL {
protected $sep;
protected $what;
protected $source;
+ protected $from_sqlmeta_exists;
+ protected $to_sqlmeta_exists;
function __construct( $delim=';;', $sep=TRUE ) {
$this->set_what( $this->schema_types() );
$this->delim = $delim;
$this->sep = $sep;
}
+
+ function sqlmeta_exists($schema) {
+ foreach ($schema->tables as $table) {
+ if ( count($this->table_meta($table)) ) {
+ return true;
+ }
+ foreach ($table->columns as $column) {
+ if ( count($this->column_meta($column)) ) {
+ return true;
+ }
+ }
+ foreach ($table->indexes as $index) {
+ if ( count($this->index_meta($index)) ) {
+ return true;
+ }
+ }
+ }
+ foreach ($schema->routines as $routine) {
+ if ( count($this->routine_meta($routine)) ) {
+ return true;
+ }
+ }
+ return false;
+ }
function set_what($what) {
$this->what = $what;
@@ -49,6 +75,9 @@ function alter_sql( Modyllic_Diff $diff ) {
}
function alter( Modyllic_Diff $diff ) {
+ $this->from_sqlmeta_exists = $this->sqlmeta_exists($diff->from);
+ $this->to_sqlmeta_exists = $this->sqlmeta_exists($diff->to);
+
$this->source = $diff->changeset;
if ( ! $diff->changeset->has_changes() ) {
$this->cmd("-- No changes detected.");
@@ -57,8 +86,10 @@ function alter( Modyllic_Diff $diff ) {
if ( isset($this->what['database']) ) {
$this->alter_database( $diff->changeset->schema );
}
- if ( isset($this->what['sqlmeta']) and $diff->changeset->sqlmeta_exists ) {
- $this->create_sqlmeta();
+ if ( isset($this->what['sqlmeta']) ) {
+ if ( $this->to_sqlmeta_exists and ! $this->from_sqlmeta_exists ) {
+ $this->create_sqlmeta();
+ }
}
if ( isset($this->what['triggers']) ) {
@@ -108,6 +139,11 @@ function alter( Modyllic_Diff $diff ) {
if ( isset($this->what['triggers']) ) {
$this->alter_triggers( $diff->changeset->update['triggers'] );
}
+ if ( isset($this->what['sqlmeta']) ) {
+ if ( $this->from_sqlmeta_exists and ! $this->to_sqlmeta_exists ) {
+ $this->drop_sqlmeta();
+ }
+ }
$this->source = null;
return $this;
}
@@ -121,9 +157,13 @@ function create_sql( Modyllic_Schema $schema ) {
function create( Modyllic_Schema $schema) {
$this->source = $schema;
+ $this->to_sqlmeta_exists = $this->sqlmeta_exists($schema);
if ( isset($this->what['database']) ) {
$this->create_database( $schema );
}
+ if ( isset($this->what['sqlmeta']) and $this->to_sqlmeta_exists ) {
+ $this->create_sqlmeta();
+ }
if ( isset($this->what['tables']) ) {
$this->create_tables( $schema->tables, $schema );
}
@@ -169,6 +209,7 @@ function drop_sql( Modyllic_Schema $schema ) {
function drop( Modyllic_Schema $schema ) {
$this->source = $schema;
+ $this->to_sqlmeta_exists = $this->sqlmeta_exists($schema);
if ( isset($this->what['triggers']) ) {
$this->drop_triggers( $schema->triggers );
}
@@ -184,7 +225,7 @@ function drop( Modyllic_Schema $schema ) {
if ( isset($this->what['tables']) ) {
$this->drop_tables( $schema->tables );
}
- if ( isset($this->what['sqlmeta']) and $schema->sqlmeta_exists ) {
+ if ( isset($this->what['sqlmeta']) and $this->to_sqlmeta_exists ) {
$this->drop_sqlmeta();
}
if ( isset($this->what['database']) ) {
@@ -438,17 +479,12 @@ function alter_table( $table ) {
$this->end_cmd();
}
- $tometa = $this->table_meta($table);
- $frommeta = $this->table_meta($table->from);
- if ( $tometa != $frommeta ) {
- if ( count($tometa) == 0 ) {
- $this->delete_meta( "TABLE", $table->name );
- }
- else if ( count($frommeta) == 0 ) {
- $this->insert_meta( "TABLE", $table->name, $tometa );
+ if ( isset($table->static) ) {
+ if ( $table->static ) {
+ $this->insert_meta( "TABLE", $table->name, $this->table_meta($table) );
}
else {
- $this->update_meta( "TABLE", $table->name, $tometa );
+ $this->delete_meta( "TABLE", $table->name );
}
}
foreach ($table->add['columns'] as $column) {
@@ -468,40 +504,42 @@ function alter_table( $table ) {
}
}
- if ( isset($table->static) and $table->static ) {
+ if ( isset($table->static) and $table->static and ! $table->from->static ) {
$this->cmd("TRUNCATE %id", $table->name);
}
- foreach ($table->remove['data'] as $row ) {
- $this->begin_cmd();
- $this->partial( "DELETE FROM %id WHERE ", $table->name);
- $this->begin_list( " AND " );
- foreach ($row as $col=>$val) {
- $this->next_list_item();
- $this->partial( "%id=%lit", $col, $val );
+ if ( $table->static ) {
+ foreach ($table->remove['data'] as $row ) {
+ $this->begin_cmd();
+ $this->partial( "DELETE FROM %id WHERE ", $table->name);
+ $this->begin_list( " AND " );
+ foreach ($row as $col=>$val) {
+ $this->next_list_item();
+ $this->partial( "%id=%lit", $col, $val );
+ }
+ $this->end_list();
+ $this->end_cmd();
}
- $this->end_list();
- $this->end_cmd();
- }
- foreach ($table->update['data'] as $row ) {
- $this->begin_cmd();
- $this->partial( "UPDATE %id SET ", $table->name );
- $this->begin_list();
- foreach ($row['updated'] as $col=>$val) {
- $this->next_list_item();
- $this->partial( "%id=%lit", $col, $val );
+ foreach ($table->update['data'] as $row ) {
+ $this->begin_cmd();
+ $this->partial( "UPDATE %id SET ", $table->name );
+ $this->begin_list();
+ foreach ($row['updated'] as $col=>$val) {
+ $this->next_list_item();
+ $this->partial( "%id=%lit", $col, $val );
+ }
+ $this->end_list();
+ $this->partial(" WHERE ");
+ $this->begin_list(" AND ");
+ foreach ($row['where'] as $col=>$val) {
+ $this->next_list_item();
+ $this->partial( "%id=%lit", $col, $val );
+ }
+ $this->end_list();
+ $this->end_cmd();
}
- $this->end_list();
- $this->partial(" WHERE ");
- $this->begin_list(" AND ");
- foreach ($row['where'] as $col=>$val) {
- $this->next_list_item();
- $this->partial( "%id=%lit", $col, $val );
+ foreach ($table->add['data'] as $row ) {
+ $this->create_data( $table, $row );
}
- $this->end_list();
- $this->end_cmd();
- }
- foreach ($table->add['data'] as $row ) {
- $this->create_data( $table, $row );
}
return $this;
}
@@ -1042,34 +1080,27 @@ function drop_event( $event ) {
function insert_meta($kind,$which,array $meta) {
if ( count($meta) > 0 ) {
if ( ! isset($this->what['sqlmeta']) ) { return; }
- if ( ! $this->source->sqlmeta_exists ) {
- $this->create_sqlmeta();
- $this->source->sqlmeta_exists = true;
- }
$this->cmd( "INSERT INTO SQLMETA (kind,which,value) VALUES (%str, %str, %str)",
$kind, $which, json_encode($meta) );
}
}
function delete_meta($kind,$which) {
if ( ! isset($this->what['sqlmeta']) ) { return; }
- if ( ! $this->source->sqlmeta_exists ) {
- $this->create_sqlmeta();
- $this->source->sqlmeta_exists = true;
- }
+ if ( ! $this->to_sqlmeta_exists ) { return; }
$this->cmd( "DELETE FROM SQLMETA WHERE kind=%str AND which=%str",
$kind, $which );
}
function update_meta($kind,$which,array $meta) {
if ( ! isset($this->what['sqlmeta']) ) { return; }
- if ( ! $this->source->sqlmeta_exists ) {
- $this->create_sqlmeta();
- $this->source->sqlmeta_exists = true;
- }
- $this->delete_meta($kind,$which);
if ( count($meta) > 0 ) {
- $this->insert_meta($kind,$which,$meta);
+ $this->cmd( "INSERT INTO SQLMETA SET kind=%str, which=%str, value=%str ON DUPLICATE KEY UPDATE meta=%str",
+ $kind, $which, $meta, $meta );
+ }
+ else {
+ if ( ! $this->to_sqlmeta_exists ) { return; }
+ $this->delete_meta($kind,$which);
}
}
View
@@ -35,7 +35,7 @@ class Modyllic_Schema extends Modyllic_Diffable {
const DEFAULT_COLLATE = "utf8_general_ci";
public $collate = self::DEFAULT_COLLATE;
public $docs = "";
- public $sqlmeta_exists = false;
+ public $sqlmeta_exists;
function reset() {
$this->triggers = array();
@@ -48,7 +48,7 @@ function reset() {
$this->charset = self::DEFAULT_CHARSET;
$this->collate = self::DEFAULT_COLLATE;
$this->docs = "";
- $this->sqlmeta_exists = false;
+ $this->sqlmeta_exists = null;
}
function nameIsDefault() {
@@ -137,7 +137,7 @@ function unquote_sql_str($sql) {
$tok = new Modyllic_Tokenizer( $sql );
return $tok->next()->unquote();
}
-
+
/**
* Generates a meta table entry that wasn't in the schema
*/

0 comments on commit 47dd792

Please sign in to comment.