Skip to content

Commit

Permalink
feat: ✨ migration update (#62)
Browse files Browse the repository at this point in the history
* feat: ✨ enable migrations

* feat: ✨ add migrate script to bot package.json

* fix: 🐛 enable sign in button
  • Loading branch information
slugb0t authored Aug 29, 2024
1 parent 099bf89 commit ff0c312
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 70 deletions.
115 changes: 115 additions & 0 deletions bot/dev-migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import fs from "fs";
import dotenv from "dotenv";
import { MongoClient } from "mongodb";
import { consola } from "consola";

if (process.env.NODE_ENV !== "production") {
dotenv.config({
path: ".env",
});
}

if (!process.env.MONGODB_URI) {
throw new Error("Please define the MONGODB_URI environment variable");
}

if (!process.env.MONGODB_DB_NAME) {
throw new Error("Please define the MONGODB_DB_NAME environment variable");
}

const uri = process.env.MONGODB_URI;
const dbName = process.env.MONGODB_DB_NAME;
const devDbName = `${dbName}-migrations-test`;

// Create a new MongoClient
const client = new MongoClient(uri);

await client.connect();

const database = client.db(dbName);

if (!database) {
throw new Error("Database not found");
}

const devDatabase = client.db(devDbName);

// FOR DEV ONLY
// Clone the original database
// Get the list of collections in the original database
consola.start("Cloning the original database...");

const collections = await database.listCollections().toArray();

for (const collection of collections) {
const collectionName = collection.name;

// Get the collection
const dbCollection = database.collection(collectionName);

// Get the documents in the collection
const documents = await dbCollection.find().toArray();

// Drop the collection in the new database if it exists
await devDatabase.dropCollection(collectionName).catch(() => {});

// Create a new collection in the new database
const newCollection = devDatabase.collection(collectionName);

// Insert the documents into the new collection
if (documents.length > 0) {
await newCollection.insertMany(documents);
}
}

consola.success("Database cloned!");

// Get the list of files in the migrations folder
const files = fs.readdirSync("./migrations");

// Order the files by name
files.sort();

const migrationsCollection = devDatabase.collection("migrations");

for (const file of files) {
// Get the file name without the extension
const migrationId = file.replace(".ts", "");

// Check if the migration has already been applied
const migration = await migrationsCollection.findOne({
migration_id: migrationId,
});

if (migration) {
consola.warn(`Migration ${migrationId} has already been applied`);
continue;
}

// Import the migration file
const { default: migrationFunction } = await import(
`./migrations/${migrationId}`
);

consola.start(`Applying migration ${migrationId}`);

try {
// Run the migration
await migrationFunction(uri, devDbName, migrationId);
} catch (error) {
consola.error(`Error applying migration ${migrationId}: ${error.message}`);
throw error;
}

consola.success(`Migration ${migrationId} has been applied!`);

// Insert the migration into the migrations collection
// await migrationsCollection.insertOne({
// migration_id: migrationId,
// created_at: new Date(),
// });
}

await client.close();

process.exit();
48 changes: 7 additions & 41 deletions bot/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from "fs";
import dotenv from "dotenv";
import { MongoClient } from "mongodb";
import fs from "fs";
import { consola } from "consola";
import { nanoid } from "nanoid";

if (process.env.NODE_ENV !== "production") {
dotenv.config({
Expand All @@ -20,7 +19,6 @@ if (!process.env.MONGODB_DB_NAME) {

const uri = process.env.MONGODB_URI;
const dbName = process.env.MONGODB_DB_NAME;
const devDbName = `${dbName}-migrations-test`;

// Create a new MongoClient
const client = new MongoClient(uri);
Expand All @@ -33,45 +31,13 @@ if (!database) {
throw new Error("Database not found");
}

const devDatabase = client.db(devDbName);

// FOR DEV ONLY
// Clone the original database
// Get the list of collections in the original database
consola.start("Cloning the original database...");

const collections = await database.listCollections().toArray();

for (const collection of collections) {
const collectionName = collection.name;

// Get the collection
const dbCollection = database.collection(collectionName);

// Get the documents in the collection
const documents = await dbCollection.find().toArray();

// Drop the collection in the new database if it exists
await devDatabase.dropCollection(collectionName).catch(() => {});

// Create a new collection in the new database
const newCollection = devDatabase.collection(collectionName);

// Insert the documents into the new collection
if (documents.length > 0) {
await newCollection.insertMany(documents);
}
}

consola.success("Database cloned!");

// Get the list of files in the migrations folder
const files = fs.readdirSync("./migrations");

// Order the files by name
files.sort();

const migrationsCollection = devDatabase.collection("migrations");
const migrationsCollection = database.collection("migrations");

for (const file of files) {
// Get the file name without the extension
Expand All @@ -96,7 +62,7 @@ for (const file of files) {

try {
// Run the migration
await migrationFunction(uri, devDbName, migrationId);
await migrationFunction(uri, dbName, migrationId);
} catch (error) {
consola.error(`Error applying migration ${migrationId}: ${error.message}`);
throw error;
Expand All @@ -105,10 +71,10 @@ for (const file of files) {
consola.success(`Migration ${migrationId} has been applied!`);

// Insert the migration into the migrations collection
// await migrationsCollection.insertOne({
// migration_id: migrationId,
// created_at: new Date(),
// });
await migrationsCollection.insertOne({
created_at: new Date(),
migration_id: migrationId,
});
}

await client.close();
Expand Down
22 changes: 6 additions & 16 deletions bot/migrations/1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ const migrationFunction = async (
const installationCollection = database.collection("installation");

consola.start(
"Replacing 'installationId' with 'installation_id' in the 'installation' collection",
"Adding 'action_count' to entries in the 'installation' collection",
);

// Get all the records where the 'installationId' field exists
const installationsWithInstallationId = await installationCollection
.find({
installationId: { $exists: true },
action_count: { $exists: false },
})
.toArray();

Expand All @@ -40,33 +40,23 @@ const migrationFunction = async (
document_id: installation._id,
field: "installation_id",
migration_id: migrationId,
text: "Update the 'installationId' field to 'installation_id'",
value: installation.installationId,
text: "Set action_count to 0",
value: null,
});

// Update the 'installationId' field to 'installation_id'
await installationCollection.updateOne(
{ _id: installation._id },
{
$set: {
installation_id: installation.installationId,
},
},
);

// Delete the 'installationId' field
await installationCollection.updateOne(
{ _id: installation._id },
{
$unset: {
installationId: "",
action_count: 0,
},
},
);
});

consola.success(
"Replaced 'installationId' with 'installation_id' in the 'installation' collection",
"Add action_count key entries in the 'installation' collection",
);
};

Expand Down
5 changes: 3 additions & 2 deletions bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
"scripts": {
"dev-old": "probot run ./index.js ",
"dev": "nodemon --watch ./index.js --watch .env --watch ./package.json --watch ./utils/* --exec \"run-p dev:smee dev:start\"",
"start": "node ./main.js",
"start": "npm run migrate && node ./main.js",
"dev:smee": "smee -u https://smee.io/n0NQN1LUpx2kI2T1 -p 3001",
"dev:start": "node -r dotenv/config ./main.js",
"build-css": "tailwindcss build -i ./public/assets/css/tailwind.css -o ./public/assets/css/styles.css",
"watch-css": "npx tailwindcss build -i ./public/assets/css/tailwind.css -o ./public/assets/css/styles.css --watch",
"build": "npm run build-css",
"dev-migrate": "tsx ./migrate.ts",
"migrate": "tsx ./migrate.ts",
"dev-migrate": "tsx ./dev-migrate.ts",
"test": "jest"
},
"dependencies": {
Expand Down
12 changes: 1 addition & 11 deletions ui/components/ProfileStatus.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
<script setup lang="ts">
const user = useUser();
const route = useRoute();
const loggedIn = computed(() => !!user.value);
const hideLoginPages = ["/login", "/", "/fairsoftware", "/codefair"];
const devMode = process.env.NODE_ENV === "development";
const onHideLoginPages = computed(() => {
return hideLoginPages.includes(route.path);
// return false;
});
async function logout() {
await $fetch("/api/logout", {
method: "POST",
Expand All @@ -36,7 +26,7 @@ async function logout() {
</n-flex>

<div v-else>
<a v-if="!onHideLoginPages" href="/login/github">
<a href="/login/github">
<n-button color="black">
<template #icon>
<Icon name="bi:github" />
Expand Down

0 comments on commit ff0c312

Please sign in to comment.