title: C++ - Database schema using migration
body: <p>I'm working on an application that requires the use of a database. We are currently using migration files, however, this is just raw sql and can get quite messy and confusing, especially with everyone adding to it and also keeping it up to date is becoming a lot harder.</p>
  
  <p>I have been contemplating the idea of having a migration type functionality into the application, where we create a class for each of the migrations and then define the table and columns etc.. Using lambdas. However, what I've come up with does not seem to be working. Here is an example so far:</p>
  
  <pre><code>class Schema {
       public:
  
            Schema() { } // construct 
  
            template&lt;typename T&gt;
            void static create(std::string name, T t)
            {
                 cout &lt;&lt; "Create is started.."; 
                 // This is where all the logic will go 
            } 
  
            void static integer(std::string column_name)
            { 
                std::cout &lt;&lt; "Column integer is called";
                // put SQL here for integer value
            }
  };
  </code></pre>
  
  <p>Then the migration file will work something like:</p>
  
  <pre><code>class CreateUserTable {
  
      public:
         CreateUserTable() {
  
              Schema::create("users", []()
              {
                  Schema::integer("id");
              }); 
         }
  
  
  }; 
  </code></pre>
  
  <p>The idea is that when I call these migration files, essentially it will produce a series of well formed sql statements that I can use for my migrations. </p>
  
  <p>The question:</p>
  
  <p>With using Lambdas (as it is), how would I go about calling each of the function calls for the columns before the create function is executed fully? </p>
  
  <p>EDIT:</p>
  
  <p>To clear things up, essentially, in the body of the lambda function, whenever I call:</p>
  
  <pre><code>Schema::integer("COLUM_NAME");
  </code></pre>
  
  <p>It will add another layer to the final SQL statement. So let's say I did the following:</p>
  
  <pre><code>Schema::create("users", []()
  {
        Schema::integer("id");
        Schema::integer("number"); 
  }); 
  </code></pre>
  
  <p>It would create the following string:</p>
  
  <pre><code>CREATE TABLE `users` (
    id INT NOT NULL AUTO_INCREMENT, 
    number INT NOT NOW AUTO_INCREMENT
  
  )
  </code></pre>
  
  <p>But, by creating a new object of "CreateUserTable" the only thing that is outputted is: <code>cout &lt;&lt; "Create is started..";</code> whereas I need <code>std::cout &lt;&lt; "Column integer is called";</code> to be called before it.. It doesn't look like it's doing anything with the function?</p>