   @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

      @log('Unexpected schema:', check, e)
      upgrade = true

  if upgrade
    @flash('Better BibTeX: updating database', 'Updating database, this could take a while')

    tip = Zotero.DB.transactionInProgress()
    Zotero.DB.beginTransaction() unless tip

    @pref.set('scanCitekeys', true)

    for table in tables
      @debug('attachdatabase: backing up', table)
      Zotero.DB.query("alter table betterbibtex.#{table} rename to \"-#{table}-\"")

    ### clean slate ###

    Zotero.DB.query('create table betterbibtex.keys (itemID primary key, citekey not null, citekeyFormat)')

    Zotero.DB.query("
      create table betterbibtex.cache (
        itemID not null,

        exportCharset not null,
        exportNotes default 'false' CHECK(exportNotes in ('true', 'false')),
        getCollections default 'false' CHECK(getCollections in ('true', 'false')),
        translatorID not null,
        useJournalAbbreviation default 'false' CHECK(useJournalAbbreviation in ('true', 'false')),

        citekey not null,
        bibtex not null,
        lastaccess not null default CURRENT_TIMESTAMP,
        PRIMARY KEY (itemID, exportCharset, exportNotes, getCollections, translatorID, useJournalAbbreviation)
        )
      ")

    Zotero.DB.query("
      create table betterbibtex.autoexport (
        id INTEGER PRIMARY KEY NOT NULL DEFAULT NULL,

        collection not null,
        path not null,

        exportCharset not null,
        exportNotes default 'false' CHECK(exportNotes in ('true', 'false')),
        translatorID not null,
        useJournalAbbreviation default 'false' CHECK(useJournalAbbreviation in ('true', 'false')),

        exportedRecursively CHECK(exportedRecursively in ('true', 'false')),
        status,

        UNIQUE (path)
        )
      ")

    ### migrate data where needed ###

    if @tableExists('betterbibtex."-keys-"', true)
      @debug('attachdatabase: migrating keys')
      if @table_info('betterbibtex."-keys-"').pinned
        @debug('attachdatabase: migrating old-style keys')
        Zotero.BetterBibTeX.debug('Upgrading betterbibtex.keys')
        Zotero.DB.query('insert into betterbibtex.keys (itemID, citekey, citekeyFormat)
                        select itemID, citekey, case when pinned = 1 then null else ? end from betterbibtex."-keys-"', [@citekeyFormat])
      else
        @debug('attachdatabase: migrating keys')
        @copyTable('betterbibtex."-keys-"', 'betterbibtex.keys')

    if @tableExists('betterbibtex."-autoexport-"', true)
      @debug('attachdatabase: migrating autoexport')
      if @table_info('betterbibtex."-autoexport-"').context
        # sorry my dear colleague, but this was a mess
      else
        @copyTable('betterbibtex."-autoexport-"', 'betterbibtex.autoexport', 'id')

    ### cleanup ###

    for table in Zotero.DB.columnQuery("SELECT name FROM betterbibtex.sqlite_master WHERE type='table' AND name like '-%-'") || []
      @debug('attachdatabase: deleting', table)
      Zotero.DB.query("drop table if exists betterbibtex.\"#{table}\"")

    Zotero.DB.commitTransaction() unless tip

  if @pref.get('scanCitekeys')
    @flash('Citation key rescan', "Scanning 'extra' fields for fixed keys\nFor a large library, this might take a while")

  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))
