Baykov Nikita
Baykov Nikita

Reputation: 618

Laravel 10 migrations. Alter table to increase timestamp precision

I'm currently working with a Laravel application. Having some difficulties with writing a migration that alters the already existing table. What I am trying to achieve is to increase the precision of a timestamp column. Currently, its values are rounded up to a second, and I want to be able to track created_at and and updated_at up to a microsecond. I also have a view based on this table. After a quick google search, I came up with the two possible solutions.

The first one is:

    public function up(): void                                                  
    {                                                                                                                                              
        Schema::table('pd_proposals', function (Blueprint $table) {             
                                                                                
            $table_name = $table->getTable();                                   
            $created_at = PDProposal::CREATED_AT;                               
            $updated_at = PDProposal::UPDATED_AT;                               
                                                                                
            $query = <<<SQL                                                     
                do $$                                                           
                  declare view_pd_proposals_def text;                           
                  declare exec_text text;                                       
                begin                                                           
                  view_pd_proposals_def := pg_get_viewdef('view_pd_proposals'); 
                  drop view view_pd_proposals;                                  
                                                                                
                  ALTER TABLE $table_name ALTER COLUMN $created_at TYPE timestamp(6);
                  ALTER TABLE $table_name ALTER COLUMN $updated_at TYPE timestamp(6);
                                                                                
                  exec_text := format('create view view_pd_proposals as %s',    
                      view_pd_proposals_def);                                   
                  execute exec_text;                                            
                end $$                                                          
            SQL;                                                                
                                                                                
            DB::statement($query);                                              
        });   

The second is:

    public function up(): void                                                  
    {                   
        $res = DB::select("SELECT pg_get_viewdef('view_pd_proposals')")[0];     
        $view_pd_proposals_def = $res->pg_get_viewdef;                          
                                                                                
        DB::statement("drop view view_pd_proposals");                           
                                                                                
        Schema::table('pd_proposals', function (Blueprint $table) {             
            $table->timestamp('created_at', 6)->change();                       
            $table->timestamp('updated_at', 6)->change();                       
        });                                                                     
                                                                                
        DB::statement("create view view_pd_proposals as {$view_pd_proposals_def}");
    }  

In both cases, migrations run successfully. However, it does not seem to have any effect. If I try to add a row into my database directly with the INSERT statement and set the created_at value with now(), it is still rounded up a whole second.

But if I run the SQL inline code block directly in my PostgreSQL, it works as I expect it does. The inserted value will have 6 digits after the decimal point. It also works if I change the timestamp type in the second version, lets say, with the string type. So, I can confirm that my migrations are executed. Cannot see how to make it work with timestamp('created_at', 6).

Upvotes: 0

Views: 99

Answers (0)

Related Questions