--  FROM:2,TO:3
BEGIN;
    ALTER TABLE JobSessions ADD COLUMN retried_session_id DEFAULT NULL;
COMMIT;

--  FROM:3,TO:4
BEGIN;
    ALTER TABLE "ObjectsInJobs" ADD COLUMN "hint" TEXT DEFAULT NULL;
COMMIT;

--  FROM:4,TO:5
BEGIN;
    ALTER TABLE "BackupStorages" ADD COLUMN "modif_time_utc" DATETIME DEFAULT 0;
COMMIT;

-- FROM:5,TO:6
BEGIN;
    ALTER TABLE "ObjectsInBackups" ADD COLUMN "has_index" BOOL DEFAULT FALSE;
COMMIT;

--  FROM:6,TO:100
BEGIN;
    ALTER TABLE "JobSessions" ADD COLUMN "usn" INTEGER DEFAULT 0;

    CREATE TABLE IF NOT EXISTS "SessionsUsn" ("value"  PRIMARY KEY  NOT NULL  UNIQUE);

    CREATE TRIGGER SessionsUpdUsn_AfterIns_Trigger AFTER INSERT
    ON 'JobSessions' FOR EACH ROW
    BEGIN
        INSERT OR IGNORE INTO 'SessionsUsn' (rowid, value) VALUES(1, 0);
        UPDATE 'SessionsUsn' SET Value=Value+1;
        UPDATE 'JobSessions' SET Usn=(SELECT Value FROM 'SessionsUsn' ORDER BY ROWID ASC LIMIT 1) WHERE ID=NEW.id;
    END;

    CREATE TRIGGER SessionsUpdUsn_AfterUpd_Trigger AFTER UPDATE
    ON 'JobSessions' FOR EACH ROW
        WHEN (NEW.Usn = Old.Usn)
    BEGIN
        INSERT OR IGNORE INTO 'SessionsUsn' (rowid, value) VALUES(1, 0);
        UPDATE 'SessionsUsn' SET Value=Value+1;
        UPDATE 'JobSessions' SET Usn=(SELECT Value FROM 'SessionsUsn' ORDER BY ROWID ASC LIMIT 1) WHERE ID=NEW.id;
    END;
COMMIT;

-- FROM:100,TO:101
BEGIN;
    CREATE TABLE "CloudProviders" ("id" PRIMARY KEY  NOT NULL  UNIQUE ,
                           "name" TEXT NOT NULL ,
                           "login" TEXT NOT NULL ,
                           "password" BLOB NOT NULL ,
                           "endpoints" TEXT NOT NULL );
COMMIT;

-- FROM:101,TO:102
BEGIN;
    ALTER TABLE "CloudProviders" ADD COLUMN "main_endpoint" TEXT DEFAULT '';
    UPDATE "CloudProviders" SET "main_endpoint"="endpoints", "endpoints"='';
COMMIT;

-- FROM:102,TO:103
BEGIN;
    CREATE TABLE "CryptoKeys" ("id" PRIMARY KEY  NOT NULL  UNIQUE ,
                              "key_set_id" BLOB NOT NULL ,
                              "type" INTEGER NOT NULL ,
                              "crypto_alg" INTEGER NOT NULL ,
                              "key_value" BLOB NOT NULL,
                              "hint" TEXT NOT NULL ,
                              "creation_time" DATETIME NOT NULL , 
                              "backup_id" TEXT NOT NULL );

    CREATE TABLE "CryptoKeysRecoveryRecs" ("id" PRIMARY KEY  NOT NULL  UNIQUE ,
                                           "encrypted_key_id" TEXT NOT NULL ,
                                           "recovery_key_id" TEXT NOT NULL ,
                                           "restore_blob" BLOB NOT NULL ,
                                           FOREIGN KEY("encrypted_key_id") REFERENCES "CryptoKeys"("id") );
COMMIT;

-- FROM:103,TO:104
BEGIN;
    ALTER TABLE "CloudProviders" ADD COLUMN "fingerprint" TEXT DEFAULT '';
COMMIT;

