@@ -47,6 +47,18 @@ func findTtlColumn(cols []sqlchemy.IColumnSpec) sColumnTTL {
4747 return ret
4848}
4949
50+ func findOrderByColumns (cols []sqlchemy.IColumnSpec ) []string {
51+ var ret []string
52+ for _ , col := range cols {
53+ if clickCol , ok := col .(IClickhouseColumnSpec ); ok {
54+ if (clickCol .IsOrderBy () || clickCol .IsPrimary ()) && len (clickCol .PartitionBy ()) == 0 {
55+ ret = append (ret , clickCol .Name ())
56+ }
57+ }
58+ }
59+ return ret
60+ }
61+
5062func findPartitions (cols []sqlchemy.IColumnSpec ) []string {
5163 parts := make ([]string , 0 )
5264 for i := range cols {
@@ -158,18 +170,41 @@ func (clickhouse *SClickhouseBackend) CommitTableChangeSQL(ts sqlchemy.ITableSpe
158170 }*/
159171
160172 // check TTL
161- oldTtlSpec := findTtlColumn (changes .OldColumns )
162- newTtlSpec := findTtlColumn (ts .Columns ())
163- log .Debugf ("old: %s new: %s" , jsonutils .Marshal (oldTtlSpec ), jsonutils .Marshal (newTtlSpec ))
164- if oldTtlSpec != newTtlSpec {
165- if oldTtlSpec .Count > 0 && newTtlSpec .Count == 0 {
166- // remove
167- sql := fmt .Sprintf ("REMOVE TTL" )
168- alters = append (alters , sql )
169- } else {
173+ {
174+ oldTtlSpec := findTtlColumn (changes .OldColumns )
175+ newTtlSpec := findTtlColumn (ts .Columns ())
176+ log .Debugf ("old: %s new: %s" , jsonutils .Marshal (oldTtlSpec ), jsonutils .Marshal (newTtlSpec ))
177+ if oldTtlSpec != newTtlSpec {
178+ if oldTtlSpec .Count > 0 && newTtlSpec .Count == 0 {
179+ // remove
180+ sql := fmt .Sprintf ("REMOVE TTL" )
181+ alters = append (alters , sql )
182+ } else {
183+ // alter
184+ sql := fmt .Sprintf ("MODIFY TTL `%s` + INTERVAL %d %s" , newTtlSpec .ColName , newTtlSpec .Count , newTtlSpec .Unit )
185+ alters = append (alters , sql )
186+ }
187+ }
188+ }
189+
190+ // check order by
191+ {
192+ oldOrderBys := findOrderByColumns (changes .OldColumns )
193+ newOrderBys := findOrderByColumns (ts .Columns ())
194+ log .Debugf ("old: %s new: %s" , jsonutils .Marshal (oldOrderBys ), jsonutils .Marshal (newOrderBys ))
195+ if jsonutils .Marshal (oldOrderBys ).String () != jsonutils .Marshal (newOrderBys ).String () {
170196 // alter
171- sql := fmt .Sprintf ("MODIFY TTL `%s` + INTERVAL %d %s" , newTtlSpec .ColName , newTtlSpec .Count , newTtlSpec .Unit )
172- alters = append (alters , sql )
197+ altered := false
198+ for i := range newOrderBys {
199+ if ! utils .IsInStringArray (newOrderBys [i ], oldOrderBys ) {
200+ oldOrderBys = append (oldOrderBys , newOrderBys [i ])
201+ altered = true
202+ }
203+ }
204+ if altered {
205+ sql := fmt .Sprintf ("MODIFY ORDER BY (%s)" , strings .Join (oldOrderBys , ", " ))
206+ alters = append (alters , sql )
207+ }
173208 }
174209 }
175210
0 commit comments