1212This is a simple library written in PHP for database version control. Currently supports Sqlite, MySql, Sql Server and Postgres.
1313
1414Database Migration can be used as:
15- - Command Line Interface
16- - PHP Library to be integrated in your functional tests
17- - Integrated in you CI/CD indenpent of your programming language or framework.
18-
15+
16+ * Command Line Interface
17+ * PHP Library to be integrated in your functional tests
18+ * Integrated in you CI/CD indenpent of your programming language or framework.
19+
1920Database Migrates uses only SQL commands for versioning your database.
2021
2122## Why pure SQL commands?
2223
23- The most of the frameworks tend to use programming statements for versioning your database instead of use pure SQL.
24+ The most of the frameworks tend to use programming statements for versioning your database instead of use pure SQL.
2425
2526There are some advantages to use the native programming language of your framework to maintain the database:
26- - Framework commands have some trick codes to do complex tasks;
27- - You can code once and deploy to different database systems;
28- - And others
27+
28+ * Framework commands have some trick codes to do complex tasks;
29+ * You can code once and deploy to different database systems;
30+ * And others
2931
3032But at the end despite these good features the reality in big projects someone will use the MySQL Workbench to change your database and then spend some hours translating that code for PHP. So, why do not use the feature existing in MySQL Workbench, JetBrains DataGrip and others that provides the SQL Commands necessary to update your database and put directly into the database versioning system?
3133
@@ -37,51 +39,51 @@ Because of that this is an agnostic project (independent of framework and Progra
3739
3840If you want to use only the PHP Library in your project:
3941
40- ```
42+ ``` bash
4143composer require " byjg/migration" :" 4.2.*"
4244```
45+
4346### Command Line Interface
4447
4548The command line interface is standalone and does not require you install with your project.
4649
47- You can install global and create a symbolic lynk
50+ You can install global and create a symbolic lynk
4851
49- ```
52+ ``` bash
5053composer require " byjg/migration-cli" :" 4.1.*"
5154```
5255
53- Please visit https://github.com/byjg/migration-cli to get more informations about Migration CLI.
56+ Please visit [ byjg/migration-cli ] ( https://github.com/byjg/migration-cli ) to get more informations about Migration CLI.
5457
55- ## Supported databases:
58+ ## Supported databases
5659
5760| Database | Driver | Connection String |
5861| --------------| ------------------------------------------------------------------------------- | -------------------------------------------------------- |
59- | Sqlite | [ pdo_sqlite] ( https://www.php.net/manual/en/ref.pdo-sqlite.php ) | sqlite:///path/to/file |
62+ | Sqlite | [ pdo_sqlite] ( https://www.php.net/manual/en/ref.pdo-sqlite.php ) | sqlite:///path/to/file |
6063| MySql/MariaDb | [ pdo_mysql] ( https://www.php.net/manual/en/ref.pdo-mysql.php ) | mysql://username: password @hostname: port /database |
61- | Postgres | [ pdo_pgsql] ( https://www.php.net/manual/en/ref.pdo-pgsql.php ) | pgsql://username: password @hostname: port /database |
64+ | Postgres | [ pdo_pgsql] ( https://www.php.net/manual/en/ref.pdo-pgsql.php ) | pgsql://username: password @hostname: port /database |
6265| Sql Server | [ pdo_dblib, pdo_sysbase] ( https://www.php.net/manual/en/ref.pdo-dblib.php ) Linux | dblib://username: password @hostname: port /database |
6366| Sql Server | [ pdo_sqlsrv] ( http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx ) Windows | sqlsrv://username: password @hostname: port /database |
6467
65-
6668## How It Works?
6769
68- The Database Migration uses PURE SQL to manage the database versioning.
70+ The Database Migration uses PURE SQL to manage the database versioning.
6971In order to get working you need to:
7072
71- - Create the SQL Scripts
72- - Manage using Command Line or the API.
73+ * Create the SQL Scripts
74+ * Manage using Command Line or the API.
7375
7476### The SQL Scripts
7577
7678The scripts are divided in three set of scripts:
7779
78- - The BASE script contains ALL sql commands for create a fresh database;
79- - The UP scripts contain all sql migration commands for "up" the database version;
80- - The DOWN scripts contain all sql migration commands for "down" or revert the database version;
80+ * The BASE script contains ALL sql commands for create a fresh database;
81+ * The UP scripts contain all sql migration commands for "up" the database version;
82+ * The DOWN scripts contain all sql migration commands for "down" or revert the database version;
8183
8284The directory scripts is :
8385
84- ```
86+ ``` text
8587 <root dir>
8688 |
8789 +-- base.sql
@@ -96,43 +98,42 @@ The directory scripts is :
9698 |
9799 +-- 00000.sql
98100 +-- 00001.sql
99- ```
101+ ```
100102
101- - "base.sql" is the base script
102- - "up" folder contains the scripts for migrate up the version.
103- For example: 00002.sql is the script for move the database from version '1' to '2'.
104- - "down" folder contains the scripts for migrate down the version.
103+ * "base.sql" is the base script
104+ * "up" folder contains the scripts for migrate up the version.
105+ For example: 00002.sql is the script for move the database from version '1' to '2'.
106+ * "down" folder contains the scripts for migrate down the version.
105107 For example: 00001.sql is the script for move the database from version '2' to '1'.
106- The "down" folder is optional.
108+ The "down" folder is optional.
107109
108- ### Multi Development environment
110+ ### Multi Development environment
109111
110112If you work with multiple developers and multiple branches it is to difficult to determine what is the next number.
111113
112- In that case you have the suffix "-dev" after the version number.
114+ In that case you have the suffix "-dev" after the version number.
113115
114116See the scenario:
115117
116- - Developer 1 create a branch and the most recent version in e.g. 42.
117- - Developer 2 create a branch at the same time and have the same database version number.
118+ * Developer 1 create a branch and the most recent version in e.g. 42.
119+ * Developer 2 create a branch at the same time and have the same database version number.
118120
119121In both case the developers will create a file called 43-dev.sql. Both developers will migrate UP and DOWN with
120- no problem and your local version will be 43.
122+ no problem and your local version will be 43.
121123
122124But developer 1 merged your changes and created a final version 43.sql (` git mv 43-dev.sql 43.sql ` ). If the developer 2
123- update your local branch he will have a file 43.sql (from dev 1) and your file 43-dev.sql.
125+ update your local branch he will have a file 43.sql (from dev 1) and your file 43-dev.sql.
124126If he is try to migrate UP or DOWN
125127the migration script will down and alert him there a TWO versions 43. In that case, developer 2 will have to update your
126- file do 44-dev.sql and continue to work until merge your changes and generate a final version.
128+ file do 44-dev.sql and continue to work until merge your changes and generate a final version.
127129
130+ ## Using the PHP API and Integrate it into your projects
128131
129- ## Using the PHP API and Integrate it into your projects.
132+ The basic usage is
130133
131- The basic usage is
132-
133- - Create a connection a ConnectionManagement object. For more information see the "byjg/anydataset" component
134- - Create a Migration object with this connection and the folder where the scripts sql are located.
135- - Use the proper command for "reset", "up" or "down" the migrations scripts.
134+ * Create a connection a ConnectionManagement object. For more information see the "byjg/anydataset" component
135+ * Create a Migration object with this connection and the folder where the scripts sql are located.
136+ * Use the proper command for "reset", "up" or "down" the migrations scripts.
136137
137138See an example:
138139
@@ -142,13 +143,12 @@ See an example:
142143// See more: https://github.com/byjg/anydataset#connection-based-on-uri
143144$connectionUri = new \ByJG\Util\Uri('mysql://migrateuser:migratepwd@localhost/migratedatabase');
144145
146+ // Register the Database or Databases can handle that URI:
147+ \ByJG\DbMigration\Migration::registerDatabase(\ByJG\DbMigration\Database\MySqlDatabase::class);
148+
145149// Create the Migration instance
146150$migration = new \ByJG\DbMigration\Migration($connectionUri, '.');
147151
148- // Register the Database or Databases can handle that URI:
149- $migration->registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class);
150- $migration->registerDatabase('maria', \ByJG\DbMigration\Database\MySqlDatabase::class);
151-
152152// Add a callback progress function to receive info from the execution
153153$migration->addCallbackProgress(function ($action, $currentVersion, $fileInfo) {
154154 echo "$action, $currentVersion, ${fileInfo['description']}\n";
@@ -160,23 +160,22 @@ $migration->reset();
160160
161161// Run ALL existing scripts for up or down the database version
162162// from the current version until the $version number;
163- // If the version number is not specified migrate until the last database version
163+ // If the version number is not specified migrate until the last database version
164164$migration->update($version = null);
165165```
166166
167167The Migration object controls the database version.
168168
169-
170- ### Creating a version control in your project:
169+ ### Creating a version control in your project
171170
172171``` php
173172<?php
173+ // Register the Database or Databases can handle that URI:
174+ \ByJG\DbMigration\Migration::registerDatabase(\ByJG\DbMigration\Database\MySqlDatabase::class);
175+
174176// Create the Migration instance
175177$migration = new \ByJG\DbMigration\Migration($connectionUri, '.');
176178
177- // Register the Database or Databases can handle that URI:
178- $migration->registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class);
179-
180179// This command will create the version table in your database
181180$migration->createVersion();
182181```
@@ -204,8 +203,7 @@ $migration->addCallbackProgress(function ($command, $version, $fileInfo) {
204203$migration->getDbDriver();
205204```
206205
207- To use it, please visit: https://github.com/byjg/anydataset-db
208-
206+ To use it, please visit: < https://github.com/byjg/anydataset-db >
209207
210208## Tips on writing SQL migrations for Postgres
211209
@@ -338,24 +336,22 @@ Finally, writing manual SQL migrations can be tiresome, but it is significantly
338336you use an editor capable of understanding the SQL syntax, providing autocomplete,
339337introspecting your current database schema and/or autoformatting your code.
340338
341-
342339## Handling different migration inside one schema
343340
344341If you need to create different migration scripts and version inside the same schema it is possible
345- but is too risky and I ** do not** recommend at all.
342+ but is too risky and I ** do not** recommend at all.
346343
347- To do this, you need to create different "migration tables" by passing the parameter to the constructor.
344+ To do this, you need to create different "migration tables" by passing the parameter to the constructor.
348345
349346``` php
350347<?php
351348$migration = new \ByJG\DbMigration\Migration("db:/uri", "/path", true, "NEW_MIGRATION_TABLE_NAME");
352349```
353350
354351For security reasons, this feature is not available at command line, but you can use the environment variable
355- ` MIGRATION_VERSION ` to store the name.
356-
357- We really recommend do not use this feature. The recommendation is one migration for one schema.
352+ ` MIGRATION_VERSION ` to store the name.
358353
354+ We really recommend do not use this feature. The recommendation is one migration for one schema.
359355
360356## Running Unit tests
361357
@@ -370,21 +366,21 @@ vendor/bin/phpunit
370366Run integration tests require you to have the databases up and running. We provided a basic ` docker-compose.yml ` and you
371367can use to start the databases for test.
372368
373- ** Running the databases**
369+ ### Running the databases
374370
375371``` bash
376372docker-compose up -d postgres mysql mssql
377373```
378374
379- ** Run the tests**
375+ ### Run the tests
380376
381- ```
377+ ``` bash
382378vendor/bin/phpunit
383379vendor/bin/phpunit tests/SqliteDatabase*
384380vendor/bin/phpunit tests/MysqlDatabase*
385381vendor/bin/phpunit tests/PostgresDatabase*
386- vendor/bin/phpunit tests/SqlServerDblibDatabase*
387- vendor/bin/phpunit tests/SqlServerSqlsrvDatabase*
382+ vendor/bin/phpunit tests/SqlServerDblibDatabase*
383+ vendor/bin/phpunit tests/SqlServerSqlsrvDatabase*
388384```
389385
390386Optionally you can set the host and password used by the unit tests
@@ -395,17 +391,16 @@ export MYSQL_PASSWORD=newpassword # use '.' if want have a null password
395391export PSQL_TEST_HOST=localhost # defaults to localhost
396392export PSQL_PASSWORD=newpassword # use '.' if want have a null password
397393export MSSQL_TEST_HOST=localhost # defaults to localhost
398- export MSSQL_PASSWORD=Pa55word
394+ export MSSQL_PASSWORD=Pa55word
399395export SQLITE_TEST_HOST=/tmp/test.db # defaults to /tmp/test.db
400396```
401397
402-
403398## Related Projects
404399
405- - [ Micro ORM] ( https://github.com/byjg/micro-orm )
406- - [ Anydataset] ( https://github.com/byjg/anydataset )
407- - [ PHP Rest Template] ( https://github.com/byjg/php-rest-template )
408- - [ USDocker] ( https://github.com/usdocker/usdocker )
400+ * [ Micro ORM] ( https://github.com/byjg/micro-orm )
401+ * [ Anydataset] ( https://github.com/byjg/anydataset )
402+ * [ PHP Rest Template] ( https://github.com/byjg/php-rest-template )
403+ * [ USDocker] ( https://github.com/usdocker/usdocker )
409404
410405----
411406[ Open source ByJG] ( http://opensource.byjg.com )
0 commit comments