BEGIN;
PRAGMA user_version = 1;
CREATE TABLE IF NOT EXISTS "BackupJobs" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                        "name" TEXT NOT NULL ,
                                        "options" TEXT,
                                        "repository_id" TEXT NOT NULL ,
                                        "creation_time" DATETIME NOT NULL ,
                                        "current_backup_id" TEXT NOT NULL );
CREATE TABLE IF NOT EXISTS "BackupPoints" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                           "link_id"  NOT NULL,
                                           "creation_time" DATETIME NOT NULL ,
                                           "creation_time_utc" DATETIME NOT NULL ,
                                           "alg" INTEGER NOT NULL ,
                                           "backup_id",
                                           "group_id",
                                           "num" INTEGER NOT NULL,
                                           "type" INTEGER NOT NULL,
                                            FOREIGN KEY("backup_id") REFERENCES "Backups"("id") );
CREATE TABLE IF NOT EXISTS "BackupRepositories" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                                "name" TEXT NOT NULL ,
                                                "type" INTEGER NOT NULL ,
                                                "options" TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS "BackupStorages" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                            "name" TEXT,
                                            "in_backup_path" TEXT NOT NULL,
                                            "backup_id" ,
                                            "creation_time" DATETIME NOT NULL,
                                            "creation_time_utc" DATETIME NOT NULL ,
                                            "modif_time" DATETIME NOT NULL ,
                                            "block_size" INTEGER NOT NULL,
                                            "stat" TEXT,
                                            "link_id" TEXT,
                                            FOREIGN KEY("backup_id") REFERENCES "Backups"("id") );
CREATE TABLE IF NOT EXISTS "Backups" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                      "repository_id" TEXT NOT NULL ,
                                      "creation_time" DATETIME NOT NULL ,
                                      "creation_time_utc" DATETIME NOT NULL ,
                                      "job_name" TEXT NOT NULL ,
                                      "job_id" ,
                                      "meta_file_name" TEXT NOT NULL ,
                                      "in_repository_dir_path" TEXT,
                                      FOREIGN KEY("repository_id") REFERENCES "BackupRepositories"("id") );
CREATE TABLE IF NOT EXISTS "ObjectsInBackups" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                               "point_id"  NOT NULL ,
                                               "storage_id"  NOT NULL ,
                                               "prev_oib_id"  NOT NULL ,
                                               "name" TEXT,
                                               "is_consistent" BOOL NOT NULL ,
                                               "is_corrupted" BOOL NOT NULL ,
                                               "inside_dir" TEXT NOT NULL ,
                                               "creation_time" DATETIME NOT NULL ,
                                               "creation_time_utc" DATETIME NOT NULL ,
                                               "alg" INTEGER NOT NULL,
                                               "guest_info" TEXT NOT NULL,
                                               "aux_data" TEXT NOT NULL,
                                               "fqdn" TEXT NOT NULL,
                                               "vm_id" TEXT NOT NULL,
                                               "approx_size" INTEGER NOT NULL,
                                               "memory_mb" INTEGER NOT NULL,
                                               FOREIGN KEY("point_id") REFERENCES "BackupPoints"("id"),
                                               FOREIGN KEY("storage_id") REFERENCES "BackupStorages"("id") );
CREATE TABLE IF NOT EXISTS "ObjectsInJobs" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                            "job_id" NOT NULL ,
                                            "rec_type" INTEGER NOT NULL ,
                                            "obj_type" INTEGER NOT NULL ,
                                            "value" TEXT NOT NULL,
                                            FOREIGN KEY("job_id") REFERENCES "BackupJobs"("id") );
CREATE TABLE IF NOT EXISTS "BObjects" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                       "vm_ref" NOT NULL,
                                       "host_id" NOT NULL,
                                       "display_name" NOT NULL,
                                       "guest_info_xml" NOT NULL );
CREATE TABLE IF NOT EXISTS "JobSessions" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                          "job_id" ,
                                          "job_name" TEXT,
                                          "type" INTEGER ,
                                          "start_time" DATETIME ,
                                          "end_time" DATETIME ,
                                          "state" INTEGER ,
                                          "state_changed" DATETIME ,
                                          "log_text" ,
                                          "logs_dir" TEXT,
                                          "progress" INTEGER,
                                          "progress_details" TEXT,
                                          "details" TEXT);
CREATE TABLE "VbrServers" ("id" PRIMARY KEY  NOT NULL  UNIQUE ,
                           "name" TEXT NOT NULL ,
                           "login" TEXT NOT NULL ,
                           "password" BLOB NOT NULL ,
                           "domain" TEXT NOT NULL ,
                           "fqdn" TEXT NOT NULL ,
                           "onlyForRestore" BOOL NOT NULL ,
                           "endpoint" TEXT NOT NULL ,
                           "license" TEXT);
COMMIT;
