Skip to content

Commit

Permalink
feat(script): add isupcomingchange table to seed script (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
moT01 authored May 14, 2024
1 parent 95fe1fe commit fe0266c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
24 changes: 24 additions & 0 deletions dolt/block-is-upcoming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { readFileSync, readdirSync } from 'fs';

let upcomingBlocks = {};
const metaPath = '../curriculum/challenges/_meta';

const filesToIgnore = ['.DS_Store'];

try {
const metaDir = readdirSync(metaPath);

metaDir.forEach(file => {
if (!filesToIgnore.includes(file)) {
const metaFile = readFileSync(`${metaPath}/${file}/meta.json`, 'utf8');
const metaJson = JSON.parse(metaFile);
upcomingBlocks[metaJson.dashedName] = metaJson.isUpcomingChange;
}
});
} catch (error) {
console.error('Error:', error);
}

export const getBlockIsUpcoming = dashedName => {
return upcomingBlocks[dashedName];
};
28 changes: 18 additions & 10 deletions dolt/challenge-type-to-tables-map.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
import { challengeTypes } from '../shared/config/challenge-types.js';
import { insert } from './utils.js';

const ids = {
app_url: 1,
source_code_url: 1,
local_address_allowed: 1,
editor_address_allowed: 1,
display_preview_modal: 1
};

const app_url = required => async (connection, challenge) =>
await insert(
connection,
'app_url',
['challenge_id', 'required'],
[challenge.id, required]
['id', 'challenge_id', 'required'],
[ids.app_url++, challenge.id, required]
);
const source_code_url = required => async (connection, challenge) =>
await insert(
connection,
'source_code_url',
['challenge_id', 'required'],
[challenge.id, required]
['id', 'challenge_id', 'required'],
[ids.source_code_url++, challenge.id, required]
);
const local_address_allowed = async (connection, challenge) =>
await insert(
connection,
'local_address_allowed',
['challenge_id'],
[challenge.id]
['id', 'challenge_id'],
[ids.local_address_allowed++, challenge.id]
);
const editor_address_allowed = async (connection, challenge) =>
await insert(
connection,
'editor_address_allowed',
['challenge_id'],
[challenge.id]
['id', 'challenge_id'],
[ids.editor_address_allowed++, challenge.id]
);

const display_preview_modal = async (connection, challenge) => {
Expand All @@ -39,8 +47,8 @@ const display_preview_modal = async (connection, challenge) => {
await insert(
connection,
'display_preview_modal',
['challenge_id'],
[challenge.id]
['id', 'challenge_id'],
[ids.display_preview_modal++, challenge.id]
);
}
};
Expand Down
27 changes: 24 additions & 3 deletions dolt/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from './utils.js';
import { challengeTypeToTablesMap } from './challenge-type-to-tables-map.js';
import { getSuperblockTitle, getBlockTitle } from './dashed-names-to-titles.js';
import { getBlockIsUpcoming } from './block-is-upcoming.js';

export async function withConnection(connectionString, callback) {
const connection = createConnection(connectionString);
Expand Down Expand Up @@ -97,6 +98,15 @@ CREATE TABLE IF NOT EXISTS block_time_to_complete(
)`;
await runCreateTable(connection, sql);

sql = `
CREATE TABLE IF NOT EXISTS block_is_upcoming(
id INT NOT NULL AUTO_INCREMENT,
block_id INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (block_id) REFERENCES blocks(id)
)`;
await runCreateTable(connection, sql);

// Additional feature tables to replace challengeTypes
sql = `
CREATE TABLE IF NOT EXISTS app_url(
Expand Down Expand Up @@ -252,6 +262,7 @@ export async function addChallenges(connection, data) {
const feature_table_ids = {};
let block_id = 1;
let superblock_id = 1;
let block_is_upcoming_id = 1;
let c = 1;

for (const challengeNode of data) {
Expand Down Expand Up @@ -304,14 +315,24 @@ export async function addChallenges(connection, data) {
blockSet.add(block);
block_to_block_id_map.set(block, block_id);

// Add `block_time_to_complete` as special case, because it is per-block
// Add `block_time_to_complete` and `block_is_upcoming` as special case, because it is per-block
await insert(
connection,
'block_time_to_complete',
['block_id', 'time_to_complete'],
[block_id, time]
['id', 'block_id', 'time_to_complete'],
[block_id, block_id, time]
);

const blockIsUpcoming = getBlockIsUpcoming(block);
if (blockIsUpcoming) {
await insert(
connection,
'block_is_upcoming',
['id', 'block_id'],
[block_is_upcoming_id++, block_id]
);
}

const superblockId = superblock_to_superblock_id_map.get(superBlock);
await insert(
connection,
Expand Down
8 changes: 8 additions & 0 deletions dolt/queries/create-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ CREATE TABLE IF NOT EXISTS block_time_to_complete(
)


CREATE TABLE IF NOT EXISTS block_is_upcoming(
id INT NOT NULL AUTO_INCREMENT,
block_id INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (block_id) REFERENCES blocks(id)
)


CREATE TABLE IF NOT EXISTS app_url(
id INT AUTO_INCREMENT,
challenge_id INT NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions dolt/queries/drop-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ DROP TABLE IF EXISTS app_url;
DROP TABLE IF EXISTS assignments;
DROP TABLE IF EXISTS audio_path;
DROP TABLE IF EXISTS bilibili_ids;
DROP TABLE IF EXISTS block_is_upcoming;
DROP TABLE IF EXISTS block_time_to_complete;
DROP TABLE IF EXISTS blocks_challenges;
DROP TABLE IF EXISTS certifications_prerequisites;
Expand Down

0 comments on commit fe0266c

Please sign in to comment.