Hello all!

For those who want to test new Midgard 1.2.6 beta but failed to find a
way to transfer old sites to it, I'll suggest simple-and-dirty utility
for this work. 

All described below requires that Midgard 1.2.6-beta2 is installed
except database installation (it means that you compiled and installed
Midgard-lib, Mod_Midgard, and Midgard-PHP, because dumper utility uses
new Midgard-Lib, and Admin Site uses new Midgard-PHP, and Mod_Midgard).

Admin and Examples sites in Midgard 1.2.6beta2 use the same IDs as their
previous versions but content of some records was changed and will be
changed before final release definitely too. In order to update your
database, you'll need to walk several steps:

1. Update database structure in order to include three new tables -
'repligard', 'snippet', and 'snippetdir' - using update.sql file
attached.

2. Create new MySQL database named 'testgard' and run mysql command line
utility:
        mysqladmin create testgard
        mysql testgard <midgard.sql
   where midgard.sql is from midgard-data-1.2.6-beta2.tar.gz

3. Get attached dumper.c and change parameters for mgd_connect()
function to fit your situation. First parameter is database name
('testgard') and others are username and password.
Compile dumper.c against Midgard-lib 1.2.6beta2 using the following
command line:
gcc -o dumper dumper.c -I/usr/include/mysql -L/usr/lib/mysql
-I/usr/local/include -I/usr/local/lib -lmidgard -lmysqlclient 
Note that actual paths to MySQL and Midgard includes and libraries
depends on your installation.

4. Run dumper, it will write database content on stdout. You might
redirect it to mysql command line client:
./dumper | mysql midgard
where midgard is a name of your actual database but I suggest to
redirect its output to file and do some modifications:
./dumper >midgard.update

5. Open midgard.update in an editor and find first occurence of
"name='localhost'" (without double quotes). It will be definition of
Admin Site host info. Change localhost to your actual host and correct
'prefix' field if it differs from '/admin'. Do the same with Example
Site definition below.

6. Now you're ready to place update into your database:
mysql midgard <midgard.update

-- 
Sincerely yours, 
Alexander Bokovoy 
<!-- 2:450/144.58 --- bokovoyATminsk.lug.net --- FractalsAtTheEdge -->
/* $Id: dumper.c,v 1.0 2000/02/08 12:43:05 ab Exp $
 *
 * Dumper: Creates updates for Midgard Admin and Example sites
 *
 * Copyright (C) 2000 Alexander Bokovoy <[EMAIL PROTECTED]>
 *
 */
#include "midgard.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void mgd_dump_insert_object(midgard *mgd, midgard_res *object, const char *table) {
    midgard_pool *pool;
    char *fields, *values, *stmp;
    int i;
    
    if(!object) return;
    pool = mgd_alloc_pool();
    if(!pool) return;
    /* 1. Create fields for query */
    fields = NULL; values = NULL;
    for(i = 0; i<mgd_cols(object); i++) {
            stmp = fields;
            fields = fields ? 
                mgd_format(mgd, pool, "$s,$s", fields, mgd_colname(object, i)) :
                mgd_strdup(pool, mgd_colname(object, i));
            if(stmp) mgd_free_from_pool(pool, stmp);
            stmp = values;
            values = values ? 
             mgd_format(mgd, pool, "$s,$q", values, mgd_colvalue(object, i)) :
             mgd_strdup(pool, mgd_colvalue(object, i));
            if(stmp) mgd_free_from_pool(pool, stmp);
    }
    /* 2. Create new object */
    stmp = mgd_format(mgd, pool, "INSERT INTO $s ($s) VALUES ($s);", table, fields, 
values);
    if(stmp) fprintf(stdout, "%s\n", stmp);
    mgd_free_pool(pool);
    return;
}

void mgd_dump_insert_table(midgard *mgd, const char *table) {
    midgard_res *res;
    assert(mgd || table);
    res = mgd_select(mgd, "*", table, NULL, NULL);    
    if(!res) return;
    fprintf(stdout, "# Dumping data for table '%s'\nLOCK TABLES %s WRITE;\n", table, 
table);
    while(mgd_fetch(res)) {
        mgd_dump_insert_object(mgd, res, table);
    }
    fprintf(stdout, "UNLOCK TABLES;\n\n");
    mgd_release(res);
}

