# What's the proper way to conditionallly create a view?

**URL:** https://forum.liquibase.org/t/whats-the-proper-way-to-conditionallly-create-a-view/4053
**Category:** General Discussion
**Created:** [November 16, 2019, 4:18pm UTC](https://forum.liquibase.org/t/whats-the-proper-way-to-conditionallly-create-a-view/4053 "2019-11-16T16:18:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![un1382561729492r88id](https://avatars.discourse-cdn.com/v4/letter/u/c4cdca/32.png) [@un1382561729492r88id](https://forum.liquibase.org/u/un1382561729492r88id)
#### Post date: [November 16, 2019, 4:18pm UTC](https://forum.liquibase.org/t/whats-the-proper-way-to-conditionallly-create-a-view/4053/1 "2019-11-16T16:18:00Z")

</div>

The suggestion to have two changesets with the two different definitions is what I would recommend. You can use multiple dbms names separated by commas to show that the changeset should apply to any in the list, and you can also use ! to indicate not - so you can do things like use dbms=“postgresql” on one changeset and dbms="!postgresql" on another.&nbsp;

Steve Donie  
Principal Software Engineer  
Datical, Inc. [http://www.datical.com/](http://www.datical.com/)

---

<div class="post-metadata">

### Author: ![un1382561729492r88id](https://avatars.discourse-cdn.com/v4/letter/u/c4cdca/32.png) [@un1382561729492r88id](https://forum.liquibase.org/u/un1382561729492r88id)
#### Post date: [November 16, 2019, 4:18pm UTC](https://forum.liquibase.org/t/whats-the-proper-way-to-conditionallly-create-a-view/4053/2 "2019-11-16T16:18:00Z")

</div>

Change the dbms attribute will not affect the checksum of a changeset. This is also true for the labels and contexts attributes.&nbsp;

Steve Donie  
Principal Software Engineer  
Liquibase Community Engagement  
Datical, Inc. [http://www.datical.com/](http://www.datical.com/)

---

<div class="post-metadata">

### Author: ![SteinarBang](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@SteinarBang](https://forum.liquibase.org/u/SteinarBang)
#### Post date: [November 16, 2019, 4:18pm UTC](https://forum.liquibase.org/t/whats-the-proper-way-to-conditionallly-create-a-view/4053/3 "2019-11-16T16:18:00Z")

</div>

I use derby and PostgreSQL and they have different syntaxes for aggregating over years and months (“YEAR(timestamp)” vs. “extract(year from timestamp) as year” (both result in a column name year or YEAR to be used in the query).

  
  

Derby do it this way:

select sum(t.transaction\_amount), extract(year from t.transaction\_time) as year, extract(month from t.transaction\_time) as month from transactions t join transaction\_types tt on tt.transaction\_type\_id=t.transaction\_type\_id join accounts a on a.account\_id=t.account\_id where tt.transaction\_is\_work and a.username=? group by extract(year from t.transaction\_time), extract(month from t.transaction\_time) order by extract(year from t.transaction\_time), extract(month from t.transaction\_time)

What's the best way to create a view conditionally based on the dbms used?&nbsp;

  

Is there some kind of if-then-else logic? (I have googled, but haven't found)

  

The only way I can think about is to have two separate changesets for creating the same view with a precondition on the dbms used...?

  

Also, instead of selecting on dbms, I would have preferred to select on capability so that instead of creating a view just for derby I would create a view for all dbms'es that follow the same syntax (and ditto for PostgreSQL). Is this possible?

---

<div class="post-metadata">

### Author: ![un1495701166182r38id](https://avatars.discourse-cdn.com/v4/letter/u/e47c2d/32.png) [@un1495701166182r38id](https://forum.liquibase.org/u/un1495701166182r38id)
#### Post date: [November 16, 2019, 4:18pm UTC](https://forum.liquibase.org/t/whats-the-proper-way-to-conditionallly-create-a-view/4053/4 "2019-11-16T16:18:00Z")

</div>

> &nbsp; Also, instead of selecting on dbms, I would have preferred to select on capability so that instead of creating a view just for derby I would create a view for all dbms’es that follow the same syntax (and ditto for PostgreSQL). Is this possible?

 Is it possible to eg. use sqlCheck and continue only if the SQL doesn't fail with a syntax error?

---

<div class="post-metadata">

### Author: ![un1495701166182r38id](https://avatars.discourse-cdn.com/v4/letter/u/e47c2d/32.png) [@un1495701166182r38id](https://forum.liquibase.org/u/un1495701166182r38id)
#### Post date: [November 16, 2019, 4:18pm UTC](https://forum.liquibase.org/t/whats-the-proper-way-to-conditionallly-create-a-view/4053/5 "2019-11-16T16:18:00Z")

</div>

Thanks for the feedback!

I don’t know which DBMSes that use the same syntax as derby…?&nbsp; Maybe all/most of them, but “!postgresql” seems a bit drastic…?&nbsp; ![]()

Will changing the dbms list in the precondition change the checksum of the changeset? Ie. can I add a dbms without breaking on installations that already have applied the changeset?

---

<div class="post-metadata">

### Author: ![un1495701166182r38id](https://avatars.discourse-cdn.com/v4/letter/u/e47c2d/32.png) [@un1495701166182r38id](https://forum.liquibase.org/u/un1495701166182r38id)
#### Post date: [November 16, 2019, 4:18pm UTC](https://forum.liquibase.org/t/whats-the-proper-way-to-conditionallly-create-a-view/4053/6 "2019-11-16T16:18:00Z")

</div>

> &nbsp; _I don’t know which DBMSes that use the same syntax as derby…?&nbsp; Maybe all/most of them, but “!postgresql” seems a bit drastic…?_ &nbsp; ![]()

(According to a bit of googling, [MySQL](https://stackoverflow.com/a/3366929) and [MSSQL](https://stackoverflow.com/questions/5406170/sql-grouping-by-month-and-year/5406280) use the same syntax as derby (or at least: looks very similar), while e.g. [Oracle has a third variant](https://stackoverflow.com/a/11532489). )

---

<div class="post-metadata">

### Author: ![un1495701166182r38id](https://avatars.discourse-cdn.com/v4/letter/u/e47c2d/32.png) [@un1495701166182r38id](https://forum.liquibase.org/u/un1495701166182r38id)
#### Post date: [November 16, 2019, 4:18pm UTC](https://forum.liquibase.org/t/whats-the-proper-way-to-conditionallly-create-a-view/4053/7 "2019-11-16T16:18:00Z")

</div>

> &nbsp;The added changesets supporting derby and PostgreSQL, can be seen here.
