Skip to content

Commit

Permalink
fix(datastore): failing private table tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Jan 11, 2023
1 parent e10a7e7 commit e844ebe
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions datastore/client/lib/DatastoreInternal.ts
Expand Up @@ -170,7 +170,7 @@ export default class DatastoreInternal<
const { name, description, paymentAddress, giftCardIssuerIdentity, remoteDatastores } =
this.components;

const metadata = {
const metadata: IDatastoreMetadata = {
name,
description,
paymentAddress,
Expand Down Expand Up @@ -211,11 +211,11 @@ export default class DatastoreInternal<
}

for (const [funcName, table] of Object.entries(this.tables ?? {})) {
if (!table.isPublic) continue;
const passThrough = table as unknown as PassthroughTable<any, any>;
metadata.tablesByName[funcName] = {
name: table.name,
description: table.description,
isPublic: table.isPublic !== false,
schema: table.schema,
remoteSource: passThrough?.remoteSource,
remoteTable: passThrough?.remoteTable,
Expand Down
9 changes: 5 additions & 4 deletions datastore/core/endpoints/Datastore.query.ts
Expand Up @@ -40,15 +40,16 @@ export default new DatastoreApiHandler('Datastore.query', {
const sqlParser = new SqlParser(request.sql);
if (!sqlParser.isSelect()) throw new Error('Invalid SQL command');

const metadata = datastore.metadata;
sqlParser.functionNames.forEach(functionName => {
const schema = metadata.functionsByName[functionName].schema || {};
const schema = datastore.functions[functionName].schema ?? {};
storage.addFunctionSchema(functionName, schema);
});

sqlParser.tableNames.forEach(name => {
const schema = metadata.tablesByName[name]?.schema || {};
storage.addTableSchema(name, schema, !!metadata.tablesByName[name]?.remoteSource);
const table = datastore.tables[name];
if (!table.isPublic) throw new Error(`Table ${name} is not publicly accessible.`);
const schema = table.schema ?? {};
storage.addTableSchema(name, schema, !!table?.remoteSource);
});

const inputByFunctionName = sqlParser.extractFunctionInputs(
Expand Down
2 changes: 1 addition & 1 deletion datastore/core/endpoints/Datastore.queryInternalTable.ts
Expand Up @@ -21,7 +21,7 @@ export default new DatastoreApiHandler('Datastore.queryInternalTable', {
);
const datastore = await DatastoreVm.open(registryEntry.path, manifest);
storage = new DatastoreStorage(storagePath);
schema = datastore.metadata.tablesByName[tableName].schema;
schema = datastore.tables[tableName].schema;
} else {
context.connectionToClient.datastoreStorage ??= new DatastoreStorage();
storage = context.connectionToClient?.datastoreStorage;
Expand Down
2 changes: 1 addition & 1 deletion datastore/core/lib/SqlQuery.ts
Expand Up @@ -47,7 +47,7 @@ export default class SqlQuery {
// eslint-disable-next-line @typescript-eslint/no-loop-func
*rows() {
for (const record of outputs)
yield SqlGenerator.convertFunctionRecordToSqliteRow(record, schema, tmpSchemas);
yield SqlGenerator.convertTableRecordToSqlite(record, schema);
},
});
}
Expand Down
24 changes: 13 additions & 11 deletions datastore/core/test/Datastore.clone.test.ts
Expand Up @@ -10,6 +10,7 @@ const storageDir = Path.resolve(process.env.ULX_DATA_DIR ?? '.', 'Datastore.clon

let miner: UlixeeMiner;
let client: DatastoreApiClient;
let versionHash: string;

beforeAll(async () => {
if (Fs.existsSync(`${__dirname}/datastores/cloneme.dbx`)) {
Expand All @@ -28,7 +29,12 @@ beforeAll(async () => {
miner.router.datastoreConfiguration = { datastoresDir: storageDir };
await miner.listen();
client = new DatastoreApiClient(await miner.address);
});

const packager = new DatastorePackager(`${__dirname}/datastores/cloneme.ts`);
await packager.build();
await client.upload(await packager.dbx.asBuffer());
versionHash = packager.manifest.versionHash;
}, 45e3);

afterEach(Helpers.afterEach);

Expand All @@ -38,14 +44,6 @@ afterAll(async () => {
});

test('should be able to clone a datastore', async () => {
let versionHash: string;
{
const packager = new DatastorePackager(`${__dirname}/datastores/cloneme.ts`);
await packager.build();
await client.upload(await packager.dbx.asBuffer());
versionHash = packager.manifest.versionHash;
}

const url = `ulx://${await miner.address}/${versionHash}`;
await expect(cloneDatastore(url, `${__dirname}/datastores/cloned.ts`)).resolves.toBeUndefined();

Expand Down Expand Up @@ -92,5 +90,9 @@ test('should be able to clone a datastore', async () => {
// can query the passthrough table
await expect(
client.query(packager.manifest.versionHash, 'select * from users', {}),
).resolves.toEqual([{ name: 'me', birthdate: expect.any(Date) }]);
});
).resolves.toEqual({
metadata: expect.any(Object),
outputs: [{ name: 'me', birthdate: expect.any(Date) }],
latestVersionHash: packager.manifest.versionHash,
});
}, 45e3);
2 changes: 2 additions & 0 deletions datastore/packager/index.ts
Expand Up @@ -131,6 +131,8 @@ export default class DatastorePackager {

if (this.meta.tablesByName) {
for (const [name, tableMeta] of Object.entries(this.meta.tablesByName)) {
// don't publish private tables
if (tableMeta.isPublic === false) continue;
const { schema } = tableMeta;
if (schema) {
schemaInterface.tables[name] = schemaFromJson(schema);
Expand Down
2 changes: 1 addition & 1 deletion sql/engine/lib/Generator.ts
Expand Up @@ -97,7 +97,7 @@ export default class SqlGenerator {
}

public static convertFunctionRecordToSqliteRow(
record: any,
record: Record<string, any>,
schema: Record<string, IAnySchemaJson>,
tmpSchema: any = {},
): any {
Expand Down

0 comments on commit e844ebe

Please sign in to comment.