<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kushal Raj Shrestha</title>
    <description>The latest articles on Forem by Kushal Raj Shrestha (@kushalraj).</description>
    <link>https://forem.com/kushalraj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F334590%2Ff31611fb-a5b8-4ae9-b8f7-775942720bdb.png</url>
      <title>Forem: Kushal Raj Shrestha</title>
      <link>https://forem.com/kushalraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kushalraj"/>
    <language>en</language>
    <item>
      <title>Timestamp SQL Files for Consistent Laravel Migration</title>
      <dc:creator>Kushal Raj Shrestha</dc:creator>
      <pubDate>Wed, 05 Jul 2023 03:40:50 +0000</pubDate>
      <link>https://forem.com/kushalraj/timestamp-sql-files-for-consistent-laravel-migration-2mn3</link>
      <guid>https://forem.com/kushalraj/timestamp-sql-files-for-consistent-laravel-migration-2mn3</guid>
      <description>&lt;h3&gt;
  
  
  Background
&lt;/h3&gt;

&lt;p&gt;As applications scale and become more complex, advanced database concepts like functions, triggers, and stored procedures are often introduced. In &lt;a href="https://laravel.com/"&gt;Laravel&lt;/a&gt;, these concepts can be implemented by writing SQL query files and executing them through migration files. However, as the application evolves and additional functionality is added, the need to update these database functions arises. This can lead to migration issues if the modified function declaration refers to new relations and columns that have not been created yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;To overcome this challenge, one effective approach is to create timestamped SQL files that correspond to the migration files using them. By associating timestamped SQL files with migration files, developers can ensure consistency between the database schema and the application code. These timestamped SQL files serve as snapshots of the database function at the time of their usage, ensuring that modifications to the functions do not disrupt the migration process.&lt;/p&gt;

&lt;p&gt;This blog post explores the benefits and implementation of timestamped SQL files for &lt;a href="https://laravel.com/docs/10.x/migrations"&gt;Laravel migration&lt;/a&gt;, focusing on the application stack of Laravel and &lt;a href="https://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Benefits of Timestamped SQL Files
&lt;/h3&gt;

&lt;p&gt;By associating timestamped SQL files with migration files, developers can ensure consistency between the database schema and the application code. Here are some key benefits of using this approach:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Seamless Migration Execution:&lt;/strong&gt; Timestamped SQL files allow migrations to be executed without conflicts, even when modifications are made to the database functions. This prevents errors and ensures smooth application deployment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Version Control Integration:&lt;/strong&gt; Timestamped SQL files enable easy tracking of changes made to database functions. By following a timestamped naming convention, it becomes simple to identify and compare different versions of SQL files, enhancing version control practices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Team Collaboration:&lt;/strong&gt; Timestamped SQL files facilitate collaboration among team members by providing a clear history of modifications. Developers can work concurrently on different SQL files and migrations, reducing conflicts and improving productivity.&lt;/p&gt;




&lt;h3&gt;
  
  
  Implementation Steps
&lt;/h3&gt;

&lt;p&gt;Now, let's dive into the steps required to implement timestamped SQL files for Laravel migration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Creating Timestamped SQL Files&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose a naming convention for the SQL files that includes a timestamp. A proper way would to name an SQL file would be timestamp, action, entity name and type. For example, &lt;code&gt;YYYY_MM_DD_HHMMSS_extract_cleaned_company_name_function.sql&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Note: Here we have used timestamp format used by Laravel when creating migration files. Using this way ensures our files are listed properly in IDE's file explorer.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Store the SQL files in a designated directory within your Laravel project, such as the &lt;code&gt;database/migrations/sql&lt;/code&gt; folder.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of SQL above file &lt;code&gt;2020_01_09_072809_create_extract_cleaned_company_name_function.sql&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;FUNCTION&lt;/span&gt; &lt;span class="n"&gt;extract_cleaned_company_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;companyName&lt;/span&gt; &lt;span class="nb"&gt;varchar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;RETURNS&lt;/span&gt; &lt;span class="nb"&gt;varchar&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="err"&gt;$$&lt;/span&gt;
&lt;span class="k"&gt;DECLARE&lt;/span&gt;
    &lt;span class="n"&gt;cleanedCompanyName&lt;/span&gt; &lt;span class="nb"&gt;varchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;replacePattern&lt;/span&gt; &lt;span class="nb"&gt;varchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
    &lt;span class="n"&gt;replacePattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="s1"&gt;+|-'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;cleanedCompanyName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;REGEXP_REPLACE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cleanedCompanyName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;replacePattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;cleanedCompanyName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;LOWER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cleanedCompanyName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;RETURN&lt;/span&gt; &lt;span class="n"&gt;cleanedCompanyName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt;
