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

[11.x] Improve readability of SQLite schema dumps #52172

Merged
merged 10 commits into from
Jul 18, 2024

Conversation

bakerkretzmar
Copy link
Contributor

This PR adds the --indent option to the SQLite schema dump command to generate an indented dump file that is much easier to read than the default (which puts each table on a single line). This also now matches the format of dumps from other databases.

Before

CREATE TABLE IF NOT EXISTS "users"("id" integer primary key autoincrement not null, "current_team_id" integer, "name" varchar not null, "email" varchar not null, "email_verified_at" datetime, "password" varchar not null, "remember_token" varchar, "created_at" datetime, "updated_at" datetime);

After

CREATE TABLE IF NOT EXISTS "users"(
  "id" integer primary key autoincrement not null,
  "current_team_id" integer,
  "name" varchar not null,
  "email" varchar not null,
  "email_verified_at" datetime,
  "password" varchar not null,
  "remember_token" varchar,
  "created_at" datetime,
  "updated_at" datetime,
);
MySQL for comparison
CREATE TABLE `users` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `current_team_id` bigint unsigned DEFAULT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `remember_token` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

This PR also fixes the SqliteSchemaState test, which was changed in #52139 and generated an empty dump file, so was no longer testing anything. It requires a real database file - @crynobone if there's a cleaner way to do this I'm happy to change anything you like, but for the test to be useful there has to be an internal sqlite_ database table created and the generated dump file has to not include it. I added assertions to make that intent clearer.

bakerkretzmar and others added 8 commits July 17, 2024 13:06
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
if (getenv('DB_CONNECTION') !== 'testing') {
parent::defineEnvironment($app);

if ($this->driver !== 'sqlite') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes allow the TestCase to be executed on persistent and in-memory SQLite

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@taylorotwell taylorotwell merged commit a128fc5 into laravel:11.x Jul 18, 2024
29 checks passed
@bakerkretzmar bakerkretzmar deleted the sqlite-dump-indent branch July 18, 2024 16:05
$migrations = collect(preg_split("/\r\n|\n|\r/", $process->getOutput()))->reject(function ($line) {
return str_starts_with($line, 'CREATE TABLE sqlite_');
})->all();
$migrations = preg_replace('/CREATE TABLE sqlite_.+\);[\r\n]+/is', '', $process->getOutput());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change here seems to have affected an application I have in which it used to return all the tables I had in my sqlite database file, but now only returns the migrations one. I'm not sure how to fix it to make it work with all of them though. I think this test is only testing the sqlite in memory and not the sqlite database file and so it got missed.

@dmyers
Copy link
Contributor

dmyers commented Jul 24, 2024

@bakerkretzmar Have you tried this with a sqlite database file? I updated today to the latest Laravel framework and my previous commands that worked now don't and I was able to trace it to this and the variable $migrations which changed how it parses and filters on them it seems like for the indention output change.

Here is how I've been generating these dump files which worked prior to this merge:

Create a new sqlite database

rm database/database.sqlite && touch database/database.sqlite

Run migrations

php artisan migrate:fresh --database=sqlite

Dump the schema file

php artisan schema:dump --database=sqlite

Previously after the 3rd step, the schema dump had all of my tables (CREATE xyz), but now is just one single CREATE migrations entry in the file after upgrading Laravel and trying it today.

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

Successfully merging this pull request may close these issues.

4 participants