-- FROM:104,TO:105
BEGIN;
     CREATE TABLE "OIBsInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                             "point_id"  NOT NULL ,
                             "storage_id"  NOT NULL ,
                             "prev_oib_id"  NOT NULL ,
                             "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 ,
                             "approx_size" INTEGER NOT NULL ,
                             "alg" INTEGER NOT NULL ,
                             "vm_id" TEXT NOT NULL ,
                             FOREIGN KEY("point_id") REFERENCES "BackupPoints"("id") ,
                             FOREIGN KEY("storage_id") REFERENCES "BackupStorages"("id") );

    CREATE TABLE "OIBsSensetiveInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                      "oib_id" TEXT NOT NULL ,
                                      "memory_mb" INTEGER NOT NULL ,
                                      "fqdn" TEXT NOT NULL ,
                                      "name" TEXT NOT NULL ,
                                      "guest_info" TEXT NOT NULL ,
                                      "aux_data" TEXT NOT NULL ,
                                      "has_index" BOOL DEFAULT FALSE ,
                                      FOREIGN KEY("oib_id") REFERENCES "OIBsInfo"("id") );

    CREATE TABLE "OIBsEncryptedData" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                      "oib_id" TEXT NOT NULL ,
                                      "encrypted_sensitive_info" BLOB NOT NULL ,
                                      "encrypted_aux_data" BLOB NOT NULL ,
                                      "guest_os_info" BLOB NOT NULL ,
                                      "encrypted_application_info" BLOB NOT NULL ,
                                      "db_key_id" TEXT,
                                      FOREIGN KEY("oib_id") REFERENCES "OIBsInfo"("id") );

    CREATE TABLE "BObjectsInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                 "vm_ref" NOT NULL ,
                                 "host_id" NOT NULL );

    CREATE TABLE "BObjectsSensetiveInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                          "bobjects_id" TEXT NOT NULL,
                                          "display_name" TEXT NOT NULL,
                                          "guest_info_xml" TEXT NOT NULL ,
                                          FOREIGN KEY("bobjects_id") REFERENCES "BObjectsInfo"("id") );

    CREATE TABLE "BObjectsEncryptedData" ("id"  PRIMARY KEY  NOT NULL UNIQUE ,
                                          "bobjects_id" TEXT NOT NULL,
                                          "encrypted_sensitive_info" BLOB NOT NULL,
                                          "encrypted_guest_info" BLOB NOT NULL ,
                                          "encrypted_host_info" BLOB NOT NULL ,
                                          "host_instance_id_hash" TEXT NOT NULL ,
                                          "db_key_id" TEXT,
                                          FOREIGN KEY("bobjects_id") REFERENCES "BObjectsInfo"("id") );

    INSERT INTO "OIBsInfo" SELECT id,point_id,storage_id,prev_oib_id,is_consistent,is_corrupted,inside_dir,creation_time,creation_time_utc,approx_size,alg,vm_id FROM "ObjectsInBackups";
    INSERT INTO "OIBsSensetiveInfo" (id,oib_id,memory_mb,fqdn,name,guest_info,aux_data,has_index) SELECT id,id,memory_mb,fqdn,name,guest_info,aux_data,has_index FROM "ObjectsInBackups";
    INSERT INTO "BObjectsInfo" SELECT id,vm_ref,host_id FROM "BObjects";
    INSERT INTO "BObjectsSensetiveInfo"(id,bobjects_id,display_name,guest_info_xml ) SELECT id,id,display_name,guest_info_xml FROM "BObjects";

    ALTER TABLE "BackupJobs" ADD COLUMN "pwd_key_id" TEXT;

    ALTER TABLE "BackupStorages" ADD COLUMN "storage_key_id" TEXT;
    ALTER TABLE "BackupStorages" ADD COLUMN "meta_key_id" TEXT;

    DROP TABLE "BObjects";
    DROP TABLE "ObjectsInBackups";

COMMIT;

-- FROM:105,TO:106
BEGIN;
    ALTER TABLE "BackupJobs" ADD COLUMN "policy_tag" TEXT DEFAULT '';
COMMIT;

-- FROM:106,TO:107
BEGIN;
    ALTER TABLE "ObjectsInJobs" RENAME TO "JobFilters";
COMMIT;

-- FROM:107,TO:108
BEGIN;
    CREATE TABLE "ObjectInJob" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                "job_id" TEXT NOT NULL ,
                                "bobject_id" TEXT NOT NULL );
COMMIT;

-- FROM:108,TO:109
BEGIN;
    CREATE TABLE "Certificates" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                 "display_name" TEXT ,
                                 "certificate" TEXT NOT NULL,
                                 "private_key" BLOB );
COMMIT;

-- FROM:109,TO:110
BEGIN;
    CREATE TABLE "ConfigurationService" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                         "endpoint" TEXT NOT NULL ,
                                         "creds" TEXT NOT NULL );
COMMIT;

-- FROM:110,TO:111
BEGIN;
    ALTER TABLE "Backups" ADD COLUMN "encryption_state" INTEGER DEFAULT 0;
COMMIT;

-- FROM:111,TO:112
BEGIN;
    CREATE TABLE "ManagementModeSettings" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                           "mode" INTEGER NOT NULL ,
                                           "license" TEXT );

    INSERT OR IGNORE INTO 'ManagementModeSettings' (rowid, id, mode, license) VALUES(1, '{c5fce1ad-a82e-4e68-862f-3ba282f00640}', 0, '');
COMMIT;

-- FROM:112,TO:113
BEGIN;
    ALTER TABLE "BackupStorages" ADD COLUMN "block_align_size" INTEGER DEFAULT 0;
COMMIT;

-- FROM:113,TO:114
BEGIN;
    ALTER TABLE "BackupJobs" ADD COLUMN "policy_job_id" TEXT DEFAULT '{00000000-0000-0000-0000-000000000000}';
COMMIT;

-- FROM:114,TO:115
BEGIN;
    ALTER TABLE "Backups" ADD COLUMN "backup_policy_tag" TEXT DEFAULT '';
COMMIT;

-- FROM:115,TO:116
BEGIN;
    ALTER TABLE "VbrServers" ADD COLUMN "certificate_id" TEXT DEFAULT '{00000000-0000-0000-0000-000000000000}';
COMMIT;

