00__remove_fk.sql 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. PRAGMA foreign_keys = off;
  2. DROP TABLE IF EXISTS _user_old;
  3. ALTER TABLE
  4. user RENAME TO _user_old;
  5. -- user
  6. CREATE TABLE user (
  7. id INTEGER PRIMARY KEY AUTOINCREMENT,
  8. created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
  9. updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
  10. row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
  11. email TEXT NOT NULL UNIQUE,
  12. role TEXT NOT NULL CHECK (role IN ('HOST', 'USER')) DEFAULT 'USER',
  13. name TEXT NOT NULL,
  14. password_hash TEXT NOT NULL,
  15. open_id TEXT NOT NULL UNIQUE
  16. );
  17. INSERT INTO
  18. user
  19. SELECT
  20. *
  21. FROM
  22. _user_old;
  23. DROP TABLE IF EXISTS _user_old;
  24. DROP TABLE IF EXISTS _memo_old;
  25. ALTER TABLE
  26. memo RENAME TO _memo_old;
  27. -- memo
  28. CREATE TABLE memo (
  29. id INTEGER PRIMARY KEY AUTOINCREMENT,
  30. creator_id INTEGER NOT NULL,
  31. created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
  32. updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
  33. row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
  34. content TEXT NOT NULL DEFAULT '',
  35. visibility TEXT NOT NULL CHECK (visibility IN ('PUBLIC', 'PROTECTED', 'PRIVATE')) DEFAULT 'PRIVATE'
  36. );
  37. INSERT INTO
  38. memo
  39. SELECT
  40. *
  41. FROM
  42. _memo_old;
  43. DROP TABLE IF EXISTS _memo_old;
  44. DROP TABLE IF EXISTS _memo_organizer_old;
  45. ALTER TABLE
  46. memo_organizer RENAME TO _memo_organizer_old;
  47. -- memo_organizer
  48. CREATE TABLE memo_organizer (
  49. id INTEGER PRIMARY KEY AUTOINCREMENT,
  50. memo_id INTEGER NOT NULL,
  51. user_id INTEGER NOT NULL,
  52. pinned INTEGER NOT NULL CHECK (pinned IN (0, 1)) DEFAULT 0,
  53. UNIQUE(memo_id, user_id)
  54. );
  55. INSERT INTO
  56. memo_organizer
  57. SELECT
  58. *
  59. FROM
  60. _memo_organizer_old;
  61. DROP TABLE IF EXISTS _memo_organizer_old;
  62. DROP TABLE IF EXISTS _shortcut_old;
  63. ALTER TABLE
  64. shortcut RENAME TO _shortcut_old;
  65. -- shortcut
  66. CREATE TABLE shortcut (
  67. id INTEGER PRIMARY KEY AUTOINCREMENT,
  68. creator_id INTEGER NOT NULL,
  69. created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
  70. updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
  71. row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
  72. title TEXT NOT NULL DEFAULT '',
  73. payload TEXT NOT NULL DEFAULT '{}'
  74. );
  75. INSERT INTO
  76. shortcut
  77. SELECT
  78. *
  79. FROM
  80. _shortcut_old;
  81. DROP TABLE IF EXISTS _shortcut_old;
  82. DROP TABLE IF EXISTS _resource_old;
  83. ALTER TABLE
  84. resource RENAME TO _resource_old;
  85. -- resource
  86. CREATE TABLE resource (
  87. id INTEGER PRIMARY KEY AUTOINCREMENT,
  88. creator_id INTEGER NOT NULL,
  89. created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
  90. updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
  91. filename TEXT NOT NULL DEFAULT '',
  92. blob BLOB DEFAULT NULL,
  93. external_link TEXT NOT NULL DEFAULT '',
  94. type TEXT NOT NULL DEFAULT '',
  95. size INTEGER NOT NULL DEFAULT 0
  96. );
  97. INSERT INTO
  98. resource (
  99. id,
  100. creator_id,
  101. created_ts,
  102. updated_ts,
  103. filename,
  104. blob,
  105. external_link,
  106. type,
  107. size
  108. )
  109. SELECT
  110. id,
  111. creator_id,
  112. created_ts,
  113. updated_ts,
  114. filename,
  115. blob,
  116. external_link,
  117. type,
  118. size
  119. FROM
  120. _resource_old;
  121. DROP TABLE IF EXISTS _resource_old;
  122. DROP TABLE IF EXISTS _user_setting_old;
  123. ALTER TABLE
  124. user_setting RENAME TO _user_setting_old;
  125. -- user_setting
  126. CREATE TABLE user_setting (
  127. user_id INTEGER NOT NULL,
  128. key TEXT NOT NULL,
  129. value TEXT NOT NULL,
  130. UNIQUE(user_id, key)
  131. );
  132. INSERT INTO
  133. user_setting
  134. SELECT
  135. *
  136. FROM
  137. _user_setting_old;
  138. DROP TABLE IF EXISTS _user_setting_old;
  139. DROP TABLE IF EXISTS _memo_resource_old;
  140. ALTER TABLE
  141. memo_resource RENAME TO _memo_resource_old;
  142. -- memo_resource
  143. CREATE TABLE memo_resource (
  144. memo_id INTEGER NOT NULL,
  145. resource_id INTEGER NOT NULL,
  146. created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
  147. updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
  148. UNIQUE(memo_id, resource_id)
  149. );
  150. INSERT INTO
  151. memo_resource
  152. SELECT
  153. *
  154. FROM
  155. _memo_resource_old;
  156. DROP TABLE IF EXISTS _memo_resource_old;