20220512200302_add-userid.sql 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. ALTER TABLE users RENAME TO old_users;
  2. ALTER TABLE progress RENAME TO old_progress;
  3. CREATE TABLE users (
  4. id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  5. username TEXT NOT NULL UNIQUE,
  6. password TEXT NOT NULL,
  7. prefs BLOB NOT NULL DEFAULT '{}',
  8. claimed_invite TEXT NOT NULL UNIQUE,
  9. roles TEXT[] NOT NULL DEFAULT 'User',
  10. picture INTEGER UNIQUE,
  11. FOREIGN KEY(claimed_invite) REFERENCES invites(id),
  12. FOREIGN KEY(picture) REFERENCES assets(id)
  13. );
  14. INSERT INTO users (username, password, prefs, claimed_invite, roles, picture) SELECT * FROM old_users;
  15. CREATE TABLE progress (
  16. id INTEGER NOT NULL,
  17. user_id INTEGER NOT NULL,
  18. delta INTEGER NOT NULL,
  19. media_id INTEGER NOT NULL,
  20. populated INTEGER NOT NULL,
  21. PRIMARY KEY (id),
  22. FOREIGN KEY(media_id) REFERENCES _tblmedia (id) ON DELETE CASCADE ON UPDATE CASCADE,
  23. FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
  24. );
  25. INSERT INTO progress (id, user_id, delta, media_id, populated)
  26. SELECT op.id, u.id, op.delta, op.media_id, op.populated
  27. FROM old_progress op
  28. JOIN users u
  29. ON op.user_id=u.username;
  30. DROP TABLE old_users;
  31. DROP TABLE old_progress;