2024-02-21 10:04:33 +00:00
|
|
|
import { DataSource } from 'typeorm';
|
2024-01-24 05:00:19 +00:00
|
|
|
import debug from 'debug';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import { User } from '../src/entity/User';
|
|
|
|
import { Organization } from '../src/entity/Organization';
|
|
|
|
import { Project } from '../src/entity/Project';
|
|
|
|
import { UserOrganization } from '../src/entity/UserOrganization';
|
|
|
|
import { EnvironmentVariable } from '../src/entity/EnvironmentVariable';
|
|
|
|
import { Domain } from '../src/entity/Domain';
|
|
|
|
import { ProjectMember } from '../src/entity/ProjectMember';
|
|
|
|
import { Deployment } from '../src/entity/Deployment';
|
2024-02-21 10:04:33 +00:00
|
|
|
import { checkFileExists, getConfig, getEntities, loadAndSaveData } from '../src/utils';
|
2024-01-25 14:19:09 +00:00
|
|
|
import { Config } from '../src/config';
|
|
|
|
import { DEFAULT_CONFIG_FILE_PATH } from '../src/constants';
|
2024-01-24 05:00:19 +00:00
|
|
|
|
|
|
|
const log = debug('snowball:initialize-database');
|
|
|
|
|
|
|
|
const USER_DATA_PATH = './fixtures/users.json';
|
|
|
|
const PROJECT_DATA_PATH = './fixtures/projects.json';
|
|
|
|
const ORGANIZATION_DATA_PATH = './fixtures/organizations.json';
|
2024-02-20 05:23:42 +00:00
|
|
|
const USER_ORGANIZATION_DATA_PATH = './fixtures/user-organizations.json';
|
2024-01-24 11:28:07 +00:00
|
|
|
const PROJECT_MEMBER_DATA_PATH = './fixtures/project-members.json';
|
2024-01-31 12:25:19 +00:00
|
|
|
const PRIMARY_DOMAIN_DATA_PATH = './fixtures/primary-domains.json';
|
2024-01-24 11:28:07 +00:00
|
|
|
const DEPLOYMENT_DATA_PATH = './fixtures/deployments.json';
|
|
|
|
const ENVIRONMENT_VARIABLE_DATA_PATH = './fixtures/environment-variables.json';
|
2024-01-31 12:25:19 +00:00
|
|
|
const REDIRECTED_DOMAIN_DATA_PATH = './fixtures/redirected-domains.json';
|
2024-01-24 05:00:19 +00:00
|
|
|
|
|
|
|
const generateTestData = async (dataSource: DataSource) => {
|
2024-02-21 10:04:33 +00:00
|
|
|
const userEntities = await getEntities(path.resolve(__dirname, USER_DATA_PATH));
|
|
|
|
const savedUsers = await loadAndSaveData(User, dataSource, userEntities);
|
|
|
|
|
|
|
|
const orgEntities = await getEntities(path.resolve(__dirname, ORGANIZATION_DATA_PATH));
|
|
|
|
const savedOrgs = await loadAndSaveData(Organization, dataSource, orgEntities);
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
const projectRelations = {
|
|
|
|
owner: savedUsers,
|
|
|
|
organization: savedOrgs
|
|
|
|
};
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-02-21 10:04:33 +00:00
|
|
|
const projectEntities = await getEntities(path.resolve(__dirname, PROJECT_DATA_PATH));
|
|
|
|
const savedProjects = await loadAndSaveData(Project, dataSource, projectEntities, projectRelations);
|
2024-01-24 11:28:07 +00:00
|
|
|
|
2024-02-05 09:26:28 +00:00
|
|
|
const domainRepository = dataSource.getRepository(Domain);
|
|
|
|
|
2024-01-31 12:25:19 +00:00
|
|
|
const domainPrimaryRelations = {
|
2024-01-29 12:48:43 +00:00
|
|
|
project: savedProjects
|
|
|
|
};
|
|
|
|
|
2024-02-21 10:04:33 +00:00
|
|
|
const primaryDomainsEntities = await getEntities(path.resolve(__dirname, PRIMARY_DOMAIN_DATA_PATH));
|
|
|
|
const savedPrimaryDomains = await loadAndSaveData(Domain, dataSource, primaryDomainsEntities, domainPrimaryRelations);
|
2024-01-31 12:25:19 +00:00
|
|
|
|
|
|
|
const domainRedirectedRelations = {
|
|
|
|
project: savedProjects,
|
|
|
|
redirectTo: savedPrimaryDomains
|
|
|
|
};
|
|
|
|
|
2024-02-21 10:04:33 +00:00
|
|
|
const redirectDomainsEntities = await getEntities(path.resolve(__dirname, REDIRECTED_DOMAIN_DATA_PATH));
|
|
|
|
await loadAndSaveData(Domain, dataSource, redirectDomainsEntities, domainRedirectedRelations);
|
2024-01-31 12:25:19 +00:00
|
|
|
|
|
|
|
const savedDomains = await domainRepository.find();
|
2024-01-29 12:48:43 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
const userOrganizationRelations = {
|
|
|
|
member: savedUsers,
|
|
|
|
organization: savedOrgs
|
|
|
|
};
|
|
|
|
|
2024-02-21 10:04:33 +00:00
|
|
|
const userOrganizationsEntities = await getEntities(path.resolve(__dirname, USER_ORGANIZATION_DATA_PATH));
|
|
|
|
await loadAndSaveData(UserOrganization, dataSource, userOrganizationsEntities, userOrganizationRelations);
|
2024-01-24 11:28:07 +00:00
|
|
|
|
|
|
|
const projectMemberRelations = {
|
|
|
|
member: savedUsers,
|
|
|
|
project: savedProjects
|
|
|
|
};
|
|
|
|
|
2024-02-21 10:04:33 +00:00
|
|
|
const projectMembersEntities = await getEntities(path.resolve(__dirname, PROJECT_MEMBER_DATA_PATH));
|
|
|
|
await loadAndSaveData(ProjectMember, dataSource, projectMembersEntities, projectMemberRelations);
|
2024-01-24 11:28:07 +00:00
|
|
|
|
|
|
|
const deploymentRelations = {
|
|
|
|
project: savedProjects,
|
2024-01-30 10:18:50 +00:00
|
|
|
domain: savedDomains,
|
|
|
|
createdBy: savedUsers
|
2024-01-24 11:28:07 +00:00
|
|
|
};
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-02-21 10:04:33 +00:00
|
|
|
const deploymentsEntities = await getEntities(path.resolve(__dirname, DEPLOYMENT_DATA_PATH));
|
|
|
|
await loadAndSaveData(Deployment, dataSource, deploymentsEntities, deploymentRelations);
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
const environmentVariableRelations = {
|
|
|
|
project: savedProjects
|
|
|
|
};
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-02-21 10:04:33 +00:00
|
|
|
const environmentVariablesEntities = await getEntities(path.resolve(__dirname, ENVIRONMENT_VARIABLE_DATA_PATH));
|
|
|
|
await loadAndSaveData(EnvironmentVariable, dataSource, environmentVariablesEntities, environmentVariableRelations);
|
2024-01-24 05:00:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const main = async () => {
|
2024-01-25 14:19:09 +00:00
|
|
|
const config = await getConfig<Config>(DEFAULT_CONFIG_FILE_PATH);
|
|
|
|
const isDbPresent = await checkFileExists(config.database.dbPath);
|
2024-01-24 11:28:07 +00:00
|
|
|
|
|
|
|
if (!isDbPresent) {
|
|
|
|
const dataSource = new DataSource({
|
|
|
|
type: 'better-sqlite3',
|
2024-01-25 14:19:09 +00:00
|
|
|
database: config.database.dbPath,
|
2024-01-24 11:28:07 +00:00
|
|
|
synchronize: true,
|
|
|
|
logging: true,
|
|
|
|
entities: [path.join(__dirname, '../src/entity/*')]
|
|
|
|
});
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
await dataSource.initialize();
|
2024-01-24 05:00:19 +00:00
|
|
|
|
2024-01-24 11:28:07 +00:00
|
|
|
await generateTestData(dataSource);
|
2024-01-25 12:04:13 +00:00
|
|
|
log('Data loaded successfully');
|
2024-01-25 05:47:44 +00:00
|
|
|
} else {
|
2024-01-25 12:04:13 +00:00
|
|
|
log('WARNING: Database already exists');
|
2024-01-24 11:28:07 +00:00
|
|
|
}
|
2024-01-24 05:00:19 +00:00
|
|
|
};
|
|
|
|
|
2024-01-25 12:04:13 +00:00
|
|
|
main().catch((err) => {
|
|
|
|
log(err);
|
|
|
|
});
|