CREATE UNLOGGED TABLE backing_tracks (
  id VARCHAR(64) NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
  filename VARCHAR(1024) NOT NULL,

  connection_id VARCHAR(64) NOT NULL REFERENCES connections(id) ON DELETE CASCADE,
  client_track_id VARCHAR(64) NOT NULL,
  client_resource_id VARCHAR(100),
  
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP  
);

CREATE TABLE recorded_backing_tracks (
    id BIGINT PRIMARY KEY,
    user_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE,    
    backing_track_id VARCHAR(64),
    recording_id VARCHAR(64) NOT NULL,
    
    client_track_id VARCHAR(64) NOT NULL,
    is_part_uploading BOOLEAN NOT NULL DEFAULT FALSE,
    next_part_to_upload INTEGER NOT NULL DEFAULT 0,
    upload_id CHARACTER VARYING(1024),
    part_failures INTEGER NOT NULL DEFAULT 0,
    discard BOOLEAN,
    download_count INTEGER NOT NULL DEFAULT 0,
    md5 CHARACTER VARYING(100),
    length BIGINT,
    client_id VARCHAR(64) NOT NULL,
    file_offset BIGINT,

    url VARCHAR(1024) NOT NULL,
    fully_uploaded BOOLEAN NOT NULL DEFAULT FALSE,
    upload_failures INTEGER NOT NULL DEFAULT 0,    
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE recorded_backing_tracks ALTER COLUMN id SET DEFAULT nextval('tracks_next_tracker_seq');
