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

Init script should support MySQL/MariaDB Executable Comment Syntax #5108

Closed
straurob opened this issue Feb 22, 2022 · 3 comments
Closed

Init script should support MySQL/MariaDB Executable Comment Syntax #5108

straurob opened this issue Feb 22, 2022 · 3 comments

Comments

@straurob
Copy link

Using Testcontainers 1.16.3 I have the following static setup method for a MariaDB 10.4 image:

static final MariaDBContainer<?> mariaDBContainer = new MariaDBContainer<>(MARIADB_IMAGE_NAME)
        .withDatabaseName(MARIADB_DATABASE_NAME)
        .withUsername(MARIADB_USERNAME)
        .withPassword(MARIADB_PASSWORD)
        .withInitScript("db/init.sql")
        .withCommand("--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci")
        .withReuse(true);

This is the init.sql script:

-- MySQL dump 10.19  Distrib 10.3.32-MariaDB, for debian-linux-gnu (x86_64)
--
-- Host: 127.0.0.1    Database: test-db
-- ------------------------------------------------------
-- Server version	10.4.16-MariaDB-1:10.4.16+maria~focal

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- SQL statements here...
--

/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2022-01-03 16:24:14

It seems as if the conditional statements like /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; are ignored when using the withInitScript() method. In my case, the result is that the foreign key checks are not disabled when running the script.

When explicitly using SET FOREIGN_KEY_CHECKS = 0; or SET FOREIGN_KEY_CHECKS = 1; then the database can be created successfully.

@kiview
Copy link
Member

kiview commented Feb 22, 2022

Support for more advanced SQL syntax in withInitScript() is limited, also see this similar issue for PostgreSQL:
#4441

In such advanced cases, I would suggest using dedicated tools db initialization, such as Flyway or Liquibase, since it is unlikely that withInitScript() will support all SQL dialects completely.

@eddumelendez
Copy link
Member

Nowadays, we are suggesting use withCopyFileToContainer and put the scripts under /docker-entrypoint-initdb.d as is documented here.

@eddumelendez eddumelendez closed this as not planned Won't fix, can't repro, duplicate, stale Feb 10, 2023
@nanafan93
Copy link

Just curious here but why does testcontainers need to "support" vendor specific SQL ? Isn't it just passing through whatever the init script contains ? The database can then deal with unknown SQL as it sees fit...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants