Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove the very old ZF2 code and point people at SFTW

  • Loading branch information...
commit 237490d807b7347abe2ae803c9b7dea9bad117d8 1 parent 3b13121
@akrabat authored
View
77 README.md
@@ -2,6 +2,9 @@
See [Akrabat_Db_Schema_Manager: Zend Framework database migrations](http://akrabat.com/zend-framework/akrabat_db_schema_manager-zend-framework-database-migrations/) for full details. This code is licensed under the [New-BSD license](http://akrabat.com/license/new_bsd/).
+Akrabat_Db_Schema_Manager is a tool for use with ZF1. If you need a standalone tool, then have a look at [South for the Winter](https://github.com/startupdevs/sftw).
+
+
## ZF1:
To get the Zend_Tool provider working:
@@ -76,80 +79,6 @@ To get the Zend_Tool provider working:
resources.db.table_prefix = "prefix"
-
-## ZF2:
-
-To get the Zend\Tool provider working:
-
-1. Place a copy of ZF2 in `/usr/local/include/zf2/`, so that the Zend folder is in `zf2/library/Zend`:
-
- cd /usr/local/include
- git clone git://git.zendframework.com/zf.git zf2
-
- Don't forget to periodically update with `git pull origin master`
-2. Update your `~/.bash_profile` to set up an alias to the ZF2 zf.sh script
-
- alias zf2='export ZF_CONFIG_FILE=~/.zf2.ini; /www/zend-framework/zf2/bin/zf.sh'
-
- Restart your terminal or `source ~/.bash_profile`
-4. Place a copy of the Akrabat Tools in `/usr/local/include/Akrabat` if you haven't already done so:
-
- cd /usr/local/include`
- git clone http://github.com/akrabat/Akrabat.git
-
-5. Create `~/.zf2.ini` so that it contains:
-
- php.include_path = "/usr/local/include/zf2/library:/usr/local/include/Akrabat/zf2/"
- autoloadernamespaces.0 = "Akrabat\"
- basicloader.classes.0 = "Akrabat\Tool\DatabaseSchemaProvider"
-
-6. `zf` should provide a help screen with the `DatabaseSchema` provider at the bottom.
-
-
-### Akrabat\Db\Schema\Manager
-
-1. Create scripts/migrations folder in your ZF application
-2. Create migration files within migrations with the file name format of nnn-Xxxx.php. e.g. 001-Users.php
- where:
- nnn => any number. The lower numbered files are executed first
- Xxx => any name. This is the class name within the file.
-
-3. Create a class in your migrations file. Example for 001-Users.php:
-
- class Users extends \Akrabat\Db\Schema\AbstractChange
- {
- function up()
- {
- $tableName = $this->_tablePrefix . 'users';
- $sql = "
- CREATE TABLE IF NOT EXISTS $tableName (
- id int(11) NOT NULL AUTO_INCREMENT,
- username varchar(50) NOT NULL,
- password varchar(75) NOT NULL,
- roles varchar(200) NOT NULL DEFAULT 'user',
- PRIMARY KEY (id)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
- $this->_db->query($sql);
-
- $data = array();
- $data['username'] = 'admin';
- $data['password'] = sha1('password');
- $data['roles'] = 'user,admin';
- $this->_db->insert($tableName, $data);
- }
-
- function down()
- {
- $tableName = $this->_tablePrefix . 'users';
- $sql= "DROP TABLE IF EXISTS $tableName";
- $this->_db->query($sql);
- }
-
- }
-
-4. If you want a table prefix, add this to your `application.ini`:
-
- resources.db.table_prefix = "prefix"
## Phing Task
View
33 zf2/Akrabat/Db/Schema/AbstractChange.php
@@ -1,33 +0,0 @@
-<?php
-
-namespace Akrabat\Db\Schema;
-
-abstract class AbstractChange
-{
- /**
- * @var \Zend\Db\Adapter\AbstractAdapter
- */
- protected $_db;
-
- /**
- * @var string
- */
- protected $_tablePrefix;
-
- function __construct(\Zend\Db\Adapter\AbstractAdapter $db, $tablePrefix = '')
- {
- $this->_db = $db;
- $this->_tablePrefix = $tablePrefix;
- }
-
- /**
- * Changes to be applied in this change
- */
- abstract function up();
-
- /**
- * Rollback the changes made in up()
- */
- abstract function down();
-
-}
View
4 zf2/Akrabat/Db/Schema/Exception.php
@@ -1,4 +0,0 @@
-<?php
-namespace Akrabat\Db\Schema;
-
-class Exception extends \Zend\Db\Exception {}
View
205 zf2/Akrabat/Db/Schema/Manager.php
@@ -1,205 +0,0 @@
-<?php
-
-namespace Akrabat\Db\Schema;
-
-class Manager
-{
- const RESULT_OK = 'RESULT_OK';
- const RESULT_AT_CURRENT_VERSION = 'RESULT_AT_CURRENT_VERSION';
- const RESULT_NO_MIGRATIONS_FOUND = 'RESULT_NO_MIGRATIONS_FOUND';
-
- protected $_schemaVersionTableName = 'schema_version';
-
- /**
- * Directory containing migration files
- * @var string
- */
- protected $_dir;
-
- /**
- * Database adapter
- * @var Zend_Db_Adapter_Abstract
- */
- protected $_db;
-
- /**
- * Table prefix string for use by change classes
- * @var string
- */
- protected $_tablePrefix;
-
- /**
- * Constructor
- *
- * Available $options keys:
- * 'table_prefix' => prefix string to place before table names
- * 'schema_version_table_name' => name of table to use for holding the schema version number
- *
- *
- * @param $dir Directory where migrations files are stored
- * @param $db Database adapter
- * @param $tablePrefix Table prefix to be used by change files
- * @param $options Options
- */
- function __construct($dir, \Zend\Db\Adapter\AbstractAdapter $db, $tablePrefix='')
- {
- $this->_dir = $dir;
- $this->_db = $db;
- $this->_tablePrefix = $tablePrefix;
- }
-
- function getCurrentSchemaVersion()
- {
- // Ensure we have valid connection to the database
- if (!$this->_db->isConnected()) {
- $this->_db->getServerVersion();
- }
- $schemaVersionTableName = $this->getPrefixedSchemaVersionTableName();
-
- $sql = "SELECT version FROM " . $schemaVersionTableName;
- try {
- $version = $this->_db->fetchOne($sql);
- } catch (Exception $e) {
- // exception means that the schema version table doesn't exist, so create it
- $createSql = "CREATE TABLE $schemaVersionTableName (
- version bigint NOT NULL,
- PRIMARY KEY (version)
- )";
- $this->_db->query($createSql);
- $insertSql = "INSERT INTO $schemaVersionTableName (version) VALUES (0)";
- $this->_db->query($insertSql);
- $version = $this->_db->fetchOne($sql);
- }
-
- return $version;
- }
-
- function updateTo($version = null)
- {
- if (is_null($version)) {
- $version = PHP_INT_MAX;
- }
- $version = (int)$version;
- $currentVersion = $this->getCurrentSchemaVersion();
- if($currentVersion == $version) {
- return self::RESULT_AT_CURRENT_VERSION;
- }
-
- $migrations = $this->_getMigrationFiles($currentVersion, $version);
- if(empty($migrations)) {
- if ($version == PHP_INT_MAX) {
- return self::RESULT_AT_CURRENT_VERSION;
- }
- return self::RESULT_NO_MIGRATIONS_FOUND;
- }
-
- $direction = 'up';
- if ($currentVersion > $version) {
- $direction = 'down';
- }
- foreach ($migrations as $migration) {
- $this->_processFile($migration, $direction);
- }
-
- return self::RESULT_OK;
- }
-
- protected function _getMigrationFiles($currentVersion, $stopVersion, $dir = null)
- {
- if ($dir === null) {
- $dir = $this->_dir;
- }
-
- $direction = 'up';
- $from = $currentVersion;
- $to = $stopVersion;
- if($stopVersion < $currentVersion) {
- $direction = 'down';
- $from = $stopVersion;
- $to = $currentVersion;
- }
-
- $files = array();
- if (!is_dir($dir) || !is_readable($dir)) {
- return $files;
- }
-
- $d = dir($dir);
- while (false !== ($entry = $d->read())) {
- if (preg_match('/^([0-9]+)\-(.*)\.php/i', $entry, $matches) ) {
- $versionNumber = (int)$matches[1];
- $className = $matches[2];
- if ($versionNumber > $from && $versionNumber <= $to) {
- $path = $this->_relativePath($this->_dir, $dir);
- $files["v{$matches[1]}"] = array(
- 'path'=>$path,
- 'filename'=>$entry,
- 'version'=>$versionNumber,
- 'classname'=>$className);
- }
- } elseif ($entry != '.' && $entry != '..') {
- $subdir = $dir . '/' . $entry;
- if (is_dir($subdir) && is_readable($subdir)) {
- $files = array_merge(
- $files,
- $this->_getMigrationFiles(
- $currentVersion, $stopVersion, $subdir
- )
- );
- }
- }
- }
- $d->close();
-
- if($direction == 'up') {
- ksort($files);
- } else {
- krsort($files);
- }
-
- return $files;
- }
-
- protected function _processFile($migration, $direction)
- {
- $path = $migration['path'];
- $version = $migration['version'];
- $filename = $migration['filename'];
- $classname = $migration['classname'];
- require_once($this->_dir.'/'.$path.'/'.$filename);
- if (!class_exists($classname, false)) {
- throw new Exception("Could not find class '$classname' in file '$filename'");
- }
- $class = new $classname($this->_db, $this->_tablePrefix);
- $class->$direction();
-
- if($direction == 'down') {
- // current version is actually one lower than this version now
- $version--;
- }
- $this->_updateSchemaVersion($version);
- }
-
- protected function _updateSchemaVersion($version)
- {
- $schemaVersionTableName = $this->getPrefixedSchemaVersionTableName();
- $sql = "UPDATE $schemaVersionTableName SET version = " . (int)$version;
- $this->_db->query($sql);
- }
-
- public function getPrefixedSchemaVersionTableName()
- {
- return $this->_tablePrefix . $this->_schemaVersionTableName;
- }
-
- protected function _relativePath($from, $to, $ps = DIRECTORY_SEPARATOR)
- {
- $arFrom = explode($ps, rtrim($from, $ps));
- $arTo = explode($ps, rtrim($to, $ps));
- while (count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0])) {
- array_shift($arFrom);
- array_shift($arTo);
- }
- return str_pad("", count($arFrom) * 3, '..'.$ps).implode($ps, $arTo);
- }
-}
View
153 zf2/Akrabat/Tool/DatabaseSchemaProvider.php
@@ -1,153 +0,0 @@
-<?php
-
-namespace Akrabat\Tool;
-
-class DatabaseSchemaProvider
- extends \Zend\Tool\Project\Provider\AbstractProvider
-{
- /**
- * @var Zend_Db_Adapter_Interface
- */
- protected $_db;
-
- /**
- * @var string
- */
- protected $_tablePrefix;
-
- /**
- * @var Zend_Config
- */
- protected $_config;
-
- /**
- * Section name to load from config
- * @var string
- */
- protected $_appConfigSectionName;
-
- public function update($env='development', $dir='./scripts/migrations')
- {
- return $this->updateTo(null, $env, $dir);
- }
-
- public function updateTo($version, $env='development', $dir='./scripts/migrations')
- {
- $this->_init($env);
- $response = $this->_registry->getResponse();
- try {
- $db = $this->_getDbAdapter();
- $manager = new \Akrabat\Db\Schema\Manager($dir, $db, $this->getTablePrefix());
-
- $result = $manager->updateTo($version);
-
- switch ($result) {
- case \Akrabat\Db\Schema\Manager::RESULT_AT_CURRENT_VERSION:
- if (!$version) {
- $version = $manager->getCurrentSchemaVersion();
- }
- $response->appendContent("Already at version $version");
- break;
-
- case \Akrabat\Db\Schema\Manager::RESULT_NO_MIGRATIONS_FOUND :
- $response->appendContent("No migration files found to migrate from {$manager->getCurrentSchemaVersion()} to $version");
- break;
-
- default:
- $response->appendContent('Schema updated to version ' . $manager->getCurrentSchemaVersion());
- }
-
- return true;
- } catch (Exception $e) {
- $response->appendContent('AN ERROR HAS OCCURED:');
- $response->appendContent($e->getMessage());
- return false;
- }
- }
-
-
- /**
- * Provide the current schama version number
- */
- public function current($env='development', $dir='./migrations')
- {
- $this->_init($env);
- try {
-
- // Initialize and retrieve DB resource
- $db = $this->_getDbAdapter();
- $manager = new \Akrabat\Db\Schema\Manager($dir, $db, $this->getTablePrefix());
- echo 'Current schema version is ' . $manager->getCurrentSchemaVersion() . PHP_EOL;
-
- return true;
- } catch (Exception $e) {
- echo 'AN ERROR HAS OCCURED:' . PHP_EOL;
- echo $e->getMessage() . PHP_EOL;
- return false;
- }
- }
-
- protected function _getDirectory()
- {
- $dir = './scripts/migrations';
- return realpath($dir);
- }
-
- protected function _init($env)
- {
- $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
- $appConfigFileResource = $profile->search('applicationConfigFile');
- if ($appConfigFileResource == false) {
- throw new \Zend\Tool\Project\Exception('A project with an application config file is required to use this provider.');
- }
- $appConfigFilePath = $appConfigFileResource->getPath();
- $this->_config = new \Zend\Config\Ini($appConfigFilePath, $env);
-
- require_once 'Zend/Loader/StandardAutoloader.php';
- $autoloader = new \Zend\Loader\StandardAutoloader();
- $autoloader->registerNamespace('Akrabat', realpath(__DIR__. '/../'));
- $autoloader->register();
- }
-
- /**
- * Retrieve initialized DB connection
- *
- * @return \Zend\Db\Adapter\AbstractAdapter
- */
- protected function _getDbAdapter()
- {
- if ((null === $this->_db)) {
- if($this->_config->resources->db){
- $dbConfig = $this->_config->resources->db;
- $this->_db = \Zend\Db\Db::factory($dbConfig->adapter, $dbConfig->params);
- } elseif($this->_config->resources->multidb){
- foreach ($this->_config->resources->multidb as $db) {
- if($db->default){
- $this->_db = \Zend\Db\Db::factory($db->adapter, $db);
- }
- }
- }
- if($this->_db instanceof \Zend\Db\Adapter\AbstractAdapter) {
- throw new Akrabat\Db\Schema\Exception('Database was not initialized');
- }
- }
- return $this->_db;
- }
-
- /**
- * Retrieve table prefix
- *
- * @return string
- */
- public function getTablePrefix()
- {
- if ((null === $this->_tablePrefix)) {
- $prefix = '';
- if (isset($this->_config->resources->db->table_prefix)) {
- $prefix = $this->_config->resources->db->table_prefix . '_';
- }
- }
- return $this->_tablePrefix;
- }
-
-}
Please sign in to comment.
Something went wrong with that request. Please try again.