&lt;span class="err"&gt;$$&lt;/span&gt; &lt;span class="k"&gt;LANGUAGE&lt;/span&gt; &lt;span class="n"&gt;plpgsql&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Writing Migration Files&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a migration file using the &lt;a href="https://laravel.com/docs/10.x/artisan"&gt;&lt;code&gt;artisan&lt;/code&gt;&lt;/a&gt; command-line tool, following Laravel's migration conventions.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan make:migration create_extract_cleaned_company_name_function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Inside the migration file's &lt;code&gt;up()&lt;/code&gt; method, use the &lt;a href="https://laravel.com/docs/10.x/database#running-an-unprepared-statement"&gt;DB::unprepared()&lt;/a&gt; function to execute the corresponding timestamped SQL file. &lt;/li&gt;
&lt;li&gt;For example:
&lt;code&gt;2020_01_09_072809_create_extract_cleaned_company_name_function.php&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;down&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nv"&gt;$sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file_get_contents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;database_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'migrations/sql/2020_01_09_072809_create_extract_cleaned_company_name_function.sql'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;unprepared&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sql&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;down&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"DROP FUNCTION IF EXISTS extract_cleaned_company_name(varchar);"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;unprepared&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sql&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: We call &lt;code&gt;down()&lt;/code&gt; method to avoid creating a function that already exists when we execute the SQL query to create our function.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Modifying Database Functions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When changes are required in the database functions, create a new timestamped SQL file with the updated function declaration.&lt;/li&gt;
&lt;li&gt;Ensure that the new SQL file name follows the same timestamp format as the new migration file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example,&lt;/p&gt;

&lt;p&gt;SQL file: &lt;code&gt;2022_05_09_012450_create_extract_cleaned_company_name_function.sql&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Migration file: &lt;code&gt;2022_05_09_012450_update_extract_cleaned_company_name_function.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Running Migrations&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use Laravel's migration commands &lt;code&gt;php artisan migrate&lt;/code&gt; to execute the migrations, including the timestamped SQL files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Laravel will handle the execution of the SQL files, ensuring consistency between the database schema and the application code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Timestamped SQL files offer a robust solution for managing changes in advanced database concepts within Laravel applications. By associating SQL files with migration files and adopting a timestamp naming convention, developers can seamlessly update database functions without risking migration conflicts.&lt;/p&gt;

&lt;p&gt;This approach improves &lt;strong&gt;version control practices&lt;/strong&gt;, enhances &lt;strong&gt;collaboration&lt;/strong&gt; among team members, and ensures a &lt;strong&gt;smooth deployment process&lt;/strong&gt;. Leveraging the power of Laravel and PostgreSQL, you can confidently scale your application while maintaining a consistent and reliable database schema.&lt;/p&gt;

&lt;p&gt;Remember to regularly update your timestamped SQL files as your application evolves, and enjoy the benefits of a well-maintained and flexible database structure within your Laravel project.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>postgressql</category>
      <category>database</category>
      <category>git</category>
    </item>
  </channel>
</rss>