-- FROM:116,TO:117
BEGIN;
    ALTER TABLE "ManagementModeSettings" ADD COLUMN "vbr_server_name" TEXT DEFAULT '';
    ALTER TABLE "ManagementModeSettings" ADD COLUMN "vbr_install_id" TEXT DEFAULT '{00000000-0000-0000-0000-000000000000}';
COMMIT;

-- FROM:117,TO:118
BEGIN;
    CREATE TABLE "BackupPolicies" ("id" NOT NULL UNIQUE,
                                    "tag" NOT NULL  UNIQUE ,
                                    "parentTag" TEXT DEFAULT '',
                                    "displayName" TEXT NOT NULL,
                                    "repositoryId" TEXT NOT NULL,
                                    PRIMARY KEY ("id", "tag"));

    ALTER TABLE "Backups" ADD COLUMN "backup_policy_name" TEXT DEFAULT '';
COMMIT;

-- FROM:118,TO:119
BEGIN;
    ALTER TABLE "BObjectsInfo" ADD COLUMN "parent_id" TEXT DEFAULT '{00000000-0000-0000-0000-000000000000}';
COMMIT;

-- FROM:119,TO:120
BEGIN;
    CREATE TABLE "PoliciesJobsConfig" ("id" NOT NULL UNIQUE,
                                    "policy_id" TEXT NOT NULL,
                                    "hash" BLOB NOT NULL);
COMMIT;

-- FROM:120,TO:121
BEGIN;
    ALTER TABLE "Backups" ADD COLUMN "meta_version" INTEGER NOT NULL DEFAULT -1;
    ALTER TABLE "Backups" ADD COLUMN "meta_update_time" DATETIME NOT NULL DEFAULT '0';
COMMIT;

-- FROM:121,TO:122
BEGIN;
    ALTER TABLE "PoliciesJobsConfig" ADD COLUMN "spec" TEXT DEFAULT '';
COMMIT;

-- FROM:122,TO:123
BEGIN;
    ALTER TABLE "JobSessions" ADD COLUMN "policy_tag" TEXT DEFAULT '';
COMMIT;

-- FROM:123,TO:124
BEGIN;
    ALTER TABLE "OIBsSensetiveInfo" ADD COLUMN has_oracle BOOL DEFAULT FALSE;
COMMIT;

-- FROM:124,TO:125
BEGIN;
    CREATE TABLE "ApplicationJobFilters" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                                   "job_id" TEXT NOT NULL ,
                                                   "type" TEXT NOT NULL ,
                                                   "config" TEXT NOT NULL ,
                                                   "credential_id" TEXT ,
                                                   "hint" TEXT NOT NULL ,
                                                   FOREIGN KEY("job_id") REFERENCES "BackupJobs"("id") );
COMMIT;

-- FROM:125,TO:126
BEGIN;
    DROP TABLE "ConfigurationService";

    CREATE TABLE "ConfigurationService" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                         "endpoints" TEXT NOT NULL ,
                                         "name" TEXT NOT NULL);

COMMIT;

-- FROM:126,TO:127
BEGIN;
    ALTER TABLE "PoliciesJobsConfig" ADD COLUMN "usn" INTEGER DEFAULT 0;
COMMIT;

-- FROM:127,TO:128
BEGIN;
    CREATE TABLE "OracleGuestDatabaseInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                           "obj_id" TEXT NOT NULL ,
                                           "db_id" TEXT NOT NULL,
                                           FOREIGN KEY("obj_id") REFERENCES "BObjectsInfo"("id") );

    CREATE TABLE "OracleGuestDatabaseSensitiveInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                         "oracle_guest_db_id" TEXT NOT NULL ,
                                         "db_name" TEXT NOT NULL ,
                                         "home_name" TEXT NOT NULL ,
                                         "server_name" TEXT NOT NULL ,
                                         "oracle_sid" TEXT NOT NULL ,
                                         "db_global_name" TEXT NOT NULL ,
                                         FOREIGN KEY("oracle_guest_db_id") REFERENCES "OracleGuestDatabaseInfo"("id") );


    CREATE TABLE "OracleGuestDatabaseEncryptedData" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                      "oracle_guest_db_id" TEXT NOT NULL ,
                                      "encrypted_sensitive_info" BLOB NOT NULL ,
                                      "db_key_id" TEXT,
                                      FOREIGN KEY("oracle_guest_db_id") REFERENCES "OracleGuestDatabaseInfo"("id") );
COMMIT;

-- FROM:128,TO:129
BEGIN;
    CREATE TABLE "OibOracleArchiveLogsInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                             "oib_id" TEXT NOT NULL,
                                              FOREIGN KEY("oib_id") REFERENCES "OIBsInfo"("id") );

    CREATE TABLE "OibOracleArchiveLogsSensitiveInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                                      "oracle_archive_log_id" TEXT NOT NULL ,
                                                      "path" TEXT NOT NULL ,
                                                      "size" INTEGER DEFAULT 0,
                                                      "scn_from" TEXT NOT NULL ,
                                                      "scn_to" TEXT NOT NULL ,
                                                      "utc_time_from" DATETIME NOT NULL ,
                                                      "utc_time_to" DATETIME NOT NULL ,
                                                      "seq_num" TEXT NOT NULL ,
                                                      "database_id" TEXT NOT NULL ,
                                                      "db_global_name" TEXT NOT NULL ,
                                                      FOREIGN KEY("oracle_archive_log_id") REFERENCES "OibOracleArchiveLogsInfo"("id") );


    CREATE TABLE "OibOracleArchiveLogsEncryptedData" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                                      "oracle_archive_log_id" TEXT NOT NULL ,
                                                      "encrypted_sensitive_info" BLOB NOT NULL ,
                                                      "db_key_id" TEXT,
                                                      FOREIGN KEY("oracle_archive_log_id") REFERENCES "OibOracleArchiveLogsInfo"("id") );
