Permalink
Browse files

Updated for 2.3

  • Loading branch information...
1 parent a99b292 commit 2633b54ba2961b3018387435d6ca76b645aca14e @nickdunn nickdunn committed Mar 15, 2012
Showing with 59 additions and 100 deletions.
  1. +4 −5 README.markdown
  2. +0 −19 class.mysql.php.txt
  3. +54 −21 extension.driver.php
  4. +1 −1 extension.meta.xml
  5. +0 −54 lib/class.logquery.php
View
@@ -4,14 +4,13 @@
1. Download and upload the 'db_sync' folder to your Symphony 'extensions' folder.
2. Enable the extension by selecting "Database Synchroniser" in the list and choose Enable from the with-selected menu, then click Apply.
-3. Modify the `query()` function in `symphony/lib/toolkit/class.mysql.php` adding the lines between `// Start database logger` and `// End database logger` from the `class.mysql.php.txt` file included with this extension. Place these at the very end of the function just before the `return` to ensure this query does not interfere with Profile performance logging.
## Warning
-Since this extension requires a core file modification, changes you make to the MySQL class will be lost when you upgrade Symphony. Remember to add in the logging call back into `class.mysql.php` if you update Symphony!
-
-As of version 0.7 the queries are stored in a file named `db_sync.sql` in your `/manifest` folder. This is unsecured, and therefore I strongly advise that this extension only be enabled on development environments.
+As of version 0.7, queries are stored in a file named `db_sync.sql` in your `/manifest` folder. This file is visible to anyone, therefore I strongly advise that this extension only be enabled on development environments. Don't deploy it to production, or disable it entirely by looking for `db_sync` in Symphony's config file.
## Disclaimer
-While this extension has worked well for my own projects, I can't guarantee its stability. My workflow when using a development/staging/production environment is to install this extension on the development server only. When making a release I pull the production database back to staging where I apply the db_sync SQL file. If all goes well after testing, I back up production and run the same db_sync file. The log is then flushed and I can continue developing towards another release.
+While this extension has worked well for my own projects, I can't guarantee its stability for your own. My workflow when using a development/staging/production environment is to install this extension on the development server only. When making a release I pull the production database back to staging where I apply the db_sync SQL file. If all goes well after testing, I back up production and run the same db_sync file there. The file is then removed locally and I can continue developing towards another release.
+
+Please, please, please back up your production database before applying any structural changes.
View
@@ -1,19 +0,0 @@
-public function query($query){
-
- ...
- ...
- ...
-
- // ==============================================================
- // Start database logger
- // ==============================================================
- require_once(EXTENSIONS . '/db_sync/lib/class.logquery.php');
- if (class_exists('LogQuery')) LogQuery::log($query);
- // ==============================================================
- // End database logger
- // ==============================================================
-
-
- return true;
-
-}
View
@@ -1,43 +1,76 @@
<?php
-
- require_once(EXTENSIONS . '/db_sync/lib/class.logquery.php');
Class extension_db_sync extends Extension {
-
- public function about() {
+
+ public static $meta_written = FALSE;
+
+ public function getSubscribedDelegates() {
return array(
- 'name' => 'Database Synchroniser',
- 'version' => '0.9.1',
- 'release-date' => '2011-02-10',
- 'author' => array(
- 'name' => 'Nick Dunn, Richard Warrender',
- 'website' => 'http://airlock.com',
- 'email' => 'nick.dunn@airlock.com'
- ),
- 'description' => 'Logs structural database changes to allow syncing between builds.'
+ array(
+ 'page' => '/backend/',
+ 'delegate' => 'PostQueryExecution',
+ 'callback' => 'log'
+ )
);
}
public function install() {
Symphony::Configuration()->set('enabled', 'yes', 'db_sync');
Administration::instance()->saveConfig();
- return true;
+ return TRUE;
}
public function uninstall() {
if (file_exists(MANIFEST . '/db_sync.sql')) unlink(MANIFEST . '/db_sync.sql');
-
Symphony::Configuration()->remove('db_sync');
Administration::instance()->saveConfig();
}
- public static function addToLogFile($line) {
+ public static function log($context) {
+ if(Symphony::Configuration()->get('enabled', 'db_sync') == 'no') return;
+
+ $query = $context['query'];
+
+ $tbl_prefix = Symphony::Configuration()->get('tbl_prefix', 'database');
+
+ /* FILTERS */
+ // only structural changes, no SELECT statements
+ if (!preg_match('/^(insert|update|delete|create|drop|alter|rename)/i', $query)) return;
+ // un-tracked tables (sessions, cache, authors)
+ if (preg_match("/{$tbl_prefix}(authors|cache|forgotpass|sessions)/i", $query)) return;
+ // content updates in tbl_entries (includes tbl_entries_fields_*)
+ if (preg_match('/^(insert|delete|update)/i', $query) && preg_match("/({$config->tbl_prefix}entries)/i", $query)) return;
+
+ $line = '';
+
+ if(self::$meta_written == FALSE) {
+
+ $line .= "\n" . '-- ' . date('Y-m-d H:i:s', time());
+
+ $author = Administration::instance()->Author;
+ if (isset($author)) $line .= ', ' . $author->getFullName();
+
+ $url = Administration::instance()->getCurrentPageURL();
+ if (!is_null($url)) $line .= ', ' . $url;
+
+ $line .= "\n";
+
+ self::$meta_written = TRUE;
+
+ }
+
+ $query = trim($query);
+
+ // append query delimeter if it doesn't exist
+ if (!preg_match('/;$/', $query)) $query .= ";";
+
+ $line .= $query . "\n";
+
$logfile = MANIFEST . '/db_sync.sql';
$handle = @fopen($logfile, 'a');
fwrite($handle, $line);
- fclose($handle);
- }
+ fclose($handle);
+
+ }
- }
-
-?>
+ }
View
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
-<extension id="db_sync">
+<extension id="db_sync" status="released" xmlns="http://symphony-cms.com/schemas/extension/1.0">
<name>Database Synchroniser</name>
<description>Log database changes to easily synchronise structure (sections, fields, pages) development and production databases, without affecting content.</description>
<repo type="github">https://github.com/nickdunn/db_sync</repo>
View
@@ -1,54 +0,0 @@
-<?php
-
-class LogQuery {
-
- public static $meta_written = FALSE;
-
- static function log($query) {
- // prevent execution on the frontend
- if(!class_exists('Administration')) return;
- //if(!Symphony::Engine() instanceOf Administration) return;
-
- if(Symphony::Configuration()->get('enabled', 'db_sync') == 'no') return;
-
- $tbl_prefix = Symphony::Configuration()->get('tbl_prefix', 'database');
-
- /* FILTERS */
- // only structural changes, no SELECT statements
- if (!preg_match('/^(insert|update|delete|create|drop|alter|rename)/i', $query)) return;
- // un-tracked tables (sessions, cache, authors)
- if (preg_match("/{$tbl_prefix}(authors|cache|forgotpass|sessions)/i", $query)) return;
- // content updates in tbl_entries (includes tbl_entries_fields_*)
- if (preg_match('/^(insert|delete|update)/i', $query) && preg_match("/({$config->tbl_prefix}entries)/i", $query)) return;
-
- $line = '';
-
- if(self::$meta_written == FALSE) {
-
- $line .= "\n" . '-- ' . date('Y-m-d H:i:s', time());
-
- $author = Administration::instance()->Author;
- if (isset($author)) $line .= ', ' . $author->getFullName();
-
- $url = Administration::instance()->getCurrentPageURL();
- if (!is_null($url)) $line .= ', ' . $url;
-
- $line .= "\n";
-
- self::$meta_written = TRUE;
-
- }
-
- $query = trim($query);
-
- // append query delimeter if it doesn't exist
- if (!preg_match('/;$/', $query)) $query .= ";";
-
- $line .= $query . "\n";
-
- require_once(EXTENSIONS . '/db_sync/extension.driver.php');
- extension_db_sync::addToLogFile($line);
-
- }
-
-}

0 comments on commit 2633b54

Please sign in to comment.