<?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>DEV Community: Ronak Gupta</title>
    <description>The latest articles on DEV Community by Ronak Gupta (@iamronakgupta).</description>
    <link>https://dev.to/iamronakgupta</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%2F1140953%2Fcd7ba19e-7efc-41f0-83cd-4e4adb01c65a.jpeg</url>
      <title>DEV Community: Ronak Gupta</title>
      <link>https://dev.to/iamronakgupta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iamronakgupta"/>
    <language>en</language>
    <item>
      <title>How does rails knows that migration is pending?</title>
      <dc:creator>Ronak Gupta</dc:creator>
      <pubDate>Sat, 11 May 2024 19:03:17 +0000</pubDate>
      <link>https://dev.to/iamronakgupta/how-does-rails-knows-that-migration-is-pending-2bmf</link>
      <guid>https://dev.to/iamronakgupta/how-does-rails-knows-that-migration-is-pending-2bmf</guid>
      <description>&lt;p&gt;You might have enountered an condition of migration pending when you are trying to access a route, we may wonder sometime how does rails knows that migrations are pending.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9yti0wik645vgf27wgly.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9yti0wik645vgf27wgly.png" alt="Image showing pending migration error in browser" width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Rails keeps the track of every migration that has been ran in a table &lt;code&gt;schema_migration&lt;/code&gt;. This table has only one column &lt;code&gt;version&lt;/code&gt; that store the version of migration files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8awi3vd3haqiarutscnt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8awi3vd3haqiarutscnt.png" alt="Image showing data from schema_migration table" width="732" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Versions are the creation timestamp of migration file, this version makes every migration unique from each other. So every time we runs a migration, corresponding version are stored in this table and whenever we runs a rollback corresponding version are removed from the table.&lt;/p&gt;

&lt;h2&gt;
  
  
  How schema_migration table is created
&lt;/h2&gt;

&lt;p&gt;If we check the source of code of activerecord gem, we can see &lt;a href="https://github.com/rails/rails/blob/main/activerecord/lib/active_record/schema_migration.rb"&gt;SchemaMigration&lt;/a&gt; class. This class is reponsible for table creation and  all related operation like insertion and deletion in the table.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Some important method from SchemaMigration class&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SchemaMigration # :nodoc:
    class NullSchemaMigration # :nodoc:
    end

    attr_reader :arel_table

    def initialize(pool)
      @pool = pool
      @arel_table = Arel::Table.new(table_name)
    end

    def create_version(version)
      im = Arel::InsertManager.new(arel_table)
      im.insert(arel_table[primary_key] =&amp;gt; version)
      @pool.with_connection do |connection|
        connection.insert(im, "#{self.class} Create", primary_key, version)
      end
    end

    def delete_version(version)
      dm = Arel::DeleteManager.new(arel_table)
      dm.wheres = [arel_table[primary_key].eq(version)]

      @pool.with_connection do |connection|
        connection.delete(dm, "#{self.class} Destroy")
      end
    def create_table
      @pool.with_connection do |connection|
        unless connection.table_exists?(table_name)
          connection.create_table(table_name, id: false) do |t|
            t.string :version, **connection.internal_string_options_for_primary_key
          end
        end
      end
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code, &lt;code&gt;create_table&lt;/code&gt; create a table in database and &lt;code&gt;create_version&lt;/code&gt; inserts version in table and &lt;code&gt;delete_version&lt;/code&gt; removes version from table. Other method are also available in source code.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does rails raise pending migration error
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://github.com/rails/rails/blob/b13a5cb83ea00d6a3d71320fd276ca21049c2544/activerecord/lib/active_record/migration.rb#L1079"&gt;Migration&lt;/a&gt; class in activerecord gem there is class available &lt;a href="https://github.com/rails/rails/blob/b13a5cb83ea00d6a3d71320fd276ca21049c2544/activerecord/lib/active_record/migration.rb#L548C6-L548C22"&gt;CheckPending&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;call method from CheckPending class&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  def call(env)
        mtime = ActiveRecord::Base.connection.migration_context.last_migration.mtime.to_i
        if @last_check &amp;lt; mtime
          ActiveRecord::Migration.check_pending!(connection)
          @last_check = mtime
        end
        @app.call(env)
      end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;call&lt;/code&gt; method in CheckPending class calls an another method of Migration class &lt;code&gt;check_pending!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Some methods from Migration class&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def check_pending!(connection = Base.connection)
        raise ActiveRecord::PendingMigrationError if connection.migration_context.needs_migration?
      end

