Content


NTL
Core
CDbMigrationGenerator

CDbMigrationGenerator


This class provides static method for generating migration header and source files.

You can find more information in comments below.

class CDbMigrationGenerator
{
public:
	// Generate migration header and source files 
	static bool Generate(const String &name, const String &path, const StringArray &namespaces, const bool revert, const bool force_empty); // path should have directory separator at the end 
};

Usage example:

#include "stdafx.h"

#pragma comment(lib, "Nitisa.lib")
#pragma comment(lib, "Platform.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "cryptlib.lib")

using nitisa::CDbMigrationGenerator;
using nitisa::AnsiToString;
using nitisa::AnsiString;
using nitisa::String;
using nitisa::StringArray;

// To generate migration files call the program with arguments "create migration migration_name --revert --empty" where "--revert" and "--empty" are optional and "migration_name" is the name for your migration (for example "create_table_users")
int main(int argc, char *argv[])
{
    // Check if needed command line arguments are present
	if (argc >= 4 && AnsiString{ argv[1] } == "create" && AnsiString{ argv[2] } == "migration")
	{
		const String name{ AnsiToString(argv[3]) }; // Name of the migration
		const String path{ L"C:/Projects/MyApp/Migrations/" }; // Path where to store generated files
		const StringArray namespaces{ L"nitisa", L"app", L"migrations" }; // Namespace hierarchy
		const bool revert{ (argc > 4 && AnsiString{ argv[4] } == "--revert") || (argc > 5 && AnsiString{ argv[5] } == "--revert") }; // Whether the Revert() method should be generated in migration files
		const bool force_empty{ (argc > 4 && AnsiString{ argv[4] } == "--empty") || (argc > 5 && AnsiString{ argv[5] } == "--empty") }; // Whether migration Run() and Revert() methods should be empty (with no default staff which may be generated depending on migration name)
		
		std::wcout << L"Generating " << name << L"...";
		if (CDbMigrationGenerator::Generate(name, path, namespaces, revert, force_empty)) // Generate migration files
			std::wcout << L"OK" << std::endl;
		else
			std::wcout << L"FAILED" << std::endl;
		std::system("pause");
		return 0;
	}
        
    // ...
	return 0;
}
Namespace: nitisa
Include: Nitisa/Db/DbMigrationGenerator.h