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

Add Indexes to Schema API #4240

Merged
merged 12 commits into from
Nov 25, 2017
Merged

Conversation

dplewis
Copy link
Member

@dplewis dplewis commented Oct 6, 2017

Adds an optional indexes field to the Schema API.

Proposal: #4212 (comment)

Just a first pass though to make sure we're on the right track.

Thoughts?

@codecov
Copy link

codecov bot commented Oct 6, 2017

Codecov Report

Merging #4240 into 3.x will increase coverage by 0.12%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##              3.x    #4240      +/-   ##
==========================================
+ Coverage   92.45%   92.57%   +0.12%     
==========================================
  Files         118      118              
  Lines        8218     8341     +123     
==========================================
+ Hits         7598     7722     +124     
+ Misses        620      619       -1
Impacted Files Coverage Δ
src/Routers/SchemasRouter.js 97.91% <100%> (ø) ⬆️
src/Controllers/SchemaController.js 97.11% <100%> (+0.11%) ⬆️
src/Adapters/Storage/Mongo/MongoStorageAdapter.js 94.5% <100%> (+1.54%) ⬆️
...rc/Adapters/Storage/Mongo/MongoSchemaCollection.js 96.15% <100%> (+0.2%) ⬆️
...dapters/Storage/Postgres/PostgresStorageAdapter.js 96.93% <100%> (+0.13%) ⬆️
src/Controllers/DatabaseController.js 94.62% <100%> (-0.19%) ⬇️
src/RestWrite.js 93.46% <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 c36400a...102af94. Read the comment docs.

@dplewis
Copy link
Member Author

dplewis commented Oct 6, 2017

@vitaly-t Can you tell me if I used connections correctly on Postgres?

}

getIndexes(className) {
const qs = `SELECT * FROM pg_indexes WHERE tablename = '${className}'`;
Copy link
Contributor

@vitaly-t vitaly-t Oct 6, 2017

Choose a reason for hiding this comment

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

Do not use ES6 strings to format values, replace this with:

const qs = 'SELECT * FROM pg_indexes WHERE tablename = ${className}';
return this._client.any(qs, {className});

Copy link
Contributor

Choose a reason for hiding this comment

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

This is just one example, u have other places that can be improved the same ;)


dropIndexes(className, indexes, conn) {
conn = conn || this._client;
return conn.tx(t => {
Copy link
Contributor

@vitaly-t vitaly-t Oct 6, 2017

Choose a reason for hiding this comment

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

Much simpler code:

return conn.tx(t => t.batch(indexes.map(i => t.none('DROP INDEX $1', i))));

@vitaly-t
Copy link
Contributor

vitaly-t commented Oct 6, 2017

@dplewis it appears to be correct, at first sight anyway.

I left some comments there for you, as examples of:

  • need to avoid use of ES6 string to format values, it is dangerous
  • some transactions/tasks can be much simpler ;)

@dplewis
Copy link
Member Author

dplewis commented Oct 6, 2017

@vitaly-t Thanks a lot. Why is it dangerous to use ES6 Strings?

@vitaly-t
Copy link
Contributor

vitaly-t commented Oct 6, 2017

@dplewis ES6 strings know nothing about how to correctly escape values for PostgreSQL, only the pg-promise formatting engine knows it :)

@vitaly-t
Copy link
Contributor

vitaly-t commented Oct 6, 2017

@dplewis worth noting that some of the code there can probably be improved due to the following recent update in the driver: https://github.com/vitaly-t/pg-promise/releases/tag/v.6.10.0

@dplewis
Copy link
Member Author

dplewis commented Oct 6, 2017

@vitaly-t I know the formatting but I have a bad habit of not using pg-promise the way it was intended.

@vitaly-t
Copy link
Contributor

vitaly-t commented Oct 6, 2017

Agreed, it is a bad habit :)

@dplewis
Copy link
Member Author

dplewis commented Oct 6, 2017

@flovilmart can you look this over?

@flovilmart
Copy link
Contributor

Kinda busy today, gotta have a look tomorrow :)

@@ -1397,6 +1451,30 @@ export class PostgresStorageAdapter {
console.error(error);
});
}

