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

Next Revision #31

Merged
merged 12 commits into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
17 changes: 17 additions & 0 deletions .idea/runConfigurations/Run_Databases.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 0 additions & 38 deletions .idea/runConfigurations/Start_MySql.xml

This file was deleted.

42 changes: 0 additions & 42 deletions .idea/runConfigurations/Start_Postgres.xml

This file was deleted.

42 changes: 0 additions & 42 deletions .idea/runConfigurations/Start_SQL_Server.xml

This file was deleted.

22 changes: 5 additions & 17 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
language: php

addons:
hosts:
- mysql-container
- postgres-container
- dblib-container

php:
- "7.3"
- "7.2"
Expand All @@ -19,21 +13,15 @@ services:
before_install:
- sudo service mysql stop || echo "mysql not stopped"
- sudo service postgresql stop || echo "postgresql not stopped"
# - docker run --name mssql-container --rm -e ACCEPT_EULA=Y -e SA_PASSWORD=Pa55word -p 1433:1433 -d mcr.microsoft.com/mssql/server
- docker run --name postgres-container --rm -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:9-alpine
- docker run --name mysql-container --rm -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 -d mysql:5.7
- docker-compose up -d postgres mysql

install:
- composer install

script:
- vendor/bin/phpunit
- vendor/bin/phpunit tests/PostgresDatabaseTest.php
- vendor/bin/phpunit tests/MysqlDatabaseTest.php
- vendor/bin/phpunit tests/SqlServerDatabaseTest.php

- vendor/bin/phpunit tests/SqliteDatabaseCustomTest.php
- vendor/bin/phpunit tests/PostgresDatabaseCustomTest.php
- vendor/bin/phpunit tests/MysqlDatabaseCustomTest.php
- vendor/bin/phpunit tests/SqlServerDatabaseCustomTest.php
- vendor/bin/phpunit tests/SqliteDatabase*
- ./wait-for-db.sh postgres && vendor/bin/phpunit tests/PostgresDatabase*
- ./wait-for-db.sh mysql && vendor/bin/phpunit tests/MysqlDatabase*
# - ./wait-for-db.sh mssql && vendor/bin/phpunit tests/SqlServer*

73 changes: 33 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ $migration = new \ByJG\DbMigration\Migration($connectionUri, '.');
$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";
});

// Restore the database using the "base.sql" script
// and run ALL existing scripts for up the database version to the latest version
$migration->reset();
Expand Down Expand Up @@ -179,8 +184,8 @@ $migration->getCurrentVersion();

```php
<?php
$migration->addCallbackProgress(function ($command, $version) {
echo "Doing Command: $command at version $version";
$migration->addCallbackProgress(function ($command, $version, $fileInfo) {
echo "Doing Command: $command at version $version - ${fileInfo['description']}, ${fileInfo['exists']}, ${fileInfo['file']}, ${fileInfo['checksum']}\n";
});
```

Expand Down Expand Up @@ -344,60 +349,48 @@ For security reasons, this feature is not available at command line, but you can
We really recommend do not use this feature. The recommendation is one migration for one schema.


# Unit Tests

This library has integrated tests and need to be setup for each database you want to test.
# Running Unit tests

Basiclly you have the follow tests:
Basic unit tests can be running by:

```
vendor/bin/phpunit tests/SqliteDatabaseTest.php
vendor/bin/phpunit tests/MysqlDatabaseTest.php
vendor/bin/phpunit tests/PostgresDatabaseTest.php
vendor/bin/phpunit tests/SqlServerDatabaseTest.php
```bash
vendor/bin/phpunit
```

## Using Docker for testing
# Running database tests

### MySql
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**

```bash
docker run --name mysql-container --rm -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 -d mysql:5.7

docker run -it --rm \
--link mysql-container \
-v $PWD:/work \
-w /work \
byjg/php:7.2-cli \
phpunit tests/MysqlDatabaseTest
docker-compose up -d postgres mysql
```

### Postgresql
**Run the tests**

```bash
docker run --name postgres-container --rm -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:9-alpine

docker run -it --rm \
--link postgres-container \
-v $PWD:/work \
-w /work \
byjg/php:7.2-cli \
phpunit tests/PostgresDatabaseTest
```
vendor/bin/phpunit
vendor/bin/phpunit tests/SqliteDatabase*
vendor/bin/phpunit tests/MysqlDatabase*
vendor/bin/phpunit tests/PostgresDatabase*
vendor/bin/phpunit tests/SqlServerDatabase*
```

### Microsoft SqlServer
Optionally you can set the host and password used by the unit tests

