Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring method createTable #4456

Merged
merged 5 commits into from
Dec 26, 2017
Merged

Refactoring method createTable #4456

merged 5 commits into from
Dec 26, 2017

Conversation

vitaly-t
Copy link
Contributor

@vitaly-t vitaly-t commented Dec 24, 2017

Replacing the needless task + transaction chain, by replacing it with just one transaction that encapsulates the complete logic.


Ended up having the transaction inside the task, to make it possible to execute the transaction when the task fails.

Replacing the weird task + transaction chain, by replacing it with just one transaction that encapsulates the complete logic.
correcting the sequence to match the original exactly.
@codecov
Copy link

codecov bot commented Dec 24, 2017

Codecov Report

Merging #4456 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4456      +/-   ##
==========================================
+ Coverage   92.68%   92.69%   +0.01%     
==========================================
  Files         118      118              
  Lines        8348     8349       +1     
==========================================
+ Hits         7737     7739       +2     
+ Misses        611      610       -1
Impacted Files Coverage Δ
...dapters/Storage/Postgres/PostgresStorageAdapter.js 96.92% <100%> (ø) ⬆️
src/Adapters/Auth/meetup.js 84.21% <0%> (-5.27%) ⬇️
src/RestWrite.js 93.64% <0%> (+0.36%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 34453bf...c2f00ce. Read the comment docs.

Nesting the transaction inside a task, so it can execute successfully no matter if the containing task succeeds or fails.
adding the missing bracket.
@vitaly-t
Copy link
Contributor Author

@flovilmart one more nailed 😉

@@ -684,6 +684,7 @@ export class PostgresStorageAdapter {
// Just create a table, do not insert in schema
createTable(className, schema, conn) {
conn = conn || this._client;
const self = this;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick question, shouldn't we use .bind(this) instead of self capturing?

@vitaly-t
Copy link
Contributor Author

Not sure how it would work in that context...

@flovilmart
Copy link
Contributor

@vitaly-t

 return conn.task('create-table', function * (t) {
      try {
        yield this._ensureSchemaCollectionExists(t);
        yield t.none(qs, values);
      } catch(error) {
          if (error.code !== PostgresDuplicateRelationError) {
            throw error;
          }
        // ELSE: Table already exists, must have been created by a different request. Ignore the error.
      }
      yield t.tx('create-table-tx', tx => {
        return tx.batch(relations.map(fieldName => {
          return tx.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
        }));
      });
    }.bind(this));

@flovilmart
Copy link
Contributor

@vitaly-t but not required, I'm fine with the current implementation :)

@vitaly-t
Copy link
Contributor Author

vitaly-t commented Dec 25, 2017

It is debatable. The thing is, methods .task and .tx on its own provide this equal the t parameter, which would cause a contradiction/conflict, not sure even would even work then.

The alternative approach, using the library's feature is to do .task.call(this,..., and then access the context via t.ctx.context, see TaskContext.

But I just went for the simplest solution that's also known not to cause any conflict ;)

// ELSE: Table already exists, must have been created by a different request. Ignore the error.
}
yield t.tx('create-table-tx', tx => {
return tx.batch(relations.map(fieldName => {
Copy link
Member

@dplewis dplewis Dec 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitaly-t Merry Christmas. Quick question. I've notice you got rid of t.batch in favor of t.none(this._pgp.helpers(...)) in other functions. Is there a reason why you use it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dplewis as per that PR, it is a performance optimization, to execute all queries via a single IO operation, as opposed to executing them separately. It is much faster that way ;)

Copy link
Member

@dplewis dplewis Dec 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always thought .batch was a single I/O operation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

batch is an operation irrelevant as to the IO, it simply resolves an array of promises, like Promise.all does, except it also settles all the promises.

@vitaly-t vitaly-t merged commit a868bed into master Dec 26, 2017
@vitaly-t vitaly-t deleted the vitaly-t-patch-1 branch December 26, 2017 00:14
flovilmart pushed a commit that referenced this pull request Dec 29, 2017
* Refactoring method createTable

Replacing the weird task + transaction chain, by replacing it with just one transaction that encapsulates the complete logic.

* Update PostgresStorageAdapter.js

correcting the sequence to match the original exactly.

* Update PostgresStorageAdapter.js

Nesting the transaction inside a task, so it can execute successfully no matter if the containing task succeeds or fails.

* Update PostgresStorageAdapter.js

adding the missing bracket.
UnderratedDev pushed a commit to UnderratedDev/parse-server that referenced this pull request Mar 21, 2020
* Refactoring method createTable

Replacing the weird task + transaction chain, by replacing it with just one transaction that encapsulates the complete logic.

* Update PostgresStorageAdapter.js

correcting the sequence to match the original exactly.

* Update PostgresStorageAdapter.js

Nesting the transaction inside a task, so it can execute successfully no matter if the containing task succeeds or fails.

* Update PostgresStorageAdapter.js

adding the missing bracket.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants