Search code examples
postgresqlliquibase

How to add a generated column to a PostgreSQL table with Liquibase?


I'd like to add a generated column https://www.postgresql.org/docs/current/ddl-generated-columns.html to my PostgreSQL table with Liquibase. I've tried everything that came to my mind, even something like:

 - column:
     computed: true
     name: final_blows_death_ratio numeric GENERATED ALWAYS AS (CASE deaths WHEN 0 THEN 'NaN' ELSE TRUNC(final_blows::NUMERIC / deaths, 2) END) STORED

which results in

Unexpected error running Liquibase: ERROR: syntax error at end of input
  Position: 195 [Failed SQL: (0) ALTER TABLE public.overwatch_player_performances ADD "final_blows_death_ratio numeric GENERATED ALWAYS AS (CASE deaths WHEN 0 THEN 'NaN' ELSE TRUNC(final_blows::NUMERIC / deaths, 2) END) STORED"]

Solution

  • Got an hour to work on it again. This gets the job done:

    - addColumn:
        columns:
          - column:
              name: column_name
              type: >
                column_type GENERATED ALWAYS AS (expression) STORED
              constraints:
                nullable: true