Skip to content
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
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.github export-ignore
/tests export-ignore
/phpunit.xml export-ignore
/phpunit.xml export-ignore
/vendor-bin export-ignore
38 changes: 36 additions & 2 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ jobs:
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
postgres:
image: postgres:15
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: testing
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U root"
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- name: Checkout
Expand Down Expand Up @@ -53,14 +66,35 @@ jobs:
- name: Install dependencies (composer.lock)
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run DB Migrate
run: |
composer bin phinx install
composer run db-migrate
env:
TESTS_DB_MYSQL_HOSTNAME: 127.0.0.1
TESTS_DB_MYSQL_HOSTPORT: 3306
TESTS_DB_MYSQL_USERNAME: root
TESTS_DB_MYSQL_PASSWORD: password
TESTS_DB_MYSQL_DATABASE: testing
TESTS_DB_PGSQL_HOSTNAME: 127.0.0.1
TESTS_DB_PGSQL_HOSTPORT: 5432
TESTS_DB_PGSQL_USERNAME: root
TESTS_DB_PGSQL_PASSWORD: password
TESTS_DB_PGSQL_DATABASE: testing

- name: Run test suite
run: composer exec -- phpunit --coverage-clover=coverage.xml -v
env:
TESTS_DB_MYSQL_HOST: 127.0.0.1
TESTS_DB_MYSQL_PORT: 3306
TESTS_DB_MYSQL_HOSTNAME: 127.0.0.1
TESTS_DB_MYSQL_HOSTPORT: 3306
TESTS_DB_MYSQL_USERNAME: root
TESTS_DB_MYSQL_PASSWORD: password
TESTS_DB_MYSQL_DATABASE: testing
TESTS_DB_PGSQL_HOSTNAME: 127.0.0.1
TESTS_DB_PGSQL_HOSTPORT: 5432
TESTS_DB_PGSQL_USERNAME: root
TESTS_DB_PGSQL_PASSWORD: password
TESTS_DB_PGSQL_DATABASE: testing

- name: Codecov
uses: codecov/codecov-action@v4
Expand Down
40 changes: 37 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ jobs:
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
postgres:
image: postgres:15
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: testing
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U root"
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- name: Checkout
Expand Down Expand Up @@ -65,11 +78,32 @@ jobs:
- name: Install dependencies (composer.lock)
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run DB Migrate
run: |
composer bin phinx install
composer run db-migrate
env:
TESTS_DB_MYSQL_HOSTNAME: 127.0.0.1
TESTS_DB_MYSQL_HOSTPORT: 3306
TESTS_DB_MYSQL_USERNAME: root
TESTS_DB_MYSQL_PASSWORD: password
TESTS_DB_MYSQL_DATABASE: testing
TESTS_DB_PGSQL_HOSTNAME: 127.0.0.1
TESTS_DB_PGSQL_HOSTPORT: 5432
TESTS_DB_PGSQL_USERNAME: root
TESTS_DB_PGSQL_PASSWORD: password
TESTS_DB_PGSQL_DATABASE: testing

- name: Run test suite
run: composer exec -- phpunit
run: composer exec -- phpunit --testdox
env:
TESTS_DB_MYSQL_HOST: 127.0.0.1
TESTS_DB_MYSQL_PORT: 3306
TESTS_DB_MYSQL_HOSTNAME: 127.0.0.1
TESTS_DB_MYSQL_HOSTPORT: 3306
TESTS_DB_MYSQL_USERNAME: root
TESTS_DB_MYSQL_PASSWORD: password
TESTS_DB_MYSQL_DATABASE: testing
TESTS_DB_PGSQL_HOSTNAME: 127.0.0.1
TESTS_DB_PGSQL_HOSTPORT: 5432
TESTS_DB_PGSQL_USERNAME: root
TESTS_DB_PGSQL_PASSWORD: password
TESTS_DB_PGSQL_DATABASE: testing
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/vendor
composer.phar
composer.lock
.phpunit.result.cache
.DS_Store
Thumbs.db
/.idea
/.vscode
/.settings
/.settings
/vendor-bin/**/vendor/
38 changes: 36 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"topthink/think-helper":"^3.1"
},
"require-dev": {
"phpunit/phpunit": "^9.6|^10"
"bamarni/composer-bin-plugin": "^1.8",
"phpunit/phpunit": "^9.6"
},
"autoload": {
"psr-4": {
Expand All @@ -40,6 +41,39 @@
"ext-mongodb": "provide mongodb support"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"bamarni/composer-bin-plugin": true
}
},
"scripts": {
"post-install-cmd": [
"@composer bin phinx install --ansi"
],
"phinx-update": [
"@composer bin phinx update --ansi"
],
"db-migrate": [
"./vendor/bin/phinx migrate -e mysql -vvv",
"./vendor/bin/phinx migrate -e pgsql -vvv"
],
"db-rollback": [
"./vendor/bin/phinx rollback -f -e mysql",
"./vendor/bin/phinx rollback -f -e pgsql"
],
"db-status": [
"./vendor/bin/phinx status -e mysql",
"./vendor/bin/phinx status -e pgsql"
],
"db-rebuild": [
"@composer run db-rollback",
"@composer run db-migrate"
]
},
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": false
}
}
}
31 changes: 31 additions & 0 deletions phinx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

