项目作者: graze

项目描述 :
Tools for parsing, extracting, and diffing mysqldump files
高级语言: PHP
项目地址: git://github.com/graze/morphism.git
创建时间: 2014-09-08T10:45:19Z
项目社区:https://github.com/graze/morphism

开源协议:MIT License

下载


morphism

Latest Version on Packagist
Software License
Build Status
Coverage Status
Quality Score
Total Downloads
PHP Version
MicroBadger Size

Morph and Chas

This package provides a set of tools for parsing, extracting, and diffing mysqldump
files.

A typical application of this is for managing schema changes during application
development (keeping schemas in sync with code when switching branches), and during
deployment (migrating the schema to match the deployed code).

Using this tool allows you to store the complete database schema in the repository.
When a branch
requires a schema update to work properly, you should edit your checkout’s schema
and run the new tool to figure out the necessary ALTER / CREATE / DROP statements
to run, and apply them. Similarly, when switching branches you can simply run the
tool and it will apply the necessary changes automatically.

This has the additional benefit that the complete history of the schema is stored
under version control, instead of a series of incremental change scripts. If more
than one party changes the same table, git will merge the changes automatically,
or generate a conflict for manual merging where it cannot. All the usual git tools
become useful - e.g. a simple git annotate schema/catalog/product.sql can tell
you who added a redundant index on pr_name.

Install

Via Composer

  1. $ composer require graze/morphism

Running With Docker

  1. $ docker run --rm graze/morphism

Examples:

  1. $ docker run --rm -v $PWD/config:/app/config -v $PWD/schema:/app/schema:cached graze/morphism diff config/morphism.yml
  2. $ docker run --rm -v $PWD/config:/app/config -v $PWD/schema:/app/schema:delegated graze/morphism dump config/morphism.yml

Attaching to an existing network when developing

You can add morphism to your docker-compose file and can talk to your databases locally.
Or if you have an existing docker network you can do:

  1. $ docker run --rm -v $PWD/config:/app/config -v $PWD/schema:/app/schema:cached --network app_default graze/morphsim diff config/morphism.yml

Tools

All commands support the --help parameter which give more information on usage.

  • morphism extract: Extract schema definitions from a mysqldump file.
  • morphism dump: Dump database schema for a named database connection.
  • morphism lint: Check database schema files for correctness.
  • morphism diff: Show necessary DDL statements to make a given database match the schema files. Optionally apply the changes too.

Config File

The config file used by some of morphism’s tools uses yaml format, as follows:

  1. # All connection definitions appear under the 'databases' key
  2. databases:
  3. # name of connection
  4. catalog:
  5. # Connection details - this is just an example, you may want to specify
  6. # different properties, e.g. if connecting to a remote server. You are
  7. # advised to refer to the 'pdo' documentation for further details.
  8. user: 'my-user'
  9. password: 'my-password'
  10. host: 'localhost'
  11. driver: 'pdo_mysql'
  12. unix_socket: '/var/lib/mysql/catalog.sock'
  13. # morphism specific options
  14. morphism:
  15. # morphism diff only operates on connections with 'enable: true'
  16. enable: true
  17. # Path where schema files live.
  18. # Defaults to "schema/<connection-name>"
  19. schemaDefinitionPath:
  20. - schema/catalog
  21. # you may optionally specify one or more regexes matching tables
  22. # to exclude (any changes, creation or deletion of matching tables
  23. # will be ignored). The regex must match the entire table name, i.e.
  24. # it is implicitly anchored with ^...$
  25. exclude:
  26. - temp_.*
  27. - page_load_\d{4}-\d{2}-\d{2}
  28. # similarly, you may optionally specify tables for explicit inclusion.
  29. include:
  30. ...
  31. # you may specify more connections
  32. ...
  33. # other top level keys are ignored
  34. ...

Example Usage

This example uses morphism dump to generate schema files from a database, morphism lint for checking the files and morphism diff to apply changes both interactively and automatically.

  1. (master) $ # create a baseline for the schema
  2. (master) $ mkdir schema
  3. (master) $ bin/morphism dump --write config.yml catalog
  4. (master) $ git add schema/catalog
  5. (master) $ git commit -m "initial checkin of catalog schema"
  6. (master) $
  7. (master) $ # start work on changes to the catalog...
  8. (master) $ git checkout -b catalog-fixes
  9. (catalog-fixes) $ vi schema/catalog/product.sql # edit table definition
  10. (catalog-fixes) $ vi schema/catalog/product_dimensions.sql # add new table
  11. (catalog-fixes) $ bin/morphism lint schema/catalog # check syntax
  12. ERROR schema/catalog/product_dimensions.sql, line 2: unknown datatype 'intt'
  13. 1: CREATE TABLE product_dimensions (
  14. 2: `pd_id` intt<<HERE>>(10) unsigned NOT NULL AUTO_INCREMENT,
  15. (catalog-fixes) $ vi schema/catalog/product_dimensions.sql # fix table definition
  16. (catalog-fixes) $ bin/morphism lint schema/catalog # check syntax
  17. (catalog-fixes) $ git add schema/catalog
  18. (catalog-fixes) $ git rm schema/catalog/discontinued.sql # delete a table
  19. (catalog-fixes) $ git commit -m "various changes to catalog schema"
  20. (catalog-fixes) $ # alter the database to match the schema files
  21. (catalog-fixes) $ bin/morphism diff --apply-changes=confirm config.yml catalog
  22. -- --------------------------------
  23. -- Connection: catalog
  24. -- --------------------------------
  25. DROP TABLE IF EXISTS `discontinued`;
  26. ALTER TABLE `product`
  27. MODIFY COLUMN `pr_name` varchar(255) NOT NULL,
  28. MODIFY COLUMN `pr_description` text NOT NULL,
  29. ADD COLUMN `pr_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER `pr_description`;
  30. CREATE TABLE `product_dimensions` (
  31. `pd_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  32. `pd_width` decimal(10,2) NOT NULL,
  33. `pd_height` decimal(10,2) NOT NULL,
  34. `pd_depth` decimal(10,2) NOT NULL,
  35. PRIMARY KEY (`pd_id`)
  36. ) ENGINE=InnoDB;
  37. -- Confirm changes to catalog:
  38. DROP TABLE IF EXISTS `discontinued`;
  39. -- Apply this change? [y]es [n]o [a]ll [q]uit: y
  40. ALTER TABLE `product`
  41. MODIFY COLUMN `pr_name` varchar(255) NOT NULL,
  42. MODIFY COLUMN `pr_description` text NOT NULL,
  43. ADD COLUMN `pr_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER `pr_description`;
  44. -- Apply this change? [y]es [n]o [a]ll [q]uit: y
  45. CREATE TABLE `product_dimensions` (
  46. `pd_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  47. `pd_width` decimal(10,2) NOT NULL,
  48. `pd_height` decimal(10,2) NOT NULL,
  49. `pd_depth` decimal(10,2) NOT NULL,
  50. PRIMARY KEY (`pd_id`)
  51. ) ENGINE=InnoDB;
  52. -- Apply this change? [y]es [n]o [a]ll [q]uit: y
  53. (catalog-fixes) $ # hack hack hack
  54. (catalog-fixes) $ ...
  55. (catalog-fixes) $ # do some work back on master...
  56. (catalog-fixes) $ git checkout master
  57. (master) $ # restore schema to previous state
  58. (master) $ bin/morphism diff --apply-changes=yes config.yml catalog

Testing

  1. $ make test

Security

If you discover any security related issues, please email security@graze.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.