```bash
docker run --name mssql-container --rm -e ACCEPT_EULA=Y -e SA_PASSWORD=Pa55word -p 1433:1433 -d mcr.microsoft.com/mssql/server

docker run -it --rm \
--link mssql-container \
-v $PWD:/work \
-w /work \
byjg/php:7.2-cli \
phpunit tests/SqlServerDatabaseTest
export MYSQL_TEST_HOST=localhost # defaults to localhost
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 SQLITE_TEST_HOST=/tmp/test.db # defaults to /tmp/test.db
```


# Related Projects

- [Micro ORM](https://github.com/byjg/micro-orm)
Expand Down
42 changes: 42 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
version: '3.4'
services:
mssql:
container_name: anydataset_db_mssql
image: mcr.microsoft.com/mssql/server
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=Pa55word
ports:
- "1433:1433"
healthcheck:
test: ["CMD", "/opt/mssql-tools/bin/sqlcmd", "-S", "localhost,1433", "-U", "sa", "-P", "Pa55word", "-Q", "SELECT 1"]
timeout: 20s
interval: 10s
retries: 10

postgres:
container_name: anydataset_db_postgres
image: postgres:9-alpine
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
ports:
- "5432:5432"
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-h", "localhost", "-U", "postgres" ]
timeout: 20s
interval: 10s
retries: 10

mysql:
container_name: anydataset_db_mysql
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=password
ports:
- "3306:3306"
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
timeout: 20s
interval: 10s
retries: 10
2 changes: 1 addition & 1 deletion example/mysql/migrations/down/00001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
-- This is the reverse operation of the script up/00002
-- --------------------------------------------------------

drop table roles;
drop table posts;
12 changes: 8 additions & 4 deletions example/mysql/migrations/up/00002.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
-- from version '1' to version '2'
-- --------------------------------------------------------

create table roles
create table posts
(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
rolename char(1) NOT NULL,
userid int not null
)
userid int not null,
title varchar(50) not null,
post text not null,
CONSTRAINT fk_posts_users foreign key (userid) references users(id)
);

insert into posts (userid, title, post) values (1, 'Testing', '<!-- wp:paragraph -->\\n<p>This is an example page. It\'s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p>\\n<!-- /wp:paragraph -->\\n\\n<!-- wp:quote -->\\n<blockquote class="wp-block-quote"><p>Hi there! I\'m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin\' caught in the rain.)</p></blockquote>\\n<!-- /wp:quote -->\\n\\n<!-- wp:paragraph -->\\n<p>...or something like this:</p>\\n<!-- /wp:paragraph -->\\n\\n<!-- wp:quote -->\\n<blockquote class="wp-block-quote"><p>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</p></blockquote>\\n<!-- /wp:quote -->\\n\\n<!-- wp:paragraph -->\\n<p>As a new WordPress user, you should go to <a href="http://home.home.lcl/wordpress/wp-admin/">your dashboard</a> to delete this page and create new pages for your content. Have fun!</p>\\n<!-- /wp:paragraph -->');
2 changes: 1 addition & 1 deletion example/postgres/migrations/down/00001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
-- This is the reverse operation of the script up/00002
-- --------------------------------------------------------

drop table roles;
drop table posts;
12 changes: 8 additions & 4 deletions example/postgres/migrations/up/00002.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
-- from version '1' to version '2'
-- --------------------------------------------------------

create table roles
create table posts
(
id SERIAL NOT NULL PRIMARY KEY,
rolename char(1) NOT NULL,
userid int not null
)
userid int not null,
title varchar(50) not null,
post text not null,
CONSTRAINT fk_posts_users foreign key (userid) references users(id)
);

insert into posts (userid, title, post) values (1, 'Testing', '<!-- wp:paragraph -->\n<p>This is an example page. It''s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:quote -->\n<blockquote class="wp-block-quote"><p>Hi there! I''m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin'' caught in the rain.)</p></blockquote>\n<!-- /wp:quote -->\n\n<!-- wp:paragraph -->\n<p>...or something like this:</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:quote -->\n<blockquote class="wp-block-quote"><p>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</p></blockquote>\n<!-- /wp:quote -->\n\n<!-- wp:paragraph -->\n<p>As a new WordPress user, you should go to <a href="http://home.home.lcl/wordpress/wp-admin/">your dashboard</a> to delete this page and create new pages for your content. Have fun!</p>\n<!-- /wp:paragraph -->');
2 changes: 1 addition & 1 deletion example/sql_server/migrations/down/00001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
-- This is the reverse operation of the script up/00002
-- --------------------------------------------------------

drop table roles;
drop table posts;
Loading