From f7ecb554a47fb88f1165c34db3cf4a52ca4c2738 Mon Sep 17 00:00:00 2001 From: silverqx Date: Tue, 17 May 2022 09:25:58 +0200 Subject: [PATCH] docs added seeding to Building: Migrations --- docs/building-migrations.mdx | 80 ++++++++++++++++++++++++++++++++++-- docs/building-tinyorm.mdx | 10 +++-- 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/docs/building-migrations.mdx b/docs/building-migrations.mdx index 259b36572..1cb937340 100644 --- a/docs/building-migrations.mdx +++ b/docs/building-migrations.mdx @@ -25,6 +25,9 @@ import { - [Prerequisites](#prerequisites) - [vcpkg.json manifest](#vcpkg-json-manifest) - [Source code](#source-code) + - [Main file](#main-file) + - [Migrations](#migrations) + - [Seeders](#seeders) - [Migrations with CMake](#migrations-with-cmake) - [CMake project](#cmake-project) - [Build migrations](#build-migrations-cmake) @@ -115,6 +118,8 @@ Let's start in the `tom` project folder. +### Main file + Create `main.cpp` source file. ```bash @@ -133,12 +138,15 @@ And paste the following code. #include "migrations/2014_10_12_000000_create_posts_table.hpp" + #include "seeders/databaseseeder.hpp" + using Orm::DatabaseManager; using Orm::DB; using TomApplication = Tom::Application; using namespace Migrations; // NOLINT(google-build-using-namespace) + using namespace Seeders; // NOLINT(google-build-using-namespace) /*! Build the database manager instance and add a database connection. */ std::shared_ptr setupManager(); @@ -151,8 +159,8 @@ And paste the following code. auto db = setupManager(); return TomApplication(argc, argv, db, "TOM_MIGRATIONS_ENV") - // Migration classes .migrations() + .seeders() // Fire it up 🔥🚀✨ .run(); @@ -189,7 +197,7 @@ And paste the following code. QStringLiteral("tinyorm_tom")); } -#### Migrations +### Migrations If you have already built the `tom` application then you can generate a migrations using the [`make:migration`](migrations.mdx#generating-migrations) command 😎. @@ -199,11 +207,15 @@ tom make:migration create_posts_table Below is the expected folders structure for the migrations. The [`migrations.pri`](#migrations-source-files) file is used only by the `qmake` build system and is not needed with `CMake` builds. + + ```text tom/ └── database/ ├── migrations/ - └── migrations.pri + ├── seeders/ + ├── migrations.pri + └── seeders.pri ``` Let's create the first migration manually. @@ -262,6 +274,55 @@ And paste the following code. If you want, you can also build the `tom` application without the migrations, simply comment out the `migrations()` method and the corresponding `#include "migrations/xyz.hpp"` files. ::: +### Seeders + +The expected folders structure is described a few paragraphs [above](#folders-structure). The [`seeders.pri`](#seeders-source-files) file is used only by the `qmake` build system and is not needed with `CMake` builds. + +Let's create the root seeder class manually. + + + + +{`mkdir database/seeders\n +vim database/seeders/databaseseeder.hpp`} + + + + +{`mkdir -p database/seeders\n +vim database/seeders/databaseseeder.hpp`} + + + + +And paste the following code. + + #pragma once + + #include + + namespace Seeders + { + + /*! Main database seeder. */ + struct DatabaseSeeder : public Seeder + { + /*! Run the database seeders. */ + void run() override + { + DB::table("posts")->insert({ + {"name", "1. post"}, + {"name", "2. post"}, + }); + } + }; + + } // namespace Seeders + +:::tip +You can create more seeder classes like this and use the `call<>()` method to invoke them as is described in the [Calling Additional Seeders](seeding.mdx#calling-additional-seeders) section. +::: + ## Migrations with CMake Create a folder for the `CMake` build. @@ -591,6 +652,8 @@ SOURCES += $$PWD/main.cpp # Database migrations include($$PWD/database/migrations.pri) +# Database seeders +include($$PWD/database/seeders.pri) # Configure TinyORM library for the migrations purposes include($$TINY_MAIN_DIR/TinyORM/qmake/tom.pri) @@ -645,6 +708,17 @@ HEADERS += \ $$PWD/migrations/2014_10_12_000000_create_posts_table.hpp \ ``` +#### Seeders source files + +Create `database/seeders.pri` file and paste the following code. + +```qmake +INCLUDEPATH += $$PWD + +HEADERS += \ + $$PWD/seeders/databaseseeder.hpp \ +``` + ### Build migrations {#build-migrations-qmake} :::tip diff --git a/docs/building-tinyorm.mdx b/docs/building-tinyorm.mdx index 2c7c5ebbf..06b8e5c12 100644 --- a/docs/building-tinyorm.mdx +++ b/docs/building-tinyorm.mdx @@ -120,7 +120,9 @@ All `tinyorm.org` examples are based on the following folders structure. The `to │ ├── tom/ │ │ └── database/ │ │ ├── migrations/ -│ │ └── migrations.pri +│ │ ├── seeders/ +│ │ ├── migrations.pri +│ │ └── seeders.pri │ ├── tom-builds-cmake/ │ │ └── build-TinyOrm-Desktop_Qt_6_2_4_MSVC2019_64bit-Debug/ │ └── tom-builds-qmake/ @@ -157,8 +159,10 @@ All `tinyorm.org` examples are based on the following folders structure. The `to │ └── tom/ │ ├── tom/ │ │ └── database/ -│ │ └── migrations/ -│ │ └── migrations.pri +│ │ ├── migrations/ +│ │ ├── seeders/ +│ │ ├── migrations.pri +│ │ └── seeders.pri │ ├── tom-builds-cmake/ │ │ └── build-TinyOrm-Desktop_Qt_6_2_4_clang14_64bit_ccache-Debug/ │ └── tom-builds-qmake/