-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION konfigraf" to load this file. \quit
CREATE OR REPLACE FUNCTION create_repository(repoName text)
RETURNS bigint AS
'$libdir/konfigraf', 'CreateRepository'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION create_repository(text) IS 'Creates a new repository and returns the repository ID.
';

CREATE OR REPLACE FUNCTION delete_repository(repoName text)
RETURNS VOID AS
'$libdir/konfigraf', 'DeleteRepository'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION delete_repository(text) IS 'Deletes the specified repository.
';

CREATE OR REPLACE FUNCTION create_branch(repoName text,source text,newBranch text)
RETURNS VOID AS
'$libdir/konfigraf', 'CreateBranch'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION create_branch(text,text,text) IS 'Creates a new branch
';

CREATE OR REPLACE FUNCTION delete_branch(repoName text,branch text)
RETURNS VOID AS
'$libdir/konfigraf', 'DeleteBranch'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION delete_branch(text,text) IS 'Removes an existing branch
';

CREATE OR REPLACE FUNCTION commit_file(repoName text,path text,content text,author text,message text,email text)
RETURNS text[] AS
'$libdir/konfigraf', 'CommitFile'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION commit_file(text,text,text,text,text,text) IS 'Commits a file on the master branch
';

CREATE OR REPLACE FUNCTION commit_branch_file(repoName text,branch text,path text,content text,author text,message text,email text)
RETURNS text[] AS
'$libdir/konfigraf', 'CommitBranchFile'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION commit_branch_file(text,text,text,text,text,text,text) IS 'Commits a file on a specific branch
';

CREATE OR REPLACE FUNCTION get_file(repoName text,path text)
RETURNS text AS
'$libdir/konfigraf', 'GetFile'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION get_file(text,text) IS 'Gets a file from a repository at the specified path of the master branch.
';

CREATE OR REPLACE FUNCTION get_branch_file(repoName text,branch text,path text)
RETURNS text AS
'$libdir/konfigraf', 'GetBranchFile'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION get_branch_file(text,text,text) IS 'Gets a file from a repository at the specified path of the master branch.
';

CREATE OR REPLACE FUNCTION list_files(repoName text,path text)
RETURNS text[] AS
'$libdir/konfigraf', 'ListFiles'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION list_files(text,text) IS 'Lists files from a specific path of the master branch
';

CREATE OR REPLACE FUNCTION list_branch_files(repoName text,branch text,path text)
RETURNS text[] AS
'$libdir/konfigraf', 'ListBranchFiles'
LANGUAGE c VOLATILE STRICT;
COMMENT ON FUNCTION list_branch_files(text,text,text) IS 'Lists files from a specific path of the specified branch
';

CREATE OR REPLACE FUNCTION get_log(repoName text,since timestamptz,until timestamptz,file text)
RETURNS text[] AS
'$libdir/konfigraf', 'GetLog'
LANGUAGE c VOLATILE;
COMMENT ON FUNCTION get_log(text,timestamptz,timestamptz,text) IS 'Gets log from the master branch
';

CREATE OR REPLACE FUNCTION get_branch_log(repoName text,branch text,since timestamptz,until timestamptz,file text)
RETURNS text[] AS
'$libdir/konfigraf', 'GetBranchLog'
LANGUAGE c VOLATILE;
COMMENT ON FUNCTION get_branch_log(text,text,timestamptz,timestamptz,text) IS 'Lists files from a specific path of the master branch
';


create table if not exists repository
(
  id         serial      not null
    constraint repository_pkey
      primary key,
  name       varchar(50) not null,
  remote_url text
);
create unique index if not exists repository_name_uindex
  on repository (name);

create table if not exists objects
(
  repo_id  integer  not null
    constraint config_objects_id_fk
      references repository
      on delete cascade,
  obj_type integer  not null,
  hash     char(40) not null,
  blob     bytea    not null,
  constraint objects_pk
    primary key (repo_id, obj_type, hash)
);

create table if not exists refs
(
  repo_id integer not null
    constraint config_refs_id_fk
      references repository
      on delete cascade,
  name    text    not null,
  target  text,
  constraint refs_pk
    primary key (repo_id, name)
);

create table if not exists config
(
  repo_id integer not null
    constraint config_pk
      primary key
    constraint config_repository_id_fk
      references repository
      on delete cascade,
  data    bytea   not null
);

create table if not exists shallow
(
  repo_id integer not null
    constraint shallow_pk
      primary key
    constraint config_shallow_id_fk
      references repository
      on delete cascade,
  data    json
);

create table if not exists index
(
  repo_id integer not null
    constraint index_pk
      primary key
    constraint config_index_id_fk
      references repository
      on delete cascade,
  data    json    not null
);