CREATE TABLE IF NOT EXISTS "user" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" text NOT NULL UNIQUE, "pw" text, "is_cox" boolean NOT NULL DEFAULT FALSE, "is_admin" boolean NOT NULL DEFAULT FALSE, "is_guest" boolean NOT NULL DEFAULT TRUE, "deleted" boolean NOT NULL DEFAULT FALSE, "last_access" DATETIME ); CREATE TABLE IF NOT EXISTS "trip_type" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" text NOT NULL UNIQUE, "desc" text NOT NULL, "question" text NOT NULL, "icon" text NOT NULL ); CREATE TABLE IF NOT EXISTS "trip_details" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "planned_starting_time" text NOT NULL, "max_people" INTEGER NOT NULL, "day" TEXT NOT NULL, "allow_guests" boolean NOT NULL default false, "notes" TEXT, "trip_type_id" INTEGER, FOREIGN KEY(trip_type_id) REFERENCES trip_type(id) ); CREATE TABLE IF NOT EXISTS "planned_event" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" text NOT NULL, "planned_amount_cox" INTEGER unsigned NOT NULL, "trip_details_id" INTEGER NOT NULL, "created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, "always_show" boolean NOT NULL default false, FOREIGN KEY(trip_details_id) REFERENCES trip_details(id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS "trip" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "cox_id" INTEGER NOT NULL, "trip_details_id" INTEGER, "planned_event_id" INTEGER, "created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY(cox_id) REFERENCES user(id), FOREIGN KEY(trip_details_id) REFERENCES trip_details(id) ON DELETE CASCADE, FOREIGN KEY(planned_event_id) REFERENCES planned_event(id) ON DELETE CASCADE, CONSTRAINT unq UNIQUE (cox_id, planned_event_id) -- allow cox to participate only once for each planned event ); CREATE TABLE IF NOT EXISTS "user_trip" ( "user_id" INTEGER NOT NULL, "trip_details_id" INTEGER NOT NULL, "created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY(user_id) REFERENCES user(id), FOREIGN KEY(trip_details_id) REFERENCES trip_details(id), CONSTRAINT unq UNIQUE (user_id, trip_details_id) -- allow user to participate only once for each trip ); CREATE TABLE IF NOT EXISTS "log" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "msg" text NOT NULL, "created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP );