cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
migrate.c File Reference

Schema migration helpers for applying and rolling back ordered SQL revisions. More...

#include <cwist/core/db/migrate.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for migrate.c:

Go to the source code of this file.

Functions

int cwist_migrate_version (sqlite3 *db)
 Query the highest applied migration version recorded in the database.
 
int cwist_migrate_up (sqlite3 *db, const cwist_migration_t *migrations, int count)
 Apply all pending up migrations after sorting them by version.
 
int cwist_migrate_down (sqlite3 *db, const cwist_migration_t *migrations, int count, int steps)
 Roll back applied migrations in reverse version order.
 

Detailed Description

Schema migration helpers for applying and rolling back ordered SQL revisions.

Definition in file migrate.c.

Function Documentation

◆ cwist_migrate_down()

int cwist_migrate_down ( sqlite3 *  db,
const cwist_migration_t *  migrations,
int  count,
int  steps 
)

Roll back applied migrations in reverse version order.

Roll back the most recent steps migrations.

Parameters
dbSQLite connection to migrate.
migrationsCaller-supplied migration array.
countNumber of entries in migrations.
stepsMaximum number of applied versions to roll back, or all when <= 0.
Returns
CWIST_MIGRATE_OK on success, or a specific migration error code.

Definition at line 174 of file migrate.c.

References count, CWIST_MIGRATE_ERR_ARGS, CWIST_MIGRATE_ERR_GENERIC, CWIST_MIGRATE_ERR_SQL, CWIST_MIGRATE_OK, and cwist_migrate_version().

◆ cwist_migrate_up()

int cwist_migrate_up ( sqlite3 *  db,
const cwist_migration_t *  migrations,
int  count 
)

Apply all pending up migrations after sorting them by version.

Apply all migrations whose version > current schema version.

Parameters
dbSQLite connection to migrate.
migrationsCaller-supplied migration array.
countNumber of entries in migrations.
Returns
CWIST_MIGRATE_OK on success, or a specific migration error code.

Definition at line 123 of file migrate.c.

References count, CWIST_MIGRATE_ERR_ARGS, CWIST_MIGRATE_ERR_GENERIC, CWIST_MIGRATE_ERR_SQL, CWIST_MIGRATE_OK, and cwist_migrate_version().

◆ cwist_migrate_version()

int cwist_migrate_version ( sqlite3 *  db)

Query the highest applied migration version recorded in the database.

Return the highest version number that has been applied.

Parameters
dbSQLite connection to inspect.
Returns
Highest applied version, or -1 when the query fails.

Definition at line 99 of file migrate.c.

References CWIST_MIGRATE_OK.

Referenced by cwist_migrate_down(), and cwist_migrate_up().