mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-25 03:33:33 +01:00
✨ db migrate script
This commit is contained in:
parent
7b63016872
commit
cb0885dd71
@ -15,7 +15,7 @@ import "missing-native-js-functions";
|
|||||||
// btw. we don't use class-validator for everything, because we need to explicitly set the type instead of deriving it from typescript also it doesn't easily support nested objects
|
// btw. we don't use class-validator for everything, because we need to explicitly set the type instead of deriving it from typescript also it doesn't easily support nested objects
|
||||||
|
|
||||||
export class BaseClassWithoutId extends BaseEntity {
|
export class BaseClassWithoutId extends BaseEntity {
|
||||||
constructor(private props?: any) {
|
constructor(props?: any) {
|
||||||
super();
|
super();
|
||||||
this.assign(props);
|
this.assign(props);
|
||||||
}
|
}
|
||||||
@ -56,7 +56,6 @@ export class BaseClassWithoutId extends BaseEntity {
|
|||||||
@BeforeUpdate()
|
@BeforeUpdate()
|
||||||
@BeforeInsert()
|
@BeforeInsert()
|
||||||
validate() {
|
validate() {
|
||||||
this.assign(this.props);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
47
util/src/migrations/migrate_db_engine.ts
Normal file
47
util/src/migrations/migrate_db_engine.ts
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import { config } from "dotenv";
|
||||||
|
config();
|
||||||
|
import * as Models from "../entities";
|
||||||
|
import { User } from "../entities/User";
|
||||||
|
import { createConnection, Connection } from "typeorm";
|
||||||
|
import { initDatabase } from "../util/Database";
|
||||||
|
import "missing-native-js-functions";
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
if (!process.env.FROM) throw new Error("FROM database env connection string not set");
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
const entities = Object.values(Models).filter((x) => x.constructor.name !== "Object" && x.name);
|
||||||
|
|
||||||
|
const newDB = await initDatabase();
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
const oldDB = await createConnection({
|
||||||
|
type: process.env.FROM.split(":")[0]?.replace("+srv", ""),
|
||||||
|
url: process.env.FROM,
|
||||||
|
entities,
|
||||||
|
name: "old",
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
entities.map(async (x) => {
|
||||||
|
const data = await oldDB.manager.find(User);
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
data.map(async (x) => {
|
||||||
|
try {
|
||||||
|
await newDB.manager.insert(User, x);
|
||||||
|
} catch (error) {
|
||||||
|
if (!x.id) throw new Error("object doesn't have a unique id: " + x);
|
||||||
|
await newDB.manager.update(User, { id: x.id }, x);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
// @ts-ignore
|
||||||
|
console.log("migrated all " + x.name);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log("SUCCESS migrated all data");
|
||||||
|
}
|
||||||
|
|
||||||
|
main().caught();
|
@ -11,7 +11,7 @@ var promise: Promise<any>;
|
|||||||
var dbConnection: Connection | undefined;
|
var dbConnection: Connection | undefined;
|
||||||
let dbConnectionString = process.env.DATABASE || path.join(process.cwd(), "database.db");
|
let dbConnectionString = process.env.DATABASE || path.join(process.cwd(), "database.db");
|
||||||
|
|
||||||
export function initDatabase() {
|
export function initDatabase(): Promise<Connection> {
|
||||||
if (promise) return promise; // prevent initalizing multiple times
|
if (promise) return promise; // prevent initalizing multiple times
|
||||||
|
|
||||||
const type = dbConnectionString.includes(":") ? dbConnectionString.split(":")[0]?.replace("+srv", "") : "sqlite";
|
const type = dbConnectionString.includes(":") ? dbConnectionString.split(":")[0]?.replace("+srv", "") : "sqlite";
|
||||||
@ -23,7 +23,8 @@ export function initDatabase() {
|
|||||||
type,
|
type,
|
||||||
url: isSqlite ? undefined : dbConnectionString,
|
url: isSqlite ? undefined : dbConnectionString,
|
||||||
database: isSqlite ? dbConnectionString : undefined,
|
database: isSqlite ? dbConnectionString : undefined,
|
||||||
entities: Object.values(Models).filter((x) => x.constructor.name !== "Object"),
|
// @ts-ignore
|
||||||
|
entities: Object.values(Models).filter((x) => x.constructor.name !== "Object" && x.name),
|
||||||
synchronize: type !== "mongodb",
|
synchronize: type !== "mongodb",
|
||||||
logging: false,
|
logging: false,
|
||||||
cache: {
|
cache: {
|
||||||
@ -31,6 +32,7 @@ export function initDatabase() {
|
|||||||
},
|
},
|
||||||
bigNumberStrings: false,
|
bigNumberStrings: false,
|
||||||
supportBigNumbers: true,
|
supportBigNumbers: true,
|
||||||
|
name: "default",
|
||||||
});
|
});
|
||||||
|
|
||||||
promise.then((connection) => {
|
promise.then((connection) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user