return [
'paths' => [
'migrations' => '%%PHINX_CONFIG_DIR%%/tests/db/migrations',
'seeds' => '%%PHINX_CONFIG_DIR%%/tests/db/seeds'
],
'environments' => [
'default_migration_table' => 'phinxlog',
'default_environment' => '',
'mysql' => [
'adapter' => 'mysql',
'host' => getenv('TESTS_DB_MYSQL_HOSTNAME') ?: 'localhost',
'name' => getenv('TESTS_DB_MYSQL_DATABASE') ?: 'tp_orm_test',
'user' => getenv('TESTS_DB_MYSQL_USERNAME') ?: 'homestead',
'pass' => getenv('TESTS_DB_MYSQL_PASSWORD') ?: 'secret',
'port' => getenv('TESTS_DB_MYSQL_HOSTPORT') ?: '3306',
'charset' => 'utf8',
],
'pgsql' => [
'adapter' => 'pgsql',
'host' => getenv('TESTS_DB_PGSQL_HOSTNAME') ?: 'localhost',
'name' => getenv('TESTS_DB_PGSQL_DATABASE') ?: 'tp_orm_test',
'user' => getenv('TESTS_DB_PGSQL_USERNAME') ?: 'homestead',
'pass' => getenv('TESTS_DB_PGSQL_PASSWORD') ?: 'secret',
'port' => getenv('TESTS_DB_PGSQL_HOSTPORT') ?: '5432',
'charset' => 'utf8',
]
],
'version_order' => 'creation'
];
10 changes: 8 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertDeprecationsToExceptions="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnWarning="false"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
>
<coverage processUncoveredFiles="true">
<include>
Expand All @@ -30,5 +31,10 @@
<env name="TESTS_DB_MYSQL_DATABASE" value="testing"/>
<env name="TESTS_DB_MYSQL_USERNAME" value="root"/>
<env name="TESTS_DB_MYSQL_PASSWORD" value="password"/>
<env name="TESTS_DB_PGSQL_HOSTNAME" value="127.0.0.1"/>
<env name="TESTS_DB_PGSQL_HOSTPORT" value="5432"/>
<env name="TESTS_DB_PGSQL_DATABASE" value="testing"/>
<env name="TESTS_DB_PGSQL_USERNAME" value="root"/>
<env name="TESTS_DB_PGSQL_PASSWORD" value="password"/>
</php>
</phpunit>
67 changes: 67 additions & 0 deletions src/db/connector/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace think\db\connector;

use PDO;
use think\db\BaseQuery;
use think\db\PDOConnection;

/**
Expand Down Expand Up @@ -109,4 +110,70 @@
{
return true;
}

protected function getFieldType(string $type): string
{
// 将字段类型转换为小写以进行比较
$type = strtolower($type);

return match (true) {
str_starts_with($type, 'set') => 'set',
str_starts_with($type, 'enum') => 'enum',
str_starts_with($type, 'bigint'),
str_contains($type, 'numeric') => 'bigint',
str_contains($type, 'float') || str_contains($type, 'double') ||
str_contains($type, 'decimal') || str_contains($type, 'real') ||
str_contains($type, 'int') || str_contains($type, 'serial') ||
str_contains($type, 'bit') => 'int',
str_contains($type, 'bool') => 'bool',
str_starts_with($type, 'timestamp') => 'timestamp',
str_starts_with($type, 'datetime') => 'datetime',
str_starts_with($type, 'date') => 'date',
default => 'string',
};
}

public function insert(BaseQuery $query, bool $getLastInsID = false)
{
// 分析查询表达式
$options = $query->parseOptions();

// 生成SQL语句
$sql = $this->builder->insert($query);

// 执行操作
$result = '' == $sql ? 0 : $this->pdoExecute($query, $sql);

if ($result) {
// todo 应该改造为使用 returning 返回完全解决该问题
$sequence = $options['sequence'] ?? null;
$lastInsId = $getLastInsID ? $this->getLastInsID($query, $sequence) : null;

$data = $options['data'];

if ($lastInsId) {
$pk = $query->getAutoInc();
if ($pk && is_string($pk)) {
$data[$pk] = $lastInsId;

Check warning on line 157 in src/db/connector/Pgsql.php

View check run for this annotation

Codecov / codecov/patch

src/db/connector/Pgsql.php#L157

Added line #L157 was not covered by tests
}
}

$query->setOption('data', $data);

$this->db->trigger('after_insert', $query);

if ($getLastInsID && $lastInsId) {
return $lastInsId;
}
}

return $result;
}

public function getLastInsID(BaseQuery $query, ?string $sequence = null)
{
$insertId = $this->linkID->lastInsertId($sequence);

return $this->autoInsIDType($query, $insertId);
}
}
21 changes: 0 additions & 21 deletions tests/Base.php

This file was deleted.

Loading