COMMIT;


-- FROM:129,TO:130
BEGIN;
    CREATE TABLE "Credential" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                               "username" TEXT NOT NULL,
                               "domain" TEXT, 
                               "password" BLOB NOT NULL);
COMMIT;

-- FROM:130,TO:131
BEGIN;
    ALTER TABLE "PoliciesJobsConfig" ADD COLUMN "last_update_time" DATETIME NOT NULL DEFAULT '0';
COMMIT;

-- FROM:131,TO:132
BEGIN;
    ALTER TABLE "ApplicationJobFilters" ADD COLUMN "processing_type" INTEGER DEFAULT 0;
COMMIT;

-- FROM:132,TO:133
BEGIN;
    ALTER TABLE "Backups" ADD COLUMN "enable_deleted_retention" BOOL DEFAULT FALSE;
    ALTER TABLE "Backups" ADD COLUMN "deleted_retention_days_period" INTEGER DEFAULT 0;
COMMIT;

-- FROM:133,TO:135
BEGIN;
    CREATE TABLE "Snapshots" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                               "job_id" TEXT NOT NULL,
                               "oib_id" TEXT NOT NULL,
                               "snapshot_type" TEXT NOT NULL,
                               "spec" TEXT NOT NULL);
COMMIT;

-- FROM:135,TO:136
BEGIN;
    CREATE TABLE "ProductInfo" ("version" PRIMARY KEY  NOT NULL  UNIQUE,
                               "update_time" DATETIME NOT NULL);

    INSERT OR IGNORE INTO 'ProductInfo' (rowid, version, update_time) VALUES(1, "", "0");
COMMIT;

-- FROM:136,TO:137
BEGIN;
    CREATE TABLE "Options" ("id"    PRIMARY KEY NOT NULL UNIQUE,
                            "value" TEXT);
COMMIT;

-- FROM:137,TO:138
BEGIN;
    ALTER TABLE "OIBsInfo" ADD COLUMN "product_version_flags" INTEGER NOT NULL DEFAULT 3;
COMMIT;

-- FROM:138,TO:139
BEGIN;
    ALTER TABLE "OIBsInfo" ADD COLUMN "product_is_rental_license" BOOL DEFAULT NULL;
COMMIT;

-- FROM:139,TO:140
BEGIN;
    ALTER TABLE "JobSessions" ADD COLUMN "bobject_id" TEXT DEFAULT '';
COMMIT;

-- FROM:140,TO:141
BEGIN;
    ALTER TABLE "PoliciesJobsConfig" ADD COLUMN "applied_hash" BLOB;
COMMIT;

-- FROM:141,TO:142
BEGIN;
    ALTER TABLE "PoliciesJobsConfig" ADD COLUMN "bobject_id" TEXT DEFAULT '';
COMMIT;

-- FROM:142,TO:143
BEGIN;
    ALTER TABLE "BackupRepositories" ADD COLUMN "is_available" BOOL DEFAULT TRUE;
COMMIT;

-- FROM:143,TO:144
BEGIN;
    CREATE TABLE "Licenses" ("id" PRIMARY KEY  NOT NULL  UNIQUE,
                             "data" TEXT NOT NULL,
                             "source_type" INTEGER DEFAULT 0,
                             "owner_id" TEXT);

    INSERT OR IGNORE INTO Licenses (id,data,source_type,owner_id) VALUES (
                                                                '{f7251018-1c70-4e3b-a94a-58daffed1507}',
                                                                (SELECT license FROM "VbrServers" WHERE onlyForRestore=0 AND license!=""),
                                                                2,
                                                                (SELECT id FROM "VbrServers" WHERE onlyForRestore=0 AND license!="")
                                                                );

    INSERT OR IGNORE INTO Licenses (id,data,source_type,owner_id) VALUES (
                                                                '{5bdcf8f5-ad08-4ca5-8061-db43d4554a16}',
                                                                (SELECT license FROM "ManagementModeSettings" WHERE mode!=0 AND license!=""),
                                                                3,
                                                                (SELECT id FROM "ManagementModeSettings" WHERE mode!=0 AND license!="")
                                                                );

    CREATE TEMPORARY TABLE VbrServers_tmp(id,name,login,password,domain,fqdn,onlyForRestore,endpoint,certificate_id);
    INSERT INTO VbrServers_tmp SELECT id,name,login,password,domain,fqdn,onlyForRestore,endpoint,certificate_id FROM VbrServers;
    DROP TABLE VbrServers;
    CREATE TABLE VbrServers(id,name,login,password,domain,fqdn,onlyForRestore,endpoint,certificate_id);
    INSERT INTO VbrServers SELECT id,name,login,password,domain,fqdn,onlyForRestore,endpoint,certificate_id FROM VbrServers_tmp;
    DROP TABLE VbrServers_tmp;

    CREATE TEMPORARY TABLE ManagementModeSettings_tmp(id,mode,vbr_server_name,vbr_install_id);
    INSERT INTO ManagementModeSettings_tmp SELECT id,mode,vbr_server_name,vbr_install_id FROM ManagementModeSettings;
    DROP TABLE ManagementModeSettings;
    CREATE TABLE ManagementModeSettings(id,mode,vbr_server_name,vbr_install_id);
    INSERT INTO ManagementModeSettings SELECT id,mode,vbr_server_name,vbr_install_id FROM ManagementModeSettings_tmp;
    DROP TABLE ManagementModeSettings_tmp;
