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

fix database dropping for views and fileId size #88

Merged
merged 1 commit into from
Apr 6, 2024
Merged
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
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
Loading