
CREATE TABLE "cd_project"
(
  "id"          integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  "user_id"     integer      NOT NULL,
  "name"        varchar(512) NOT NULL,
  "admin_id"    integer      NOT NULL,
  "disabled"    boolean      NOT NULL DEFAULT (false),
  "create_time" datetime     NOT NULL DEFAULT (CURRENT_TIMESTAMP),
  "update_time" datetime     NOT NULL DEFAULT (CURRENT_TIMESTAMP)
);


CREATE INDEX "index_project_user_id" ON "cd_project" ("user_id");
CREATE INDEX "index_project_admin_id" ON "cd_project" ("admin_id");
INSERT INTO cd_project (id, user_id, "admin_id", "name", "disabled") VALUES (1, 0, 1,'default', false);

ALTER TABLE cd_cert_info ADD COLUMN  project_id integer;
CREATE INDEX "index_cert_project_id" ON "cd_cert_info" ("project_id");

ALTER TABLE cd_site_info ADD COLUMN  project_id integer;
CREATE INDEX "index_site_project_id" ON "cd_site_info" ("project_id");

ALTER TABLE cd_site_ip ADD COLUMN  project_id integer;
CREATE INDEX "index_site_ip_project_id" ON "cd_site_ip" ("project_id");

ALTER TABLE cd_open_key ADD COLUMN  project_id integer;
CREATE INDEX "index_open_key_project_id" ON "cd_open_key" ("project_id");

ALTER TABLE cd_access ADD COLUMN  project_id integer;
CREATE INDEX "index_access_project_id" ON "cd_access" ("project_id");

ALTER TABLE cd_addon ADD COLUMN  project_id integer;
CREATE INDEX "index_addon_project_id" ON "cd_addon" ("project_id");

ALTER TABLE pi_pipeline ADD COLUMN  project_id integer;
CREATE INDEX "index_pipeline_project_id" ON "pi_pipeline" ("project_id");

ALTER TABLE pi_pipeline_group ADD COLUMN  project_id integer;
CREATE INDEX "index_pipeline_group_project_id" ON "pi_pipeline_group" ("project_id");

ALTER TABLE pi_storage ADD COLUMN  project_id integer;
CREATE INDEX "index_storage_project_id" ON "pi_storage" ("project_id");

ALTER TABLE pi_notification ADD COLUMN  project_id integer;
CREATE INDEX "index_notification_project_id" ON "pi_notification" ("project_id");

ALTER TABLE pi_history ADD COLUMN  project_id integer;
CREATE INDEX "index_history_project_id" ON "pi_history" ("project_id");

ALTER TABLE pi_history_log ADD COLUMN  project_id integer;
CREATE INDEX "index_history_log_project_id" ON "pi_history_log" ("project_id");

ALTER TABLE pi_template ADD COLUMN  project_id integer;
CREATE INDEX "index_template_project_id" ON "pi_template" ("project_id");

ALTER TABLE pi_sub_domain ADD COLUMN  project_id integer;
CREATE INDEX "index_sub_domain_project_id" ON "pi_sub_domain" ("project_id");

ALTER TABLE cd_cname_record ADD COLUMN  project_id integer;
CREATE INDEX "index_cname_record_project_id" ON "cd_cname_record" ("project_id");

ALTER TABLE cd_domain ADD COLUMN  project_id integer;
CREATE INDEX "index_domain_project_id" ON "cd_domain" ("project_id");

ALTER TABLE user_settings ADD COLUMN  project_id integer;
CREATE INDEX "index_user_settings_project_id" ON "user_settings" ("project_id");




CREATE TABLE "cd_project_member"
(
  "id"          integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  "user_id"     integer      NOT NULL,
  "project_id"  integer      NOT NULL,
  "permission"  varchar(128) NOT NULL DEFAULT ('read'),
  "create_time" datetime     NOT NULL DEFAULT (CURRENT_TIMESTAMP),
  "update_time" datetime     NOT NULL DEFAULT (CURRENT_TIMESTAMP)
);


CREATE INDEX "index_project_member_user_id" ON "cd_project_member" ("user_id");
CREATE INDEX "index_project_member_project_id" ON "cd_project_member" ("project_id");


CREATE TABLE "cd_audit_log"
(
  "id"          integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  "user_id"     integer      NOT NULL,
  "username"    varchar(128) NOT NULL,
  "project_id"  integer      NOT NULL,
  "project_name" varchar(512) NOT NULL,
  "type"        varchar(128) NOT NULL,
  "action"      varchar(128) NOT NULL DEFAULT ('read'),
  "content"     text         NOT NULL,
  "ip_address"  varchar(128) NOT NULL,
  "create_time" datetime     NOT NULL DEFAULT (CURRENT_TIMESTAMP),
  "update_time" datetime     NOT NULL DEFAULT (CURRENT_TIMESTAMP)
);


CREATE INDEX "index_audit_log_user_id" ON "cd_audit_log" ("user_id");
CREATE INDEX "index_audit_log_project_id" ON "cd_audit_log" ("project_id");