COMMIT;

-- FROM:144,TO:145
BEGIN;
    ALTER TABLE "OIBsInfo" ADD COLUMN "need_healthcheck_repair" BOOL NOT NULL DEFAULT FALSE;
COMMIT;

-- FROM:145,TO:146
BEGIN;
    CREATE TABLE Ownership ("id" PRIMARY KEY  NOT NULL  UNIQUE,
                            "type" INTEGER DEFAULT 0,
                            "install_id" TEXT DEFAULT '{00000000-0000-0000-0000-000000000000}',
                            "friendly_name" TEXT DEFAULT '',
                            "mode" TEXT DEFAULT 'NotSet');

    INSERT INTO "Ownership" (id,type,install_id,friendly_name,mode) SELECT id,mode,vbr_install_id,vbr_server_name,mode FROM "ManagementModeSettings";
    UPDATE 'Ownership' SET type=0, mode='NotSet' WHERE type=0;
    UPDATE 'Ownership' SET type=1, mode='VBR.Job' WHERE type=1;
    UPDATE 'Ownership' SET type=1, mode='VBR.Policy' WHERE type=2;

    DROP TABLE ManagementModeSettings;
COMMIT;

-- FROM:146,TO:147
BEGIN;
    ALTER TABLE "Backups" ADD COLUMN backup_group_id TEXT DEFAULT '';
COMMIT;

-- FROM:147,TO:148
BEGIN;
    ALTER TABLE "OibOracleArchiveLogsSensitiveInfo" ADD COLUMN "reset_logs_change" TEXT DEFAULT '';
COMMIT;

-- FROM:148,TO:149
BEGIN;
    ALTER TABLE "OIBsInfo" ADD COLUMN "completion_time_utc" DATETIME DEFAULT 0;
COMMIT;

-- FROM:149,TO:150
BEGIN;
    CREATE TABLE VmcFeatureUsage ("feature_name" PRIMARY KEY  NOT NULL  UNIQUE,
                                  "usage_count" INTEGER DEFAULT 0,
                                  "last_update" DATETIME NOT NULL);
COMMIT;

-- FROM:150,TO:151
BEGIN;
	CREATE TABLE VacJobPolicies ("id" PRIMARY KEY NOT NULL UNIQUE,
								 "job_id" NOT NULL,
								 "user_data" TEXT DEFAULT '',
                                 FOREIGN KEY("job_id") REFERENCES "BackupJobs"("id"));
COMMIT;

-- FROM:151,TO:152
BEGIN;
    ALTER TABLE "CryptoKeys" ADD COLUMN "format_version" INTEGER DEFAULT 0;
COMMIT;


-- FROM:152,TO:153
BEGIN;
    ALTER TABLE "JobSessions" ADD COLUMN "point_id" TEXT DEFAULT '{00000000-0000-0000-0000-000000000000}';
COMMIT;

-- FROM:153,TO:154
BEGIN;
    ALTER TABLE "OIBsInfo" ADD COLUMN "is_recheck_corrupted" BOOL NOT NULL DEFAULT FALSE;
COMMIT;

-- FROM:154,TO:155
BEGIN;
    ALTER TABLE "BackupRepositories" ADD COLUMN "tag" TEXT DEFAULT '';
COMMIT;

-- FROM:155,TO:156
BEGIN;
    ALTER TABLE "CloudProviders" ADD COLUMN "password_type" INTEGER DEFAULT 0;
COMMIT;

-- FROM:156,TO:157
BEGIN;
    ALTER TABLE "BackupPolicies" ADD COLUMN "description" TEXT DEFAULT '';
COMMIT;

-- FROM:157,TO:158
BEGIN;
    ALTER TABLE "PoliciesJobsConfig" ADD COLUMN "remote_scripts_spec" BLOB DEFAULT '';
COMMIT;

-- FROM:158,TO:159
BEGIN;
    CREATE TABLE "PostgreSqlGuestInstanceInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                           "obj_id" TEXT NOT NULL ,
                                           "instance_id" INTEGER NOT NULL);

    CREATE TABLE "PostgreSqlGuestInstanceSensitiveInfo" ("id"  PRIMARY KEY  NOT NULL UNIQUE,
                                           "postgresql_instance_info_id" TEXT NOT NULL,
                                           "server_name" TEXT NOT NULL,
                                           "instance_name" TEXT NOT NULL,
                                           "wal_segment_size" INTEGER NOT NULL,
                                           FOREIGN KEY("postgresql_instance_info_id") REFERENCES "PostgreSqlGuestInstanceInfo"("id") );

    CREATE TABLE "PostgreSqlGuestInstanceEncryptedData" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                           "postgresql_instance_info_id" TEXT NOT NULL ,
                                           "encrypted_sensitive_info" BLOB NOT NULL ,
                                           "db_key_id" TEXT,
                                           FOREIGN KEY("postgresql_instance_info_id") REFERENCES "PostgreSqlGuestInstanceInfo"("id") );

    ALTER TABLE "OIBsSensetiveInfo" ADD COLUMN has_postgresql BOOL DEFAULT FALSE;
