Skip to content

Test custom pg schema - #457

Merged
DZakh merged 13 commits into
mainfrom
integration-testing-add-custom-schema
Feb 20, 2025
Merged

DZakh merged 13 commits into
mainfrom
integration-testing-add-custom-schema

Conversation

@DZakh

@DZakh DZakh commented Feb 13, 2025

Copy link
Copy Markdown
Member

No description provided.

@JonoPrest JonoPrest left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@DZakh
DZakh enabled auto-merge (squash) February 13, 2025 15:14
@DZakh
DZakh force-pushed the integration-testing-add-custom-schema branch from be58e4a to 17acae6 Compare February 13, 2025 15:41
@JonoPrest

Copy link
Copy Markdown
Collaborator

I think we need to add the schema name to the persisted state to ensure envio dev reruns codegen if it changes

Comment on lines -187 to -192
//TODO: put the start script in the generated package.json
//and run from there.
if should_use_raw_events_worker {
args.push("--");
args.push("--sync-from-raw-events");
}

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.

Removed redundant code

Comment on lines +160 to +164
let sql = Db.sql
let needsRunUpMigrations = await sql->Migrations.needsRunUpMigrations
if needsRunUpMigrations {
let _ = await Migrations.runUpMigrations(~shouldExit=false)
}

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.

Added a run up migration here when a db schema doesn't exist

Comment on lines +15 to +17
| Custom(name) => `${Env.Db.publicSchema}.${name}`
| _ => fieldType :> string
}}${isArray ? "[]" : ""}${switch defaultValue {

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.

Had to add schema name here as well

}
"configuration": {
// Otherwise the entity in gql will be prefixed with the schema name (when it's not public)
"custom_name": tableName

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.

Remove the prefix schema and optimise multiple requests into one.

@DZakh
DZakh merged commit 32d71fe into main Feb 20, 2025
@DZakh
DZakh deleted the integration-testing-add-custom-schema branch February 20, 2025 11:15
{
`"${fieldName}" ${(fieldType :> string)}${isArray ? "[]" : ""}${switch defaultValue {
`"${fieldName}" ${switch fieldType {
| Custom(name) if !(name->Js.String2.startsWith("NUMERIC(")) => `"${Env.Db.publicSchema}".${name}`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This seems a bit fragile and I'm not following. When does custom start with "NUMERIC(" ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I kind of think we should rather separate these into two variant constructors. Maybe Enum and Custom for eg.

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.

Numeric is for precision. This is actually should be a separate constructor. I just don't want to spend time on this now. And besides the Numeric custom is used only for enums

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ok cool 👍🏼 maybe just add a comment explaining then.

Comment on lines +138 to +147
`DO $$
BEGIN
${reset ? `DROP SCHEMA IF EXISTS ${Env.Db.publicSchema} CASCADE;` : ``}
IF NOT EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = '${Env.Db.publicSchema}') THEN
CREATE SCHEMA ${Env.Db.publicSchema};
GRANT ALL ON SCHEMA ${Env.Db.publicSchema} TO postgres;
GRANT ALL ON SCHEMA ${Env.Db.publicSchema} TO public;
END IF;
END $$;`,
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why does any of this run on the non reset case? I see it only drops in this case but what about the rest?

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.

For example, you run pnpm start with a different schema. In this case it won't trigger the down migration (since pnpm start doesn't have the logic), but it still should create the schema if it's missing, so it's able to work correctly.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah got you 👍🏼

| _ => Failure
}

process->exit(exitCode)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It looks like we no longer call process.exit with exit code. This could be a problem for the hosted service

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.

We call it in runUpMigrations, but the first await sql->unsafe( actually don't have it. Should I add it back? Won't it exit with 1 automatically if the query fails?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All of the other potential failures are caught and only returned in the exit code. We definitely need the exit code to work accurately for the hosted service so I would suggest we keep the same logic.

Comment on lines +236 to +240
let tableNames =
[Db.allStaticTables, Db.allEntityTables]
->Belt.Array.concatMany
->Js.Array2.map(({tableName}) => tableName)
await trackTables(~tableNames)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I guess once the tables are all tracked the relationships and permisions can also be setup concurrently as well?

TablesStatic.PersistedState.table,
TablesStatic.EndOfBlockRangeScannedData.table,
TablesStatic.RawEvents.table,
TablesStatic.DynamicContractRegistry.table,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this only affecting hasura visibility? Even so, Could be a breaking change unfortunately

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.

It's not, since the table is a part of Entity tables. So this was duplicated here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Oh I see 👍🏼

SELECT 1
FROM pg_type
WHERE typname = '${name->Js.String2.toLowerCase}'
AND typnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '${Env.Db.publicSchema}')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wow, devils here hey, was this caught by a test? :)

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.

Yep

IF EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = '${Env.Db.publicSchema}') THEN
DROP SCHEMA ${Env.Db.publicSchema} CASCADE;
END IF;
DROP SCHEMA IF EXISTS ${Env.Db.publicSchema} CASCADE;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does this work if it doesn't previously exist?

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.

Yes, it doesn't do anything in this case.

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.

2 participants