Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set max column value as sequence start value with liquibase tags

Tags:

sql

liquibase

I wonder if it possible to get maximum column value from a certain table and set it as start sequence value with no pure sql. The following code doesn't work:

   <property name="maxId" value="(select max(id)+1 from some_table)" dbms="h2,mysql,postgres"/>
   <changeSet author="author (generated)" id="1447943899053-1">
      <createSequence sequenceName="id_seq" startValue="${maxId}" incrementBy="1"/>
   </changeSet>

Got an error:

Caused by: liquibase.parser.core.ParsedNodeException: java.lang.NumberFormatException: For input string: "${m"

I've tried it with no parentheses around select ... etc. with the same result. So it's not possible to use computed value as start sequence value?

like image 796
dfche Avatar asked Nov 24 '15 08:11

dfche


1 Answers

So, such a solution worked for me:

<changeSet author="dfche" id="1448634241199-1"> 
  <createSequence sequenceName="user_id_seq" startValue="1" incrementBy="1"/> 
</changeSet> 
<changeSet author="dfche" id="1448634241199-2">
  <sql dbms="postgresql">select setval('user_id_seq', max(id)+1) from jhi_user</sql> 
  <sql dbms="h2">alter sequence user_id_seq restart with (select max(id)+1 from jhi_user)</sql>
</changeSet>
like image 54
dfche Avatar answered Oct 03 '22 18:10

dfche