COMMIT;

-- FROM:159,TO:160
BEGIN;
    CREATE TABLE "PostgreSqlGuestDatabaseInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                           "obj_id" TEXT NOT NULL ,
                                           "instance_id" TEXT NOT NULL ,
                                           "db_id" TEXT NOT NULL);

    CREATE TABLE "PostgreSqlGuestDatabaseSensitiveInfo" ("id"  PRIMARY KEY  NOT NULL UNIQUE,
                                           "postgresql_database_info_id" TEXT NOT NULL,
                                           "last_modified_time" TEXT NOT NULL,
                                           "name" TEXT NOT NULL,
                                           "owner" TEXT NOT NULL,
                                           "table_space" TEXT NOT NULL,
                                           "is_read_only" BOOL NOT NULL,
                                           FOREIGN KEY("postgresql_database_info_id") REFERENCES "PostgreSqlGuestDatabaseInfo"("id") );

    CREATE TABLE "PostgreSqlGuestDatabaseEncryptedData" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                           "postgresql_database_info_id" TEXT NOT NULL ,
                                           "encrypted_sensitive_info" BLOB NOT NULL ,
                                           "db_key_id" TEXT,
                                           FOREIGN KEY("postgresql_database_info_id") REFERENCES "PostgreSqlGuestDatabaseInfo"("id") );
COMMIT;

-- FROM:160,TO:161
BEGIN;
    ALTER TABLE "PostgreSqlGuestDatabaseSensitiveInfo" ADD COLUMN table_space_id TEXT;
    ALTER TABLE "PostgreSqlGuestDatabaseSensitiveInfo" ADD COLUMN table_space_path TEXT;
COMMIT;

-- FROM:161,TO:162
BEGIN;
    CREATE TABLE "ExternalRepoCloudContainer" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                               "containerName" TEXT NOT NULL ,
                                               "other" TEXT);
COMMIT;

-- FROM:162,TO:163
BEGIN;
    ALTER TABLE "VbrServers" ADD COLUMN "recovery_auth_token_Id" TEXT DEFAULT '{00000000-0000-0000-0000-000000000000}';
COMMIT;

-- FROM:163,TO:164
BEGIN;
    CREATE TABLE "PostgreSqlGuestTablespaceInfo" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                           "obj_id" TEXT NOT NULL ,
                                           "instance_id" TEXT NOT NULL ,
                                           "tablespace_id" TEXT NOT NULL);

    CREATE TABLE "PostgreSqlGuestTablespaceSensitiveInfo" ("id"  PRIMARY KEY  NOT NULL UNIQUE,
                                           "postgresql_tablespace_info_id" TEXT NOT NULL,
                                           "name" TEXT NOT NULL,
                                           "path" TEXT NOT NULL,
                                           FOREIGN KEY("postgresql_tablespace_info_id") REFERENCES "PostgreSqlGuestTablespaceInfo"("id") );

    CREATE TABLE "PostgreSqlGuestTablespaceEncryptedData" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                           "postgresql_tablespace_info_id" TEXT NOT NULL ,
                                           "encrypted_sensitive_info" BLOB NOT NULL ,
                                           "db_key_id" TEXT,
                                           FOREIGN KEY("postgresql_tablespace_info_id") REFERENCES "PostgreSqlGuestTablespaceInfo"("id") );
COMMIT;

-- FROM:164,TO:165
BEGIN;
    CREATE TABLE "VbrRecoveryAuthToken" ("id" PRIMARY KEY NOT NULL UNIQUE,
                                         "server_certificate" TEXT NOT NULL,
                                         "token" TEXT NOT NULL);
COMMIT;

-- FROM:165,TO:166
BEGIN;
    CREATE TABLE "Credential_tmp" ("id"  PRIMARY KEY  NOT NULL  UNIQUE ,
                                  "type" INTEGER DEFAULT 0,
                                  "username" TEXT NOT NULL,
                                  "domain" TEXT, 
                                  "password" BLOB NOT NULL,
                                  "is_temp_shared" BOOL DEFAULT FALSE);

    INSERT INTO "Credential_tmp" (id, type, username, domain, password) SELECT id, 1, username, domain, password FROM "Credential";
    DROP TABLE "Credential";
    ALTER TABLE "Credential_tmp"
        RENAME TO "Credential";
COMMIT;

-- FROM:166,TO:167
BEGIN;
    ALTER TABLE "BackupStorages" ADD COLUMN "gfs_period" INTEGER DEFAULT 0;
COMMIT;

-- FROM:167,TO:168
BEGIN;
	CREATE TABLE "DirectBackupStorageInfos" ("storage_id" PRIMARY KEY NOT NULL UNIQUE,
										 "cloud_storage_id" TEXT NOT NULL);
