ezii-postgres 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (263) hide show
  1. checksums.yaml +7 -0
  2. data/ezii-postgres.gemspec +13 -0
  3. data/natural-backend/Capfile +50 -0
  4. data/natural-backend/Gemfile +63 -0
  5. data/natural-backend/Gemfile.lock +233 -0
  6. data/natural-backend/LICENSE +21 -0
  7. data/natural-backend/Procfile +2 -0
  8. data/natural-backend/README.md +51 -0
  9. data/natural-backend/Rakefile +6 -0
  10. data/natural-backend/app.json +28 -0
  11. data/natural-backend/app/channels/application_cable/channel.rb +4 -0
  12. data/natural-backend/app/channels/application_cable/connection.rb +4 -0
  13. data/natural-backend/app/commands/base_command.rb +31 -0
  14. data/natural-backend/app/commands/create_new_or_authenticate_user.rb +60 -0
  15. data/natural-backend/app/commands/create_project_authentication_token_command.rb +25 -0
  16. data/natural-backend/app/commands/decode_authentication_command.rb +50 -0
  17. data/natural-backend/app/commands/decode_project_authentication_token_command.rb +51 -0
  18. data/natural-backend/app/controllers/application_controller.rb +3 -0
  19. data/natural-backend/app/controllers/authentication_controller.rb +17 -0
  20. data/natural-backend/app/controllers/columns_controller.rb +51 -0
  21. data/natural-backend/app/controllers/concerns/token_authenticatable.rb +26 -0
  22. data/natural-backend/app/controllers/databases_controller.rb +51 -0
  23. data/natural-backend/app/controllers/ember_controller.rb +6 -0
  24. data/natural-backend/app/controllers/projects_controller.rb +51 -0
  25. data/natural-backend/app/controllers/queries_controller.rb +59 -0
  26. data/natural-backend/app/controllers/row_values_controller.rb +51 -0
  27. data/natural-backend/app/controllers/rows_controller.rb +56 -0
  28. data/natural-backend/app/controllers/tables_controller.rb +55 -0
  29. data/natural-backend/app/jobs/application_job.rb +2 -0
  30. data/natural-backend/app/jobs/create_column_job.rb +10 -0
  31. data/natural-backend/app/jobs/create_database_job.rb +11 -0
  32. data/natural-backend/app/jobs/create_database_user_job.rb +8 -0
  33. data/natural-backend/app/jobs/create_table_job.rb +21 -0
  34. data/natural-backend/app/jobs/delete_row_job.rb +12 -0
  35. data/natural-backend/app/jobs/delete_value_job.rb +10 -0
  36. data/natural-backend/app/jobs/destroy_column_job.rb +10 -0
  37. data/natural-backend/app/jobs/destroy_database_job.rb +8 -0
  38. data/natural-backend/app/jobs/destroy_database_user_job.rb +8 -0
  39. data/natural-backend/app/jobs/destroy_table_job.rb +9 -0
  40. data/natural-backend/app/jobs/insert_value_job.rb +11 -0
  41. data/natural-backend/app/jobs/run_query_job.rb +35 -0
  42. data/natural-backend/app/jobs/sync_db_job.rb +80 -0
  43. data/natural-backend/app/jobs/update_column_type_job.rb +10 -0
  44. data/natural-backend/app/jobs/update_value_job.rb +10 -0
  45. data/natural-backend/app/mailers/application_mailer.rb +4 -0
  46. data/natural-backend/app/models/application_record.rb +3 -0
  47. data/natural-backend/app/models/column.rb +43 -0
  48. data/natural-backend/app/models/database.rb +37 -0
  49. data/natural-backend/app/models/project.rb +31 -0
  50. data/natural-backend/app/models/query.rb +20 -0
  51. data/natural-backend/app/models/row.rb +15 -0
  52. data/natural-backend/app/models/row_value.rb +30 -0
  53. data/natural-backend/app/models/table.rb +32 -0
  54. data/natural-backend/app/models/user.rb +10 -0
  55. data/natural-backend/app/serializers/column_serializer.rb +5 -0
  56. data/natural-backend/app/serializers/database_serializer.rb +6 -0
  57. data/natural-backend/app/serializers/project_serializer.rb +4 -0
  58. data/natural-backend/app/serializers/query_serializer.rb +3 -0
  59. data/natural-backend/app/serializers/row_serializer.rb +5 -0
  60. data/natural-backend/app/serializers/row_value_serializer.rb +5 -0
  61. data/natural-backend/app/serializers/table_serializer.rb +8 -0
  62. data/natural-backend/app/services/jwt_service.rb +17 -0
  63. data/natural-backend/app/views/layouts/mailer.html.erb +13 -0
  64. data/natural-backend/app/views/layouts/mailer.text.erb +1 -0
  65. data/natural-backend/bin/bundle +3 -0
  66. data/natural-backend/bin/rails +9 -0
  67. data/natural-backend/bin/rake +9 -0
  68. data/natural-backend/bin/run_migrations.sh +3 -0
  69. data/natural-backend/bin/setup +35 -0
  70. data/natural-backend/bin/spring +17 -0
  71. data/natural-backend/bin/update +29 -0
  72. data/natural-backend/config.ru +5 -0
  73. data/natural-backend/config/application.rb +33 -0
  74. data/natural-backend/config/boot.rb +3 -0
  75. data/natural-backend/config/cable.yml +10 -0
  76. data/natural-backend/config/database.yml +21 -0
  77. data/natural-backend/config/deploy.rb +36 -0
  78. data/natural-backend/config/deploy/production.rb +61 -0
  79. data/natural-backend/config/deploy/staging.rb +61 -0
  80. data/natural-backend/config/environment.rb +5 -0
  81. data/natural-backend/config/environments/development.rb +47 -0
  82. data/natural-backend/config/environments/production.rb +83 -0
  83. data/natural-backend/config/environments/test.rb +42 -0
  84. data/natural-backend/config/initializers/application_controller_renderer.rb +8 -0
  85. data/natural-backend/config/initializers/backtrace_silencers.rb +7 -0
  86. data/natural-backend/config/initializers/bugsnag.rb +3 -0
  87. data/natural-backend/config/initializers/cors.rb +16 -0
  88. data/natural-backend/config/initializers/filter_parameter_logging.rb +4 -0
  89. data/natural-backend/config/initializers/inflections.rb +16 -0
  90. data/natural-backend/config/initializers/json_api.rb +5 -0
  91. data/natural-backend/config/initializers/load_commands.rb +4 -0
  92. data/natural-backend/config/initializers/load_lib.rb +6 -0
  93. data/natural-backend/config/initializers/mime_types.rb +4 -0
  94. data/natural-backend/config/initializers/redis_classy.rb +1 -0
  95. data/natural-backend/config/initializers/sidekiq.rb +1 -0
  96. data/natural-backend/config/initializers/wrap_parameters.rb +14 -0
  97. data/natural-backend/config/locales/en.yml +33 -0
  98. data/natural-backend/config/puma.rb +56 -0
  99. data/natural-backend/config/routes.rb +14 -0
  100. data/natural-backend/config/schedule.rb +18 -0
  101. data/natural-backend/config/secrets.yml +32 -0
  102. data/natural-backend/config/spring.rb +6 -0
  103. data/natural-backend/db/migrate/20180408155000_create_projects.rb +9 -0
  104. data/natural-backend/db/migrate/20180408155011_create_databases.rb +10 -0
  105. data/natural-backend/db/migrate/20180409174859_create_tables.rb +9 -0
  106. data/natural-backend/db/migrate/20180409174916_create_columns.rb +11 -0
  107. data/natural-backend/db/migrate/20180409175800_create_rows.rb +9 -0
  108. data/natural-backend/db/migrate/20180409214650_create_row_values.rb +11 -0
  109. data/natural-backend/db/migrate/20180411110745_add_db_username_to_projects.rb +5 -0
  110. data/natural-backend/db/migrate/20180411145722_add_dbpw_to_projects.rb +5 -0
  111. data/natural-backend/db/migrate/20180411185924_add_database_identifier_to_databases.rb +6 -0
  112. data/natural-backend/db/migrate/20180412002908_add_dbid_to_rows.rb +5 -0
  113. data/natural-backend/db/migrate/20180412012433_create_queries.rb +11 -0
  114. data/natural-backend/db/migrate/20180429151758_create_users.rb +10 -0
  115. data/natural-backend/db/migrate/20180430094604_add_user_ref_to_projects.rb +5 -0
  116. data/natural-backend/db/migrate/20180430102945_add_user_ref_to_databases.rb +5 -0
  117. data/natural-backend/db/migrate/20180430102955_add_user_ref_to_tables.rb +5 -0
  118. data/natural-backend/db/migrate/20180430103004_add_user_ref_to_rows.rb +5 -0
  119. data/natural-backend/db/migrate/20180430103015_add_user_ref_to_columns.rb +5 -0
  120. data/natural-backend/db/migrate/20180430103024_add_user_ref_to_row_values.rb +5 -0
  121. data/natural-backend/db/migrate/20180430141537_add_api_token_projects.rb +5 -0
  122. data/natural-backend/db/schema.rb +112 -0
  123. data/natural-backend/db/seeds.rb +7 -0
  124. data/natural-backend/lib/database_manager/database_manager.rb +59 -0
  125. data/natural-backend/lib/database_manager/lib/connection.rb +49 -0
  126. data/natural-backend/lib/database_manager/lib/database.rb +65 -0
  127. data/natural-backend/lib/database_manager/lib/database_user.rb +46 -0
  128. data/natural-backend/lib/database_manager/lib/table.rb +129 -0
  129. data/natural-backend/public/assets/natural-frontend-598145a86019fc7faa2e0386ec6cb276.css +6 -0
  130. data/natural-backend/public/assets/natural-frontend-8e453ff9db7fc3a47e6d9e3de45aa19e.js +31 -0
  131. data/natural-backend/public/assets/vendor-7ffaee0528b64f886e9d1860fc719b5a.js +7359 -0
  132. data/natural-backend/public/assets/vendor-8bd8fa913b4f7f8b27f086c7bfd98b7f.css +1 -0
  133. data/natural-backend/public/index.html +26 -0
  134. data/natural-backend/public/robots.txt +3 -0
  135. data/natural-backend/roadmap.md +25 -0
  136. data/natural-backend/test/controllers/authentication_controller_test.rb +7 -0
  137. data/natural-backend/test/controllers/columns_controller_test.rb +38 -0
  138. data/natural-backend/test/controllers/databases_controller_test.rb +38 -0
  139. data/natural-backend/test/controllers/ember_controller_test.rb +7 -0
  140. data/natural-backend/test/controllers/projects_controller_test.rb +38 -0
  141. data/natural-backend/test/controllers/queries_controller_test.rb +38 -0
  142. data/natural-backend/test/controllers/row_values_controller_test.rb +38 -0
  143. data/natural-backend/test/controllers/rows_controller_test.rb +38 -0
  144. data/natural-backend/test/controllers/tables_controller_test.rb +38 -0
  145. data/natural-backend/test/fixtures/columns.yml +11 -0
  146. data/natural-backend/test/fixtures/databases.yml +9 -0
  147. data/natural-backend/test/fixtures/projects.yml +7 -0
  148. data/natural-backend/test/fixtures/queries.yml +9 -0
  149. data/natural-backend/test/fixtures/row_values.yml +11 -0
  150. data/natural-backend/test/fixtures/rows.yml +7 -0
  151. data/natural-backend/test/fixtures/tables.yml +7 -0
  152. data/natural-backend/test/fixtures/users.yml +9 -0
  153. data/natural-backend/test/jobs/add_column_job_test.rb +7 -0
  154. data/natural-backend/test/jobs/create_database_job_test.rb +7 -0
  155. data/natural-backend/test/jobs/create_database_user_job_test.rb +7 -0
  156. data/natural-backend/test/jobs/create_table_job_test.rb +7 -0
  157. data/natural-backend/test/jobs/delete_row_job_test.rb +7 -0
  158. data/natural-backend/test/jobs/delete_value_job_test.rb +7 -0
  159. data/natural-backend/test/jobs/destroy_database_job_test.rb +7 -0
  160. data/natural-backend/test/jobs/destroy_database_user_job_test.rb +7 -0
  161. data/natural-backend/test/jobs/destroy_table_job_test.rb +7 -0
  162. data/natural-backend/test/jobs/insert_value_job_test.rb +7 -0
  163. data/natural-backend/test/jobs/run_query_job_test.rb +7 -0
  164. data/natural-backend/test/jobs/sync_db_job_test.rb +7 -0
  165. data/natural-backend/test/jobs/update_value_job_test.rb +7 -0
  166. data/natural-backend/test/lib/database_manager/database_manager_test.rb +21 -0
  167. data/natural-backend/test/lib/database_manager/lib/database_test.rb +28 -0
  168. data/natural-backend/test/lib/database_manager/lib/database_user_test.rb +43 -0
  169. data/natural-backend/test/lib/database_manager/lib/table_test.rb +46 -0
  170. data/natural-backend/test/models/column_test.rb +7 -0
  171. data/natural-backend/test/models/database_test.rb +7 -0
  172. data/natural-backend/test/models/project_test.rb +7 -0
  173. data/natural-backend/test/models/query_test.rb +7 -0
  174. data/natural-backend/test/models/row_test.rb +7 -0
  175. data/natural-backend/test/models/row_value_test.rb +7 -0
  176. data/natural-backend/test/models/table_test.rb +7 -0
  177. data/natural-backend/test/models/user_test.rb +7 -0
  178. data/natural-backend/test/test_helper.rb +10 -0
  179. data/natural-frontend/LICENSE +21 -0
  180. data/natural-frontend/README.md +3 -0
  181. data/natural-frontend/app/adapters/application.js +33 -0
  182. data/natural-frontend/app/app.js +18 -0
  183. data/natural-frontend/app/components/column-editor.js +41 -0
  184. data/natural-frontend/app/components/row-editor.js +21 -0
  185. data/natural-frontend/app/components/row-value-editor.js +49 -0
  186. data/natural-frontend/app/controllers/application.js +11 -0
  187. data/natural-frontend/app/controllers/authenticate.js +16 -0
  188. data/natural-frontend/app/controllers/databases.js +22 -0
  189. data/natural-frontend/app/controllers/projects.js +18 -0
  190. data/natural-frontend/app/controllers/table.js +12 -0
  191. data/natural-frontend/app/controllers/table/rows.js +23 -0
  192. data/natural-frontend/app/controllers/tables.js +22 -0
  193. data/natural-frontend/app/helpers/plus-one.js +8 -0
  194. data/natural-frontend/app/index.html +25 -0
  195. data/natural-frontend/app/mixins/application-route-auth-mixin.js +8 -0
  196. data/natural-frontend/app/mixins/authenticated-route-mixin.js +10 -0
  197. data/natural-frontend/app/models/column.js +9 -0
  198. data/natural-frontend/app/models/database.js +9 -0
  199. data/natural-frontend/app/models/project.js +8 -0
  200. data/natural-frontend/app/models/row-value.js +8 -0
  201. data/natural-frontend/app/models/row.js +8 -0
  202. data/natural-frontend/app/models/table.js +9 -0
  203. data/natural-frontend/app/resolver.js +4 -0
  204. data/natural-frontend/app/router.js +22 -0
  205. data/natural-frontend/app/routes/application.js +5 -0
  206. data/natural-frontend/app/routes/authenticate.js +4 -0
  207. data/natural-frontend/app/routes/databases.js +15 -0
  208. data/natural-frontend/app/routes/function.js +20 -0
  209. data/natural-frontend/app/routes/functions.js +22 -0
  210. data/natural-frontend/app/routes/index.js +9 -0
  211. data/natural-frontend/app/routes/projects.js +8 -0
  212. data/natural-frontend/app/routes/table.js +17 -0
  213. data/natural-frontend/app/routes/table/rows.js +19 -0
  214. data/natural-frontend/app/routes/tables.js +16 -0
  215. data/natural-frontend/app/serializers/application.js +10 -0
  216. data/natural-frontend/app/services/authentication.js +38 -0
  217. data/natural-frontend/app/styles/app.scss +58 -0
  218. data/natural-frontend/app/templates/application.hbs +32 -0
  219. data/natural-frontend/app/templates/authenticate.hbs +7 -0
  220. data/natural-frontend/app/templates/components/api-usage-documentation.hbs +36 -0
  221. data/natural-frontend/app/templates/components/column-editor.hbs +37 -0
  222. data/natural-frontend/app/templates/components/row-editor.hbs +21 -0
  223. data/natural-frontend/app/templates/components/row-value-editor.hbs +1 -0
  224. data/natural-frontend/app/templates/databases.hbs +37 -0
  225. data/natural-frontend/app/templates/function.hbs +9 -0
  226. data/natural-frontend/app/templates/functions.hbs +30 -0
  227. data/natural-frontend/app/templates/index.hbs +15 -0
  228. data/natural-frontend/app/templates/projects.hbs +37 -0
  229. data/natural-frontend/app/templates/table.hbs +10 -0
  230. data/natural-frontend/app/templates/table/rows.hbs +23 -0
  231. data/natural-frontend/app/templates/tables.hbs +32 -0
  232. data/natural-frontend/config/environment.js +61 -0
  233. data/natural-frontend/config/targets.js +19 -0
  234. data/natural-frontend/deploy_rails_public.sh +6 -0
  235. data/natural-frontend/ember-cli-build.js +42 -0
  236. data/natural-frontend/package-lock.json +13288 -0
  237. data/natural-frontend/package.json +59 -0
  238. data/natural-frontend/public/robots.txt +3 -0
  239. data/natural-frontend/testem.js +20 -0
  240. data/natural-frontend/tests/index.html +33 -0
  241. data/natural-frontend/tests/integration/components/column-editor-test.js +27 -0
  242. data/natural-frontend/tests/integration/components/row-editor-test.js +27 -0
  243. data/natural-frontend/tests/integration/components/row-value-editor-test.js +27 -0
  244. data/natural-frontend/tests/integration/helpers/humanization-test.js +18 -0
  245. data/natural-frontend/tests/test-helper.js +9 -0
  246. data/natural-frontend/tests/unit/controllers/databases-test.js +13 -0
  247. data/natural-frontend/tests/unit/controllers/projects-test.js +13 -0
  248. data/natural-frontend/tests/unit/controllers/table-test.js +13 -0
  249. data/natural-frontend/tests/unit/controllers/table/rows-test.js +12 -0
  250. data/natural-frontend/tests/unit/controllers/tables-test.js +13 -0
  251. data/natural-frontend/tests/unit/models/column-test.js +15 -0
  252. data/natural-frontend/tests/unit/models/database-test.js +15 -0
  253. data/natural-frontend/tests/unit/models/project-test.js +15 -0
  254. data/natural-frontend/tests/unit/models/row-test.js +15 -0
  255. data/natural-frontend/tests/unit/models/rwo-value-test.js +15 -0
  256. data/natural-frontend/tests/unit/models/table-test.js +15 -0
  257. data/natural-frontend/tests/unit/routes/authenticate-test.js +11 -0
  258. data/natural-frontend/tests/unit/routes/databases-test.js +12 -0
  259. data/natural-frontend/tests/unit/routes/projects-test.js +12 -0
  260. data/natural-frontend/tests/unit/routes/table-test.js +12 -0
  261. data/natural-frontend/tests/unit/routes/table/rows-test.js +11 -0
  262. data/natural-frontend/tests/unit/routes/tables-test.js +12 -0
  263. metadata +303 -0
