Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump Anydataset version with Major Changes #44

Merged
merged 7 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
MSSQL_TEST_HOST: sqlserver

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
# - name: Spin up databases
# run: |
# apk add --no-cache python3 python3-dev py3-pip build-base libffi-dev
Expand All @@ -92,5 +92,5 @@ jobs:
env:
DOC_GITHUB_TOKEN: '${{ secrets.DOC_TOKEN }}'
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- run: curl https://opensource.byjg.com/add-doc.sh | bash /dev/stdin php migration
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ fabric.properties
node_modules
package-lock.json
.usdocker
.phpunit.result.cache
34 changes: 0 additions & 34 deletions .travis.yml

This file was deleted.

137 changes: 66 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@
This is a simple library written in PHP for database version control. Currently supports Sqlite, MySql, Sql Server and Postgres.

Database Migration can be used as:
- Command Line Interface
- PHP Library to be integrated in your functional tests
- Integrated in you CI/CD indenpent of your programming language or framework.


* Command Line Interface
* PHP Library to be integrated in your functional tests
* Integrated in you CI/CD indenpent of your programming language or framework.

Database Migrates uses only SQL commands for versioning your database.

## Why pure SQL commands?

The most of the frameworks tend to use programming statements for versioning your database instead of use pure SQL.
The most of the frameworks tend to use programming statements for versioning your database instead of use pure SQL.

There are some advantages to use the native programming language of your framework to maintain the database:
- Framework commands have some trick codes to do complex tasks;
- You can code once and deploy to different database systems;
- And others

* Framework commands have some trick codes to do complex tasks;
* You can code once and deploy to different database systems;
* And others

But 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?