COMMIT;

-- FROM:168,TO:169
BEGIN;
CREATE TABLE "DirectBackupInfos" ("backup_id" PRIMARY KEY NOT NULL UNIQUE,
                                  "cloud_backup_id" TEXT NOT NULL);
COMMIT;

-- FROM:169,TO:170
BEGIN;
    ALTER TABLE "DirectBackupInfos" ADD COLUMN "client_id" TEXT DEFAULT '{00000000-0000-0000-0000-000000000000}';
COMMIT;

-- FROM:170,TO:171
BEGIN;
    ALTER TABLE "VbrServers" ADD COLUMN "cloud_msg_settings" TEXT DEFAULT '';
COMMIT;

-- FROM:171,TO:172
BEGIN;
    ALTER TABLE "PostgreSqlGuestInstanceInfo" ADD COLUMN "data_directory_hash" TEXT DEFAULT '';
COMMIT;

-- FROM:172,TO:173
BEGIN;
    CREATE TABLE "PostgreSqlInstanceToOibLinks" (
        "id"  PRIMARY KEY  NOT NULL  UNIQUE ,
        "oib_id" TEXT NOT NULL ,
        "instance_id" TEXT NOT NULL,
        FOREIGN KEY("instance_id") REFERENCES "PostgreSqlGuestInstanceInfo"("id"),
        FOREIGN KEY("oib_id") REFERENCES "OIBsInfo"("id"));
COMMIT;

-- FROM:173,TO:174
BEGIN;
    CREATE TABLE "RepositoryCryptoKeys" (
        "repo_id" TEXT NOT NULL ,
        "job_id" TEXT NOT NULL ,
        "crypto_key_id" TEXT NOT NULL,
        FOREIGN KEY("repo_id") REFERENCES "BackupRepositories"("id"),
        FOREIGN KEY("job_id") REFERENCES "BackupJobs"("id"));
COMMIT;

-- FROM:174,TO:175
BEGIN;
    ALTER TABLE "DirectBackupInfos" ADD COLUMN "generation_expiration_date" DATETIME DEFAULT 0;
    ALTER TABLE "DirectBackupInfos" ADD COLUMN "generation_immutable_till" DATETIME DEFAULT 0;
COMMIT;

-- FROM:175,TO:176
BEGIN;
    ALTER TABLE "DirectBackupInfos" ADD COLUMN "latest_checkpoint" INTEGER DEFAULT 0;
COMMIT;

-- FROM:176,TO:177
BEGIN;
    ALTER TABLE "BackupStorages" ADD COLUMN "last_recheck_time_utc" DATETIME DEFAULT 0;
COMMIT;

-- FROM:177,TO:178
BEGIN;
    ALTER TABLE "OIBsInfo" ADD COLUMN "health_status" INTEGER NOT NULL DEFAULT 0;
COMMIT;

-- FROM:178,TO:179
BEGIN;

    CREATE TABLE TempTable (
        "id"  PRIMARY KEY  NOT NULL  UNIQUE ,
        "job_id" ,
        "job_name" TEXT ,
        "type" INTEGER ,
        "state" INTEGER ,
        "state_changed_utc" DATETIME ,
        "log_text" ,
        "logs_dir" TEXT ,
        "progress" INTEGER ,
        "progress_details" TEXT ,
        "details" TEXT ,
        "retried_session_id" TEXT,
        "usn" INTEGER DEFAULT 0 ,
        "policy_tag" TEXT ,
        "bobject_id" TEXT ,
        "point_id" TEXT ,
        "creation_time_utc" DATETIME ,
        "start_time_utc" DATETIME ,
        "end_time_utc" DATETIME
    );

    INSERT INTO TempTable(
        "id", "job_id", "job_name", "type", "state", "state_changed_utc", "log_text", "logs_dir", "progress", "progress_details", "details", "retried_session_id", "usn", "policy_tag", "bobject_id", "point_id", "creation_time_utc", "start_time_utc", "end_time_utc")
    SELECT
        "id", "job_id", "job_name", "type", "state", "state_changed",     "log_text", "logs_dir", "progress", "progress_details", "details", "retried_session_id", "usn", "policy_tag", "bobject_id", "point_id", "start_time",        "start_time",     "end_time"
    FROM JobSessions;

    DROP TABLE JobSessions;
    ALTER TABLE TempTable RENAME TO JobSessions;

    CREATE TRIGGER SessionsUpdUsn_AfterIns_Trigger AFTER INSERT
    ON 'JobSessions' FOR EACH ROW
    BEGIN
        INSERT OR IGNORE INTO 'SessionsUsn' (rowid, value) VALUES(1, 0);
        UPDATE 'SessionsUsn' SET Value=Value+1;
        UPDATE 'JobSessions' SET Usn=(SELECT Value FROM 'SessionsUsn' ORDER BY ROWID ASC LIMIT 1) WHERE ID=NEW.id;
    END;

    CREATE TRIGGER SessionsUpdUsn_AfterUpd_Trigger AFTER UPDATE
    ON 'JobSessions' FOR EACH ROW
        WHEN (NEW.Usn = Old.Usn)
    BEGIN
        INSERT OR IGNORE INTO 'SessionsUsn' (rowid, value) VALUES(1, 0);
        UPDATE 'SessionsUsn' SET Value=Value+1;
        UPDATE 'JobSessions' SET Usn=(SELECT Value FROM 'SessionsUsn' ORDER BY ROWID ASC LIMIT 1) WHERE ID=NEW.id;
    END;

