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

Change the public schema name with ENV vars #402

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions codegenerator/cli/templates/static/codegen/src/Env.res
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ module Db = {
let user = envSafe->EnvSafe.get("ENVIO_PG_USER", S.string, ~devFallback="postgres")
let password = envSafe->EnvSafe.get("ENVIO_POSTGRES_PASSWORD", S.string, ~devFallback="testing")
let database = envSafe->EnvSafe.get("ENVIO_PG_DATABASE", S.string, ~devFallback="envio-dev")
let publicSchema = envSafe->EnvSafe.get("ENVIO_PG_PUBLIC_SCHEMA", S.string, ~fallback="public")
let ssl = envSafe->EnvSafe.get(
"ENVIO_PG_SSL_MODE",
Postgres.sslOptionsSchema,
Expand Down
2 changes: 2 additions & 0 deletions codegenerator/cli/templates/static/codegen/src/db/Db.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ let config: Postgres.poolConfig = {
}
let sql = Postgres.makeSql(~config)

let publicSchema = Env.Db.publicSchema

let allEntityTables: array<Table.table> = Entities.allEntities->Belt.Array.map(entityMod => {
let module(Entity) = entityMod
Entity.table
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const TableModule = require("envio/src/db/Table.bs.js");
const { publicSchema } = require("../Db.bs.js");
// db operations for raw_events:
const MAX_ITEMS_PER_QUERY = 500;

Expand Down Expand Up @@ -34,7 +35,7 @@ const batchSetItemsInTableCore = (table, sql, rowDataArray) => {
);

return sql`
INSERT INTO "public".${sql(table.tableName)}
INSERT INTO ${publicSchema}.${sql(table.tableName)}
${sql(rowDataArray, ...fieldNames)}
ON CONFLICT(${sql`${commaSeparateDynamicMapQuery(
sql,
Expand All @@ -56,7 +57,7 @@ module.exports.batchDeleteItemsInTable = (table, sql, pkArray) => {
if (primaryKeyFieldNames.length === 1) {
return sql`
DELETE
FROM "public".${sql(table.tableName)}
FROM ${publicSchema}.${sql(table.tableName)}
WHERE ${sql(primaryKeyFieldNames[0])} IN ${sql(pkArray)};
`;
} else {
Expand All @@ -71,7 +72,7 @@ module.exports.batchReadItemsInTable = (table, sql, pkArray) => {
if (primaryKeyFieldNames.length === 1) {
return sql`
SELECT *
FROM "public".${sql(table.tableName)}
FROM ${publicSchema}.${sql(table.tableName)}
WHERE ${sql(primaryKeyFieldNames[0])} IN ${sql(pkArray)};
`;
} else {
Expand All @@ -83,19 +84,19 @@ module.exports.batchReadItemsInTable = (table, sql, pkArray) => {
module.exports.whereEqQuery = (table, sql, fieldName, value) => {
return sql`
SELECT *
FROM "public".${sql(table.tableName)}
FROM ${publicSchema}.${sql(table.tableName)}
WHERE ${sql(fieldName)} = ${value};
`;
};

module.exports.readLatestSyncedEventOnChainId = (sql, chainId) => sql`
SELECT *
FROM public.event_sync_state
FROM ${publicSchema}.event_sync_state
WHERE chain_id = ${chainId}`;

module.exports.batchSetEventSyncState = (sql, entityDataArray) => {
return sql`
INSERT INTO public.event_sync_state
INSERT INTO ${publicSchema}.event_sync_state
${sql(
entityDataArray,
"chain_id",
Expand All @@ -116,12 +117,12 @@ module.exports.batchSetEventSyncState = (sql, entityDataArray) => {

module.exports.readLatestChainMetadataState = (sql, chainId) => sql`
SELECT *
FROM public.chain_metadata
FROM ${publicSchema}.chain_metadata
WHERE chain_id = ${chainId}`;

module.exports.batchSetChainMetadata = (sql, entityDataArray) => {
return sql`
INSERT INTO public.chain_metadata
INSERT INTO ${publicSchema}.chain_metadata
${sql(
entityDataArray,
"chain_id",
Expand Down Expand Up @@ -154,7 +155,7 @@ module.exports.batchSetChainMetadata = (sql, entityDataArray) => {

const batchSetRawEventsCore = (sql, entityDataArray) => {
return sql`
INSERT INTO "public"."raw_events"
INSERT INTO ${publicSchema}."raw_events"
${sql(
entityDataArray,
"chain_id",
Expand All @@ -178,13 +179,13 @@ module.exports.batchSetRawEvents = (sql, entityDataArray) => {

module.exports.batchDeleteRawEvents = (sql, entityIdArray) => sql`
DELETE
FROM "public"."raw_events"
FROM ${publicSchema}."raw_events"
WHERE (chain_id, event_id) IN ${sql(entityIdArray)};`;
// end db operations for raw_events

const batchSetEndOfBlockRangeScannedDataCore = (sql, rowDataArray) => {
return sql`
INSERT INTO "public"."end_of_block_range_scanned_data"
INSERT INTO ${publicSchema}."end_of_block_range_scanned_data"
${sql(
rowDataArray,
"chain_id",
Expand All @@ -210,7 +211,7 @@ module.exports.batchSetEndOfBlockRangeScannedData = (sql, rowDataArray) => {

module.exports.readEndOfBlockRangeScannedDataForChain = (sql, chainId) => {
return sql`
SELECT * FROM "public"."end_of_block_range_scanned_data"
SELECT * FROM ${publicSchema}."end_of_block_range_scanned_data"
WHERE
chain_id = ${chainId}
ORDER BY block_number ASC;`;
Expand All @@ -224,7 +225,7 @@ module.exports.deleteStaleEndOfBlockRangeScannedDataForChain = (
) => {
return sql`
DELETE
FROM "public"."end_of_block_range_scanned_data"
FROM ${publicSchema}."end_of_block_range_scanned_data"
WHERE chain_id = ${chainId}
AND block_number < ${blockNumberThreshold}
AND block_timestamp < ${blockTimestampThreshold}
Expand All @@ -237,7 +238,7 @@ module.exports.readDynamicContractsOnChainIdAtOrBeforeBlockNumber = (
blockNumber
) => sql`
SELECT *
FROM "public"."dynamic_contract_registry"
FROM ${publicSchema}."dynamic_contract_registry"
WHERE registering_event_block_number <= ${blockNumber}
AND chain_id = ${chainId};`;

Expand All @@ -248,7 +249,7 @@ module.exports.readDynamicContractsOnChainIdMatchingEvents = (
) => {
return sql`
SELECT *
FROM "public"."dynamic_contract_registry"
FROM ${publicSchema}."dynamic_contract_registry"
WHERE chain_id = ${chainId}
AND (registering_event_contract_name, registering_event_name, registering_event_src_address) IN ${sql(
preRegisterEvents.map((item) => sql(item))
Expand All @@ -272,7 +273,7 @@ module.exports.getFirstChangeSerial_UnorderedMultichain = (
SELECT
MIN(serial) AS first_change_serial
FROM
public.${sql(makeHistoryTableName(entityName))}
${publicSchema}.${sql(makeHistoryTableName(entityName))}
WHERE
entity_history_chain_id = ${reorgChainId}
AND entity_history_block_number > ${safeBlockNumber}
Expand All @@ -292,7 +293,7 @@ module.exports.getFirstChangeSerial_OrderedMultichain = (
SELECT
MIN(serial) AS first_change_serial
FROM
public.${sql(makeHistoryTableName(entityName))}
${publicSchema}.${sql(makeHistoryTableName(entityName))}
WHERE
entity_history_block_timestamp > ${safeBlockTimestamp}
OR
Expand All @@ -317,7 +318,7 @@ module.exports.getFirstChangeEntityHistoryPerChain = (
SELECT DISTINCT
ON (entity_history_chain_id) *
FROM
public.${sql(makeHistoryTableName(entityName))}
${publicSchema}.${sql(makeHistoryTableName(entityName))}
WHERE
serial >= (
SELECT
Expand All @@ -344,7 +345,7 @@ module.exports.deleteRolledBackEntityHistory = (
)
-- Step 2: Delete all rows that have a serial >= the first change serial
DELETE FROM
public.${sql(makeHistoryTableName(entityName))}
${publicSchema}.${sql(makeHistoryTableName(entityName))}
WHERE
serial >= (
SELECT
Expand All @@ -371,7 +372,7 @@ module.exports.pruneStaleEntityHistory = (
SELECT
MIN(serial) AS first_change_serial
FROM
public.${sql(tableName)}
${publicSchema}.${sql(tableName)}
WHERE
${Utils.$$Array.interleave(
safeChainIdAndBlockNumberArray.map(
Expand All @@ -385,7 +386,7 @@ module.exports.pruneStaleEntityHistory = (
SELECT DISTINCT
ON (id) *
FROM
public.${sql(tableName)}
${publicSchema}.${sql(tableName)}
WHERE
serial >= (SELECT first_change_serial FROM first_change)
ORDER BY
Expand All @@ -400,7 +401,7 @@ module.exports.pruneStaleEntityHistory = (
prev.id,
prev.serial
FROM
public.${sql(tableName)} prev
${publicSchema}.${sql(tableName)} prev
INNER JOIN
items_in_reorg_threshold r
ON
Expand All @@ -415,7 +416,7 @@ module.exports.pruneStaleEntityHistory = (
: sql``
}
DELETE FROM
public.${sql(tableName)} eh
${publicSchema}.${sql(tableName)} eh
WHERE
-- Delete all entity history of entities that are not in the reorg threshold
eh.id NOT IN (SELECT id FROM items_in_reorg_threshold)
Expand All @@ -442,7 +443,7 @@ module.exports.getRollbackDiff = (sql, entityName, getFirstChangeSerial) => sql`
SELECT DISTINCT
ON (id) after.*
FROM
public.${sql(makeHistoryTableName(entityName))} after
${publicSchema}.${sql(makeHistoryTableName(entityName))} after
WHERE
after.serial >= (
SELECT
Expand All @@ -469,7 +470,7 @@ module.exports.getRollbackDiff = (sql, entityName, getFirstChangeSerial) => sql`
COALESCE(before.entity_history_log_index, 0) AS entity_history_log_index
FROM
-- Use a RIGHT JOIN, to ensure that nulls get returned if there is no "before" row
public.${sql(makeHistoryTableName(entityName))} before
${publicSchema}.${sql(makeHistoryTableName(entityName))} before
RIGHT JOIN rollback_ids after ON before.id = after.id
AND before.entity_history_block_timestamp = after.previous_entity_history_block_timestamp
AND before.entity_history_chain_id = after.previous_entity_history_chain_id
Expand Down
Loading