christianparpart / Lightweight

thin and lightweight, fast, ODBC API wrapper for modern C++ uses
4 stars 1 forks source link

Support DB migration #6

Open christianparpart opened 2 months ago

christianparpart commented 2 months ago

Requirements

Ideas

A single C++ migration might look like this:

struct Migration<Year, Month, Day, Hour, Minute, Second, Seq> {
    void Migrate(Migration& migration)
    {
        migration.CreateTable([](auto& table) {
            table.Column<std::optional<int>>("secret_number").Indexed();
            table.Column<std::optional<std::string>, MaxChars { 30 }>("description");
            table.Column<Guid>("id");
            table.PrimaryKey("id");
        });
        migration.AlterTable([](auto& table) {
            table.Add().Column<int>("prime_number").Required();
            table.Remove().Columns("blurb");
            // change e.g. column's CHAR(30) width to 40
            table.Change().ColumnWidth("name", 40);
        });
        migration.DropTable("deprecated_records");
    }
};
FelixTheC commented 1 day ago

with my background I would suggest something like this

struct Migration
{
    constexpr std::string revision = "20241011130024120" // current datetime with microseconds and also the suffix of the current file
    constexpr std::string down_revision = "" // the previous one if any

    [[ nodiscard ]] bool upgrade()
    {
        migration.CreateTable([](auto& table) {
            table.Column<std::optional<int>>("secret_number").Indexed();
            table.Column<std::optional<std::string>, MaxChars { 30 }>("description");
            table.Column<Guid>("id");
            table.PrimaryKey("id");
        });
    }

   // will be executed if upgrade fails
   [[ nodiscard ]] bool downgrade()
   {
       migration.DropTable("deprecated_records");
   }
}

the idea is if from Alembic