0

Because for testing there's no testing database I use a manually generated sql script to clean a database clone of my production database. Assuming that my the legacy database is the following:

ohimesama
id: PK
namae: Varchar (200)

oujisama:
id: pk
namae: Varchar(200)


ohimesamagasuki:
id: pk
ohimesama_id: fk ohimesama
oujisama_id: fk oujisama

And the test database cleanup sql (cleanup.sql) script is:

DELETE * from ohimesama where namae not in ['Gardinelia', 'Jasmine'];
DELETE * from oujisama where namae not in ['Gaouron', 'Sasuke','Aladin'];
DELETE * from ohimesamagasuki where ohimesama_id not in (SELECT id from ohimesama) and oujisma not in
(SELECT id from oujisama); 

And because I want to be able to execute all theese commands with one transaction IU want to be able to read the cleanup.sql file and execute the sql commands using Laravel Database Layer without the need for writing it manually.

How I can do that?

1 Answer 1

2

As seen in this medium article you can use this one single liner:

    DB::unprepared(file_get_contents('cleanup.sql'));

The only issue is that sql commands are not chunked so in large sql files it may cause a slowdown. Also file_get_contents has a read limit as well.

In case of large sql files is reccomended to manually read and chunk it into selerate sql commands.

Also if a single command fails to get executed does not proceed to the next one as you would in via mysql or psql commands in a shell environment

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.