createIndexes(className, indexes, conn) {
conn = conn || this._client;
Copy link
Contributor

@vitaly-t vitaly-t Oct 10, 2017

Choose a reason for hiding this comment

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

Better implementation:

    return (conn || this._client).tx(t => t.batch(indexes.map(i => {
        return t.none(`CREATE INDEX $1:name ON $2:name($3:name)`, [i.name, className, i.key]);
    })));

Copy link
Contributor

@vitaly-t vitaly-t Oct 10, 2017

Choose a reason for hiding this comment

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

Note that filter :name is very smart...

Filter :name, for SQL Names, has a special logic to it, and it can be:

  • a simple text string, which is normal, to represent a single column name
  • string * (asterisks), which is detected automatically
  • array of strings, in which case it is considered a list of column names
  • any object, to pull all property names automatically.

This is why in our case we can simply pass in an object, and :name will automatically pull all property names from it.

Copy link
Member Author

@dplewis dplewis Oct 11, 2017

Choose a reason for hiding this comment

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

I didn't know about the property names. Thats super helpful.

I really should read the pg-promise documentation again

Copy link
Contributor

Choose a reason for hiding this comment

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

@dplewis I have just updated documentation for SQL Names 😉

@vitaly-t
Copy link
Contributor

I might do another round of review after this PR is merged along with the pg-promise upgraded to version 7.x

@flovilmart flovilmart changed the base branch from master to 3.x October 11, 2017 21:32
Copy link
Contributor

@acinader acinader left a comment

Choose a reason for hiding this comment

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

much needed!

@flovilmart
Copy link
Contributor

@dplewis quick question, does it reads/loads all existing indexes?

@dplewis
Copy link
Member Author

dplewis commented Oct 20, 2017

@flovilmart No, only if someone has a compound index existing for full text search. Should we load all indexes?

@flovilmart
Copy link
Contributor

That would be neat, this way we bridge fully compared to the schema don't you think?

@dplewis
Copy link
Member Author

dplewis commented Oct 20, 2017

I agree. Should we get the indexes on initialization?

@flovilmart
Copy link
Contributor

Seems like a good idea!

Copy link
Contributor

@montymxb montymxb left a comment

Choose a reason for hiding this comment

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

Much better with the history, this looks good now!

Before I give this a thumbs-up, and I'm fairly certain, but this does load up pre-existing indexes, compound or not, on startup right? I'm pretty sure that it got added in but I wanted to be clear that it is present & accounted for.

@dplewis
Copy link
Member Author

dplewis commented Nov 16, 2017

@montymxb Indexes are available on startup but they aren't transformed. I'll have to add a transform for every index.

@montymxb
Copy link
Contributor

@dplewis so long as we don't create another issue like #4212 . I think we're good here though.

Copy link
Contributor

@montymxb montymxb left a comment

Choose a reason for hiding this comment

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

Alright, @flovilmart we're ready here. Any thoughts before we merge this?

@dplewis
Copy link
Member Author

dplewis commented Nov 16, 2017

I found a few issues I’ll submit a fix for shortly

@flovilmart
Copy link
Contributor

Awesome thanks @dplewis !

@montymxb
Copy link
Contributor

@dplewis just marking this as WIP until you finish the stuff you're working on .

@@ -2336,8 +2348,12 @@ describe('schemas', () => {
headers: masterKeyHeaders,
json: true,
}, (error, response, body) => {
console.log(body);
Copy link
Contributor

Choose a reason for hiding this comment

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

log here should be removed

@@ -1251,6 +1254,7 @@ export class PostgresStorageAdapter {
}

find(className, schema, query, { skip, limit, sort, keys }) {
console.log('find');
Copy link
Contributor

Choose a reason for hiding this comment

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

here too

@dplewis
Copy link
Member Author

dplewis commented Nov 22, 2017

@flovilmart I added a fix for #4212
It should work on startup or if the indexes were created later on.

Also the indexes saved and returned from mongo are in the same format.
{ indexName: { indexColumn: value } }

@flovilmart
Copy link
Contributor

Alright let's go with it!.

@flovilmart flovilmart merged commit d5c4324 into parse-community:3.x Nov 25, 2017
flovilmart added a commit that referenced this pull request Nov 25, 2017
@dplewis dplewis deleted the schema-index branch November 25, 2017 19:57
flovilmart pushed a commit that referenced this pull request Nov 25, 2017
* Add Indexes to Schema API

* error handling

* ci errors

* postgres support

* full text compound indexes

* pg clean up

* get indexes on startup

* test compound index on startup

* add default _id to index, full Text index on startup

* lint

* fix test
@montymxb
Copy link
Contributor

@flovilmart something up here?

@flovilmart
Copy link
Contributor

Ir’s Merged :)

UnderratedDev pushed a commit to UnderratedDev/parse-server that referenced this pull request Mar 21, 2020
* Add Indexes to Schema API

* error handling

* ci errors

* postgres support

* full text compound indexes

* pg clean up

* get indexes on startup

* test compound index on startup

* add default _id to index, full Text index on startup

* lint

* fix test
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.

5 participants