migrations_v1.sql 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. CREATE TABLE "chain" (
  2. "chain_id" SERIAL PRIMARY KEY,
  3. "chain_name" TEXT NOT NULL
  4. );
  5. CREATE TABLE "company" (
  6. "company_id" SERIAL PRIMARY KEY,
  7. "company_name" TEXT NOT NULL,
  8. "company_flag" TEXT NOT NULL
  9. );
  10. CREATE TABLE "role" (
  11. "role_id" SERIAL PRIMARY KEY,
  12. "company_id" INTEGER NOT NULL,
  13. "role_name" TEXT NOT NULL,
  14. "role_permission" JSONB NOT NULL,
  15. "role_flag" TEXT NOT NULL,
  16. CHECK (jsonb_typeof("role_permission") = 'object'),
  17. FOREIGN KEY ("company_id") REFERENCES "company" ("company_id")
  18. );
  19. CREATE TABLE "status" (
  20. "status_id" SERIAL PRIMARY KEY,
  21. "status_status" TEXT NOT NULL
  22. );
  23. CREATE TABLE "cpr" (
  24. "cpr_id" SERIAL PRIMARY KEY,
  25. "cpr_additive" TEXT NOT NULL,
  26. "cpr_agents_sender_phone" TEXT NOT NULL,
  27. "cpr_agents_sender_cep" TEXT NOT NULL,
  28. "cpr_agents_creditor_document_number" TEXT NOT NULL,
  29. "cpr_agents_creditor_status" BOOLEAN NOT NULL,
  30. "cpr_agents_creditor_person_type" INTEGER NOT NULL,
  31. "cpr_agents_endorsement_date" TEXT NOT NULL,
  32. "cpr_agents_endorser" BOOLEAN NOT NULL,
  33. "cpr_agents_creditor_id" INTEGER NOT NULL,
  34. "cpr_agents_wallet_holder" TEXT NOT NULL,
  35. "cpr_agents_cnpj_holder" TEXT NOT NULL,
  36. "cpr_agents_debtor_cep" TEXT NOT NULL,
  37. "cpr_agents_debtor_email" TEXT NOT NULL,
  38. "cpr_agents_debtor_name_corporate_name" TEXT NOT NULL,
  39. "cpr_agents_debtor_phone_number" TEXT NOT NULL,
  40. "cpr_agents_debtor_document_number" TEXT NOT NULL,
  41. "cpr_agents_debtor_status" BOOLEAN NOT NULL,
  42. "cpr_agents_debtor_person_type" INTEGER NOT NULL,
  43. "cpr_agents_debtor_id" INTEGER NOT NULL,
  44. "cpr_agents_sender_email" TEXT NOT NULL,
  45. "cpr_agents_sender_credit_agency" TEXT NOT NULL,
  46. "cpr_agents_sender_credit_bank" TEXT NOT NULL,
  47. "cpr_agents_sender_credit_checking_account" TEXT NOT NULL,
  48. "cpr_agents_guarantor_phone" TEXT NOT NULL,
  49. "cpr_agents_guarantor_email" TEXT NOT NULL,
  50. "cpr_agents_guarantor_cep" TEXT NOT NULL,
  51. "cpr_agents_guarantor_document_number" TEXT NOT NULL,
  52. "cpr_agents_guarantor_status" BOOLEAN NOT NULL,
  53. "cpr_agents_guarantor_person_type" INTEGER NOT NULL,
  54. "cpr_agents_guarantor_id" INTEGER NOT NULL,
  55. "cpr_agents_guarantor_type" INTEGER NOT NULL,
  56. "cpr_agents_sender_document_number" TEXT NOT NULL,
  57. "cpr_agents_sender_person_type" INTEGER NOT NULL,
  58. "cpr_area_total" NUMERIC NOT NULL,
  59. "cpr_area_registry" TEXT NOT NULL,
  60. "cpr_area_cep" TEXT NOT NULL,
  61. "cpr_area_address" TEXT NOT NULL,
  62. "cpr_area_property_unitary_fraction" TEXT NOT NULL,
  63. "cpr_area_id" INTEGER NOT NULL,
  64. "cpr_area_latitude" NUMERIC NOT NULL,
  65. "cpr_area_logitude" NUMERIC NOT NULL,
  66. "cpr_area_registration" TEXT NOT NULL,
  67. "cpr_area_farm_name" TEXT NOT NULL,
  68. "cpr_area_product_cpr_necessery_area" NUMERIC NOT NULL,
  69. "cpr_area_product_total_productive_area" NUMERIC NOT NULL,
  70. "cpr_area_product_class_type_ph" TEXT NOT NULL,
  71. "cpr_area_product_culture" TEXT NOT NULL,
  72. "cpr_area_product_culture_specificity" TEXT NOT NULL,
  73. "cpr_area_product_packaging_method" TEXT NOT NULL,
  74. "cpr_area_product_id" INTEGER NOT NULL,
  75. "cpr_area_product_assessment_index" TEXT NOT NULL,
  76. "cpr_area_product_institution_responsible_index" TEXT NOT NULL,
  77. "cpr_area_product_delivery_location_city" TEXT NOT NULL,
  78. "cpr_area_product_delivery_location_regional" TEXT NOT NULL,
  79. "cpr_area_product_delivery_location_fu" TEXT NOT NULL,
  80. "cpr_area_product_volume_price" NUMERIC NOT NULL,
  81. "cpr_area_product_production" TEXT NOT NULL,
  82. "cpr_area_product_productivity" TEXT NOT NULL,
  83. "cpr_area_product_harvest" TEXT NOT NULL,
  84. "cpr_area_product_situation" TEXT NOT NULL,
  85. "cpr_area_product_status" BOOLEAN NOT NULL,
  86. "cpr_area_product_volume_mesuring_unit" TEXT NOT NULL,
  87. "cpr_area_product_register_value" TEXT NOT NULL,
  88. "cpr_area_product_volume_quantity" TEXT NOT NULL,
  89. "cpr_area_owner" TEXT NOT NULL,
  90. "cpr_area_legal_reserve" NUMERIC NOT NULL,
  91. "cpr_area_status" BOOLEAN NOT NULL,
  92. "cpr_contracts_registration_authorized_and_clear_fi_record" BOOLEAN NOT NULL, --autorizo_registro_e_declaro_que_foi_dado_baixa_no_registro_em_quaisquer_outras_if
  93. "cpr_contracts_credit_operation_contract_code" TEXT NOT NULL,
  94. "cpr_contracts_contract_scr" TEXT NOT NULL,
  95. "cpr_contracts_coin_code" TEXT NOT NULL,
  96. "cpr_contracts_other_fi_discharge_date" TIMESTAMPTZ NOT NULL,
  97. "cpr_contracts_fi_hiring_date" TIMESTAMPTZ NOT NULL,
  98. "cpr_contracts_original_register_date" TIMESTAMPTZ NOT NULL,
  99. "cpr_contracts_transaction_date" DATE NOT NULL,
  100. "cpr_contracts_scr_standardized_identification" TEXT NOT NULL,
  101. "cpr_contracts_indexation" TEXT NOT NULL,
  102. "cpr_contracts_operation_nature" TEXT NOT NULL,
  103. "cpr_contracts_indexing_percentage" NUMERIC NOT NULL,
  104. "cpr_contracts_installments_quantity_contracted" INTEGER NOT NULL,
  105. "cpr_contracts_extern_reference" TEXT NOT NULL,
  106. "cpr_contracts_operation_interest_rate" NUMERIC NOT NULL,
  107. "cpr_contracts_asset_type" TEXT NOT NULL,
  108. "cpr_contracts_contract_financed_value" NUMERIC NOT NULL,
  109. "cpr_contracts_credit_liquid_value" NUMERIC NOT NULL,
  110. "cpr_contracts_credit_total_value" NUMERIC NOT NULL,
  111. "cpr_emission_delivery_date" DATE NOT NULL,
  112. "cpr_emission_id" INTEGER NOT NULL,
  113. "cpr_emission_location" TEXT NOT NULL,
  114. "cpr_emission_issuer_corporate_name" TEXT NOT NULL,
  115. "cpr_emission_cpr_type" TEXT NOT NULL, --fisico, financeiro
  116. "cpr_guarantee_status" BOOLEAN NOT NULL,
  117. "cpr_guarantee_id" INTEGER NOT NULL,
  118. "cpr_guarantee_type" TEXT NOT NULL,
  119. "cpr_installment_control_code" TEXT NOT NULL,
  120. "cpr_installment_due_date" TEXT NOT NULL,
  121. "cpr_installment_register_id" INTEGER NOT NULL,
  122. "cpr_installment_number" INTEGER NOT NULL,
  123. "cpr_installment_assignment_price" INTEGER NOT NULL,
  124. "cpr_installment_status" INTEGER NOT NULL,
  125. "cpr_installment_value" NUMERIC NOT NULL,
  126. "cpr_installment_main_value" NUMERIC NOT NULL,
  127. "cpr_status" TEXT NOT NULL --1 - ACEITO, 2 - LIQUIDADO, 3 - RECUSADO, 4 - CANCELADO
  128. );
  129. CREATE TABLE "commodities" (
  130. "commodities_id" SERIAL PRIMARY KEY,
  131. "commodities_name" TEXT NOT NULL,
  132. "commodities_flag" TEXT NOT NULL
  133. );
  134. CREATE TABLE "wallet" (
  135. "wallet_id" SERIAL PRIMARY KEY,
  136. "company_id" INTEGER NOT NULL,
  137. "wallet_public_key" TEXT NOT NULL,
  138. "wallet_address" TEXT NOT NULL,
  139. "wallet_private_key" TEXT NOT NULL,
  140. "wallet_flag" TEXT NOT NULL,
  141. "chain_id" INTEGER NOT NULL,
  142. FOREIGN KEY ("company_id") REFERENCES "company" ("company_id"),
  143. FOREIGN KEY ("chain_id") REFERENCES "chain" ("chain_id")
  144. );
  145. CREATE TABLE "currency" (
  146. "currency_id" SERIAL PRIMARY KEY,
  147. "currency_external_id" TEXT NOT NULL,
  148. "currency_name" TEXT NOT NULL,
  149. "currency_digits" INTEGER NOT NULL,
  150. "chain_id" INTEGER NOT NULL,
  151. "currency_flag" TEXT NOT NULL,
  152. FOREIGN KEY ("chain_id") REFERENCES "chain" ("chain_id")
  153. );
  154. CREATE TABLE "token" (
  155. "token_id" SERIAL PRIMARY KEY,
  156. "token_external_id" TEXT NOT NULL,
  157. "token_commodities_amount" INTEGER NOT NULL,
  158. "token_flag" TEXT NOT NULL,
  159. "token_commodities_value" INTEGER NOT NULL,
  160. "wallet_id" INTEGER NOT NULL,
  161. "chain_id" INTEGER NOT NULL,
  162. "commodities_id" INTEGER NOT NULL,
  163. "cpr_id" INTEGER NOT NULL,
  164. FOREIGN KEY ("wallet_id") REFERENCES "wallet" ("wallet_id"),
  165. FOREIGN KEY ("chain_id") REFERENCES "chain" ("chain_id"),
  166. FOREIGN KEY ("commodities_id") REFERENCES "commodities" ("commodities_id"),
  167. FOREIGN KEY ("cpr_id") REFERENCES "cpr" ("cpr_id")
  168. );
  169. CREATE TABLE "user" (
  170. "user_id" SERIAL PRIMARY KEY,
  171. "user_name" TEXT NOT NULL,
  172. "user_email" TEXT NOT NULL UNIQUE,
  173. "user_password" TEXT NOT NULL,
  174. "user_phone" TEXT NOT NULL,
  175. "user_address" TEXT NOT NULL,
  176. "user_city" TEXT NOT NULL,
  177. "user_state" TEXT NOT NULL,
  178. "user_zip" TEXT NOT NULL,
  179. "user_country" TEXT NOT NULL,
  180. "user_kyc" INTEGER NOT NULL,
  181. "user_birthdate" INTEGER NOT NULL,
  182. "user_cpf" TEXT NOT NULL,
  183. "company_id" INTEGER NOT NULL,
  184. "role_id" INTEGER NOT NULL,
  185. "user_flag" TEXT NOT NULL,
  186. FOREIGN KEY ("company_id") REFERENCES "company" ("company_id"),
  187. FOREIGN KEY ("role_id") REFERENCES "role" ("role_id")
  188. );
  189. CREATE TABLE "tx_coin" (
  190. "tx_coin_id" TEXT PRIMARY KEY,
  191. "tx_coin_value" TEXT NOT NULL,
  192. "tx_coin_flag" TEXT NOT NULL,
  193. "tx_coin_ts" INTEGER NOT NULL,
  194. "tx_coin_from_address" TEXT NOT NULL,
  195. "tx_coin_to_address" TEXT NOT NULL,
  196. "currency_id" INTEGER NOT NULL,
  197. "chain_id" INTEGER NOT NULL,
  198. FOREIGN KEY ("currency_id") REFERENCES "currency" ("currency_id"),
  199. FOREIGN KEY ("chain_id") REFERENCES "chain" ("chain_id")
  200. );
  201. CREATE TABLE "tx_token" (
  202. "tx_token_id" TEXT PRIMARY KEY,
  203. "tx_token_flag" TEXT NOT NULL,
  204. "tx_token_ts" INTEGER NOT NULL,
  205. "tx_token_from_address" TEXT NOT NULL,
  206. "tx_token_to_address" TEXT NOT NULL,
  207. "token_id" INTEGER NOT NULL,
  208. "chain_id" INTEGER NOT NULL,
  209. FOREIGN KEY ("token_id") REFERENCES "token" ("token_id"),
  210. FOREIGN KEY ("chain_id") REFERENCES "chain" ("chain_id")
  211. );
  212. CREATE TABLE "orderbook" (
  213. "orderbook_id" SERIAL PRIMARY KEY,
  214. "orderbook_flag" TEXT NOT NULL,
  215. "orderbook_ts" INTEGER NOT NULL,
  216. "orderbook_is_token" BOOLEAN NOT NULL, -- true = venda, false = compra
  217. "orderbook_amount" TEXT NOT NULL,
  218. "status_id" INTEGER NOT NULL,
  219. "user_id" INTEGER NOT NULL,
  220. "wallet_id" INTEGER NOT NULL,
  221. "token_id" INTEGER,
  222. "currency_id" INTEGER,
  223. "chain_id" INTEGER NOT NULL,
  224. FOREIGN KEY ("status_id") REFERENCES "status" ("status_id"),
  225. FOREIGN KEY ("user_id") REFERENCES "user" ("user_id"),
  226. FOREIGN KEY ("wallet_id") REFERENCES "wallet" ("wallet_id"),
  227. FOREIGN KEY ("token_id") REFERENCES "token" ("token_id"),
  228. FOREIGN KEY ("currency_id") REFERENCES "currency" ("currency_id"),
  229. FOREIGN KEY ("chain_id") REFERENCES "chain" ("chain_id")
  230. );