<?xml version="1.0" encoding="utf-8"?>
<!--
This is the Mixxx schema history file. It keeps track of internal changes to the
Mixxx database schema and allows Mixxx to automatically upgrade itself to new
revisions of the schema on a version upgrade.

DO NOT EDIT THIS FILE OR YOU WILL BREAK YOUR MIXXX LIBRARY AND LOSE YOUR
METADATA
-->
<schema>
  <revision version="1">
    <description>
      The base schema for the Mixxx SQLITE database.
    </description>
    <sql>
      CREATE TABLE IF NOT EXISTS settings (
        name TEXT UNIQUE NOT NULL,
        value TEXT,
        locked INTEGER DEFAULT 0,
        hidden INTEGER DEFAULT 0);

      CREATE TABLE IF NOT EXISTS track_locations (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        location varchar(512) UNIQUE,
        filename varchar(512),
        directory varchar(512),
        filesize INTEGER,
        fs_deleted INTEGER,
        needs_verification INTEGER);

      CREATE TABLE IF NOT EXISTS LibraryHashes (
        directory_path VARCHAR(256) primary key,
        hash INTEGER,
        directory_deleted INTEGER);

      CREATE TABLE IF NOT EXISTS library (
        id INTEGER primary key AUTOINCREMENT,
        artist varchar(48), title varchar(48),
        album varchar(48), year varchar(16),
        genre varchar(32), tracknumber varchar(3),
        location varchar(512) REFERENCES track_locations(location),
        comment varchar(20), url varchar(256),
        duration integer,
        bitrate integer, samplerate integer,
        cuepoint integer, bpm float,
        wavesummaryhex blob,
        channels integer,
        datetime_added DEFAULT CURRENT_TIMESTAMP,
        mixxx_deleted integer,
        played integer);

      CREATE TABLE Playlists (
        id INTEGER primary key,
        name varchar(48),
        position INTEGER,
        hidden INTEGER DEFAULT 0 NOT NULL,
        date_created datetime,
        date_modified datetime);

      CREATE TABLE PlaylistTracks (
        id INTEGER primary key,
        playlist_id INTEGER REFERENCES Playlists(id),
        track_id INTEGER REFERENCES library(id),
        position INTEGER);

      CREATE TABLE IF NOT EXISTS cues (
        id integer PRIMARY KEY AUTOINCREMENT,
        track_id integer NOT NULL REFERENCES library(id),
        type integer DEFAULT 0 NOT NULL,
        position integer DEFAULT -1 NOT NULL,
        length integer DEFAULT 0 NOT NULL,
        hotcue integer DEFAULT -1 NOT NULL,
        label text DEFAULT '' NOT NULL);

      CREATE TABLE IF NOT EXISTS crates (
        id integer PRIMARY KEY AUTOINCREMENT,
        name varchar(48) UNIQUE NOT NULL,
        count integer DEFAULT 0,
        show integer DEFAULT 1);

      CREATE TABLE IF NOT EXISTS crate_tracks (
        crate_id integer NOT NULL REFERENCES crates(id),
        track_id integer NOT NULL REFERENCES library(id),
        UNIQUE (crate_id, track_id));

    </sql>
  </revision>
  <revision version="2">
    <description>
      Add a header_parsed integer column to the library to indicate when a
      track's tags have been parsed.
    </description>
    <sql>
      ALTER TABLE library ADD COLUMN header_parsed integer DEFAULT 0;
    </sql>
  </revision>
  <revision version="3">
    <description>
      Change the location column to be a an integer. Change comment to be
      varchar(256) and album/artist/title to be varchar(64).
    </description>
    <sql>
      ALTER TABLE library RENAME TO library_old;

      CREATE TABLE IF NOT EXISTS library (
        id INTEGER primary key AUTOINCREMENT,
        artist varchar(64),
        title varchar(64),
        album varchar(64),
        year varchar(16),
        genre varchar(64),
        tracknumber varchar(3),
        location integer REFERENCES track_locations(location),
        comment varchar(256),
        url varchar(256),
        duration integer,
        bitrate integer,
        samplerate integer,
        cuepoint integer,
        bpm float,
        wavesummaryhex blob,
        channels integer,
        datetime_added DEFAULT CURRENT_TIMESTAMP,
        mixxx_deleted integer,
        played integer,
        header_parsed integer DEFAULT 0);

      INSERT INTO library (id, artist, title, album, year, genre, tracknumber, location, comment, url, duration, bitrate, samplerate, bpm, cuepoint, bpm, wavesummaryhex, channels, datetime_added, mixxx_deleted, played, header_parsed) SELECT id, artist, title, album, year, genre, tracknumber, location, comment, url, duration, bitrate, samplerate, bpm, cuepoint, bpm, wavesummaryhex, channels, datetime_added, mixxx_deleted, played, header_parsed from library_old;

      DROP TABLE library_old;
    </sql>
  </revision>
  <revision version="4">
    <description>
      Add file type column.
    </description>
    <sql>
      ALTER TABLE library ADD COLUMN filetype varchar(8) DEFAULT "?";
    </sql>
  </revision>
  <revision version="5">
    <description>
      Add needs_verification column to library hashes table.
    </description>
    <sql>
      ALTER TABLE LibraryHashes ADD COLUMN needs_verification INTEGER DEFAULT 0;
    </sql>
  </revision>
  <revision version="6">
    <description>
                    Added a ReplayGain Column.
    </description>
    <sql>
        ALTER TABLE library ADD COLUMN replaygain float DEFAULT 0;
    </sql>
  </revision>
  <revision version="7" min_compatible="3">
    <description>
      Add timesplayed and rating column. Reset header state.
    </description>
    <sql>
      ALTER TABLE library ADD COLUMN timesplayed integer DEFAULT 0;
      ALTER TABLE library ADD COLUMN rating integer DEFAULT 0;
      ALTER TABLE library ADD COLUMN key varchar(8) DEFAULT "";
      UPDATE library SET timesplayed = played;
      UPDATE library SET played = 0;

      DELETE FROM settings WHERE name="mixxx.db.model.library.header_state";
      DELETE FROM settings WHERE name="mixxx.db.model.playlist.header_state";
      DELETE FROM settings WHERE name="mixxx.db.model.crate.header_state";
      DELETE FROM settings WHERE name="mixxx.db.model.prepare.header_state";
      DELETE FROM settings WHERE name="mixxx.db.model.missing.header_state";
    </sql>
  </revision>
  <revision version="8" min_compatible="3">
    <description>
      Added iTunes tables
    </description>
    <sql>
      CREATE TABLE IF NOT EXISTS itunes_library (
        id INTEGER primary key,
        artist varchar(48), title varchar(48),
        album varchar(48), year varchar(16),
        genre varchar(32), tracknumber varchar(3),
        location varchar(512),
        comment varchar(60),
        duration integer,
        bitrate integer,
        bpm integer,
        rating integer);

       CREATE TABLE IF NOT EXISTS itunes_playlists (
        id INTEGER primary key,
        name varchar(100) UNIQUE);

      CREATE TABLE IF NOT EXISTS itunes_playlist_tracks (
        id INTEGER primary key AUTOINCREMENT,
        playlist_id INTEGER REFERENCES itunes_playlist(id),
        track_id INTEGER REFERENCES itunes_library(id));
    </sql>
  </revision>
  <revision version="9" min_compatible="3">
    <description>
      Tables for Traktor library feature
    </description>
    <sql>
     CREATE TABLE IF NOT EXISTS traktor_library (
        id INTEGER primary key AUTOINCREMENT,
        artist varchar(48), title varchar(48),
        album varchar(48), year varchar(16),
        genre varchar(32), tracknumber varchar(3),
        location varchar(512) UNIQUE,
        comment varchar(60),
        duration integer,
        bitrate integer,
        bpm float,
        key varchar(6),
        rating integer
        );
      CREATE TABLE IF NOT EXISTS traktor_playlists (
        id INTEGER primary key,
        name varchar(100) UNIQUE
        );
      CREATE TABLE IF NOT EXISTS traktor_playlist_tracks (
        id INTEGER primary key AUTOINCREMENT,
        playlist_id INTEGER REFERENCES traktor_playlist(id),
        track_id INTEGER REFERENCES traktor_library(id)
        );
    </sql>
  </revision>
  <revision version="10" min_compatible="3">
    <description>
      Playlist and crate locks
    </description>
    <sql>
      ALTER TABLE crates ADD COLUMN locked integer DEFAULT 0;
      ALTER TABLE playlists ADD COLUMN locked integer DEFAULT 0;
    </sql>
  </revision>
  <revision version="11" min_compatible="3">
    <description>
      Tables for Rhythmbox library feature
    </description>
    <sql>
      CREATE TABLE IF NOT EXISTS rhythmbox_library (
        id INTEGER primary key AUTOINCREMENT,
        artist varchar(48), title varchar(48),
        album varchar(48), year varchar(16),
        genre varchar(32), tracknumber varchar(3),
        location varchar(512) UNIQUE,
        comment varchar(60),
        duration integer,
        bitrate integer,
        bpm float,
        key varchar(6),
        rating integer
        );
      CREATE TABLE IF NOT EXISTS rhythmbox_playlists (
        id INTEGER primary key AUTOINCREMENT,
        name varchar(100) UNIQUE
        );
      CREATE TABLE IF NOT EXISTS rhythmbox_playlist_tracks (
        id INTEGER primary key AUTOINCREMENT,
        playlist_id INTEGER REFERENCES rhythmbox_playlist(id),
        track_id INTEGER REFERENCES rhythmbox_library(id)
        );
    </sql>
  </revision>
  <revision version="12" min_compatible="3">
    <description>
      Add beats column to library table.
    </description>
    <sql>
      ALTER TABLE Library ADD COLUMN beats BLOB;
      ALTER TABLE Library ADD COLUMN beats_version TEXT;
    </sql>
  </revision>
  <revision version="13" min_compatible="3">
    <description>
      Add position column to Rhythmbox, iTunes, and Traktor playlist tables.
    </description>
    <sql>
      ALTER TABLE itunes_playlist_tracks ADD COLUMN position INTEGER DEFAULT 0;
      ALTER TABLE rhythmbox_playlist_tracks ADD COLUMN position INTEGER DEFAULT 0;
      ALTER TABLE traktor_playlist_tracks ADD COLUMN position INTEGER DEFAULT 0;
    </sql>
  </revision>
  <revision version="14" min_compatible="3">
    <description>
      Add composer column to library table.
    </description>
    <sql>
      ALTER TABLE library ADD COLUMN composer varchar(64) DEFAULT "";
    </sql>
  </revision>
  <revision version="15" min_compatible="3">
    <description>
      Add datetime_added to playlists tracks.
    </description>
    <sql>
      ALTER TABLE PlaylistTracks ADD COLUMN pl_datetime_added;
    </sql>
  </revision>
  <revision version="16" min_compatible="3">
    <description>
      Add track analysis table.
    </description>
    <sql>
    CREATE TABLE IF NOT EXISTS track_analysis (
      id INTEGER primary key AUTOINCREMENT,
      track_id INTEGER NOT NULL REFERENCES track_locations(id),
      type varchar(512),
      description varchar(1024),
      version varchar(512),
      created DEFAULT CURRENT_TIMESTAMP,
      data_checksum varchar(512)
    );
    UPDATE library SET wavesummaryhex = '';
    CREATE INDEX IF NOT EXISTS track_analysis_track_id_index ON track_analysis (track_id);
    </sql>
  </revision>
  <revision version="17" min_compatible="3">
    <description>
      Add columns for BPM lock and a sub-version string for beats.
    </description>
    <sql>
      ALTER TABLE Library ADD COLUMN bpm_lock INTEGER DEFAULT 0;
      ALTER TABLE Library ADD COLUMN beats_sub_version TEXT DEFAULT '';
    </sql>
  </revision>
</schema>
