Add migrations

This commit is contained in:
TheArcaneBrony
2022-08-07 21:43:38 +02:00
parent 7af574b0e8
commit 29e88a440f
6 changed files with 2519 additions and 30 deletions
+32
View File
@@ -0,0 +1,32 @@
#!/bin/sh
read -p "Enter migration filename: " FILENAME
[ -f ".env" ] && (
mv .env .env.tmp
source .env.tmp
)
make_migration() {
echo "Creating migrations for $2"
mkdir "../util/src/migrations/$2"
npm run build clean logerrors pretty-errors
THREADS=1 DATABASE="$1" DB_MIGRATE=a npm run start:bundle
THREADS=1 DATABASE="$1" DB_MIGRATE=a npx typeorm-ts-node-commonjs migration:generate "../util/src/migrations/$2/$FILENAME" -d ../util/src/util/Database.ts -p
npm run build clean logerrors pretty-errors
THREADS=1 DATABASE="$1" DB_MIGRATE=a npm run start:bundle
}
npm i sqlite3
make_migration "database.db" "sqlite"
[ -z "$FC_DB_POSTGRES" ] || (
npm i pg
make_migration "$FC_DB_POSTGRES" "postgres"
)
[ -z "$FC_DB_MARIADB" ] || (
npm i mysql2
make_migration "$FC_DB_MARIADB" "mariadb"
)
[ -f ".env.tmp" ] && mv .env.tmp .env
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -1,7 +1,7 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class test1659833042721 implements MigrationInterface {
name = 'test1659833042721'
export class initial1659899662635 implements MigrationInterface {
name = 'initial1659899662635'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
@@ -411,13 +411,6 @@ export class test1659833042721 implements MigrationInterface {
"is_primary" boolean
)
`);
await queryRunner.query(`
CREATE TABLE "migrations" (
"id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
"timestamp" bigint NOT NULL,
"name" varchar NOT NULL
)
`);
await queryRunner.query(`
CREATE TABLE "rate_limits" (
"id" varchar PRIMARY KEY NOT NULL,
@@ -3438,9 +3431,6 @@ export class test1659833042721 implements MigrationInterface {
await queryRunner.query(`
DROP TABLE "rate_limits"
`);
await queryRunner.query(`
DROP TABLE "migrations"
`);
await queryRunner.query(`
DROP TABLE "categories"
`);
+3 -3
View File
@@ -19,10 +19,10 @@ export const Config = {
config = (config || {}).merge(DefaultConfigOptions);
// try {
// const overrideConfig = JSON.parse(fs.readFileSync(overridePath, { encoding: "utf8" }));
// config = overrideConfig.merge(config);
// const overrideConfig = JSON.parse(fs.readFileSync(overridePath, { encoding: "utf8" }));
// config = overrideConfig.merge(config);
// } catch (error) {
// fs.writeFileSync(overridePath, JSON.stringify(config, null, 4));
// fs.writeFileSync(overridePath, JSON.stringify(config, null, 4));
// }
return this.set(config);
+18 -15
View File
@@ -7,6 +7,7 @@ import { yellow, green, red } from "picocolors";
import fs from "fs";
import { exit } from "process";
import { BaseClass, BaseClassWithoutId } from "../entities";
import { config } from "dotenv";
// UUID extension option is only supported with postgres
// We want to generate all id's with Snowflakes that's why we have our own BaseEntity class
@@ -14,28 +15,29 @@ import { BaseClass, BaseClassWithoutId } from "../entities";
let promise: Promise<any>;
let dataSource: DataSource;
export async function initDatabase(): Promise<DataSource> {
if (dataSource) return dataSource; // prevent initalizing multiple times
let dso = getDataSourceOptions();
console.log(`[Database] ${yellow(`Connecting to ${dso.type} database...`)}`);
//promise = dataSource.initialize();
//await promise;
//if (dataSource) return dataSource; // prevent initalizing multiple times
if(dataSource.isInitialized) return dataSource;
await dataSource.initialize();
console.log(`[Database] ${green("Connected!")}`);
await dataSource.runMigrations();
console.log(`[Database] ${green("Up to date!")}`);
return promise;
if("DB_MIGRATE" in process.env) {
console.log("DB_MIGRATE specified, exiting!")
exit(0);
}
return dataSource;
}
export function closeDatabase() {
dataSource?.destroy();
}
function getDataSourceOptions(): DataSourceOptions {
config();
//get connection string and check for migrations
const dbConnectionString = process.env.DATABASE || path.join(process.cwd(), "database.db");
const type = dbConnectionString.includes("://") ? dbConnectionString.split(":")[0]?.replace("+srv", "") : "sqlite" as any;
@@ -61,9 +63,9 @@ function getDataSourceOptions(): DataSourceOptions {
migrations_exist: migrationsExist
}, null, 4))}`);
//exit(1);
if(!("DB_MIGRATE" in process.env)) exit(1);
}
console.log(`[Database] ${yellow(`Configuring data source to use ${type} database...`)}`);
return {
type,
charset: 'utf8mb4',
@@ -82,9 +84,10 @@ function getDataSourceOptions(): DataSourceOptions {
name: "default",
migrations: synchronizeInsteadOfMigrations ? [] : [path.join(__dirname, "..", "migrations", type, "*.js")],
migrationsRun: !synchronizeInsteadOfMigrations,
//migrationsRun: false,
cli: {
migrationsDir: `src/migrations/${type}`
}
},
} as DataSourceOptions;
}
@@ -97,4 +100,4 @@ function shouldIncludeEntity(name: string): boolean {
].map(x=>x.name).includes(name);
}
export default dataSource = new DataSource(getDataSourceOptions());
export default dataSource = new DataSource(getDataSourceOptions());