   @threadManager = Components.classes['@mozilla.org/thread-manager;1'].getService()
   @windowMediator = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator)
 
-  @attachDatabase()
+  @initDatabase()
   cfi = @pref.get('cacheFlushInterval')
   cfi = 1 if typeof cfi != 'number' || cfi < 1
   cfi = 5 if cfi > 5

  Zotero.DB.query("insert or ignore into betterbibtex.schema (lock, version) values ('schema', '')")
  Zotero.DB.query("update betterbibtex.schema set version = '' where not version like '%.%.%'")

  installed = @version(Zotero.DB.valueQuery("select version from betterbibtex.schema"))
  installing = @version(@release)

  if @tableExists('betterbibtex.autoexport') && @table_info('betterbibtex.autoexport').collection
    Zotero.DB.query("update betterbibtex.autoexport set collection = (select 'library:' || libraryID from groups where 'group:' || groupID = collection) where collection like 'group:%'")
    Zotero.DB.query("update betterbibtex.autoexport set collection = 'collection:' || collection where collection <> 'library' and collection not like '%:%'")

  upgrade = (installed != installing) || Services.vc.compare(@release, '1.2.1') <= 0

  for table in Zotero.DB.columnQuery("SELECT name FROM betterbibtex.sqlite_master WHERE type='table' AND name like '-%-'") || []
    Zotero.DB.query("drop table if exists betterbibtex.\"#{table}\"")
  tables = Zotero.DB.columnQuery("SELECT name FROM betterbibtex.sqlite_master WHERE type='table' AND name <> 'schema' ORDER BY name") || []

  upgrade ||= tables.join(' + ') != 'autoexport + cache + keys'
  for check in [
    'SELECT itemID, citekey, citekeyFormat FROM betterbibtex.keys'
    'SELECT id, collection, path, exportCharset, exportNotes, translatorID, useJournalAbbreviation, exportedRecursively, status FROM betterbibtex.autoexport'

  exists = (Zotero.DB.valueQuery("SELECT count(*) FROM #{table.schema}sqlite_master WHERE type='table' and name=?", [table.name]) != 0)
  return exists && (!mustHaveData || Zotero.DB.valueQuery("select count(*) from #{name}") != 0)

Zotero.BetterBibTeX.upgradeDatabase = ->
  @flash('Better BibTeX: updating database', 'Updating database, this could take a while')

  for key in @pref.prefs.getChildList('')
    switch key
      when 'auto-abbrev.style' then @pref.set('autoAbbrevStyle', @pref.get(key))