COMMIT;

-- FROM:179,TO:180
BEGIN;
    ALTER TABLE "BackupJobs" ADD COLUMN "backup_state" TEXT;
COMMIT;

-- FROM:180,TO:181
BEGIN;
CREATE TABLE "CryptoKeysSalt" (
                                        "key_set_id" BLOB PRIMARY KEY NOT NULL UNIQUE,
                                        "salt" BLOB NOT NULL ,
                                        "key_set" BLOB NOT NULL);
COMMIT;

-- FROM:181,TO:182
BEGIN;
    ALTER TABLE "PostgreSqlGuestDatabaseSensitiveInfo" ADD COLUMN "size" TEXT;
COMMIT;

-- FROM:182,TO:183
BEGIN;
    ALTER TABLE "Credential" ADD COLUMN "last_update_time_utc" DATETIME DEFAULT 0;
COMMIT;

-- FROM:183,TO:184
BEGIN;
    ALTER TABLE "BackupStorages" ADD COLUMN "immutable_till_utc" DATETIME DEFAULT 0;
COMMIT;

-- FROM:184,TO:185
BEGIN;
    ALTER TABLE "DirectBackupInfos" ADD COLUMN "enabled_backup_features" TEXT;
COMMIT;

-- FROM:185,TO:186
BEGIN;

    CREATE TABLE TempTable ("id" PRIMARY KEY NOT NULL UNIQUE,
                                    "policy_id" TEXT NOT NULL,
                                    "hash" BLOB NOT NULL,
                                    "spec" TEXT DEFAULT '',
                                    "usn" INTEGER DEFAULT 0,
                                    "last_update_time" DATETIME NOT NULL DEFAULT '0',
                                    "applied_hash" BLOB,
                                    "bobject_id" TEXT DEFAULT '',
                                    "remote_scripts_spec" BLOB DEFAULT ''
                                    );

    INSERT INTO TempTable("id", "policy_id", "hash", "spec", "usn", "last_update_time", "applied_hash", "bobject_id", "remote_scripts_spec")
    SELECT "id", "policy_id", "hash", "spec", "usn", "last_update_time", "applied_hash", "bobject_id", "remote_scripts_spec" FROM PoliciesJobsConfig;

    DROP TABLE PoliciesJobsConfig;
    ALTER TABLE TempTable RENAME TO PoliciesJobsConfig;

--

    CREATE TABLE TempTable ("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 ,
                           "certificate_id" TEXT ,
                           "recovery_auth_token_Id" TEXT DEFAULT '{00000000-0000-0000-0000-000000000000}' ,
                           "cloud_msg_settings" TEXT DEFAULT '');
                           
    INSERT INTO TempTable(id,name,login,password,domain,fqdn,onlyForRestore,endpoint,certificate_id,recovery_auth_token_Id,cloud_msg_settings)
    SELECT id,name,login,password,domain,fqdn,onlyForRestore,endpoint,certificate_id,recovery_auth_token_Id,cloud_msg_settings FROM VbrServers;
    
    DROP TABLE VbrServers;
    ALTER TABLE TempTable RENAME TO VbrServers;

--

    CREATE TABLE TempTable (
        "repo_id" TEXT NOT NULL ,
        "job_id" TEXT NOT NULL ,
        "crypto_key_id" TEXT NOT NULL,
        FOREIGN KEY("repo_id") REFERENCES "BackupRepositories"("id"),
        FOREIGN KEY("job_id") REFERENCES "BackupJobs"("id"),
        PRIMARY KEY(repo_id, job_id)
        );

    INSERT INTO TempTable(repo_id, job_id, crypto_key_id)
    SELECT repo_id, job_id, crypto_key_id FROM RepositoryCryptoKeys;
    
    DROP TABLE RepositoryCryptoKeys;
    ALTER TABLE TempTable RENAME TO RepositoryCryptoKeys;

COMMIT;

-- FROM:186,TO:187
BEGIN;
    ALTER TABLE "VbrServers" ADD COLUMN "server_certificate_fingerprint" TEXT DEFAULT '';
COMMIT;

-- FROM:187,TO:188
BEGIN;
    DROP TABLE "VbrRecoveryAuthToken";

    CREATE TABLE TempTable ("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 ,
                           "certificate_id" TEXT ,
                           "cloud_msg_settings" TEXT DEFAULT '' ,
                           "server_certificate_fingerprint" TEXT DEFAULT '');

    INSERT INTO TempTable(id,name,login,password,domain,fqdn,onlyForRestore,endpoint,certificate_id,cloud_msg_settings,server_certificate_fingerprint)
    SELECT id,name,login,password,domain,fqdn,onlyForRestore,endpoint,certificate_id,cloud_msg_settings,server_certificate_fingerprint FROM VbrServers;

    DROP TABLE VbrServers;
    ALTER TABLE TempTable RENAME TO VbrServers;

    ALTER TABLE "VbrServers" ADD COLUMN "creds_type" INTEGER DEFAULT 0;
COMMIT;
