index a67f7b4a27f46893f16c7c2d5fa238c60308617b..985963da4a01e994e99d87dd0a858d8eef26fd9a 100644 (file)
use strict;
use warnings;
+
use Carp;
use DBI;
use GOSA::GosaSupportDaemon;
push(@col_names, $col_name);
}
$col_names->{ $table_name } = \@col_names;
+ my $col_names_string = join(", ", @col_names);
# Not activated yet
# Check schema
- # if($self->check_schema($table_name)) {
- # $self->exec_statement("DROP TABLE $table_name");
- # &main::daemon_log("WARNING: Schema of table $table_name has changed! Table will be recreated!", 3);
- # }
+ if($self->check_schema($table_name, $col_names_ref)) {
+ $self->exec_statement("DROP TABLE $table_name");
+ &main::daemon_log("WARNING: Schema of table $table_name has changed! Table will be recreated!", 3);
+ }
- my $col_names_string = join(", ", @col_names);
my $sql_statement = "CREATE TABLE IF NOT EXISTS $table_name ( $col_names_string )";
my $res = $self->exec_statement($sql_statement);
}
+sub check_schema {
+ my $self = shift;
+ my $table_name = shift;
+ my $col_names_ref = shift; # ['id INTEGER PRIMARY KEY', 'timestamp VARCHAR(14) DEFAULT \'none\'', ... ]
+ my $col_names_length = @$col_names_ref;
+
+ my $sql = "PRAGMA table_info($table_name)";
+ my $res = $self->exec_statement($sql); # [ ['0', 'id', 'INTEGER', '0', undef, '1' ], ['1', 'timestamp', 'VARCHAR(14)', '0', '\'none\'', '0'], ... ]
+ my $db_table_length = @$res;
+
+ # Tabel does not exists, so no differences
+ if ($db_table_length == 0)
+ {
+ return 0;
+ }
+
+
+
+ # The number of columns is diffrent
+ if ($col_names_length != $db_table_length)
+ {
+ return 1;
+ }
+
+ # The column name and column type to not match
+ for (my $i=0; $i < $db_table_length; $i++)
+ {
+ my @col_names_list = split(" ", @$col_names_ref[$i]);
+ if (($col_names_list[0] ne @{@$res[$i]}[1]) || ($col_names_list[1] ne @{@$res[$i]}[2]))
+ {
+ return 1;
+ }
+ }
+
+
+ return 0;
+}
+
+
+
sub add_dbentry {
my $self = shift;
my $arg = shift;