SQL (DDL) 脚本的建议位置是什么?

发布于 2024-12-08 10:16:35 字数 252 浏览 0 评论 0原文

Maven 标准目录结构

我敢打赌几乎每个 Web 项目都使用数据库和某种需要存储在某处的 SQL 脚本,那么保存这些文件的“最佳”位置可能是什么呢?

请指教。

What's the recommended location for SQL, DDL, ... scripts in the Maven standard directory structure?

I bet almost every web project uses a DB and some kind of SQL scripts that need to be stored somewhere, so what would probably be the "best" place where to keep these files?

Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

白首有我共你 2024-12-15 10:16:35

我认为这没有最佳实践。在我过去的项目中,我创建了一个单独的目录来存储此类 SQL 脚本。

例如src/main/db

默认情况下,它不会打包到最终的 JAR 中(这是大多数情况下的首选方式),但将其打包在程序集中很方便。您甚至可以通过添加相应的资源声明或使用 maven build-helper 插件将它们打包到主工件 JAR 中。

但是,一切都取决于您对此脚本的使用情况。我的简单“经​​验法则”是,只有当它们确实是要由应用程序加载的资源时,我才会考虑将它们放入 resources/ 中。

I think there is no best practice for this. In my past project, I created a separate directory for storing such SQL script.

For example src/main/db.

It won't be packaged to final JAR by default (which are the preferred way in most case), yet it is convenient enough to let it packaged in assembly. You can even package them in the main artifact JAR, by adding corresponding resource declaration or using maven build-helper plugin.

However, all depends on your usage on this script. My simple "rule of thumb" is that, I would consider putting them in resources/ only when they are really resources to be loaded by the application.

失而复得 2024-12-15 10:16:35

我认为这完全取决于这些脚本的处理时间和方式:

  1. 编译时间:这些是您的编译器/工具链消耗并产生工件的东西。 Maven 对此的指导非常明确,因此文件将属于 src/main/ 中的某个位置,例如 src/main/sqlsrc/main/db。虽然我不会这样做,但我可以看到编译时的任务使用它们来修改数据库。我可以看到这里使用 liquibase 脚本,然后通过 Maven 任务执行。
  2. 运行时:它们由运行时环境使用并对其进行修改,或者被其消耗以产生结果。将它们放置在 src/main/resources 中似乎是合理的,以便您的运行时进程可以使用它们来更改您的数据库,因为您认为合适 - 例如作为部署时热修复处理的一部分,或者作为您的数据库的一部分。正常的数据库动态版本控制工作。同样,也许您随应用程序一起提供 liquibase,然后以这种方式进行就地数据库更改...
  3. 设计时:在我看来,这是最有可能的情况。我应该将 DDL 存储在哪里,以便在 VCS 中正确跟踪它们,并仍然保持符合 Maven 的结构?对我来说,这是 src/scripts/sql 或 src/scripts/db 。这将它们作为“源”文件置于 Maven 的权限范围内,但位于一个旨在以更临时的方式使用的位置。

I think it depends entirely on when and how these scripts are processed:

  1. Compile Time: these are things that your compiler/toolchain consume and produce artifacts. The maven guidance on this very clear, and thus the files would belong somewhere in src/main/, like src/main/sql or src/main/db. While I wouldn't do this, I could see these being used by a task at compile to modify your DB. I could see liquibase scripts being used here and then executed via a maven task.
  2. Runtime: these are used by your runtime environment and either modify it, or are consumed by it to produce results. Placing them in src/main/resources seems reasonable so that your runtime processes can consume them change your DB as you see fit - say as part of your hot-fix processing on deployment, or as part of your normal DB on-the-fly versioning efforts. Again, maybe you ship liquibase with your app, and then do in-place DB changes that way...
  3. Design Time: This seems to me to be the most likely scenario. Where should I store my DDL in order to properly track them in VCS, and still maintain my maven-compliant structure? For me, this is src/scripts/sql or src/scripts/db. This places them as "source" files within the purview of maven, but in a place that is designed to used in a more ad-hoc manner.
聽兲甴掵 2024-12-15 10:16:35

src/main/resources 是一个好地方,但请记住它被打包到您的最终 jar 中,因此这取决于您是否想在生产代码中显示它。

如果没有,您可以通过将 maven-jar-plugin 配置摘录添加到适当的 pom.xml 来过滤掉它:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
         <excludes>src/main/resources/privateSubdir/**</excludes>
    </configuration>
</plugin>

src/main/resources is a good place, but remember it gets packed into your final jar, so it depends if you want to reveal this in your production code or not.

If not, you can filter out this by adding maven-jar-plugin configuration excerpt to appropriate pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
         <excludes>src/main/resources/privateSubdir/**</excludes>
    </configuration>
</plugin>
深府石板幽径 2024-12-15 10:16:35

我将使用 src/main/resources 来实现此目的。也许在那里创建一个子文件夹。

I would use src/main/resources for this purpose. Maybe creating a subfolder there.

放肆 2024-12-15 10:16:35

我将我的应用程序划分为多个 Eclipse 项目,每个架构层一个。这包括数据库。本质上,我专门为该项目创建了一种新的“数据库”打包类型。我创建了一个 src/main/sql 文件夹,在该文件夹下放置数据库模式(例如 src/main/sql/security),而不是 Java 包。我想如果您使用目录,那么首先是目录,然后是目录下面的模式。在每个模式文件夹中,我为每种类型的数据库对象(表、视图等)放置一个文件夹,以便最终得到 src/main/sql/security/tables,然后放置所有表定义那里有该模式的文件。我有兴趣阅读有关这样做的任何意见。

I divided my application into multiple Eclipse projects, one for each architectural layer. This includes the database. In essence, I created a new packaging type of "database" just for that project. I created a src/main/sql folder, and under that, instead of Java packages, I put database schemas (eg src/main/sql/security). I suppose if you use catalogs, it would be first the catalogs then under them the schemas. In each of the schema folders, I put a folder for each type of database object (table, view, etc) so that I ended up with, for example, src/main/sql/security/tables and then put all the table definition files for that schema in there. I'm interesting in reading any opinions on doing it this way.

我做我的改变 2024-12-15 10:16:35

这在很大程度上取决于您的情况,但首先最好将您的应用程序与底层数据库结构分开。因此,我建议您将所有与数据库相关的内容移动到单独的 Maven 项目中。完成此操作后,数据库脚本在 /src/main/scripts 中有一个很好的位置。

It very much depends on your mileage but first of all it's a good idea to separate your application from the underlying database structure. Therefore i'll recommend that you move all database related stuff to a separate maven project. Having done that, database scripts have a nice slot in /src/main/scripts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文