void mgd_dump_object(midgard *mgd, midgard_res *object, const char *table) {
    midgard_pool *pool;
    char *query, *stmp;
    int i, id;
    
    if(!object) return;
    pool = mgd_alloc_pool();
    if(!pool) return;
    /* 1. Create query */
    query = NULL; 
    for(i = 0; i<mgd_cols(object); i++) {
        if(strcmp("id", mgd_colname(object, i)) != 0) {
            stmp = query;
            query = query ? 
                mgd_format(mgd, pool, "$s,$s=$q", query, mgd_colname(object, i), 
mgd_colvalue(object,i)) :
                mgd_format(mgd, pool, "$s=$q", mgd_colname(object, i), 
mgd_colvalue(object, i));
            if(stmp) mgd_free_from_pool(pool, stmp);
        } else {
            id = mgd_sql2id(object, i);
        }
    }
    stmp = mgd_format(mgd, pool, "UPDATE $s SET $s WHERE id=$i;", table, query, id);
    if(stmp) fprintf(stdout, "%s\n", stmp);
    mgd_free_pool(pool);
}

void mgd_dump_table(midgard *mgd, const char *table) {
    midgard_res *res;
    assert(mgd || table);
    res = mgd_select(mgd, "*", table, NULL, NULL);    
    if(!res) return;
    fprintf(stdout, "# Dumping data for table '%s'\nLOCK TABLES %s WRITE;\n", table, 
table);
    while(mgd_fetch(res)) {
        mgd_dump_object(mgd, res, table);
    }
    fprintf(stdout, "UNLOCK TABLES;\n\n");
    mgd_release(res);
}

int main() {
    midgard *mgd;
    int *ids;
    int i;
    mgd_init();
    /* Change database name, username, and password to fit your situation */
    /* I suggest to create temporary database 'testgard' for update purposes */
    mgd = mgd_connect("testgard","midgard","midgard");
    if(!mgd) fprintf(stderr, "Error in DB connection\n");
    
    /* We select russian parser in order to dump content unchanged */
    mgd_select_parser(mgd, "russian");
    
    mgd_dump_table(mgd, "topic");
    mgd_dump_table(mgd, "article");
    mgd_dump_table(mgd, "person");
    mgd_dump_table(mgd, "grp");
    mgd_dump_table(mgd, "style");
    mgd_dump_table(mgd, "element");
    mgd_dump_table(mgd, "file");
    mgd_dump_table(mgd, "page");
    mgd_dump_table(mgd, "pageelement");
    mgd_dump_table(mgd, "host");
    mgd_dump_table(mgd, "image");
    mgd_dump_table(mgd, "member");
    mgd_dump_table(mgd, "preference");
    mgd_dump_table(mgd, "snippetdir");
    mgd_dump_table(mgd, "snippet");
    mgd_dump_insert_table(mgd, "repligard");

    mgd_close(mgd);
    mgd_done();
}
# MySQL dump 6.4
#
# Host: localhost    Database: midgard
#--------------------------------------------------------
# Server version        3.22.27-log

#
# Table structure for table 'repligard'
#
DROP TABLE IF EXISTS repligard;
CREATE TABLE repligard (
  autoid smallint(5) unsigned DEFAULT '0' NOT NULL auto_increment,
  id smallint(5) unsigned DEFAULT '0' NOT NULL,
  timestamp datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  realm varchar(32) DEFAULT '' NOT NULL,
  tag varchar(32) DEFAULT '' NOT NULL,
  PRIMARY KEY (autoid),
  KEY id (tag(16),id)
);

#
# Dumping data for table 'repligard'
#

LOCK TABLES repligard WRITE;
UNLOCK TABLES;

#
# Table structure for table 'snippet'
#
DROP TABLE IF EXISTS snippet;
CREATE TABLE snippet (
  id smallint(5) unsigned DEFAULT '0' NOT NULL auto_increment,
  up smallint(5) unsigned DEFAULT '0' NOT NULL,
  name varchar(255) DEFAULT '' NOT NULL,
  code text NOT NULL,
  PRIMARY KEY (id),
  KEY up (up,name(14))
);

#
# Dumping data for table 'snippet'
#

LOCK TABLES snippet WRITE;
UNLOCK TABLES;

#
# Table structure for table 'snippetdir'
#
DROP TABLE IF EXISTS snippetdir;
CREATE TABLE snippetdir (
  id smallint(5) unsigned DEFAULT '0' NOT NULL auto_increment,
  up smallint(5) unsigned DEFAULT '0' NOT NULL,
  name varchar(255) DEFAULT '' NOT NULL,
  description text NOT NULL,
  PRIMARY KEY (id),
  KEY up (up,name(14))
);

#
# Dumping data for table 'snippetdir'
#

LOCK TABLES snippetdir WRITE;
UNLOCK TABLES;



--
This is The Midgard Project's mailing list. For more information,
please visit the project's web site at http://www.midgard-project.org

To unsubscribe the list, send an empty email message to address
[EMAIL PROTECTED]

Reply via email to