Skip to content

Commit 0d636cb

Browse files
authored
Merge pull request #56 from szhajdu/v3-fix-last-insert-id-sqlsrv
[bugfix] #49 Fix last insert id return type in case of dblib (3.x)
2 parents 5666795 + ab9d53a commit 0d636cb

File tree

14 files changed

+500
-42
lines changed

14 files changed

+500
-42
lines changed

.github/workflows/main.yml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,35 @@ jobs:
3131
--health-retries 5
3232
ports:
3333
- 5432:5432
34+
mssql:
35+
image: mcr.microsoft.com/mssql/server:2019-latest
36+
env:
37+
SA_PASSWORD: P@ssw0rd
38+
ACCEPT_EULA: 'Y'
39+
ports:
40+
- 1433:1433
41+
options: >-
42+
--health-cmd "/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'P@ssw0rd' -d master -Q 'SELECT COUNT(*) FROM master.dbo.spt_values;'"
43+
--health-interval 10s
44+
--health-timeout 5s
45+
--health-retries 5
3446
3547
strategy:
3648
matrix:
3749
php: [8.0, 8.1, 8.2]
3850

3951
steps:
52+
- name: Create default database for sqlsrv as image does not support it
53+
run: /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'P@ssw0rd' -Q 'CREATE DATABASE codeception_test'
54+
4055
- name: Checkout code
4156
uses: actions/checkout@v2
4257

4358
- name: Setup PHP
4459
uses: shivammathur/setup-php@v2
4560
with:
4661
php-version: ${{ matrix.php }}
47-
extensions: pdo, pgsql, mysql, sqlite
62+
extensions: pdo, pgsql, mysql, sqlite, sqlsrv, pdo_sqlsrv, pdo_dblib
4863
coverage: none
4964

5065
- name: Validate composer.json and composer.lock
@@ -56,5 +71,11 @@ jobs:
5671
- name: Run test suite
5772
run: php vendor/bin/codecept run
5873
env:
59-
PGPASSWORD: postgres
6074
MYSQL_HOST: 127.0.0.1
75+
MYSQL_DB: codeception_test
76+
PG_HOST: 127.0.0.1
77+
PG_DB: codeception_test
78+
PG_PASSWORD: postgres
79+
MSSQL_HOST: 127.0.0.1
80+
MSSQL_DB: codeception_test
81+
MSSQL_PASSWORD: P@ssw0rd

docker-compose.yml

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,46 @@ services:
77
context: .
88
dockerfile: ./php81.Dockerfile
99
environment:
10-
MYSQL_DSN: "mysql:host=host.docker.internal;port=3102;dbname=codeception"
11-
MYSQL_USER: root
10+
MYSQL_HOST: host.docker.internal
11+
MYSQL_DB: codeception
1212
MYSQL_PASSWORD: codeception
13+
PG_HOST: host.docker.internal
14+
PG_DB: codeception
15+
PG_PASSWORD: codeception
16+
MSSQL_HOST: host.docker.internal
17+
MSSQL_DB: codeception
18+
MSSQL_PASSWORD: 'P@ssw0rd'
1319
XDEBUG_MODE: "debug"
1420
XDEBUG_CONFIG: "client_host=host.docker.internal; client_port=9000; mode=debug; start_wih_request=1"
1521
PHP_IDE_CONFIG: "serverName=codeception-module-db" # the name must be the same as in your PHP -> Server -> "name" field
1622
volumes:
1723
- ".:/var/www/html"
1824

19-
mariadb105:
20-
image: mariadb:10.5
25+
mysql:
26+
image: mysql:5.7
2127
environment:
22-
MARIADB_ROOT_PASSWORD: codeception
23-
MARIADB_DATABASE: codeception
28+
MYSQL_ROOT_PASSWORD: codeception
29+
MYSQL_DATABASE: codeception
2430
ports:
25-
- "3102:3306"
31+
- "3306:3306"
32+
33+
postgres:
34+
image: postgres
35+
environment:
36+
POSTGRES_PASSWORD: codeception
37+
POSTGRES_DB: codeception
38+
ports:
39+
- "5432:5432"
40+
41+
mssql:
42+
image: mcr.microsoft.com/mssql/server:2019-latest
43+
environment:
44+
SA_PASSWORD: 'P@ssw0rd'
45+
MSSQL_DATABASE: codeception
46+
ACCEPT_EULA: 'Y'
47+
ports:
48+
- "1433:1433"
49+
volumes:
50+
- ./tests/data/scripts:/scripts:ro
51+
entrypoint: [ "/bin/bash", "-c", "/scripts/mssql.sh" ]
2652

php81.Dockerfile

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
FROM php:8.1-cli
22

3+
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
4+
35
RUN apt-get update && \
46
apt-get install -y \
57
unzip \
68
wget \
79
git \
810
zlib1g-dev \
911
libzip-dev \
10-
mariadb-client-10.5
11-
12-
RUN docker-php-ext-install pdo pdo_mysql && docker-php-ext-enable pdo pdo_mysql
13-
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
14-
RUN docker-php-ext-install zip
15-
16-
RUN pecl install xdebug-3.1.5 && \
17-
echo zend_extension=xdebug.so > $PHP_INI_DIR/conf.d/xdebug.ini
12+
libpq-dev \
13+
mariadb-client-10.5
14+
15+
RUN install-php-extensions \
16+
pdo_mysql-stable \
17+
pdo_pgsql-stable \
18+
pdo_dblib-stable \
19+
pdo_sqlsrv-5.11.0 \
20+
pgsql-stable \
21+
zip-stable \
22+
xdebug-3.1.5
1823

