-
Notifications
You must be signed in to change notification settings - Fork 0
Add schema discovery and more testing #13
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
Open
Rho-Oof
wants to merge
1
commit into
main
Choose a base branch
from
schema-discovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,12 @@ const createConnection = async (connectionString) => { | |
| return client | ||
| } | ||
|
|
||
| const getAllSchemas = () => { | ||
| return `SELECT SCHEMA_NAME AS name FROM information_schema.SCHEMATA WHERE SCHEMA_NAME NOT IN ('pg_toast','information_schema','pg_catalog');` | ||
| } | ||
|
|
||
| const getTableSchemas = (schema) => { | ||
| return typeof schema == 'array' ? schema.map(s => `c.table_schema = '${s}'`).join(' OR ') : `c.table_schema = 'public'` | ||
| return schema ? schema.map(s => `c.table_schema = '${s}'`).join(' OR ') : `c.table_schema = 'public'` | ||
| } | ||
|
|
||
| const getRoutineSchemas = (schema) => { | ||
|
|
@@ -60,8 +64,6 @@ SELECT | |
| fk.foreign_column_name as ref_column | ||
| FROM information_schema.columns AS c | ||
| LEFT JOIN information_schema.tables AS t ON (c.table_schema=t.table_schema AND c.table_name = t.table_name) | ||
|
|
||
| --LEFT JOIN information_schema.table_constraints AS uk ON (c.table_schema=uk.table_schema AND c.table_name = uk.table_name AND uk.constraint_type = 'UNIQUE') | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still need this? |
||
| LEFT JOIN information_schema.table_constraints AS pk ON (c.table_schema=pk.table_schema AND c.table_name = pk.table_name AND pk.constraint_type = 'PRIMARY KEY') | ||
| LEFT JOIN fk ON (c.table_schema=fk.table_schema AND c.table_name = fk.table_name AND c.column_name = fk.column_name) | ||
| WHERE | ||
|
|
@@ -90,17 +92,32 @@ SELECT | |
|
|
||
|
|
||
| exports.PostgresProcessor = { | ||
| generateErd: async (connectionString, schemas) => { | ||
| generateErd: async (connectionString, schemas, setSchema) => { | ||
| const connection = await createConnection(connectionString) | ||
| tableSchemas = getTableSchemas(schemas) | ||
| sprocSchemas = getRoutineSchemas(schemas) | ||
| let tableSchemas | ||
| let sprocSchemas | ||
| let s | ||
| if (schemas && schemas?.length > 0) { | ||
| tableSchemas = getTableSchemas(schemas) | ||
| sprocSchemas = getRoutineSchemas(schemas) | ||
| } else { | ||
| const schema = await connection.query(getAllSchemas()) | ||
| s = schema.rows.map(i => i.name) | ||
| if (setSchema instanceof Function) | ||
| setSchema(s) | ||
| tableSchemas = getTableSchemas(s) | ||
| sprocSchemas = getRoutineSchemas(s) | ||
| } | ||
|
|
||
| const tablesQuery = getTableQuery(tableSchemas) | ||
| const sprocQuery = getRoutineQuery(sprocSchemas) | ||
| const {rows: tableData} = await connection.query(tablesQuery) | ||
| const {rows: sprocData} = await connection.query(sprocQuery) | ||
| const tables = {} | ||
| const sprocs = {} | ||
| if (tableData.length == 0 && sprocData.length == 0) { | ||
| throw new Error ('no data found for schemas: ' + schemas || s) | ||
| } | ||
| tableData.forEach(row => { | ||
| const name = `${row.schema}.${row.table}` | ||
| if (!tables.hasOwnProperty(name)) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| CREATE TABLE users ( | ||
| userid SERIAL PRIMARY KEY, | ||
| name TEXT, | ||
| email TEXT, | ||
| password TEXT, | ||
| created timestamp default current_timestamp, | ||
| updated timestamp default current_timestamp, | ||
| deleted timestamp default current_timestamp | ||
| ); | ||
|
|
||
| CREATE TABLE posts ( | ||
| postid SERIAL PRIMARY KEY, | ||
| title TEXT, | ||
| body TEXT, | ||
| userid INT, | ||
| inreplyto INT, | ||
| created timestamp default current_timestamp, | ||
| updated timestamp default current_timestamp, | ||
| deleted timestamp default current_timestamp, | ||
| FOREIGN KEY(userid) REFERENCES users(userid), | ||
| FOREIGN KEY(inreplyto) REFERENCES posts(postid) | ||
| ); | ||
|
|
||
| CREATE TABLE likes ( | ||
| postid INT, | ||
| userid INT, | ||
| created timestamp default current_timestamp, | ||
| deleted timestamp default current_timestamp, | ||
| PRIMARY KEY(postid,userid), | ||
| FOREIGN KEY(postid) REFERENCES posts(postid), | ||
| FOREIGN KEY(userid) REFERENCES users(userid) | ||
| ); | ||
|
|
||
|
|
||
| CREATE PROCEDURE prune_accounts() | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| BEGIN | ||
| UPDATE users SET deleted=NOW() WHERE updated < CURRENT_DATE - '6 months'::interval; | ||
| END;$$ ; | ||
|
|
||
| CREATE PROCEDURE prune_posts() | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| BEGIN | ||
| -- this is a bad implementation, need to fix later | ||
| UPDATE posts SET deleted=NOW() WHERE deleted IS NULL AND userid in (SELECT userid FROM users WHERE deleted > CURRENT_DATE - '1 months'::interval); | ||
| END;$$; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| ALTER TABLE users ADD COLUMN nickname TEXT; | ||
| CREATE UNIQUE INDEX unique_emails ON users(email); | ||
|
|
||
| CREATE TABLE new_likes ( | ||
| postid INT, | ||
| userid INT, | ||
| created timestamp default current_timestamp, | ||
| PRIMARY KEY(postid,userid), | ||
| FOREIGN KEY(postid) REFERENCES posts(postid), | ||
| FOREIGN KEY(userid) REFERENCES users(userid) | ||
| ); | ||
|
|
||
| INSERT INTO new_likes SELECT postid, userid, created FROM likes; | ||
|
|
||
| DROP TABLE likes; | ||
|
|
||
| ALTER TABLE new_likes RENAME TO likes; | ||
|
|
||
| CREATE VIEW stats AS | ||
| WITH lc AS ( | ||
| SELECT | ||
| postid, | ||
| count(userid) AS count | ||
| FROM likes GROUP BY postid | ||
| ),rc AS ( | ||
| SELECT | ||
| posts.postid, | ||
| posts.userid, | ||
| count(replies.postid) AS count | ||
| FROM posts AS posts | ||
| LEFT JOIN posts AS replies ON (posts.postid = replies.inreplyto) | ||
| GROUP BY posts.postid, posts.userid) | ||
| SELECT | ||
| posts.userid, | ||
| posts.title, | ||
| lc.count AS likecount, | ||
| rc.count AS replycount, | ||
| rc.count/lc.count AS ratio | ||
| FROM posts AS posts | ||
| LEFT JOIN lc USING(postid) | ||
| LEFT JOIN rc USING (postid, userid) | ||
| WHERE | ||
| posts.inreplyto IS NULL | ||
| ; | ||
|
|
||
| DROP PROCEDURE IF EXISTS prune_posts; | ||
| CREATE PROCEDURE prune_posts() | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| BEGIN | ||
| UPDATE posts | ||
| SET deleted=NOW() | ||
| WHERE postid in ( | ||
| SELECT posts.postid AS postid | ||
| FROM posts | ||
| JOIN users ON (posts.userid=users.userid) | ||
| WHERE | ||
| posts.deleted IS NULL AND | ||
| users.deleted > CURRENT_DATE - '1 months'::interval | ||
| ); | ||
| END;$$; | ||
|
|
||
| CREATE PROCEDURE top_likers(IN foremail text) | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| BEGIN | ||
| SELECT | ||
| count(1) AS num, | ||
| likers.name | ||
| FROM users AS source | ||
| JOIN posts AS posts ON (source.userid=posts.userid) | ||
| JOIN likes AS liked ON (posts.postid=liked.postid) | ||
| JOIN users AS likers ON (liked.userid=likers.userid) | ||
| WHERE | ||
| liked.deleted IS NULL | ||
| AND source.email = foremail | ||
|
|
||
| GROUP BY likers.name | ||
| HAVING num > 1 | ||
| ORDER BY 1 | ||
| LIMIT 5 | ||
| ; | ||
| END;$$; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| version: '3.7' | ||
| services: | ||
| previous: | ||
| image: postgres:alpine | ||
| volumes: | ||
| - ./previous:/docker-entrypoint-initdb.d | ||
| ports: | ||
| - "5433:5432" | ||
| environment: | ||
| POSTGRES_DB: database | ||
| POSTGRES_USER: user | ||
| POSTGRES_PASSWORD: rootpassword | ||
| current: | ||
| image: postgres:alpine | ||
| volumes: | ||
| - ./current:/docker-entrypoint-initdb.d | ||
| ports: | ||
| - "5432:5432" | ||
| environment: | ||
| POSTGRES_DB: database | ||
| POSTGRES_USER: user | ||
| POSTGRES_PASSWORD: rootpassword |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| CREATE TABLE users ( | ||
| userid SERIAL PRIMARY KEY, | ||
| name TEXT, | ||
| email TEXT, | ||
| password TEXT, | ||
| created timestamp default current_timestamp, | ||
| updated timestamp default current_timestamp, | ||
| deleted timestamp default current_timestamp | ||
| ); | ||
|
|
||
| CREATE TABLE posts ( | ||
| postid SERIAL PRIMARY KEY, | ||
| title TEXT, | ||
| body TEXT, | ||
| userid INT, | ||
| inreplyto INT, | ||
| created timestamp default current_timestamp, | ||
| updated timestamp default current_timestamp, | ||
| deleted timestamp default current_timestamp, | ||
| FOREIGN KEY(userid) REFERENCES users(userid), | ||
| FOREIGN KEY(inreplyto) REFERENCES posts(postid) | ||
| ); | ||
|
|
||
| CREATE TABLE likes ( | ||
| postid INT, | ||
| userid INT, | ||
| created timestamp default current_timestamp, | ||
| deleted timestamp default current_timestamp, | ||
| PRIMARY KEY(postid,userid), | ||
| FOREIGN KEY(postid) REFERENCES posts(postid), | ||
| FOREIGN KEY(userid) REFERENCES users(userid) | ||
| ); | ||
|
|
||
|
|
||
| CREATE PROCEDURE prune_accounts() | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| BEGIN | ||
| UPDATE users SET deleted=NOW() WHERE updated < CURRENT_DATE - '6 months'::interval; | ||
| END;$$ ; | ||
|
|
||
| CREATE PROCEDURE prune_posts() | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| BEGIN | ||
| -- this is a bad implementation, need to fix later | ||
| UPDATE posts SET deleted=NOW() WHERE deleted IS NULL AND userid in (SELECT userid FROM users WHERE deleted > CURRENT_DATE - '1 months'::interval); | ||
| END;$$; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #!/bin/bash | ||
|
|
||
| docker-compose up -d | ||
|
|
||
| sleep 30 | ||
|
|
||
| node ../../index.js -s public -c 'postgres://user:rootpassword@127.0.0.1:5432/database' -p 'postgres://user:rootpassword@127.0.0.1:5433/database' > output.html | ||
| node ../../index.js -c 'postgres://user:rootpassword@127.0.0.1:5432/database' -p 'postgres://user:rootpassword@127.0.0.1:5433/database' > schemaless.html | ||
|
|
||
| docker-compose down | ||
|
|
||
| diff output.html schemaless.html > /dev/null && rm output.html schemaless.html |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #!/bin/bash | ||
|
|
||
| echo $(cd sqlite/; ./test.sh) && \ | ||
| echo $(cd postgres/; ./test.sh) && \ | ||
| echo $(cd mysql/; ./test.sh) && \ | ||
| echo success |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var?