Expand All @@ -37,51 +39,51 @@ Because of that this is an agnostic project (independent of framework and Progra

If you want to use only the PHP Library in your project:

```
```bash
composer require "byjg/migration":"4.2.*"
```

### Command Line Interface

The command line interface is standalone and does not require you install with your project.

You can install global and create a symbolic lynk
You can install global and create a symbolic lynk

```
```bash
composer require "byjg/migration-cli":"4.1.*"
```

Please visit https://github.com/byjg/migration-cli to get more informations about Migration CLI.
Please visit [byjg/migration-cli](https://github.com/byjg/migration-cli) to get more informations about Migration CLI.

## Supported databases:
## Supported databases

| Database | Driver | Connection String |
| --------------| ------------------------------------------------------------------------------- | -------------------------------------------------------- |
| Sqlite | [pdo_sqlite](https://www.php.net/manual/en/ref.pdo-sqlite.php) | sqlite:///path/to/file |
| Sqlite | [pdo_sqlite](https://www.php.net/manual/en/ref.pdo-sqlite.php) | sqlite:///path/to/file |
| MySql/MariaDb | [pdo_mysql](https://www.php.net/manual/en/ref.pdo-mysql.php) | mysql://username:password@hostname:port/database |
| Postgres | [pdo_pgsql](https://www.php.net/manual/en/ref.pdo-pgsql.php) | pgsql://username:password@hostname:port/database |
| Postgres | [pdo_pgsql](https://www.php.net/manual/en/ref.pdo-pgsql.php) | pgsql://username:password@hostname:port/database |
| Sql Server | [pdo_dblib, pdo_sysbase](https://www.php.net/manual/en/ref.pdo-dblib.php) Linux | dblib://username:password@hostname:port/database |
| Sql Server | [pdo_sqlsrv](http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx) Windows | sqlsrv://username:password@hostname:port/database |


## How It Works?

The Database Migration uses PURE SQL to manage the database versioning.
The Database Migration uses PURE SQL to manage the database versioning.
In order to get working you need to:

- Create the SQL Scripts
- Manage using Command Line or the API.
* Create the SQL Scripts
* Manage using Command Line or the API.

### The SQL Scripts

The scripts are divided in three set of scripts:

- The BASE script contains ALL sql commands for create a fresh database;
- The UP scripts contain all sql migration commands for "up" the database version;
- The DOWN scripts contain all sql migration commands for "down" or revert the database version;
* The BASE script contains ALL sql commands for create a fresh database;
* The UP scripts contain all sql migration commands for "up" the database version;
* The DOWN scripts contain all sql migration commands for "down" or revert the database version;

The directory scripts is :

```
```text
<root dir>
|
+-- base.sql
Expand All @@ -96,43 +98,42 @@ The directory scripts is :
|
+-- 00000.sql
+-- 00001.sql
```
```

- "base.sql" is the base script
- "up" folder contains the scripts for migrate up the version.
For example: 00002.sql is the script for move the database from version '1' to '2'.
- "down" folder contains the scripts for migrate down the version.
* "base.sql" is the base script
* "up" folder contains the scripts for migrate up the version.
For example: 00002.sql is the script for move the database from version '1' to '2'.
* "down" folder contains the scripts for migrate down the version.
For example: 00001.sql is the script for move the database from version '2' to '1'.
The "down" folder is optional.
The "down" folder is optional.

### Multi Development environment
### Multi Development environment

If you work with multiple developers and multiple branches it is to difficult to determine what is the next number.

In that case you have the suffix "-dev" after the version number.
In that case you have the suffix "-dev" after the version number.

See the scenario:

- Developer 1 create a branch and the most recent version in e.g. 42.
- Developer 2 create a branch at the same time and have the same database version number.
* Developer 1 create a branch and the most recent version in e.g. 42.
* Developer 2 create a branch at the same time and have the same database version number.

In both case the developers will create a file called 43-dev.sql. Both developers will migrate UP and DOWN with
no problem and your local version will be 43.
no problem and your local version will be 43.

But developer 1 merged your changes and created a final version 43.sql (`git mv 43-dev.sql 43.sql`). If the developer 2
update your local branch he will have a file 43.sql (from dev 1) and your file 43-dev.sql.
update your local branch he will have a file 43.sql (from dev 1) and your file 43-dev.sql.
If he is try to migrate UP or DOWN
the migration script will down and alert him there a TWO versions 43. In that case, developer 2 will have to update your
file do 44-dev.sql and continue to work until merge your changes and generate a final version.
file do 44-dev.sql and continue to work until merge your changes and generate a final version.

## Using the PHP API and Integrate it into your projects

## Using the PHP API and Integrate it into your projects.
The basic usage is

The basic usage is

- Create a connection a ConnectionManagement object. For more information see the "byjg/anydataset" component
- Create a Migration object with this connection and the folder where the scripts sql are located.
- Use the proper command for "reset", "up" or "down" the migrations scripts.
* Create a connection a ConnectionManagement object. For more information see the "byjg/anydataset" component
* Create a Migration object with this connection and the folder where the scripts sql are located.
* Use the proper command for "reset", "up" or "down" the migrations scripts.

See an example:

Expand All @@ -142,13 +143,12 @@ See an example:
// See more: https://github.com/byjg/anydataset#connection-based-on-uri
$connectionUri = new \ByJG\Util\Uri('mysql://migrateuser:migratepwd@localhost/migratedatabase');

// Register the Database or Databases can handle that URI:
\ByJG\DbMigration\Migration::registerDatabase(\ByJG\DbMigration\Database\MySqlDatabase::class);

// Create the Migration instance
$migration = new \ByJG\DbMigration\Migration($connectionUri, '.');

// Register the Database or Databases can handle that URI:
$migration->registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class);
$migration->registerDatabase('maria', \ByJG\DbMigration\Database\MySqlDatabase::class);

// Add a callback progress function to receive info from the execution
$migration->addCallbackProgress(function ($action, $currentVersion, $fileInfo) {
echo "$action, $currentVersion, ${fileInfo['description']}\n";
Expand All @@ -160,23 +160,22 @@ $migration->reset();

// Run ALL existing scripts for up or down the database version
// from the current version until the $version number;
// If the version number is not specified migrate until the last database version
// If the version number is not specified migrate until the last database version
$migration->update($version = null);
```

The Migration object controls the database version.


### Creating a version control in your project:
### Creating a version control in your project

```php
<?php
// Register the Database or Databases can handle that URI:
\ByJG\DbMigration\Migration::registerDatabase(\ByJG\DbMigration\Database\MySqlDatabase::class);

// Create the Migration instance
$migration = new \ByJG\DbMigration\Migration($connectionUri, '.');

// Register the Database or Databases can handle that URI:
$migration->registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class);

// This command will create the version table in your database
$migration->createVersion();
```
Expand Down Expand Up @@ -204,8 +203,7 @@ $migration->addCallbackProgress(function ($command, $version, $fileInfo) {
$migration->getDbDriver();
```

To use it, please visit: https://github.com/byjg/anydataset-db

To use it, please visit: <https://github.com/byjg/anydataset-db>

## Tips on writing SQL migrations for Postgres

Expand Down Expand Up @@ -338,24 +336,22 @@ Finally, writing manual SQL migrations can be tiresome, but it is significantly
you use an editor capable of understanding the SQL syntax, providing autocomplete,
introspecting your current database schema and/or autoformatting your code.


## Handling different migration inside one schema

If you need to create different migration scripts and version inside the same schema it is possible
but is too risky and I **do not** recommend at all.
but is too risky and I **do not** recommend at all.

To do this, you need to create different "migration tables" by passing the parameter to the constructor.
To do this, you need to create different "migration tables" by passing the parameter to the constructor.

```php
<?php
$migration = new \ByJG\DbMigration\Migration("db:/uri", "/path", true, "NEW_MIGRATION_TABLE_NAME");
```

For security reasons, this feature is not available at command line, but you can use the environment variable
`MIGRATION_VERSION` to store the name.

We really recommend do not use this feature. The recommendation is one migration for one schema.
`MIGRATION_VERSION` to store the name.

We really recommend do not use this feature. The recommendation is one migration for one schema.

## Running Unit tests

Expand All @@ -370,21 +366,21 @@ vendor/bin/phpunit
Run integration tests require you to have the databases up and running. We provided a basic `docker-compose.yml` and you
can use to start the databases for test.

**Running the databases**
### Running the databases

```bash
docker-compose up -d postgres mysql mssql
```

**Run the tests**
### Run the tests

```
```bash
vendor/bin/phpunit
vendor/bin/phpunit tests/SqliteDatabase*
vendor/bin/phpunit tests/MysqlDatabase*
vendor/bin/phpunit tests/PostgresDatabase*
vendor/bin/phpunit tests/SqlServerDblibDatabase*
vendor/bin/phpunit tests/SqlServerSqlsrvDatabase*
vendor/bin/phpunit tests/SqlServerDblibDatabase*
vendor/bin/phpunit tests/SqlServerSqlsrvDatabase*
```

Optionally you can set the host and password used by the unit tests
Expand All @@ -395,17 +391,16 @@ export MYSQL_PASSWORD=newpassword # use '.' if want have a null password
export PSQL_TEST_HOST=localhost # defaults to localhost
export PSQL_PASSWORD=newpassword # use '.' if want have a null password
export MSSQL_TEST_HOST=localhost # defaults to localhost
export MSSQL_PASSWORD=Pa55word
export MSSQL_PASSWORD=Pa55word
export SQLITE_TEST_HOST=/tmp/test.db # defaults to /tmp/test.db
```


## Related Projects

- [Micro ORM](https://github.com/byjg/micro-orm)
- [Anydataset](https://github.com/byjg/anydataset)
- [PHP Rest Template](https://github.com/byjg/php-rest-template)
- [USDocker](https://github.com/usdocker/usdocker)
* [Micro ORM](https://github.com/byjg/micro-orm)
* [Anydataset](https://github.com/byjg/anydataset)
* [PHP Rest Template](https://github.com/byjg/php-rest-template)
* [USDocker](https://github.com/usdocker/usdocker)

----
[Open source ByJG](http://opensource.byjg.com)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"byjg/anydataset-db": "4.1.*",
"byjg/anydataset-db": "4.9.*",
"ext-pdo": "*"
},
"require-dev": {
Expand Down
8 changes: 6 additions & 2 deletions example/mysql/test_mysql.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use ByJG\DbMigration\Database\MySqlDatabase;
use ByJG\DbMigration\Migration;

require __DIR__ . "/../../vendor/autoload.php";

/**
Expand All @@ -10,8 +13,9 @@

$uri = new \ByJG\Util\Uri('mysql://root:password@mysql-container/migratedatabase');

$migration = new \ByJG\DbMigration\Migration($uri, __DIR__);
$migration->registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class);
Migration::registerDatabase(MySqlDatabase::class);

$migration = new Migration($uri, __DIR__);

$migration->prepareEnvironment();

Expand Down
Loading