# Copyright (C) 2004  Joshua Hoblitt
#
# $Id: Nebulous.pm,v 1.1.1.1 2004-12-22 02:16:23 jhoblitt Exp $

package Test::IData;

use strict;

use DBI;
use File::Path qw( mkpath rmtree );
use File::Temp qw( tempdir );

my $dsn     = "DBI:mysql:database=test:host=localhost";
my $user    = "test";
my $passwd  = "";

my $dbh = DBI->connect( $dsn, $user, $passwd );

#my $dir1 = tempdir( CLEANUP => 0 );
#my $dir2 = tempdir( CLEANUP => 0 );

my $dir1 = "/tmp/foo1";
my $dir2 = "/tmp/foo1";


sub setup {
    my $self = shift;

    $self->cleanup;

    mkpath([$dir1, $dir2]);

    local $/ = ';';

    my $pos = tell DATA;

    foreach my $statement (<DATA>) {
        last unless ( $statement =~ /\S+/ );
        $dbh->do( $statement );
    }

    seek DATA, $pos, 0;

    # FIXME
    $dbh->do( qq{ INSERT INTO volume VALUES (1,'node01',?) }, undef, 'file:' . $dir1 );
    $dbh->do( qq{ INSERT INTO volume VALUES (2,'node02',?) }, undef, 'file:' . $dir2 );
}

sub cleanup {
    $dbh->do( "DROP TABLE IF EXISTS storage_object" );
    $dbh->do( "DROP TABLE IF EXISTS instance" );
    $dbh->do( "DROP TABLE IF EXISTS lock_record" );
    $dbh->do( "DROP TABLE IF EXISTS volume" );
    $dbh->do( "DROP TABLE IF EXISTS class" );

    rmtree([$dir1, $dir2], 0, 1);
}

1;

__DATA__
DROP TABLE IF EXISTS storage_object;
CREATE TABLE storage_object (
    so_id BIGINT NOT NULL AUTO_INCREMENT,
    ext_id VARCHAR(255) BINARY NOT NULL UNIQUE,
    class_id TINYINT NOT NULL,
    comment VARCHAR(255),
    read_lock TINYINT DEFAULT 0 NOT NULL,
    write_lock ENUM( 'write' ),
    epoch TIMESTAMP NOT NULL,
    PRIMARY KEY(so_id),
    KEY(ext_id(64)),
    KEY(class_id)
) ENGINE=innodb;

DROP TABLE IF EXISTS instance;
CREATE TABLE instance (
    ins_id BIGINT NOT NULL AUTO_INCREMENT,
    so_id BIGINT NOT NULL,
    uri VARCHAR(255) BINARY NOT NULL UNIQUE,
    sha1sum CHAR(40),
    assigned_location BOOL,
    epoch TIMESTAMP NOT NULL,
    atime TIMESTAMP NOT NULL,
    PRIMARY KEY(ins_id),
    KEY(so_id),
    KEY(uri(64))
) ENGINE=innodb;

DROP TABLE IF EXISTS lock_record;
CREATE TABLE lock_record (
        so_id BIGINT NOT NULL,
        type ENUM( 'read', 'write' ) NOT NULL,
        epoch TIMESTAMP,
        KEY(so_ID)
    ) ENGINE=innodb;

DROP TABLE IF EXISTS volume;
CREATE TABLE volume (
        vol_id INT NOT NULL AUTO_INCREMENT,
        name VARCHAR(255) UNIQUE BINARY NOT NULL,
        uri VARCHAR(255) BINARY NOT NULL,
        key(vol_id)
    ) ENGINE=innodb;

DROP TABLE IF EXISTS class;
CREATE TABLE class (
        class_id TINYINT NOT NULL AUTO_INCREMENT,
        instance TINYINT NOT NULL,
        priority TINYINT,
        comment VARCHAR(255),
        key(class_id)
    ) ENGINE=innodb;
