仅在表不存在时如何执行DDL? [英] How to execute DDL only when tables don't exist?

查看:142
本文介绍了仅在表不存在时如何执行DDL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Slick 1.0与Play Framework 2.1和MySQL配合使用.

I'm using Slick 1.0 with Play Framework 2.1 and MySQL.

我想控制ddl表的创建,以便仅在表不存在时才进行创建.也就是说,这些表应该仅在我第一次开始玩游戏时创建.

I'd like to control the ddl table creation so that it only takes place if the tables don't exist. That is to say that the tables should only get created the first time I start play.

如何在Slick中做到这一点?

How to do it in Slick?

推荐答案

为了他人的利益,SLICK提供了

For the benefit of others SLICK provides an MTable Object that you can use to count the number of tables present in your database.

如果不存在,则可以有条件地调用ddl.在下面的情况下,我希望有11个表+ play_evolutions表

You can then conditionally call the ddl if they are not present. In the case below I expect to have 11 tables + the play_evolutions table

import scala.slick.jdbc.meta._

 if (MTable.getTables.list().size < 12) {
        (Contacts.ddl ++ ThirdParties.ddl ++ Directorates.ddl ++ ServiceAreas.ddl ++ ICTServers.ddl
          ++ ICTServerDependencies.ddl ++ ICTSystems.ddl ++ ICTSystemDependencies.ddl ++ ICTSystemServerDependencies.ddl
              ++ CouncilServices.ddl ++ CouncilServiceDependencies.ddl).create
}

这篇关于仅在表不存在时如何执行DDL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