1924
COPY --from=composer /usr/bin/composer /usr/bin/composer
2025

2126
WORKDIR /var/www/html
2227

23-
COPY composer.json .
24-
COPY composer.lock .
25-
26-
RUN composer install --no-autoloader
27-
28-
COPY . .
29-
30-
RUN composer dump-autoload -o
31-
3228
ENTRYPOINT ["tail"]
3329
CMD ["-f", "/dev/null"]

src/Codeception/Lib/Driver/Db.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function deleteQueryByCriteria(string $tableName, array $criteria): void
250250

251251
public function lastInsertId(string $tableName): string
252252
{
253-
return $this->getDbh()->lastInsertId();
253+
return (string)$this->getDbh()->lastInsertId();
254254
}
255255

256256
public function getQuotedName(string $name): string

tests/data/dumps/mssql.sql

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
CREATE TABLE [dbo].[groups] (
2+
[id] INT NOT NULL IDENTITY(1,1),
3+
[name] VARCHAR(100) NULL,
4+
[enabled] BIT NULL,
5+
[created_at] DATETIME NOT NULL CONSTRAINT DF_groups_created_at DEFAULT GETDATE(),
6+
CONSTRAINT PK_groups PRIMARY KEY CLUSTERED ([id] ASC)
7+
);
8+
9+
INSERT INTO [dbo].[groups]([name],[enabled],[created_at])
10+
VALUES
11+
('coders', 1, '2012-02-01 21:17:50'),
12+
('jazzman', 0, '2012-02-01 21:18:40');
13+
14+
15+
CREATE TABLE [dbo].[users] (
16+
[id] INT NOT NULL IDENTITY(1,1),
17+
[uuid] BINARY(16) NULL,
18+
[name] VARCHAR(30) NULL,
19+
[email] VARCHAR(255) NULL,
20+
[is_active] BIT NOT NULL CONSTRAINT DF_users_is_active DEFAULT 1,
21+
[created_at] DATETIME NOT NULL CONSTRAINT DF_users_created_at DEFAULT GETDATE(),
22+
CONSTRAINT PK_users PRIMARY KEY CLUSTERED ([id] ASC)
23+
);
24+
25+
INSERT INTO [dbo].[users]([uuid],[name],[email],[is_active],[created_at])
26+
VALUES
27+
(0x11edc34b01d972fa9c1d0242ac120006, 'davert', '[email protected]', 1, '2012-02-01 21:17:04'),
28+
(null, 'nick', '[email protected]', 1, '2012-02-01 21:17:15'),
29+
(null, 'miles', '[email protected]', 1, '2012-02-01 21:17:25'),
30+
(null, 'bird', '[email protected]', 0, '2012-02-01 21:17:39');
31+
32+
33+
CREATE TABLE [dbo].[permissions] (
34+
[id] INT NOT NULL IDENTITY(1,1),
35+
[user_id] INT NULL,
36+
[group_id] INT NULL,
37+
[role] VARCHAR(30) NULL,
38+
CONSTRAINT PK_permissions PRIMARY KEY CLUSTERED ([id] ASC),
39+
CONSTRAINT FK_permissions FOREIGN KEY ([group_id]) REFERENCES [dbo].[groups] ([id]) ON DELETE CASCADE,
40+
CONSTRAINT FK_users FOREIGN KEY ([user_id]) REFERENCES [dbo].[users] ([id]) ON DELETE CASCADE
41+
);
42+
43+
INSERT INTO [dbo].[permissions]([user_id],[group_id],[role])
44+
VALUES
45+
(1,1,'member'),
46+
(2,1,'member'),
47+
(3,2,'member'),
48+
(4,2,'admin');
49+
50+
51+
CREATE TABLE [dbo].[order] (
52+
[id] INT NOT NULL IDENTITY(1,1),
53+
[name] VARCHAR(255) NOT NULL,
54+
[status] VARCHAR(255) NOT NULL,
55+
CONSTRAINT PK_order PRIMARY KEY CLUSTERED ([id] ASC)
56+
);
57+
58+
INSERT INTO [dbo].[order]([name],[status]) VALUES ('main', 'open');
59+
60+
61+
CREATE TABLE [dbo].[composite_pk] (
62+
[group_id] INT NOT NULL,
63+
[id] INT NOT NULL,
64+
[status] VARCHAR(255) NOT NULL,
65+
CONSTRAINT PK_composite_pk PRIMARY KEY CLUSTERED ([group_id] ASC, [id] ASC)
66+
);
67+
68+
CREATE TABLE [dbo].[no_pk] (
69+
[status] varchar(255) NOT NULL
70+
);
71+
72+
CREATE TABLE [dbo].[empty_table] (
73+
[id] int NOT NULL IDENTITY(1,1),
74+
[field] varchar(255),
75+
CONSTRAINT [PK_empty_table] PRIMARY KEY CLUSTERED ([id])
76+
);

tests/data/scripts/mssql.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
/opt/mssql/bin/sqlservr &
4+
/scripts/wait-for-it.sh 127.0.0.1:1433
5+
6+
for i in {1..50};
7+
do
8+
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -d master -Q "CREATE DATABASE $MSSQL_DATABASE;"
9+
if [ $? -eq 0 ]
10+
then
11+
echo "database created"
12+
break
13+
else
14+
echo "not ready yet..."
15+
sleep 1
16+
fi
17+
done
18+
19+
sleep infinity # Keep the container running forever

0 commit comments

Comments
 (0)