

-- -----------------------------------------------------
-- Table `profile`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `profile` ;

CREATE  TABLE IF NOT EXISTS `profile` (
  `id` INTEGER NULL DEFAULT NULL ,
  `name` VARCHAR(45) NULL ,
  `birth_date` TIMESTAMP NOT NULL ,
  `gender` INTEGER NULL DEFAULT NULL ,
  `height` INTEGER NULL DEFAULT NULL ,
  `photo` BLOB NULL ,
  PRIMARY KEY (`id`) );


-- -----------------------------------------------------
-- Table `category`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `category` ;

CREATE  TABLE IF NOT EXISTS `category` (
  `id` INTEGER NOT NULL ,
  `profile` INTEGER NULL ,
  `parent` INTEGER NULL ,
  `name` VARCHAR(255) NULL ,
  PRIMARY KEY (`id`) ,
  CONSTRAINT `fk_category_profile`
    FOREIGN KEY (`profile` )
    REFERENCES `profile` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE);

CREATE INDEX `fk_category_profile` ON `category` (`profile` ASC) ;


-- -----------------------------------------------------
-- Table `activity`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `activity` ;

CREATE  TABLE IF NOT EXISTS `activity` (
  `id` INTEGER NULL DEFAULT NULL ,
  `profile` INTEGER NULL ,
  `name` VARCHAR(255) NULL ,
  `category` INTEGER NULL ,
  `equipment` INTEGER NULL DEFAULT NULL ,
  `ts` TIMESTAMP NULL ,
  `temperature` INTEGER NULL ,
  `weather` INTEGER NULL ,
  `wind` VARCHAR(255) NULL ,
  `notes` TEXT NULL DEFAULT NULL ,
  PRIMARY KEY (`id`) ,
  CONSTRAINT `fk_activity_profile`
    FOREIGN KEY (`profile` )
    REFERENCES `profile` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_activity_category`
    FOREIGN KEY (`category` )
    REFERENCES `category` (`id` )
    ON DELETE SET NULL
    ON UPDATE CASCADE);

CREATE INDEX `fk_activity_profile` ON `activity` (`profile` ASC) ;

CREATE INDEX `fk_activity_category` ON `activity` (`category` ASC) ;


-- -----------------------------------------------------
-- Table `equipment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `equipment` ;

CREATE  TABLE IF NOT EXISTS `equipment` (
  `id` INTEGER NULL DEFAULT NULL ,
  `profile` INTEGER NULL ,
  `name` VARCHAR(255) NULL ,
  `model` VARCHAR(255) NULL DEFAULT NULL ,
  `brand` VARCHAR(255) NULL DEFAULT NULL ,
  `purchase_date` TIMESTAMP NOT NULL ,
  `type` VARCHAR(255) NULL DEFAULT NULL ,
  `weight` INTEGER NULL DEFAULT NULL ,
  `in_use` INTEGER NULL DEFAULT NULL ,
  `notes` TEXT NULL DEFAULT NULL ,
  `photo` BLOB NULL DEFAULT NULL ,
  PRIMARY KEY (`id`) ,
  CONSTRAINT `fk_equipment_profile`
    FOREIGN KEY (`profile` )
    REFERENCES `profile` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE);

CREATE INDEX `fk_equipment_profile` ON `equipment` (`profile` ASC) ;


-- -----------------------------------------------------
-- Table `lap`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lap` ;

CREATE  TABLE IF NOT EXISTS `lap` (
  `id` INTEGER NULL DEFAULT NULL ,
  `activity` INTEGER NULL ,
  `start_time` TIMESTAMP NULL ,
  `duration` INTEGER NULL DEFAULT NULL ,
  `distance` INTEGER NULL DEFAULT NULL ,
  `max_speed` INTEGER NULL DEFAULT NULL ,
  `energy_consumption` INTEGER NULL DEFAULT NULL ,
  `average_heart_rate` INTEGER NULL DEFAULT NULL ,
  `max_heart_rate` INTEGER NULL DEFAULT NULL ,
  `average_cadence` INTEGER NULL DEFAULT NULL ,
  `ascent` INTEGER NULL DEFAULT NULL ,
  `descent` INTEGER NULL DEFAULT NULL ,
  PRIMARY KEY (`id`) ,
  CONSTRAINT `fk_lap_activity`
    FOREIGN KEY (`activity` )
    REFERENCES `activity` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE);

CREATE INDEX `fk_lap_activity` ON `lap` (`activity` ASC) ;


-- -----------------------------------------------------
-- Table `athletelog`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `athletelog` ;

CREATE  TABLE IF NOT EXISTS `athletelog` (
  `id` INTEGER NULL DEFAULT NULL ,
  `profile` INTEGER NULL ,
  `date` TIMESTAMP NOT NULL ,
  `weight` INTEGER NULL DEFAULT NULL ,
  `body_fat_percent` INTEGER NULL DEFAULT NULL ,
  `resting_heart_rate` INTEGER NULL DEFAULT NULL ,
  `max_heart_rate` INTEGER NULL DEFAULT NULL ,
  `blood_pressure_systolic` INTEGER NULL DEFAULT NULL ,
  `blood_pressure_diastolic` INTEGER NULL DEFAULT NULL ,
  `energy_consumption` INTEGER NULL DEFAULT NULL ,
  `sleep_duration` INTEGER NULL DEFAULT NULL ,
  `notes` TEXT NULL ,
  `photo` BLOB NULL ,
  PRIMARY KEY (`id`) ,
  CONSTRAINT `fk_profiledetail_profile`
    FOREIGN KEY (`profile` )
    REFERENCES `profile` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE);

CREATE INDEX `fk_profiledetail_profile` ON `athletelog` (`profile` ASC) ;


-- -----------------------------------------------------
-- Table `track`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `track` ;

CREATE  TABLE IF NOT EXISTS `track` (
  `id` INTEGER NULL DEFAULT NULL ,
  `lap` INTEGER NULL ,
  PRIMARY KEY (`id`) ,
  CONSTRAINT `fk_track_lap`
    FOREIGN KEY (`lap` )
    REFERENCES `lap` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE);

CREATE INDEX `fk_track_lap` ON `track` (`lap` ASC) ;


-- -----------------------------------------------------
-- Table `trackpoint`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `trackpoint` ;

CREATE  TABLE IF NOT EXISTS `trackpoint` (
  `id` INTEGER NULL DEFAULT NULL ,
  `track` INTEGER NULL ,
  `ts` TIMESTAMP NOT NULL ,
  `latitude` DOUBLE NULL DEFAULT NULL ,
  `longitude` DOUBLE NULL DEFAULT NULL ,
  `speed` INTEGER NULL DEFAULT NULL ,
  `altitude` INTEGER NULL DEFAULT NULL ,
  `heart_rate` INTEGER NULL DEFAULT NULL ,
  `cadence` INTEGER NULL DEFAULT NULL ,
  PRIMARY KEY (`id`) ,
  CONSTRAINT `fk_trackpoint_track`
    FOREIGN KEY (`track` )
    REFERENCES `track` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE);

CREATE INDEX `fk_trackpoint_track` ON `trackpoint` (`track` ASC) ;



