8077140e1e99_v3_api_database_revision.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. """v3_api_database_revision
  2. Revision ID: 8077140e1e99
  3. Revises:
  4. Create Date: 2024-12-03 12:10:10.878485
  5. """
  6. import os
  7. from typing import Sequence, Union
  8. import sqlalchemy as sa
  9. from alembic import op
  10. # revision identifiers, used by Alembic.
  11. revision: str = "8077140e1e99"
  12. down_revision: Union[str, None] = "2fac23e4d91b"
  13. branch_labels: Union[str, Sequence[str], None] = None
  14. depends_on: Union[str, Sequence[str], None] = None
  15. project_name = os.getenv("R2R_PROJECT_NAME")
  16. if not project_name:
  17. raise ValueError(
  18. "Environment variable `R2R_PROJECT_NAME` must be provided migrate, it should be set equal to the value of `project_name` in your `r2r.toml`."
  19. )
  20. if (
  21. input(
  22. "WARNING: This migration will delete all graph data. Are you sure you want to continue? (yes/no) "
  23. ).lower()
  24. != "yes"
  25. ):
  26. raise ValueError("Migration aborted.")
  27. def upgrade() -> None:
  28. # Collections table migration
  29. op.alter_column(
  30. "collections",
  31. "collection_id",
  32. new_column_name="id",
  33. schema=project_name,
  34. )
  35. op.drop_column(
  36. "collections",
  37. "kg_enrichment_status",
  38. schema=project_name,
  39. )
  40. op.add_column(
  41. "collections",
  42. sa.Column(
  43. "owner_id",
  44. sa.UUID,
  45. server_default=sa.text("'2acb499e-8428-543b-bd85-0d9098718220'"),
  46. ),
  47. schema=project_name,
  48. )
  49. op.add_column(
  50. "collections",
  51. sa.Column(
  52. "graph_sync_status", sa.Text, server_default=sa.text("'pending'")
  53. ),
  54. schema=project_name,
  55. )
  56. op.add_column(
  57. "collections",
  58. sa.Column(
  59. "graph_cluster_status",
  60. sa.Text,
  61. server_default=sa.text("'pending'"),
  62. ),
  63. schema=project_name,
  64. )
  65. # Documents table migration
  66. op.rename_table(
  67. "document_info",
  68. "documents",
  69. schema=project_name,
  70. )
  71. op.alter_column(
  72. "documents",
  73. "document_id",
  74. new_column_name="id",
  75. schema=project_name,
  76. )
  77. op.alter_column(
  78. "documents",
  79. "user_id",
  80. new_column_name="owner_id",
  81. schema=project_name,
  82. )
  83. op.drop_column(
  84. "documents",
  85. "kg_extraction_status",
  86. schema=project_name,
  87. )
  88. op.add_column(
  89. "documents",
  90. sa.Column(
  91. "extraction_status",
  92. sa.Text,
  93. server_default=sa.text("'pending'"),
  94. ),
  95. schema=project_name,
  96. )
  97. op.alter_column(
  98. "documents",
  99. "doc_search_vector",
  100. new_column_name="raw_tsvector",
  101. schema=project_name,
  102. )
  103. # Files table migration
  104. op.rename_table(
  105. "file_storage",
  106. "files",
  107. schema=project_name,
  108. )
  109. op.alter_column(
  110. "files",
  111. "file_name",
  112. new_column_name="name",
  113. schema=project_name,
  114. )
  115. op.alter_column(
  116. "files",
  117. "file_oid",
  118. new_column_name="oid",
  119. schema=project_name,
  120. )
  121. op.alter_column(
  122. "files",
  123. "file_size",
  124. new_column_name="size",
  125. schema=project_name,
  126. )
  127. op.alter_column(
  128. "files",
  129. "file_type",
  130. new_column_name="type",
  131. schema=project_name,
  132. )
  133. # Prompts table migration
  134. op.alter_column(
  135. "prompts",
  136. "prompt_id",
  137. new_column_name="id",
  138. schema=project_name,
  139. )
  140. # Users table migration
  141. op.alter_column(
  142. "users",
  143. "user_id",
  144. new_column_name="id",
  145. schema=project_name,
  146. )
  147. # Chunks table migration
  148. op.rename_table(
  149. "vectors",
  150. "chunks",
  151. schema=project_name,
  152. )
  153. op.alter_column(
  154. "chunks",
  155. "extraction_id",
  156. new_column_name="id",
  157. schema=project_name,
  158. )
  159. op.alter_column(
  160. "chunks",
  161. "user_id",
  162. new_column_name="owner_id",
  163. schema=project_name,
  164. )
  165. def downgrade() -> None:
  166. # Collections table migration
  167. op.alter_column(
  168. "collections",
  169. "id",
  170. new_column_name="collection_id",
  171. schema=project_name,
  172. )
  173. op.add_column(
  174. "collections",
  175. sa.Column(
  176. "kg_enrichment_status",
  177. sa.Text,
  178. server_default=sa.text("'pending'"),
  179. ),
  180. schema=project_name,
  181. )
  182. op.drop_column(
  183. "collections",
  184. "owner_id",
  185. schema=project_name,
  186. )
  187. op.drop_column(
  188. "collections",
  189. "graph_sync_status",
  190. schema=project_name,
  191. )
  192. op.drop_column(
  193. "collections",
  194. "graph_cluster_status",
  195. schema=project_name,
  196. )
  197. # Documents table migration
  198. op.rename_table(
  199. "documents",
  200. "document_info",
  201. schema=project_name,
  202. )
  203. op.alter_column(
  204. "document_info",
  205. "id",
  206. new_column_name="document_id",
  207. schema=project_name,
  208. )
  209. op.alter_column(
  210. "document_info",
  211. "owner_id",
  212. new_column_name="user_id",
  213. schema=project_name,
  214. )
  215. op.add_column(
  216. "document_info",
  217. sa.Column(
  218. "kg_extraction_status",
  219. sa.Text,
  220. server_default=sa.text("'pending'"),
  221. ),
  222. schema=project_name,
  223. )
  224. op.drop_column(
  225. "document_info",
  226. "extraction_status",
  227. schema=project_name,
  228. )
  229. op.alter_column(
  230. "document_info",
  231. "raw_tsvector",
  232. new_column_name="doc_search_vector",
  233. schema=project_name,
  234. )
  235. # Files table migration
  236. op.rename_table(
  237. "files",
  238. "file_storage",
  239. schema=project_name,
  240. )
  241. op.alter_column(
  242. "file_storage",
  243. "name",
  244. new_column_name="file_name",
  245. schema=project_name,
  246. )
  247. op.alter_column(
  248. "file_storage",
  249. "oid",
  250. new_column_name="file_oid",
  251. schema=project_name,
  252. )
  253. op.alter_column(
  254. "file_storage",
  255. "size",
  256. new_column_name="file_size",
  257. schema=project_name,
  258. )
  259. op.alter_column(
  260. "file_storage",
  261. "type",
  262. new_column_name="file_type",
  263. schema=project_name,
  264. )
  265. # Prompts table migration
  266. op.alter_column(
  267. "prompts",
  268. "id",
  269. new_column_name="prompt_id",
  270. schema=project_name,
  271. )
  272. # Users table migration
  273. op.alter_column(
  274. "users",
  275. "id",
  276. new_column_name="user_id",
  277. schema=project_name,
  278. )
  279. # Chunks table migration
  280. op.rename_table(
  281. "chunks",
  282. "vectors",
  283. schema=project_name,
  284. )
  285. op.alter_column(
  286. "vectors",
  287. "id",
  288. new_column_name="extraction_id",
  289. schema=project_name,
  290. )
  291. op.alter_column(
  292. "vectors",
  293. "owner_id",
  294. new_column_name="user_id",
  295. schema=project_name,
  296. )