-- SQL script to create tables
-- To run: sqlite3 dqc_workflow.db < create_database.sql

-- Main database table
CREATE TABLE dataset (
	"_datasetId" TEXT PRIMARY KEY,
	"datasetGroup" TEXT,
	"lastUpdated" TEXT
);

-- Stores all files for a dataset X (normally a file per month)
CREATE TABLE file (
	"_fname" TEXT,
	"downRequired" INTEGER,
	"downPerformed" INTEGER,
	-- Foreign keys
	"_ds" TEXT,
	FOREIGN KEY (_ds) REFERENCES dataset(_datasetId),
	PRIMARY KEY (_fname, _ds)
);

-- Each file goes through D datachecks (standard_compliance, spatial_completeness, ... , valid_ranges)
CREATE TABLE filedatacheck (
	"_checkId" TEXT,
	"checkResult" INTEGER DEFAULT 0,
	"checkMsg"	INTEGER,
	-- Foreign keys
	"_ds" TEXT,
	"_fn" TEXT,
	FOREIGN KEY (_ds) REFERENCES dataset(_datasetId),
	FOREIGN KEY (_fn) REFERENCES file(_fname),
	PRIMARY KEY (_ds, _fn, _checkId)
);

-- Each file has T timestamps (this table is filled and used by the temporal_completeness and temporal_consistency checks)
CREATE TABLE filedatetime (
	"_timestamp" TEXT,
	-- Foreign keys
	"_ds" TEXT,
	"_fn" TEXT,
	FOREIGN KEY (_ds) REFERENCES dataset(_datasetId),
	FOREIGN KEY (_fn) REFERENCES file(_fname),
	PRIMARY KEY (_ds, _fn, _timestamp)
);

