summaryrefslogtreecommitdiff
path: root/sql/Pg-central.sql
blob: 15c16ad7709c86e84050ce166221d865e1b5d3ca (plain)
  1. begin;
  2. -- Central DB structure
  3. -- This is the central database stuff which is used across all datasets
  4. -- in the ledger-smb.conf it is called 'ledgersmb' by default, but obviously
  5. -- can be named anything.
  6. -- USERS stuff --
  7. CREATE TABLE users (id serial UNIQUE, username varchar(30) primary key);
  8. COMMENT ON TABLE users IS $$username is the actual primary key here because we do not want duplicate users$$;
  9. CREATE TABLE users_conf(id integer primary key references users(id) deferrable initially deferred,
  10. acs text,
  11. address text,
  12. businessnumber text,
  13. company text,
  14. countrycode text,
  15. currency text,
  16. dateformat text,
  17. dbdriver text default 'Pg',
  18. dbhost text default 'localhost',
  19. dbname text,
  20. dboptions text,
  21. dbpasswd text,
  22. dbport text,
  23. dbuser text,
  24. email text,
  25. fax text,
  26. menuwidth text,
  27. name text,
  28. numberformat text,
  29. password varchar(32) check(length(password) = 32),
  30. crypted_password text,
  31. print text,
  32. printer text,
  33. role text,
  34. sid text,
  35. signature text,
  36. stylesheet text,
  37. tel text,
  38. templates text,
  39. timeout numeric,
  40. vclimit numeric);
  41. COMMENT ON TABLE users_conf IS 'This is a completely dumb table that is a place holder to get usersconf into the database. Next major release will have a much more sane implementation';
  42. COMMENT ON COLUMN users_conf.id IS 'Yes primary key with a FOREIGN KEY to users(id) is correct';
  43. COMMENT ON COLUMN users_conf.password IS 'This means we have to get rid of the current password stuff and move to presumably md5()';
  44. -- The two below statements must be run from a single session
  45. INSERT INTO users(username) VALUES ('admin');
  46. INSERT INTO users_conf(id,password) VALUES (currval('users_id_seq'),NULL);
  47. CREATE OR REPLACE FUNCTION create_user(text) RETURNS bigint AS $$
  48. INSERT INTO users(username) VALUES ($1);
  49. SELECT currval('users_id_seq');
  50. $$ LANGUAGE 'SQL';
  51. COMMENT ON FUNCTION create_user(text) IS $$ Function to create user. Returns users.id if successful, else it is an error. $$;
  52. CREATE OR REPLACE FUNCTION update_user(int4,text) RETURNS int4 AS $$
  53. UPDATE users SET username = $2 WHERE id = $1;
  54. SELECT 1;
  55. $$ LANGUAGE 'SQL';
  56. COMMENT ON FUNCTION update_user(int4,text) IS $$ Takes int4 which is users.id and text which is username. Will update username based on id. Username is unique $$;
  57. -- Session tracking table
  58. CREATE TABLE session(
  59. session_id serial PRIMARY KEY,
  60. token VARCHAR(32) CHECK(length(token) = 32),
  61. last_used TIMESTAMP default now(),
  62. users_id INTEGER NOT NULL references users(id),
  63. transaction_id INTEGER NOT NULL
  64. );
  65. commit;