diff --git a/package/postgres/pg-neo-migration/hectic-chan b/package/postgres/pg-neo-migration/hectic-chan index deef93d..bfa0aaf 100755 Binary files a/package/postgres/pg-neo-migration/hectic-chan and b/package/postgres/pg-neo-migration/hectic-chan differ diff --git a/package/postgres/pg-neo-migration/pup b/package/postgres/pg-neo-migration/pup new file mode 100644 index 0000000..c9939fc --- /dev/null +++ b/package/postgres/pg-neo-migration/pup @@ -0,0 +1 @@ +Error: pup diff --git a/package/postgres/pg-neo-migration/src/logger.c b/package/postgres/pg-neo-migration/src/logger.c new file mode 100644 index 0000000..2cb5908 --- /dev/null +++ b/package/postgres/pg-neo-migration/src/logger.c @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include "macros.h" + +typedef enum { + LOG_LEVEL_DEBUG, + LOG_LEVEL_LOG, + LOG_LEVEL_INFO, + LOG_LEVEL_NOTICE, + LOG_LEVEL_WARN, + LOG_LEVEL_EXCEPTION +} LogLevel; + +const char* log_level_to_string(LogLevel level) { + switch (level) { + case LOG_LEVEL_DEBUG: return "DEBUG"; + case LOG_LEVEL_LOG: return "LOG"; + case LOG_LEVEL_INFO: return "INFO"; + case LOG_LEVEL_NOTICE: return "NOTICE"; + case LOG_LEVEL_WARN: return "WARN"; + case LOG_LEVEL_EXCEPTION: return "EXCEPTION"; + default: return "UNKNOWN"; + } +} + +LogLevel log_level_from_string(const char *level_str) { + if (!level_str) return LOG_LEVEL_INFO; + if (strcmp(level_str, "DEBUG") == 0) + return LOG_LEVEL_DEBUG; + else if (strcmp(level_str, "LOG") == 0) + return LOG_LEVEL_LOG; + else if (strcmp(level_str, "INFO") == 0) + return LOG_LEVEL_INFO; + else if (strcmp(level_str, "NOTICE") == 0) + return LOG_LEVEL_NOTICE; + else if (strcmp(level_str, "WARN") == 0) + return LOG_LEVEL_WARN; + else if (strcmp(level_str, "EXCEPTION") == 0) + return LOG_LEVEL_EXCEPTION; + else + return LOG_LEVEL_INFO; +} + +LogLevel current_log_level = LOG_LEVEL_INFO; + +void init_logger(void) { + current_log_level = log_level_from_string(getenv("LOG_LEVEL")); +} + +void log_message(LogLevel level, int line, const char *format, ...) { + if (level < current_log_level) { + return; + } + + time_t now = time(NULL); + struct tm tm_info; + localtime_r(&now, &tm_info); + char timeStr[20]; + strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &tm_info); + + fprintf(stderr, "%s %d %s: ", timeStr, line, log_level_to_string(level)); + + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + + fprintf(stderr, "\n"); +} + +// DEBUG level +#define raise_debug_1(fmt) \ + log_message(LOG_LEVEL_DEBUG, __LINE__, fmt) +#define raise_debug_2(fmt, ...) \ + log_message(LOG_LEVEL_DEBUG, __LINE__, fmt, __VA_ARGS__) +#define raise_debug(...) \ + PP_CAT(raise_debug_, PP_NARG(__VA_ARGS__))(__VA_ARGS__) + +// LOG level +#define raise_log_1(fmt) \ + log_message(LOG_LEVEL_LOG, __LINE__, fmt) +#define raise_log_2(fmt, ...) \ + log_message(LOG_LEVEL_LOG, __LINE__, fmt, __VA_ARGS__) +#define raise_log(...) \ + PP_CAT(raise_log_, PP_NARG(__VA_ARGS__))(__VA_ARGS__) + +// INFO level +#define raise_info_1(fmt) \ + log_message(LOG_LEVEL_INFO, __LINE__, fmt) +#define raise_info_2(fmt, ...) \ + log_message(LOG_LEVEL_INFO, __LINE__, fmt, __VA_ARGS__) +#define raise_info(...) \ + PP_CAT(raise_info_, PP_NARG(__VA_ARGS__))(__VA_ARGS__) + +// NOTICE level +#define raise_notice_1(fmt) \ + log_message(LOG_LEVEL_NOTICE, __LINE__, fmt) +#define raise_notice_2(fmt, ...) \ + log_message(LOG_LEVEL_NOTICE, __LINE__, fmt, __VA_ARGS__) +#define raise_notice(...) \ + PP_CAT(raise_notice_, PP_NARG(__VA_ARGS__))(__VA_ARGS__) + +// WARN level +#define raise_warn_1(fmt) \ + log_message(LOG_LEVEL_WARN, __LINE__, fmt) +#define raise_warn_2(fmt, ...) \ + log_message(LOG_LEVEL_WARN, __LINE__, fmt, __VA_ARGS__) +#define raise_warn(...) \ + PP_CAT(raise_warn_, PP_NARG(__VA_ARGS__))(__VA_ARGS__) + +// EXCEPTION level +#define raise_exception_1(fmt) \ + log_message(LOG_LEVEL_EXCEPTION, __LINE__, fmt) +#define raise_exception_2(fmt, ...) \ + log_message(LOG_LEVEL_EXCEPTION, __LINE__, fmt, __VA_ARGS__) +#define raise_exception(...) \ + PP_CAT(raise_exception_, PP_NARG(__VA_ARGS__))(__VA_ARGS__) diff --git a/package/postgres/pg-neo-migration/src/macros.c b/package/postgres/pg-neo-migration/src/macros.c new file mode 100644 index 0000000..1b61d85 --- /dev/null +++ b/package/postgres/pg-neo-migration/src/macros.c @@ -0,0 +1,9 @@ +#include "macros.h" + +// Static color mode variable +static ColorMode color_mode = COLOR_MODE_AUTO; + +// Function to set color mode +void set_output_color_mode(ColorMode mode) { + color_mode = mode; +} diff --git a/package/postgres/pg-neo-migration/src/macros.h b/package/postgres/pg-neo-migration/src/macros.h new file mode 100644 index 0000000..b1649be --- /dev/null +++ b/package/postgres/pg-neo-migration/src/macros.h @@ -0,0 +1,45 @@ +#ifndef EPRINTF_H +#define EPRINTF_H + +#include +#include + +// Color mode enumeration +typedef enum { + COLOR_MODE_AUTO, + COLOR_MODE_FORCE, + COLOR_MODE_DISABLE +} ColorMode; + +// Function to set the color mode +void set_output_color_mode(ColorMode mode); + +// Macros for detecting terminal and color usage +#define IS_TERMINAL() (isatty(fileno(stderr))) +#define USE_COLOR() ((color_mode == COLOR_MODE_FORCE) || (color_mode == COLOR_MODE_AUTO && IS_TERMINAL())) + +// Define color macros based on output type +#define ERROR_PREFIX (IS_TERMINAL() ? "\033[1;31mError: " : "Error: ") +#define ERROR_SUFFIX (IS_TERMINAL() ? "\033[0m\n" : "\n") + +// Helper macros for argument counting +// NOTE(yukkop): this ugly macroses for avoid all posible warnings +#define PP_CAT(a, b) PP_CAT_I(a, b) +#define PP_CAT_I(a, b) a##b + +#define PP_NARG(...) PP_NARG_(__VA_ARGS__, PP_RSEQ_N()) +#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__) +#define PP_ARG_N(_1,_2,_3,_4,_5,_6,_7,_8,_9,N,...) N +#define PP_RSEQ_N() 9,8,7,6,5,4,3,2,1,0 + +// eprintf handling 1 or more arguments +#define eprintf_1(fmt) \ + fprintf(stderr, "%s" fmt "%s", ERROR_PREFIX, ERROR_SUFFIX) + +#define eprintf_2(fmt, ...) \ + fprintf(stderr, "%s" fmt "%s", ERROR_PREFIX, __VA_ARGS__, ERROR_SUFFIX) + +#define eprintf(...) \ + PP_CAT(eprintf_, PP_NARG(__VA_ARGS__))(__VA_ARGS__) + +#endif // EPRINTF_H diff --git a/package/postgres/pg-neo-migration/src/main.c b/package/postgres/pg-neo-migration/src/main.c index 6883f23..ebc96ce 100644 --- a/package/postgres/pg-neo-migration/src/main.c +++ b/package/postgres/pg-neo-migration/src/main.c @@ -1,27 +1,168 @@ #include #include -#include "type.c" - -LogLevel currentLogLevel = LOG_LEVEL_DEBUG; +#include "logger.c" +#include "macros.h" +#include +#include +#include // TODO: check on the specific psql version int check_psql_installed(void) { + raise_log("checking psql installed..."); int returned = system("psql --version > /dev/null 2>&1"); if (returned != 0) { - fprintf(stderr, "Error: psql is not installed or not in PATH.\n"); + raise_log("psql is not installed..."); + eprintf("psql is not installed or not in PATH."); return 1; } + raise_log("psql is installed..."); return 0; } -void help_message() { - fprintf(stdout, "Usage: TODO"); +/* Generate a migration name by choosing a random adjective and noun */ +void generate_migration_name(char *buffer, size_t size) { + const char *adjectives[] = {"quick", "lazy", "sleepy", "noisy", "hungry"}; + const char *nouns[] = {"fox", "dog", "cat", "mouse", "bear"}; + int num_adjectives = sizeof(adjectives) / sizeof(adjectives[0]); + int num_nouns = sizeof(nouns) / sizeof(nouns[0]); + int adj_index = rand() % num_adjectives; + int noun_index = rand() % num_nouns; + snprintf(buffer, size, "%s_%s", adjectives[adj_index], nouns[noun_index]); +} + +/* Record that a migration has been applied + * by inserting its filename into the database */ +int record_migration(const char* db_url, const char* file_name) { + char command[2048]; + snprintf(command, sizeof(command), + "psql '%s' -c \"INSERT INTO hectic.migration (name) VALUES ('%s');\"", + db_url, file_name); + int returned = system(command); + return returned; +} + +void create_migration_inner(const char* migration_path, const char* type) { + if (strcmp(type, "up") || strcmp(type, "down")) { + raise_exception("migration type can only be up or down"); + exit(1); + } + + char path[1024]; + snprintf(path, sizeof(path), "%s/%s", migration_path, type); + + /* create directory for current migration */ + if (mkdir(path, 0755) != 0 && errno != EEXIST) { + eprintf("Error creating %s", path); + exit(1); + } + + char filename[1024]; + snprintf(filename, sizeof(filename), "%s/00-entry-point.sql", path); + + FILE *fp = fopen(filename, "w"); + if (!fp) { + eprintf("Error creating %s", filename); + exit(1); + } + fprintf(fp, "-- Write your migration SQL here\n"); + fclose(fp); + printf("Created migration: %s\n", filename); + +} + +/* Create a migration in the given directory with the provided name. + * The migration name is formed as "_.sql". */ +void create_migration(const char* migration_dir, const char* name) { + /* Create the directory if it doesn't exist */ + if (mkdir(migration_dir, 0755) != 0 && errno != EEXIST) { + eprintf("Error creating migration directory"); + exit(1); + } + + /* create directory for current migration */ + time_t now = time(NULL); + char path[1024]; + snprintf(path, sizeof(path), "%s/%ld-%s", migration_dir, now, name); + + if (mkdir(path, 0755) != 0 && errno != EEXIST) { + eprintf("Error creating %s", path); + exit(1); + } + + create_migration_inner(path, "up"); + create_migration_inner(path, "down"); +} + +void help_message(char name[]) { + fprintf(stdout, "Usage %s: TODO\n", name); } int main(int argc, char *argv[]) { - if (!check_psql_installed()) { exit(1); } + srand(time(NULL)); + init_logger(); + + raise_log("init"); + + if (check_psql_installed()) { exit(1); } if (argc < 2) { - help_message(); + help_message(argv[0]); + exit(0); } + + int subcommand_index = 0; + char *migration_dir; + char *db_url; + char *migration_name; + + /* Process global options until a known subcommand is encountered */ + int i = 1; + for (; i < argc; i++) { + if (strcmp(argv[i], "create") == 0 || + strcmp(argv[i], "migrate") == 0 || + strcmp(argv[i], "fetch") == 0) { + subcommand_index = i; + break; + } + if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--migration-dir") == 0) { + if (i+1 < argc) { + migration_dir = argv[i+1]; + i++; + } + } + } + + if (subcommand_index == 0) { + eprintf("No subcommand provided.\n"); + help_message(argv[0]); + exit(1); + } + + char *subcommand = argv[subcommand_index]; + + if (strcmp(subcommand, "create") == 0) { + for (i = subcommand_index+1; i < argc; i++) { + if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--name") == 0) { + if (i+1 < argc) { + migration_name = argv[i+1]; + i++; + } + } + } + char generated_name[128]; + if (!migration_name) { + generate_migration_name(generated_name, sizeof(generated_name)); + migration_name = generated_name; + } + create_migration(migration_dir, migration_name); + return 0; + } + + if (!db_url) { + eprintf("Database URL is required for migrate subcommand.\n"); + help_message(argv[0]); + exit(1); + } + + } diff --git a/package/postgres/pg-neo-migration/src/type.c b/package/postgres/pg-neo-migration/src/type.c deleted file mode 100644 index 722deb0..0000000 --- a/package/postgres/pg-neo-migration/src/type.c +++ /dev/null @@ -1,8 +0,0 @@ -typedef enum { - LOG_LEVEL_DEBUG, - LOG_LEVEL_LOG, - LOG_LEVEL_INFO, - LOG_LEVEL_NOTICE, - LOG_LEVEL_WARN, - LOG_LEVEL_EXCEPTION -} LogLevel;