#!/usr/bin/tclsh # # # Helga sqlRake script # # # This script acts to maintain the structure of the database # by providing rolling updates to an existing database. # It does this by storing a database number in the field # site.dbVersion in the database. # # Dependent on each new sql script updating the dbVersion # field correctly. Also dependent on strict naming conventions # of files in the scripts/db folder. # # by brian 2/12/08 package require helga 1.0 set helgaRoot $::HELGAROOT set scriptPath /[join [lrange [file split [file normalize $argv0]] 1 end-1] /] set dbRoot $scriptPath/db puts "Found database name $::SQLDB." # get all sql files in the directory # files should conform to a #.sql naming convention, where # # is the db revision number of the sql script set filelist [lsort -dictionary [glob -nocomplain -directory $dbRoot \[0-9\]*.sql]] set updates {} # check for the database to already have been created if { $argc < 1 } { puts -nonewline "Enter your MySQL root password: " flush stdout exec stty -echo gets stdin rootpass exec stty echo puts "" } else { set rootpass [lindex $argv 0] } set cmd "::helga::db::initialize root $rootpass" # if the command above errors, it's because no Helga database was found if { [catch $cmd out] } { # create the database if { [catch "exec $scriptPath/createDb" out] } { puts "Error: creating db: $out" exit 1 } puts "Database created, running all SQL scripts..." set updates $filelist } else { # find the version of the database that the site has if { [catch "hget attr dbRevision -name /" dbRev] } { puts "Could not retrieve database version, running all scripts..." set updates $filelist } elseif { $dbRev == "" } { puts "Could not retrieve database version, running all scripts..." set updates $filelist } else { puts "Found $::SQLDB database at version $dbRev..." for { set i 0 } { $i < [llength $filelist] } { incr i } { set file [lindex $filelist $i] set curRev [join [lrange [split [lindex [split $file "/"] end] "."] 0 end-1] "."] if { $curRev > $dbRev } { set updates [lrange $filelist $i end] break } } } } set numUpdates [llength $updates] if { $numUpdates < 1 } { puts "Database is up to date." } else { set i 1 foreach file $updates { puts -nonewline "Running sql script $i of $numUpdates..." if { [catch "::helga::db::batch $file root $rootpass" batchOut] } { puts "Error: $batchOut" exit 1 } puts "done" incr i set revNum [lrange [split [lindex [split $file "/"] end] "."] 0 end-1] } if { $::db == "" } { ::helga::db::init root $rootpass } puts "Database updated to version $revNum." }