<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Akash Srivastava on Medium]]></title>
        <description><![CDATA[Stories by Akash Srivastava on Medium]]></description>
        <link>https://medium.com/@akashsrivastava_21577?source=rss-3b52c1e6d5e7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KtLQD24Y7p7oJNsb.</url>
            <title>Stories by Akash Srivastava on Medium</title>
            <link>https://medium.com/@akashsrivastava_21577?source=rss-3b52c1e6d5e7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 22:59:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akashsrivastava_21577/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Fixing PG::ConnectionBad after brew upgrade]]></title>
            <link>https://medium.com/@akashsrivastava_21577/fixing-pg-connectionbad-after-brew-upgrade-6a946093fb6e?source=rss-3b52c1e6d5e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a946093fb6e</guid>
            <category><![CDATA[homebrew]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[macos]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[postgresql]]></category>
            <dc:creator><![CDATA[Akash Srivastava]]></dc:creator>
            <pubDate>Fri, 14 Feb 2020 06:06:29 GMT</pubDate>
            <atom:updated>2020-02-14T06:14:40.530Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*jrrCay9KJ1irXxC03js_bw.png" /></figure><p>On a lazy workday, I started my work half-awake by casually doing a brew upgrade , which I hadn’t done in 2 months. Little did I know that it will cause something that would make me jump in my chair and wake me up.</p><p>After it got finished, I got back to work doing db reseton a rails project. Here is what I saw:</p><pre> ~/m/v/scheduling-service/service  on #v19.2.0-rc8 *24 !5  bundle exec rake db:reset<br>rake aborted!<br>PG::ConnectionBad: could not connect to server: No such file or directory<br> Is the server running locally and accepting<br> connections on Unix domain socket &quot;/tmp/.s.PGSQL.5432&quot;?<br>/Users/akashsrivastava/.rbenv/versions/2.5.1/bin/bundle:23:in `load&#39;<br>/Users/akashsrivastava/.rbenv/versions/2.5.1/bin/bundle:23:in `&lt;main&gt;&#39;<br>Tasks: TOP =&gt; db:reset =&gt; db:drop =&gt; db:check_protected_environments<br>(See full trace by running task with --trace)</pre><p>PG::ConnectionBad ? I tried restarting the postgresql service</p><pre>brew services restart postgresql</pre><p>But that didn’t solve the problem. No db operations were running. That’s when I realized that postgres version after brew upgrade had become 12.1 while it was earlier 11.6 . I knew postgres wouldn’t delete my data even if I did a brew uninstall , so in haste of fixing I did</p><pre>brew uninstall postgresql</pre><p>followed by a</p><pre>brew install postgresql</pre><p>which brought in postgres 12.1 , as I had realized earlier. But what solved my problem is something I saw in the install logs of brew install postgresql</p><pre>To migrate existing data from a previous major version of PostgreSQL run:<br>  brew postgresql-upgrade-database</pre><p>This looked like something that could work, I tried it (of course after stopping postgres service),</p><pre> ~/m/v/scheduling-service/service  on #v19.2.0-rc8 *24 !5  brew services stop postgresql<br>Stopping `postgresql`... (might take a while)<br>==&gt; Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)</pre><pre> ~/m/v/scheduling-service/service  on #v19.2.0-rc8 *24 !5  brew postgresql-upgrade-database</pre><p>and voila:</p><pre>==&gt; Upgrading postgresql data from 11 to 12...<br>waiting for server to start....2020-02-14 10:42:54.697 IST [28252] LOG:  listening on IPv6 address &quot;::1&quot;, port 5432<br>2020-02-14 10:42:54.697 IST [28252] LOG:  listening on IPv4 address &quot;127.0.0.1&quot;, port 5432<br>2020-02-14 10:42:54.698 IST [28252] LOG:  listening on Unix socket &quot;/tmp/.s.PGSQL.5432&quot;<br>2020-02-14 10:42:54.745 IST [28253] LOG:  database system was interrupted; last known up at 2020-02-14 09:57:10 IST<br>2020-02-14 10:42:55.312 IST [28254] FATAL:  the database system is starting up<br>2020-02-14 10:42:55.312 IST [28255] FATAL:  the database system is starting up<br>2020-02-14 10:42:55.312 IST [28256] FATAL:  the database system is starting up<br>2020-02-14 10:42:55.313 IST [28253] LOG:  database system was not properly shut down; automatic recovery in progress<br>2020-02-14 10:42:55.317 IST [28257] FATAL:  the database system is starting up<br>2020-02-14 10:42:55.318 IST [28259] FATAL:  the database system is starting up<br>2020-02-14 10:42:55.318 IST [28258] FATAL:  the database system is starting up<br>2020-02-14 10:42:55.320 IST [28261] FATAL:  the database system is starting up<br>2020-02-14 10:42:55.320 IST [28260] FATAL:  the database system is starting up<br>2020-02-14 10:42:55.321 IST [28262] FATAL:  the database system is starting up<br>2020-02-14 10:42:55.331 IST [28253] LOG:  redo starts at 1/65F69FA8<br>2020-02-14 10:42:55.333 IST [28253] LOG:  invalid record length at 1/65F6ACA0: wanted 24, got 0<br>2020-02-14 10:42:55.333 IST [28253] LOG:  redo done at 1/65F6AC68<br>2020-02-14 10:42:55.333 IST [28253] LOG:  last completed transaction was at log time 2020-02-14 09:59:22.657123+05:30<br>2020-02-14 10:42:55.355 IST [28252] LOG:  database system is ready to accept connections<br> done<br>server started<br>waiting for server to shut down....2020-02-14 10:42:55.826 IST [28252] LOG:  received fast shutdown request<br>2020-02-14 10:42:55.827 IST [28252] LOG:  aborting any active transactions<br>2020-02-14 10:42:55.828 IST [28252] LOG:  background worker &quot;logical replication launcher&quot; (PID 28268) exited with exit code 1<br>2020-02-14 10:42:55.828 IST [28263] LOG:  shutting down<br>2020-02-14 10:42:55.833 IST [28252] LOG:  database system is shut down<br> done<br>server stopped<br>==&gt; Moving postgresql data from /usr/local/var/postgres to /usr/local/var/postgres.old...<br>==&gt; Creating database...<br>The files belonging to this database system will be owned by user &quot;akashsrivastava&quot;.<br>This user must also own the server process.</pre><pre>The database cluster will be initialized with locale &quot;C&quot;.<br>The default text search configuration will be set to &quot;english&quot;.</pre><pre>Data page checksums are disabled.</pre><pre>fixing permissions on existing directory /usr/local/var/postgres ... ok<br>creating subdirectories ... ok<br>selecting dynamic shared memory implementation ... posix<br>selecting default max_connections ... 100<br>selecting default shared_buffers ... 128MB<br>selecting default time zone ... Asia/Kolkata<br>creating configuration files ... ok<br>running bootstrap script ... ok<br>performing post-bootstrap initialization ... ok<br>syncing data to disk ... ok</pre><pre>initdb: warning: enabling &quot;trust&quot; authentication for local connections<br>You can change this by editing pg_hba.conf or using the option -A, or<br>--auth-local and --auth-host, the next time you run initdb.</pre><pre>Success. You can now start the database server using:</pre><pre>/usr/local/opt/postgresql/bin/pg_ctl -D /usr/local/var/postgres -l logfile start</pre><pre>==&gt; Migrating and upgrading data...<br>Performing Consistency Checks<br>-----------------------------<br>Checking cluster versions                                   ok<br>Checking database user is the install user                  ok<br>Checking database connection settings                       ok<br>Checking for prepared transactions                          ok<br>Checking for reg* data types in user tables                 ok<br>Checking for contrib/isn with bigint-passing mismatch       ok<br>Checking for tables WITH OIDS                               ok<br>Checking for invalid &quot;sql_identifier&quot; user columns          ok<br>Creating dump of global objects                             ok<br>Creating dump of database schemas<br>                                                            ok<br>Checking for presence of required libraries                 ok<br>Checking database user is the install user                  ok<br>Checking for prepared transactions                          ok</pre><pre>If pg_upgrade fails after this point, you must re-initdb the<br>new cluster before continuing.</pre><pre>Performing Upgrade<br>------------------<br>Analyzing all rows in the new cluster                       ok<br>Freezing all rows in the new cluster                        ok<br>Deleting files from new pg_xact                             ok<br>Copying old pg_xact to new server                           ok<br>Setting next transaction ID and epoch for new cluster       ok<br>Deleting files from new pg_multixact/offsets                ok<br>Copying old pg_multixact/offsets to new server              ok<br>Deleting files from new pg_multixact/members                ok<br>Copying old pg_multixact/members to new server              ok<br>Setting next multixact ID and offset for new cluster        ok<br>Resetting WAL archives                                      ok<br>Setting frozenxid and minmxid counters in new cluster       ok<br>Restoring global objects in the new cluster                 ok<br>Restoring database schemas in the new cluster<br>                                                            ok<br>Copying user relation files<br>                                                            ok<br>Setting next OID for new cluster                            ok<br>Sync data directory to disk                                 ok<br>Creating script to analyze new cluster                      ok<br>Creating script to delete old cluster                       ok</pre><pre>Upgrade Complete<br>----------------<br>Optimizer statistics are not transferred by pg_upgrade so,<br>once you start the new server, consider running:<br>    ./analyze_new_cluster.sh</pre><pre>Running this script will delete the old cluster&#39;s data files:<br>    ./delete_old_cluster.sh<br>==&gt; Upgraded postgresql data from 11 to 12!<br>==&gt; Your postgresql 11 data remains at /usr/local/var/postgres.old</pre><p>postgres is happy now, and I’m back to work. Cheers !!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a946093fb6e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Migrating from state_machine to aasm in Rails]]></title>
            <link>https://medium.com/@akashsrivastava_21577/migrating-from-state-machine-to-aasm-in-rails-92a81d13a94c?source=rss-3b52c1e6d5e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/92a81d13a94c</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[state-machine]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[rails-5]]></category>
            <dc:creator><![CDATA[Akash Srivastava]]></dc:creator>
            <pubDate>Tue, 10 Apr 2018 17:43:10 GMT</pubDate>
            <atom:updated>2018-04-10T17:43:10.954Z</atom:updated>
            <content:encoded><![CDATA[<p>First things first. State machines are awesome, be it any part of technology you use them in.</p><p>Recently at work, we passed many pipelines on migrating a very large Rails app from Rails 4 to Rails 5. One of the major parts of this change was shifting from state_machine to aasm for our state transitions. We rely heavily on state machines for how our instances shift states. Much of our tasks associated with the models too are integrated with the after/before actions of our state machines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*RZb3yfp1MRUE_hUzZrVHyA.png" /><figcaption>Generated using <a href="https://github.com/Katee/aasm-diagram">https://github.com/Katee/aasm-diagram</a></figcaption></figure><p><strong>Need for transition:</strong></p><p>One and only one reason, <a href="https://github.com/pluginaweek/state_machine">state_machine</a> has been dead, and for quite some time. We shifted from Rails 3.2 to Rails 4.2 last year, and since it was a really, really painful migration (one that resulted in my name over 70% of the ruby code git blame, despite almost none of the business logic being mine ), we fixed our focus on changed syntax and ActiveJob, found the much famous <a href="https://github.com/pluginaweek/state_machine/issues/334#issuecomment-68168119">monkeypatch</a> for Rails 4.2 and stayed happy for the time being with state_machine. Though there is <a href="https://github.com/state-machines/state_machines-activerecord">state_machines_activerecord</a>, we wanted to move to a more reliable and tested library, and as we already use <a href="https://github.com/state-machines/state_machines-activerecord">acts_as_state_machine</a> or aasm in one of our other projects, we tried and gave it a shot, when we began our Rails 5 voyage, for which of course neither state_machine and its patch worked, nor it was recommended.</p><p><strong>What changed:</strong></p><p>As it turned out, the process was not too messy. After a small study of the way both state_machine and aasm handle state transitions, one can easily find an analogy. Here are a few things which usually are a part of a state_machine laden project and how they should be modified to work with aasm</p><ol><li><strong>The gem itself</strong></li></ol><p>Goes without saying, remove from your Gemfile/gems.rb :</p><pre>gem &#39;state_machine&#39;</pre><p>and add :</p><pre>gem &#39;aasm&#39;</pre><p><strong>2. Get rid of the state_machine monkey-patch if present</strong></p><pre>module StateMachine<br>  module Integrations<br>    module ActiveModel<br>      public :around_validation<br>    end<br>    module ActiveRecord<br>      public :around_save<br>      def define_state_initializer<br>        define_helper :instance, &lt;&lt;-end_eval, __FILE__, __LINE__ + 1<br>          def initialize(*)<br>            super do |*args|<br>              self.class.state_machines.initialize_states self<br>              yield(*args) if block_given?<br>            end<br>          end<br>        end_eval<br>      end<br>    end<br>  end<br>end</pre><p>Yes, get rid of this if you have it, most probably in one of your config/initializers.</p><p><strong>3. Transitioning the transitions:</strong></p><p>This is the major part of the change and yet the easiest to implement. This includes code change in models. Take a look at the documentation over at aasm and start changing the code. Here are a few pointers.</p><ul><li>add include AASM to your model</li></ul><pre>class Question &lt; ActiveRecord::Base<br>  include AASM<br>  ...<br>end</pre><ul><li>specify the column name on which you are observing state transitions, for eg. if the column name is state</li></ul><pre>class Question &lt; ActiveRecord::Base<br>  include AASM<br>  ...<br>  aasm.attribute_name :state<br>  ...<br>end</pre><ul><li>Initiate your state machine block by listing out all your states. The common way is using one line to specify your initial state, and a second line to list all your non-initial states</li></ul><pre>class Question &lt; ActiveRecord::Base<br>  include AASM<br>  ...<br>  aasm.attribute_name :state<br>  aasm do<br>    state :authored, initial: true<br>    state :piloted, :non_active, :active, :removed<br>    ...<br>  end<br>  ...<br>end</pre><ul><li>Convert your events. All event blocks of the form transition :a =&gt; :b will be replaced by transitions from: :a, to: :b</li></ul><pre>class Question &lt; ActiveRecord::Base<br>  include AASM<br>  ...<br>  <br>  # State machine code</pre><pre>  state_machine :state, initial: :authored do</pre><pre>    event :pilot do<br>      transition :authored =&gt; :piloted<br>    end</pre><pre>    event :activate do<br>      transition [:piloted, :non_active] =&gt; :active<br>    end</pre><pre>    ..<br>  end</pre><pre> <br> # AASM code</pre><pre>  aasm.attribute_name :state<br>  aasm do<br>    state :authored, initial: true<br>    state :piloted, :non_active, :active, :removed</pre><pre>    event :pilot do<br>      transitions from: :authored, to: :piloted<br>    end</pre><pre>    event :activate do<br>      transitions from: [:piloted, :non_active], to: :active<br>    end</pre><pre>    ...<br>  end<br>  ...<br>end</pre><ul><li>Callbacks like before_transition and after_transition from state_machine can be converted like this:</li></ul><pre>class Question &lt; ActiveRecord::Base<br>  include AASM<br>  ...<br>  <br>  # State machine code</pre><pre>  state_machine :state, initial: :authored do<br>    before_transition :authored =&gt; :piloted, :do =&gt; :prepare_cockpit<br>    after_transition :authored =&gt; :piloted, :do =&gt; :fly_the_plane</pre><pre>    event :pilot do<br>      transition :authored =&gt; :piloted<br>    end<br>    ...<br>  end</pre><pre>  # AASM code</pre><pre>  aasm.attribute_name :state<br>  aasm do<br>    state :authored, initial: true<br>    state :piloted, :non_active, :active, :removed</pre><pre>    event :pilot do<br>      before do<br>        prepare_cockpit<br>      end<br>      transitions from: :authored, to: :piloted, after: :fly_the_plane<br>    end</pre><pre>    ...<br>  end<br>  ...<br>  <br>  def prepare_cockpit<br>    ...<br>  end</pre><pre>  def fly_the_plane<br>    ...<br>  end</pre><pre><br>end</pre><ul><li>However, in case of callbacks on a part of a transitions defined inside an event, one needs to define the transitions separately</li></ul><pre>class Question &lt; ActiveRecord::Base<br>  include AASM<br>  ...<br>  <br>  # State machine code</pre><pre>  state_machine :state, initial: :authored do<br>    after_transition :authored =&gt; :piloted, :do =&gt; :fly</pre><pre>    event :pilot do<br>      transition [:inactive, :authored] =&gt; :piloted<br>    end<br>    ...<br>  end</pre><pre>  # AASM code</pre><pre>  aasm.attribute_name :state<br>  aasm do<br>    state :authored, initial: true<br>    state :piloted, :non_active, :active, :removed</pre><pre>    event :pilot do<br>      transitions from: :authored, to: :piloted, after: :fly<br>      transitions from: :inactive, to: :piloted<br>    end</pre><pre>    ...<br>  end<br>  ...<br>  <br>  def fly<br>    ...<br>  end</pre><pre>end</pre><ul><li>if and unless guard blocks on transitions work the same way as in state_machine, and can also be substituted with a guard clause. The guards as well as callbacks can take arguments, lambda as well as Proc, same as the state machine guards</li></ul><pre>class Question &lt; ActiveRecord::Base<br>  include AASM<br>  ...<br>  <br>  # State machine code</pre><pre>  state_machine :state, initial: :authored do</pre><pre>    event :pilot do<br>      transition :authored =&gt; :piloted, if: :can_fly?<br>    end<br>    ...<br>  end</pre><pre>  # AASM code</pre><pre>  aasm.attribute_name :state<br>  aasm do<br>    state :authored, initial: true<br>    state :piloted, :non_active, :active, :removed</pre><pre>    event :pilot do<br>      transitions from: :authored, to: :piloted, guard: :can_fly?<br>    end</pre><pre>    ...<br>  end<br>  ...<br>  <br>  def can_fly?<br>    ...<br>  end</pre><pre>end</pre><p>Yes, that’s it for the models. You can take a detailed look at the <a href="https://github.com/aasm/aasm">docs</a> if you have more complex needs.</p><p><strong>4. The helpers:</strong></p><p>One plus point for state_machine , it has/had a variety of useful helpers for making use of states and events in views and controllers. aasm, though lagging behind a little in this domain, still has a good pool of helpers, both class and instance to make good use of. Here are some pointers.</p><ul><li>Question.aasm.states will give you an object list of all states available for Question model</li><li>Question.aasm.events will give you an object list of all events available for the Question model</li><li>Question.first.aasm.states will give an object list of all states available for transitioning to for a Question object, in this case the first one.</li><li>Question.first.aasm.events will give an object list of all events that can be applied on the current state of the Question object, i.e the first</li><li>All of the above helpers will produce an object list that contains name as the name of object, so appending .map(&amp;:name) will give a symbol array of the name of objects, that will come handy in drop-downs. Eg.</li></ul><pre>pry(main)&gt; Question.last.aasm.events.map(&amp;:name)<br>=&gt; [:pilot, :deactivate]</pre><ul><li>Another great point in favor of state_machine is its state_event attribute over the instance. For eg.</li></ul><pre>pry(main)&gt; question = Question.first<br>pry(main)&gt; question.state_event = :deactivate<br>pry(main)&gt; question.save</pre><p>The code above will end up saving the question after calling the deactivate event over it. This attribute is highly useful in rails forms where one can easily pass what event to call from, and the transition will happen without extra hassle. Unfortunately, there’s no equivalent attribute cum method in aasm . But one can always write a common ActiveRecord::Base helper for the same.</p><ul><li>On another note, the not-so-good-looking with_state / with_states scope methods of state_machine can be replaced by the enum equivalent syntax of aasm . For eg.</li></ul><pre>Question.with_state(:active) # state_machine</pre><p>gets replaced by a much cleaner :</p><pre>Question.active</pre><p>So yes, a couple of tweaks here and there, and a <strong>good pool of existing test cases</strong> which run green, you are done and production ready. This will get you started, but do back yourself up with the aasm docs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92a81d13a94c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>