def needs_migration?
  (migrations.collect(&amp;amp;:version) - get_all_versions).size &amp;gt; 0
end

def get_all_versions
   if SchemaMigration.table_exists?
     SchemaMigration.all_versions.map(&amp;amp;:to_i)
   else
     []
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code, check_pending! calls the need_migration? method that calls the get_all_versions method. get_all_versions method with the help of SchemaMigration give all version to need_migration? method which check if number of migration files are equal to number of versions available in schema_migration table. If schema_migration table have less numbers of version it means there are some pending migration.&lt;/p&gt;

&lt;p&gt;Migration class also have an another class &lt;a href="https://github.com/rails/rails/blob/b13a5cb83ea00d6a3d71320fd276ca21049c2544/activerecord/lib/active_record/migration.rb#L130C9-L130C30"&gt;PendingMigrationError&lt;/a&gt; which have details of error for pending migrations.&lt;/p&gt;

&lt;p&gt;CheckPending and PendingMigrationError classes are accessed from railtie class during initialization.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, ruby on rails are designed with detailing to ensure its smoothness and thats what makes it different from other frameworks.&lt;/p&gt;

&lt;p&gt;I would love to know your reviews on this article. If you liked it or if you have any suggestion please put a comment.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Should we use DRY code pattern ?</title>
      <dc:creator>Ronak Gupta</dc:creator>
      <pubDate>Wed, 04 Oct 2023 14:01:02 +0000</pubDate>
      <link>https://dev.to/iamronakgupta/should-we-use-dry-code-7oe</link>
      <guid>https://dev.to/iamronakgupta/should-we-use-dry-code-7oe</guid>
      <description>&lt;p&gt;&lt;em&gt;Is it always good to have DRY(don't repeat yourself) code pattern?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;DRY is a software principal which aims to reduce the repition of code means don't repeat your code. Write your code in module, function, methods or classes so that you can use it anywhere anytime, without writing it again and again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is the advantage:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;-&amp;gt; You don't have to write code again and again.&lt;br&gt;
-&amp;gt; It take less memory (which we don't bother most of the time).&lt;br&gt;
-&amp;gt; You need to change only at one place, when there is any change in the algorithm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But here what I experienced:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's assume there is large codebase of a social media, there is a function which is performing some task, let's assume creating a comment. Now, this function is used by many users (admin, creator, viewer) to create comment. Here it is best to use DRY code because all users are creating comment in same way, but we want to change some logic of any user there is some headache occurs. Let's assume for 'special user' we want to add functionality to add stickers. Now we have to change basic functionality of comment function.Whenever we are changing something we also need to make sure if everything is working fine for other users as well.&lt;/p&gt;

&lt;p&gt;Have you also experienced this?&lt;br&gt;
Would love to hear your opinions.&lt;/p&gt;

</description>
      <category>programming</category>
    </item>
    <item>
      <title>Why Java is not pure OOPS?</title>
      <dc:creator>Ronak Gupta</dc:creator>
      <pubDate>Thu, 17 Aug 2023 17:30:16 +0000</pubDate>
      <link>https://dev.to/iamronakgupta/why-java-is-not-pure-oops-442i</link>
      <guid>https://dev.to/iamronakgupta/why-java-is-not-pure-oops-442i</guid>
      <description>&lt;p&gt;I know one reason and that is,&lt;br&gt;
Java allows data types like int, float , char which is neither a class nor a object. According to OOPS concept they should be either object or class. Hence, it is not pure Object Oriented Programming.&lt;/p&gt;

&lt;p&gt;Do you know any other reason?&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
