my %dbi_attrs = (
AutoCommit => 0,
RaiseError => 1,
- sqlite_see_if_its_a_number => 1, # see if the bind values are numbers or not
sqlite_use_immediate_transaction => 1,
sqlite_open_flags => SQLITE_OPEN_READWRITE
);
# Try to lock the database before any network traffic so we can fail
# early if the database is already locked.
$DBH->do("PRAGMA locking_mode = EXCLUSIVE");
- $DBH->do("PRAGMA foreign_keys = ON");
+ $DBH->{AutoCommit} = 1; # turned back off later
+ $DBH->do("PRAGMA foreign_keys = OFF"); # toggled later (no-op if not in autocommit mode)
}
sub msg($@) {
if ($schema_version < $DATABASE_VERSION) {
# schema creation or upgrade required
+ $DBH->begin_work();
if ($schema_version == 0) {
my $sth = $DBH->table_info(undef, undef, undef, "TABLE");
unless (defined $sth->fetch()) {
if defined $IMAP->{local}->{delimiter} and defined $IMAP->{remote}->{delimiter}
# we failed earlier if only one of them was NIL
and $IMAP->{local}->{delimiter} ne $IMAP->{remote}->{delimiter};
- $DBH->do("PRAGMA foreign_keys = OFF");
$DBH->do("CREATE TABLE _tmp${DATABASE_VERSION}_mailboxes (". join(", ",
q{idx INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT},
q{mailbox BLOB COLLATE BINARY NOT NULL CHECK (mailbox != '') UNIQUE},
}
$DBH->do("DROP TABLE mailboxes");
$DBH->do("ALTER TABLE _tmp${DATABASE_VERSION}_mailboxes RENAME TO mailboxes");
- $DBH->do("PRAGMA foreign_keys = ON");
}
+ fail("database", "Broken referential integrity! Refusing to commit changes.")
+ if defined $DBH->selectrow_arrayref("PRAGMA foreign_key_check");
SCHEMA_DONE:
$DBH->do("PRAGMA user_version = $DATABASE_VERSION");
$DBH->commit();
}
+ $DBH->do("PRAGMA foreign_keys = ON"); # no-op if not in autocommit mode
+ $DBH->{AutoCommit} = 0; # always explicitly commit changes
}