Skip to content
Permalink
Browse files

remove entry-to-snapshot migration errors

  • Loading branch information
umeshksingla authored and neverpanic committed Aug 18, 2017
1 parent ddcdadc commit 34a2162dfb43d1bb2757b72d2edfd699327980d8
@@ -33,6 +33,7 @@
#endif

#include "registry.h"
#include "portgroup.h"

#include <sqlite3.h>

@@ -30,6 +30,7 @@
#include <config.h>
#endif

#include "entry.h"
#include "snapshot.h"
#include "registry.h"
#include "sql.h"
@@ -67,12 +68,12 @@ int get_parsed_variants(char* variants_str, variant* all_variants, char* delim,
return 0;
}

reg_entry* reg_snapshot_create(reg_registry* reg, char* note, reg_error* errPtr) {
reg_snapshot* reg_snapshot_create(reg_registry* reg, char* note, reg_error* errPtr) {

printf("inside cregsitry sn cr\n");

sqlite3_stmt* stmt = NULL;
reg_entry* entry = NULL;
reg_snapshot* snapshot = NULL;
char* query = "INSERT INTO registry.snapshots (note) VALUES (?)";

if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
@@ -83,16 +84,16 @@ reg_entry* reg_snapshot_create(reg_registry* reg, char* note, reg_error* errPtr)
r = sqlite3_step(stmt);
switch (r) {
case SQLITE_DONE:
entry = malloc(sizeof(reg_entry));
if (entry) {
entry->id = sqlite3_last_insert_rowid(reg->db);
entry->reg = reg;
entry->proc = NULL;
snapshot = malloc(sizeof(reg_snapshot));
if (snapshot) {
snapshot->id = sqlite3_last_insert_rowid(reg->db);
snapshot->reg = reg;
snapshot->proc = NULL;

printf("snapshot id: %lld\n", entry->id);
printf("snapshot id: %lld\n", snapshot->id);

// TODO: move this functions to a different file
int ports_saved = snapshot_store_ports(reg, entry, errPtr);
int ports_saved = snapshot_store_ports(reg, snapshot, errPtr);

switch (ports_saved) {
case 1:
@@ -119,10 +120,10 @@ reg_entry* reg_snapshot_create(reg_registry* reg, char* note, reg_error* errPtr)
sqlite3_finalize(stmt);
}
printf("done with the snapshot\n");
return entry;
return snapshot;
}

int snapshot_store_ports(reg_registry* reg, reg_entry* snap_entry, reg_error* errPtr){
int snapshot_store_ports(reg_registry* reg, reg_snapshot* snapshot, reg_error* errPtr){
reg_entry** entries;
reg_error error;
int i, entry_count;
@@ -151,7 +152,7 @@ int snapshot_store_ports(reg_registry* reg, reg_entry* snap_entry, reg_error* er
"VALUES (?, ?, ?, ?)";

if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
&& (sqlitse3_bind_int64(stmt, 1, snap_entry->id) == SQLITE_OK)
&& (sqlite3_bind_int64(stmt, 1, snapshot->id) == SQLITE_OK)
&& (sqlite3_bind_text(stmt, 2, port_name, -1, SQLITE_STATIC) == SQLITE_OK)
&& (sqlite3_bind_int64(stmt, 3, atoi(requested)) == SQLITE_OK)
&& (sqlite3_bind_text(stmt, 4, state, -1, SQLITE_STATIC) == SQLITE_OK)) {
@@ -286,13 +287,14 @@ int snapshot_store_port_variants(reg_registry* reg, reg_entry* port_entry,

char* reg_snapshot_get_id(reg_registry* reg, reg_error* errPtr) {

printf("inside cregistry get snapshot..\n");
sqlite3_stmt* stmt = NULL;
reg_entry* entry = NULL;
//printf("inside cregistry get snapshot..\n");
//sqlite3_stmt* stmt = NULL;
//reg_entry* entry = NULL;
char* query = "SELECT id FROM registry.snapshots ORDER BY id DESC LIMIT 1";
return query;
}

int reg_snapshot_get(reg_registry* reg, char* id, reg_snapshot* snapshot, reg_error* errPtr) {
int reg_snapshot_get(reg_registry* reg, sqlite_int64 id, reg_snapshot* snapshot, reg_error* errPtr) {

printf("inside cregistry get snapshot..\n");

@@ -304,7 +306,7 @@ int reg_snapshot_get(reg_registry* reg, char* id, reg_snapshot* snapshot, reg_er
const char* state;

if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
&& (sqlite3_bind_int64(stmt, 1, (sqlite_int64)id) == SQLITE_OK )) {
&& (sqlite3_bind_int64(stmt, 1, id) == SQLITE_OK )) {

port** result = malloc(10 * sizeof(port*));

@@ -377,7 +379,7 @@ int reg_snapshot_get(reg_registry* reg, char* id, reg_snapshot* snapshot, reg_er
s->id = NULL;
s->note = NULL;
s->proc = NULL;
s->ports = result;
s->ports = *result;
*snapshot = *s;

return result_count;
@@ -33,6 +33,7 @@
#endif

#include "registry.h"
#include "entry.h"

#include <sqlite3.h>

@@ -49,7 +50,7 @@ typedef struct {
} port;

typedef struct {
char* id;
sqlite_int64 id; /* rowid in database */
char* note;
port* ports;
reg_registry* reg; /* associated registry */
@@ -59,15 +60,15 @@ typedef struct {
int get_parsed_variants(char* variants_str, variant* all_variants,
char* delim, int* variant_count);

reg_entry* reg_snapshot_create(reg_registry* reg, char* note,
reg_snapshot* reg_snapshot_create(reg_registry* reg, char* note,
reg_error* errPtr);
char* reg_snapshot_get_id(reg_registry* reg, reg_error* errPtr);
int reg_snapshot_get(reg_registry* reg, char* id,
int reg_snapshot_get(reg_registry* reg, sqlite_int64 id,
reg_snapshot* snapshot, reg_error* errPtr);
int reg_snapshot_port_variants_get(reg_registry* reg,
sqlite_int64 snapshot_port_id, variant** variants, reg_error* errPtr);

int snapshot_store_ports(reg_registry* reg, reg_entry* entry,
int snapshot_store_ports(reg_registry* reg, reg_snapshot* snapshot,
reg_error* errPtr);
int snapshot_store_port_variants(reg_registry* reg, reg_entry* port_entry,
int snapshot_ports_id, reg_error* errPtr);
@@ -40,7 +40,8 @@ namespace eval migrate {


# create a snapshot
# set snapshot snapshot::main
set snapshot snapshot::main
return 0

# fetch ports and variants for this snapshot

@@ -42,13 +42,9 @@ namespace eval snapshot {
puts $port
}
puts
set snapshot [registry::entry create_snapshot "test snapshot"]
set snapshot [registry::snapshot create "test snapshot"]

puts $snapshot
puts [$snapshot name]
puts [$snapshot created_at]

return 0
return $snapshot
}

proc all_snapshots {opts} {
@@ -81,10 +81,10 @@ static int create_snapshot(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[])
char* note = Tcl_GetString(objv[2]);
reg_error error;
/* may be a new datatype for snapshot */
reg_entry* new_snaphot = reg_snapshot_create(reg, note, &error);
reg_snapshot* new_snaphot = reg_snapshot_create(reg, note, &error);
if (new_snaphot != NULL) {
Tcl_Obj* result;
if (entry_to_obj(interp, &result, new_snaphot, NULL, &error)) {
if (snapshot_to_obj(interp, &result, new_snaphot, NULL, &error)) {
Tcl_SetObjResult(interp, result);
return TCL_OK;
}
@@ -93,24 +93,24 @@ static int create_snapshot(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[])
}
}

static int get_snapshot(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[]) {
static int get_snapshot_by_id(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[]) {

printf("getting snapshot\n");

reg_registry* reg = registry_for(interp, reg_attached);
if (objc > 3) {
Tcl_WrongNumArgs(interp, 2, objv, "get_snapshot ?snapshot_id?");
Tcl_WrongNumArgs(interp, 2, objv, "get_by_id ?snapshot_id?");
return TCL_ERROR;
} else if (reg == NULL) {
return TCL_ERROR;
} else {
char* id = Tcl_GetString(objv[2]);
sqlite_int64 id = atoll(Tcl_GetString(objv[2]));
reg_error error;
reg_snapshot* snapshot;
int port_count = reg_snapshot_get(reg, id, &snapshot, &error);
reg_snapshot* snapshot = NULL;
int port_count = reg_snapshot_get(reg, id, snapshot, &error);
if (snapshot != NULL && port_count >= 0) {
Tcl_Obj* resultObj;
if (entry_to_obj(interp, &resultObj, snapshot, NULL, &error)) {
if (snapshot_to_obj(interp, &resultObj, snapshot, NULL, &error)) {
Tcl_SetObjResult(interp, resultObj);
return TCL_OK;
}
@@ -127,7 +127,7 @@ typedef struct {
static snapshot_cmd_type snapshot_cmds[] = {
/* Global commands */
{ "create", create_snapshot},
{ "get", get_snapshot},
{ "get_by_id", get_snapshot_by_id},
{ NULL, NULL }
};

@@ -151,4 +151,4 @@ int snapshot_cmd(ClientData clientData UNUSED, Tcl_Interp* interp, int objc,
return cmd->function(interp, objc, objv);
}
return TCL_ERROR;
}
}

0 comments on commit 34a2162

Please sign in to comment.
You can’t perform that action at this time.