Skip to content

Add Indexes to Schema API#4240

Merged
flovilmart merged 12 commits into
parse-community:3.xfrom
dplewis:schema-index
Nov 25, 2017
Merged

Add Indexes to Schema API#4240
flovilmart merged 12 commits into
parse-community:3.xfrom
dplewis:schema-index

Conversation

@dplewis

@dplewis dplewis commented Oct 6, 2017

Copy link
Copy Markdown
Member

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

codecov Bot commented Oct 6, 2017

Copy link
Copy Markdown

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

dplewis commented Oct 6, 2017

Copy link
Copy Markdown
Member Author

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

}

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

@vitaly-t vitaly-t Oct 6, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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 => {

@vitaly-t vitaly-t Oct 6, 2017

Copy link
Copy Markdown
Contributor

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

vitaly-t commented Oct 6, 2017

Copy link
Copy Markdown
Contributor

@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

dplewis commented Oct 6, 2017

Copy link
Copy Markdown
Member Author

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

@vitaly-t

vitaly-t commented Oct 6, 2017

Copy link
Copy Markdown
Contributor

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

@vitaly-t

vitaly-t commented Oct 6, 2017

Copy link
Copy Markdown
Contributor

@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

dplewis commented Oct 6, 2017

Copy link
Copy Markdown
Member Author

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

@vitaly-t

vitaly-t commented Oct 6, 2017

Copy link
Copy Markdown
Contributor

Agreed, it is a bad habit :)

@dplewis

dplewis commented Oct 6, 2017

Copy link
Copy Markdown
Member Author

@flovilmart can you look this over?

@flovilmart

Copy link
Copy Markdown
Contributor

Kinda busy today, gotta have a look tomorrow :)

}

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

@vitaly-t vitaly-t Oct 10, 2017

Copy link
Copy Markdown
Contributor

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]);
    })));

@vitaly-t vitaly-t Oct 10, 2017

Copy link
Copy Markdown
Contributor

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.

@dplewis dplewis Oct 11, 2017

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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
Copy Markdown
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

@acinader acinader left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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

@dplewis

dplewis commented Oct 20, 2017

Copy link
Copy Markdown
Member Author

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

@flovilmart

Copy link
Copy Markdown
Contributor

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

@dplewis

dplewis commented Oct 20, 2017

Copy link
Copy Markdown
Member Author

I agree. Should we get the indexes on initialization?

@flovilmart

Copy link
Copy Markdown
Contributor

Seems like a good idea!

@montymxb montymxb left a comment

Copy link
Copy Markdown
Contributor

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

dplewis commented Nov 16, 2017

Copy link
Copy Markdown
Member Author

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

@montymxb

Copy link
Copy Markdown
Contributor

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

@montymxb montymxb left a comment

Copy link
Copy Markdown
Contributor

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

dplewis commented Nov 16, 2017

Copy link
Copy Markdown
Member Author

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

@flovilmart

Copy link
Copy Markdown
Contributor

Awesome thanks @dplewis !

@montymxb

Copy link
Copy Markdown
Contributor

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

Comment thread spec/schemas.spec.js Outdated
headers: masterKeyHeaders,
json: true,
}, (error, response, body) => {
console.log(body);

Copy link
Copy Markdown
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

}

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

here too

@dplewis

dplewis commented Nov 22, 2017

Copy link
Copy Markdown
Member Author

@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
Copy Markdown
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
Copy Markdown
Contributor

@flovilmart something up here?

@flovilmart

Copy link
Copy Markdown
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