title: How to do knex.js migrations using Promise then
body: <p>I'm still not sure how to do migrations with knex.</p>
  
  <p>I just started with this recently, and I have no prior experience with Promise in general. I have tried the following, to no avail.</p>
  
  <p><strong>Case 1</strong></p>
  
  <pre><code>exports.up = (knex, Promise) =&gt; Promise.all([])
      .then(() =&gt; {
          // Create custom type
          knex.raw("CREATE TYPE a AS ENUM ('a1', 'a2', 'a3', 'a4')");
          knex.raw("CREATE TYPE b AS ENUM ('b1', 'b2', 'b3', 'b4')");
      }).then(() =&gt; {
          // Create table dependent on custom type
          knex.schema.createTable('table1', (table) =&gt; {
              table.increments('id').primary();
              table.specificType('ace', 'a').defaultTo('a1').notNullable();
              table.specificType('bee', 'b').defaultTo('b1').notNullable();
          }
      }).catch((error) =&gt; {
          console.error(error);
      });
  </code></pre>
  
  <p>and</p>
  
  <p><strong>Case 2</strong></p>
  
  <pre><code>exports.up = (knex, Promise) =&gt; Promise.then((resolve, reject) =&gt; {
          // Create custom type
          knex.raw("CREATE TYPE a AS ENUM ('a1', 'a2', 'a3', 'a4')");
          knex.raw("CREATE TYPE b AS ENUM ('b1', 'b2', 'b3', 'b4')");
  
          return knex;
      }).then((knex) =&gt; {
          // Create table dependent on custom type
          knex.schema.createTable('table1', (table) =&gt; {
              table.increments('id').primary();
              table.specificType('ace', 'a').defaultTo('a1').notNullable();
              table.specificType('bee', 'b').defaultTo('b1').notNullable();
          }
  
          return knex;
      }).catch((error) =&gt; {
          console.error(error);
      });
  </code></pre>
  
  <p>Now, you may ask why create a specific enum when knex api provided for it. The reason is that knex do not support native enumeration for Postgres, but instead use constraints to implement the enumeration, which is not what is desired. (Source: <a href="https://github.com/tgriesser/knex/issues/394" rel="nofollow">https://github.com/tgriesser/knex/issues/394</a>)</p>
  
  <p>For case 1, I thought it worked, until I realised that everything in <code>.then</code> is not getting executed. Case 2, I was told that Promise.then is not a function, which is baffling, since I thought Promise (even in Bluebird's implementation), has <code>then</code> as a method.</p>
  
  <p>As such, I'm at a loss on how to implement this. I thought about it for a few days, but since I recently encountered Promise, and did not have time to truly dive into it, I hope that you would be able to tell me how to implement migration sequentially, using Promise.</p>