drop.sh 906 Bytes
Newer Older
1 2 3 4
#! /bin/bash

# Drop a database. Removes all tables and data
dropDB() {
5
    tables=$(getTables)
6
    # Create temporary workspace (utility function)
7
    tmpWS=$(mktemp -d)
8 9

    # Create transactions files
10
    beginTrans > "$tmpWS/trans.sql"
11 12 13
    for t in $tables; do
        dropComand $t >> "$tmpWS/trans.sql"
    done
14 15
    commitTrans >> "$tmpWS/trans.sql"

16 17 18

    # Performs schema creation. As is in a transaction if fails it will
    # NOT leave the database inconsistent.
19
    execFile $tmpWS/trans.sql $tmpWS/error.out
20 21 22

    # If a error occurs will be sent to error.out
    # If error.out is not empty, should return a error
23 24
    cat $tmpWS/error.out | grep -v "drop cascades to" > $tmpWS/error2.out
    egrep . $tmpWS/error2.out
25 26 27 28 29 30 31
    if [[ $? -eq 0 ]]; then
        error=1
    else
        error=0
    fi

    # Utility function that removes the temporary workspace
32
    rm -fr $tmpWS
33 34 35
    return $error
}