auth-schema-postgresql.sql 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. CREATE TABLE roles
  2. (
  3. id serial,
  4. "name" varchar(32) NOT NULL,
  5. description text NOT NULL,
  6. CONSTRAINT roles_id_pkey PRIMARY KEY (id),
  7. CONSTRAINT roles_name_key UNIQUE (name)
  8. );
  9. CREATE TABLE roles_users
  10. (
  11. user_id integer,
  12. role_id integer
  13. );
  14. CREATE TABLE users
  15. (
  16. id serial,
  17. email varchar(254) NOT NULL,
  18. username varchar(32) NOT NULL,
  19. "password" varchar(64) NOT NULL,
  20. logins integer NOT NULL DEFAULT 0,
  21. last_login integer,
  22. CONSTRAINT users_id_pkey PRIMARY KEY (id),
  23. CONSTRAINT users_username_key UNIQUE (username),
  24. CONSTRAINT users_email_key UNIQUE (email),
  25. CONSTRAINT users_logins_check CHECK (logins >= 0)
  26. );
  27. CREATE TABLE user_tokens
  28. (
  29. id serial,
  30. user_id integer NOT NULL,
  31. user_agent varchar(40) NOT NULL,
  32. token character varying(32) NOT NULL,
  33. created integer NOT NULL,
  34. expires integer NOT NULL,
  35. CONSTRAINT user_tokens_id_pkey PRIMARY KEY (id),
  36. CONSTRAINT user_tokens_token_key UNIQUE (token)
  37. );
  38. CREATE INDEX user_id_idx ON roles_users (user_id);
  39. CREATE INDEX role_id_idx ON roles_users (role_id);
  40. ALTER TABLE roles_users
  41. ADD CONSTRAINT user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  42. ADD CONSTRAINT role_id_fkey FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE;
  43. ALTER TABLE user_tokens
  44. ADD CONSTRAINT user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
  45. INSERT INTO roles (name, description) VALUES ('login', 'Login privileges, granted after account confirmation');
  46. INSERT INTO roles (name, description) VALUES ('admin', 'Administrative user, has access to everything.');