@@ -0,0 +1,59 @@
1
+ class QueriesController < ApplicationController
2
+ skip_before_action :authenticate_user
3
+ before_action :authenticate_project
4
+ before_action :fetch_database
5
+ before_action :fetch_query, only: [:index, :show, :destroy]
6
+
7
+ # GET /queries
8
+ def index
9
+ @queries = @database.queries.all
10
+
11
+ render json: @queries
12
+ end
13
+
14
+ def show
15
+ render json: { result: @query.response_data }
16
+ end
17
+
18
+ def create
19
+ @query = @database.queries.build(request_data: params[:sql])
20
+ @query.instant_execution = !(params[:async] == '1')
21
+ if @query.save
22
+ if @query.instant_execution
23
+ render json: { result: @query.run_query }
24
+ else
25
+ render json: { id: @query.id }, status: :created, location: [@database, @query]
26
+ end
27
+ else
28
+ render json: @query.errors, status: :unprocessable_entity
29
+ end
30
+ end
31
+
32
+ def destroy
33
+ @query.destroy
34
+ end
35
+
36
+ private
37
+
38
+ # TODO: check if project provided by authentication token matches project of queried database
39
+ def authenticate_project
40
+ command = DecodeProjectAuthenticationTokenCommand.call(request.headers)
41
+ @project = command.result
42
+ unless @project
43
+ render json: { error: 'Not Authenticated' }, status: 403
44
+ end
45
+ end
46
+
47
+ def fetch_database
48
+ @database = @project.databases.find(params[:database_id])
49
+ end
50
+
51
+ def fetch_query
52
+ @query = @database.queries.find(params[:id])
53
+ end
54
+
55
+ # Only allow a trusted parameter "white list" through.
56
+ def query_params
57
+ params.require(:query).permit(:sql)
58
+ end
59
+ end
@@ -0,0 +1,51 @@
1
+ class RowValuesController < ApplicationController
2
+ before_action :set_row_value, only: [:show, :update, :destroy]
3
+
4
+ # GET /row_values
5
+ def index
6
+ @row_values = current_user.row_values.all
7
+
8
+ render json: @row_values
9
+ end
10
+
11
+ # GET /row_values/1
12
+ def show
13
+ render json: @row_value
14
+ end
15
+
16
+ # POST /row_values
17
+ def create
18
+ @row_value = current_user.row_values.build(row_value_params)
19
+
20
+ if @row_value.save
21
+ render json: @row_value, status: :created, location: @row_value
22
+ else
23
+ render json: @row_value.errors, status: :unprocessable_entity
24
+ end
25
+ end
26
+
27
+ # PATCH/PUT /row_values/1
28
+ def update
29
+ if @row_value.update(row_value_params)
30
+ render json: @row_value
31
+ else
32
+ render json: @row_value.errors, status: :unprocessable_entity
33
+ end
34
+ end
35
+
36
+ # DELETE /row_values/1
37
+ def destroy
38
+ @row_value.destroy
39
+ end
40
+
41
+ private
42
+ # Use callbacks to share common setup or constraints between actions.
43
+ def set_row_value
44
+ @row_value = current_user.row_values.find(params[:id])
45
+ end
46
+
47
+ # Only allow a trusted parameter "white list" through.
48
+ def row_value_params
49
+ ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:column, :row, :value])
50
+ end
51
+ end
@@ -0,0 +1,56 @@
1
+ class RowsController < ApplicationController
2
+ before_action :set_row, only: [:show, :update, :destroy]
3
+
4
+ # GET /rows
5
+ def index
6
+ @rows = current_user
7
+ .tables
8
+ .find(params[:table_id])
9
+ .rows
10
+ .page(params[:page] || 1)
11
+ .per(params[:per_page] || 12)
12
+
13
+ render json: @rows, meta: { total_pages: @rows.total_pages }
14
+ end
15
+
16
+ # GET /rows/1
17
+ def show
18
+ render json: @row
19
+ end
20
+
21
+ # POST /rows
22
+ def create
23
+ @row = current_user.rows.build(row_params)
24
+
25
+ if @row.save
26
+ render json: @row, status: :created, location: @row
27
+ else
28
+ render json: @row.errors, status: :unprocessable_entity
29
+ end
30
+ end
31
+
32
+ # PATCH/PUT /rows/1
33
+ def update
34
+ if @row.update(row_params)
35
+ render json: @row
36
+ else
37
+ render json: @row.errors, status: :unprocessable_entity
38
+ end
39
+ end
40
+
41
+ # DELETE /rows/1
42
+ def destroy
43
+ @row.destroy
44
+ end
45
+
46
+ private
47
+ # Use callbacks to share common setup or constraints between actions.
48
+ def set_row
49
+ @row = current_user.rows.find(params[:id])
50
+ end
51
+
52
+ # Only allow a trusted parameter "white list" through.
53
+ def row_params
54
+ ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:table])
55
+ end
56
+ end
@@ -0,0 +1,55 @@
1
+ class TablesController < ApplicationController
2
+ before_action :set_table, only: [:show, :update, :destroy]
3
+ before_action :get_tables, only: [:index]
4
+
5
+ # GET /tables
6
+ def index
7
+ render json: @tables
8
+ end
9
+
10
+ # GET /tables/1
11
+ def show
12
+ render json: @table
13
+ end
14
+
15
+ # POST /tables
16
+ def create
17
+ @table = current_user.tables.build(table_params)
18
+
19
+ if @table.save
20
+ render json: @table, status: :created, location: @table
21
+ else
22
+ render json: @table.errors, status: :unprocessable_entity
23
+ end
24
+ end
25
+
26
+ # PATCH/PUT /tables/1
27
+ def update
28
+ if @table.update(table_params)
29
+ render json: @table
30
+ else
31
+ render json: @table.errors, status: :unprocessable_entity
32
+ end
33
+ end
34
+
35
+ # DELETE /tables/1
36
+ def destroy
37
+ @table.destroy
38
+ end
39
+
40
+ private
41
+
42
+ # Get tables associated with a particular db
43
+ def get_tables
44
+ @tables = Table.for_database_id(params[:db_id])
45
+ end
46
+ # Use callbacks to share common setup or constraints between actions.
47
+ def set_table
48
+ @table = current_user.tables.find(params[:id])
49
+ end
50
+
51
+ # Only allow a trusted parameter "white list" through.
52
+ def table_params
53
+ ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:name, :database])
54
+ end
55
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,10 @@
1
+ class CreateColumnJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(column)
5
+ db_manager = ::Natural::DatabaseManager.new
6
+ db = db_manager.database(column.table.database.database_identifier)
7
+ table = db.table(column.table.name)
8
+ table.add_column(column.name)
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ class CreateDatabaseJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(database)
5
+ db_manager = ::Natural::DatabaseManager.new
6
+ db_user = db_manager.database_user(database.project.db_username,
7
+ database.project.db_password)
8
+ db = db_manager.create_database(database.database_identifier)
9
+ db_user.grant(db)
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ class CreateDatabaseUserJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(db_username, db_password)
5
+ db_manager = ::Natural::DatabaseManager.new
6
+ db_manager.create_user(db_username, db_password)
7
+ end
8
+ end
@@ -0,0 +1,21 @@
1
+ class CreateTableJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(table)
5
+ db_user = ::Natural::DatabaseUser.new(table.database.project.db_username,
6
+ table.database.project.db_password)
7
+ database = ::Natural::Database.new(table.database.database_identifier)
8
+ connection = ::Natural::Connection.new
9
+ connection.db_user = db_user
10
+ connection.database = database
11
+
12
+ connection.establish_connection
13
+
14
+ table = ::Natural::Table.new(table.name)
15
+ table.connection = connection
16
+
17
+ table.create
18
+
19
+ connection.close
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ class DeleteRowJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(db_uuid, table_name, id)
5
+ db_manager = ::Natural::DatabaseManager.new
6
+ db = db_manager.database(db_uuid)
7
+ table = db.table(table_name)
8
+ if id.present?
9
+ table.delete_row(id)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ class DeleteValueJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(db_uuid, table_name, column_name, id)
5
+ db_manager = ::Natural::DatabaseManager.new
6
+ db = db_manager.database(db_uuid)
7
+ table = db.table(table_name)
8
+ table.delete_value(column_name, id)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class DestroyColumnJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(db_uuid, table_name, column_name)
5
+ db_manager = ::Natural::DatabaseManager.new
6
+ db = db_manager.database(db_uuid)
7
+ table = db.table(table_name)
8
+ table.destroy_column(column_name)
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class DestroyDatabaseJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(db_uuid)
5
+ db_manager = ::Natural::DatabaseManager.new
6
+ db_manager.destroy_database(db_uuid)
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class DestroyDatabaseUserJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(db_username)
5
+ db_manager = ::Natural::DatabaseManager.new
6
+ db_manager.destroy_user(db_username)
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ class DestroyTableJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(db_uuid, table_name)
5
+ db_manager = ::Natural::DatabaseManager.new
6
+ db = db_manager.database(db_uuid)
7
+ db.destroy_table(table_name)
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ class InsertValueJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(row_value)
5
+ db_manager = ::Natural::DatabaseManager.new
6
+ db = db_manager.database(row_value.column.table.database.database_identifier)
7
+ table = db.table(row_value.column.table.name)
8
+ id = table.insert_value(row_value.column.name, row_value.value)
9
+ row_value.row.update_attribute(:db_id, id)
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ class RunQueryJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def exceptions_matching(&block)
5
+ Class.new do
6
+ def self.===(other)
7
+ @block.call(other)
8
+ end
9
+ end.tap do |c|
10
+ c.instance_variable_set(:@block, block)
11
+ end
12
+ end
13
+
14
+ def perform(query)
15
+ db_user = ::Natural::DatabaseUser.new(query.database.project.db_username,
16
+ query.database.project.db_password)
17
+ db = ::Natural::Database.new(query.database.database_identifier)
18
+
19
+ connection = ::Natural::Connection.new
20
+ connection.db_user = db_user
21
+ connection.database = db
22
+
23
+ connection.establish_connection
24
+
25
+ begin
26
+ result = connection.exec(query.request_data).values
27
+ query.update_attribute(:response_data, result)
28
+ rescue exceptions_matching { |e| e.class.name.split('::')[0] == 'PG' } => e
29
+ # TODO: Add error field to query and render a 500 + error message in queries#create in case of an error
30
+ query.update_attribute(:response_data, {error: e.message})
31
+ ensure
32
+ connection.close
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,80 @@
1
+ # class SyncDbJob < ApplicationJob
2
+ # queue_as :default
3
+
4
+ # def sync_tables
5
+ # db_manager = ::Natural::DatabaseManager.new
6
+ # Database.all.each do |database|
7
+ # next if database.name.blank?
8
+
9
+ # db_manager.connect_to_database(database.database_identifier)
10
+
11
+ # existing_tables = database.tables.map(&:database_identifier)
12
+ # db_manager.tables.each do |table|
13
+ # unless existing_tables.include?(table.name)
14
+ # table = database.tables.build(database_identifier: table.name, name: table.name)
15
+ # table.save!
16
+ # end
17
+ # end
18
+ # end
19
+ # end
20
+
21
+ # def sync_rows_and_columns(table_name = nil)
22
+ # db_manager = ::Natural::DatabaseManager.new
23
+ # (table_name ? Table.where(name: table_name) : Table.all).each do |table|
24
+ # next if table.name.blank?
25
+
26
+ # db_manager.connect_to_database(table.database.database_identifier)
27
+ # to_be_removed_ids = table.rows.pluck(:db_id)
28
+
29
+ # query = <<-SQL
30
+ # SELECT *
31
+ # FROM \"#{table.name}\"
32
+ # SQL
33
+
34
+ # db_manager.connection.exec(query).each do |row|
35
+ # row_db_id = row.values_at('id')[0].to_i
36
+
37
+ # to_be_removed_ids.delete(row_db_id)
38
+
39
+ # if app_layer_row = Row.find_by(db_id: row_db_id)
40
+ # table.columns.each do |column|
41
+ # value = row.values_at(column.name)[0]
42
+ # row_value = RowValue.find_or_create_by(row: app_layer_row, column: column, user: table.user)
43
+ # # TODO: find a more elegant solution than update_columns
44
+ # # * updates_at/update_on are not updated
45
+ # # last resort: set updated_at/update_on manually here
46
+ # # reason for using update_columns in the first place: callback skipping
47
+ # row_value.update_columns(value: value)
48
+ # end
49
+ # else
50
+ # puts "why tho?"
51
+ # app_layer_row = Row.create(db_id: row.values_at('id')[0], table: table, user: table.user)
52
+ # table.columns.each do |column|
53
+ # value = row.values_at(column.name)[0]
54
+ # puts column.name
55
+ # puts value
56
+ # RowValue.create(row: app_layer_row, column: column, value: value, user: table.user)
57
+ # end
58
+ # end
59
+
60
+ # end
61
+ # #
62
+ # Row.where(db_id: to_be_removed_ids).destroy_all
63
+ # end
64
+
65
+ # return nil
66
+ # end
67
+
68
+ # def perform
69
+ # %I[sync_rows_and_columns sync_tables].each do |method_name|
70
+ # RedisMutex.with_lock(method_name) do
71
+ # ActiveRecord::Base.logger.silence do
72
+ # public_send(method_name)
73
+ # end
74
+ # rescue RedisMutex::LockError
75
+ # Rails.logger.debug "Another job is running, exiting ..."
76
+ # end
77
+ # end
78
+ # end
79
+ # end
80
+