Skip to content

Commit

Permalink
fix database dropping for views and fileId size
Browse files Browse the repository at this point in the history
  • Loading branch information
wrfhowell committed Apr 6, 2024
1 parent 2944f95 commit 6869f2a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion resources/database/migrations/1710395365.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CREATE TABLE IF NOT EXISTS document (
id INT AUTO_INCREMENT PRIMARY KEY,
areaid INT NOT NULL,
title VARCHAR(500) NOT NULL,
fileid VARCHAR(2000) NOT NULL UNIQUE,
fileid VARCHAR(500) NOT NULL UNIQUE,
createdat TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedat TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Foreign Key (areaid) references area(id) ON DELETE CASCADE,
Expand Down
27 changes: 20 additions & 7 deletions resources/database/setup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import chalk from 'chalk';
import * as path from 'path';
import * as fs from 'fs';
import dotenv from 'dotenv';
import * as fs from 'fs';
import mysql, { Connection } from 'mysql2';
import * as path from 'path';
dotenv.config();

const DATABASE_META_NAME = 'meta';
const DATABASE_NAME = 'cosmic-dev';
const DATABASE_NAME = process.env.DATABASE_NAME;
const MIGRATION_PATH = './resources/database/migrations';

const setUpDatabase = async (
Expand Down Expand Up @@ -60,13 +59,27 @@ const resetDatabase = async (connection: Connection): Promise<void> => {
chalk.bold.underline(database.Database)
)
);

// delete views first
const views = await query(
connection,
`SHOW FULL TABLES IN ?? WHERE TABLE_TYPE = 'VIEW'`,
[database.Database]
);
const viewNames = views.map((view) => Object.values(view)[0]);
if (viewNames.length > 0) {
const viewDropQuery = `DROP VIEW ${viewNames.join(', ')}`;
await query(connection, viewDropQuery);
}

const tables = await query(connection, `SHOW TABLES IN ??`, [
database.Database,
]);

const tableNames = tables.map((table) => Object.values(table)[0]);
const queryStr = `DROP TABLE ${tableNames.join(', ')}`;
await query(connection, queryStr);
if (tableNames.length > 0) {
const tableDropQuery = `DROP TABLE ${tableNames.join(', ')}`;
await query(connection, tableDropQuery);
}
}
}
};
Expand Down

0 comments on commit 6869f2a

Please sign in to comment.