ALTER DEFAULT PRIVILEGES FOR ROLE migrate
IN SCHEMA public
GRANT SELECT ON TABLES TO app_base;

ALTER DEFAULT PRIVILEGES FOR ROLE migrate
IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO app_base;

-- create "abwesenheit" table
CREATE TABLE "abwesenheit" (
  "counter_id" bigserial NOT NULL,
  "card_uid" character varying(255) NULL,
  "abwesenheit_typ" smallint NULL,
  "datum" timestamptz NULL DEFAULT (now())::date,
  PRIMARY KEY ("counter_id")
);

-- create "anwesenheit" table
CREATE TABLE "anwesenheit" (
  "counter_id" bigserial NOT NULL,
  "timestamp" timestamptz NULL DEFAULT CURRENT_TIMESTAMP,
  "card_uid" character varying(255) NULL,
  "check_in_out" smallint NULL,
  "geraet_id" smallint NULL,
  PRIMARY KEY ("counter_id")
);
-- set comment to column: "check_in_out" on table: "anwesenheit"
COMMENT ON COLUMN "anwesenheit"."check_in_out" IS '1=Check In 2=Check Out , 3=Check in Manuell, 4=Check out manuell255=Automatic Check Out';
-- set comment to column: "geraet_id" on table: "anwesenheit"
COMMENT ON COLUMN "anwesenheit"."geraet_id" IS 'ID des Lesegerätes';
-- create "personal_daten" table
CREATE TABLE "personal_daten" (
  "personal_nummer" integer NOT NULL,
  "aktiv_beschaeftigt" boolean NULL,
  "vorname" character varying(255) NULL,
  "nachname" character varying(255) NULL,
  "geburtsdatum" date NULL,
  "plz" character varying(255) NULL,
  "adresse" character varying(255) NULL,
  "geschlecht" smallint NULL,
  "card_uid" character varying(255) NULL,
  "hauptbeschaeftigungs_ort" smallint NULL,
  "arbeitszeit_per_tag" real NULL,
  "arbeitszeit_min_start" time NULL,
  "arbeitszeit_max_ende" time NULL,
  "vorgesetzter_pers_nr" integer NULL,
  PRIMARY KEY ("personal_nummer")
);
-- set comment to column: "geschlecht" on table: "personal_daten"
COMMENT ON COLUMN "personal_daten"."geschlecht" IS '1==weiblich, 2==maennlich, 3==divers';
-- create "user_password" table
CREATE TABLE "user_password" (
  "personal_nummer" integer NOT NULL,
  "pass_hash" text NULL,
  "zuletzt_geandert" timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY ("personal_nummer")
);
-- create "wochen_report" table
CREATE TABLE "wochen_report" (
  "id" serial NOT NULL,
  "personal_nummer" integer NULL,
  "woche_start" date NULL,
  "bestaetigt" boolean NULL DEFAULT false,
  PRIMARY KEY ("id"),
  CONSTRAINT "wochen_report_personal_nummer_woche_start_key" UNIQUE ("personal_nummer", "woche_start")
);

GRANT INSERT, UPDATE ON abwesenheit, anwesenheit, wochen_report, user_password TO app_base;
GRANT DELETE ON abwesenheit to app_base;
