CINXE.COM
Create All DDL | Docs | Ebean
<!doctype html> <html lang="en"> <head> <title>Create All DDL | Docs | Ebean</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="shortcut icon" href="/images/favicon.ico"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Source+Sans+Pro|Ubuntu&display=swap"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous"> <link rel="stylesheet" href="/css/reset3.css"> <link rel="stylesheet" href="/css/site3.css"> <link rel="stylesheet" href="/css/pygments3.css"> </head> <body> <div id="main"> <div id="banner"> <header> <nav id="top"> <h1 id="breadcrumb"> <a class="nav-logo" href="/"><img src="/images/logo-200.png" height="35"></a> <a href="/docs">Documentation</a><span class="sep"> / </span><span class="last">DDL Generation</span> </h1> <ul> <li><a onclick="toggleTheme();" title="switch dark light theme"><i class="fas fa-adjust"></i></a></li> </ul> </nav> </header> </div> <div class="grid grid-docs"> <aside> <nav class="side"> <ul> <li class="nav0 "> <a href="/docs/getting-started">Getting started</a> </li> <li class="nav0 "> <a href="/docs/intro">Introduction</a> </li> <li class="nav0 active"> <a class="active" href="/docs">Documentation</a> <ul> <li class="nav1 "> <a href="/docs/best-practice">Best practice</a> </li> <li class="nav1 "> <a href="/docs/query">Query</a> </li> <li class="nav1 "> <a href="/docs/persist">Persist</a> </li> <li class="nav1 "> <a href="/docs/transactions">Transactions</a> </li> <li class="nav1 "> <a href="/docs/mapping">Mapping</a> </li> <li class="nav1 active"> <a class="active" href="/docs/ddl-generation">DDL & Migrations</a> <ul> <li class="nav1 active"> <a class="active" href="/docs/ddl-generation">DDL Generation</a> <ul class="nav nav-scroll"> <li > <a href="#test-config">Using ebean-test</a> </li> <li > <a href="#properties">Using properties</a> </li> <li > <a href="#create-all">db-create-all.sql</a> </li> <li > <a href="#initSql">initSql</a> </li> <li > <a href="#seedSql">seedSql</a> </li> </ul> </li> <li class="nav1 "> <a href="/docs/extra-ddl">Extra DDL</a> </li> <li class="nav1 "> <a href="/docs/db-migrations">DB Migrations</a> </li> <li class="nav1 "> <a href="/docs/db-migrations/detail">DB Migrations details</a> </li> </ul> </li> <li class="nav1 "> <a href="/docs/logging">Logging</a> </li> <li class="nav1 "> <a href="/docs/testing">Testing</a> </li> <li class="nav1 "> <a href="/docs/read-replicas">Read Replicas</a> </li> <li class="nav1 "> <a href="/docs/database">Database platforms</a> </li> <li class="nav1 "> <a href="/docs/multi-database">Multiple databases</a> </li> <li class="nav1 "> <a href="/docs/kotlin">Kotlin</a> </li> <li><a href="/docs/tuning">Tuning</a></li> <li class="nav1 "> <a href="/docs/features">Features</a> </li> </ul> </li> <li class="nav0 "> <a href="/support">Getting help</a> </li> <li class="nav0 "> <a target="_blank" href="/apidoc/13">API Javadoc</a> </li> <li class="nav0 "> <a href="/videos">Videos</a> </li> <li class="nav0 "> <a href="/docs/upgrading">Upgrading</a> </li> <li class="nav0 "> <a href="/releases">Releases</a> </li> </ul> </nav> </aside> <article> <form action="https://www.google.com/search" method="get" class="inline-form"> <input type="hidden" name="as_sitesearch" value="ebean.io"> <div id="page-search"> <div class="input-group"> <input class="frm" name="q" id="searchinput" type="text" placeholder="Search... (press 's' to focus)" data-placeholder-focus="Search... (use '↑', '↓' and '⏎' to select results)" data-placeholder-blur="Search... (press 's' to focus)" autocomplete="off"> <div class="input-group-btn"> <button class="frm" type="submit"><i class="fas fa-search"></i></button> </div> </div> <div id="page-search-results" style="display: none;"> <ul id="search-results-container" class="search-results"><li class=" active"><a href="/docs" title="Docs"><span style="color:#777;">Docs</span> Documentation </a></li><li class=""><small style="color:#999;">And 101 more...</small></li></ul> </div> </div> </form> <h2 id="overview">DDL Generation</h2> <p> Ebean can generate and execute <code>CREATE ALL</code> and <code>DROP ALL</code> DDL scripts which create all the tables. This is used for testing purposes. </p> <blockquote> When running a test or suite of tests the DB is created entirely from scratch </blockquote> <h2 id="test-config">Using ebean-test</h2> <p> The best way to control and run DDL for testing is to use <a href="/docs/testing">ebean-test</a> and the <a href="/docs/testing#ddlmode">ddlMode</a>. Using <code>dropCreate</code> means the DDL for create-all and drop-all is generated and run. </p> <h5>In application-test.yml</h5> <div class="syntax yml"><div class="highlight"><pre><span></span><span class="nt">ebean</span><span class="p">:</span> <span class="nt">test</span><span class="p">:</span> <span class="nt">platform</span><span class="p">:</span> <span class="l l-Scalar l-Scalar-Plain">h2</span> <span class="c1"># h2, postgres, mysql, oracle, sqlserver</span> <span class="nt">ddlMode</span><span class="p">:</span> <span class="l l-Scalar l-Scalar-Plain">dropCreate</span> <span class="c1"># none | dropCreate | migrations | create</span> <span class="nt">dbName</span><span class="p">:</span> <span class="l l-Scalar l-Scalar-Plain">myapp</span> </pre></div> </div> <h2 id="properties">Using properties</h2> <p> If we don't want to use <a href="/docs/testing">ebean-test</a> we can instead set both <code>ebean.ddl.generate</code> and <code>ebean.ddl.run</code> properties to true in <code>application-test.yml</code> (which is in src/test/resources) and in this way the <code>CREATE ALL</code> DDL is only generated and executed when running tests. </p> <h5>In application-test.yml</h5> <div class="syntax yml"><div class="highlight"><pre><span></span><span class="nt">ebean</span><span class="p">:</span> <span class="nt">ddl</span><span class="p">:</span> <span class="nt">generate</span><span class="p">:</span> <span class="l l-Scalar l-Scalar-Plain">true</span> <span class="nt">run</span><span class="p">:</span> <span class="l l-Scalar l-Scalar-Plain">true</span> </pre></div> </div> <h5>Or application-test.properties</h5> <div class="syntax properties"><div class="highlight"><pre><span></span><span class="na">ebean.ddl.generate</span><span class="o">=</span><span class="s">true</span> <span class="na">ebean.ddl.run</span><span class="o">=</span><span class="s">true</span> </pre></div> </div> <h2 id="create-all">db-create-all.sql</h2> <p> When DDL is generated <code>db-create-all.sql</code> and <code>db-drop-all.sql</code> are generated and put into the maven <code>target</code> or gradle <code>build</code> directory. </p> <p> When tests are run Ebean automatically regenerates these ddl script files and executes them prior to running the tests. This effectively recreates the database prior to running tests. </p> <h2 id="initSql">Initial DDL</h2> <p> We can additionally specify DDL to execute prior to running the "create-all" DDL via the <code>initSql</code> property. </p> <div class="syntax properties"><div class="highlight"><pre><span></span><span class="na">ebean.ddl.initSql</span><span class="o">=</span><span class="s">testInitialDdl.sql</span> </pre></div> </div> <h2 id="seedSql">Seed DDL</h2> <p> We can additionally specify DDL to execute after running the "create-all" DDL which we typically use to seed the database via the <code>seedSql</code> property. </p> <div class="syntax properties"><div class="highlight"><pre><span></span><span class="na">ebean.ddl.seedSql</span><span class="o">=</span><span class="s">testSeedData.sql</span> </pre></div> </div> <p> </p> <h4>Compared to DB Migrations</h4> <p> <a href="/docs/db-migrations">DB Migration</a> is different in that <code>DIFF</code> DDL scripts are generated for the changes to the model and these <code>DIFF</code> DDL scripts can be applied to the target database typically when Ebean starts. </p> <nav class="next"> <p class="next"> <a href="/docs/extra-ddl" class="next btn">Next: Extra DDL</a> </p> </nav> </article> </div> </div> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="/js/site3.js"></script> <script src="/js/search3.js"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-75181644-1"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-75181644-1'); </script> </body> </html>