import { config } from 'dotenv' config({ path: '.envtest' }) import { Client } from 'pg' import util from 'util' import * as child from 'child_process' const exec = util.promisify(child.exec) const connectionString = 'postgresql://postgres:k0fpEoFJetxkObPD@localhost:5432/' export default async function setup() { console.log(`\nGlobal setup - NODE_ENV=${process.env.NODE_ENV}`) const client = await connect() await terminateIdleConnections(client) await cleanDatabase(client) console.log('creating database & tables') await exec('npx prisma migrate up --experimental --create-db') await createUser() console.log('setup complete') } async function connect() { console.log(`connecting to ${connectionString}`) const client = new Client({ connectionString, }) await client.connect() return client } async function cleanDatabase(client: Client) { const database = 'test' console.log(`cleaning database ${database}`) try { await client.query(`DROP DATABASE IF EXISTS ${database}`) } catch (err) { console.error(err) } await client.end() } async function terminateIdleConnections(client: Client) { await client.query(`SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'test'`) } async function createUser() { const databaseConneciton = `${connectionString}test` const client = new Client({ connectionString: databaseConneciton, }) await client.connect() await client.query(` INSERT INTO public."User" (id, role, "cantDeliverEmailReason", "signedInPlatform", "createdAt", "updatedAt", email, title, username, birthday, "secondaryEmail", fullname, "firstName", "lastName", "cantDeliverSMS", "cantDeliverEmail", "hasAppNotificationEnable", "hasEmailNotificationEnable", "hasSMSNotificationEnable", "currentAppVersion", password, "hasResetPassword", "resetPasswordToken", "expoNotificationToken", "notificationToken", "signedInAt", "condoManagerNoCompteReel", "condoManagerCompteUserNumber", "appNotificationsBadgesNumber", "isSignedOut", "appLoginCode", "hasReceivedOnboarding", "hasReceivedInvitation", "hasInstalledApp", "hasInstalledDesktop", "hasUninstalledApp", "hasDog", "stripeCustomerId", "operatingSystem", "emergencyContact", "preferedLanguage", active, "isResident", "homePhoneId", "officePhoneId", "phoneId", "serviceProviderId", "avatarId", "managingCompanyRoleId", "managingCompanyId", "addressId", "propertyId", "copropertyDueAmount", "hasPaymentError", "oneSignalUserId", "isTenant", "hasReceivedInvitationAt", "hasReceivedInvitationByEmail", "hasReceivedInvitationByEmailAt", "hasOpenedEmailInvitation", "hasOpenedEmailInvitationAt", "hasClickedOnCTAInEmailInvitation", "hasClickedOnCTAInEmailInvitationAt", "hasReceivedInvitationBySMS", "hasReceivedInvitationBySMSAt", "hasRegisterAt", "waitingForApproval", "createdById", "signedInPlatformId", "hasReceivedSecondInvitation", "hasReceivedSecondInvitationAt", "hasReceivedSecondInvitationByEmail", "hasReceivedSecondInvitationByEmailAt", "hasReceivedSecondInvitationBySMS", "hasReceivedSecondInvitationBySMSAt", "hasPrivateChat", "condoManagerId", "hasAlarmSystem", "managingCompanyName", "hasOpenedSecondEmailInvitation", "hasOpenedSecondEmailInvitationAt", "hasClickedOnCTAInSecondEmailInvitation", "hasClickedOnCTAInSecondEmailInvitationAt", "hasConfirmedEmail", "hasConfirmedEmailAt", "confirmEmailToken", "hasConfirmedPhone", "hasConfirmedPhoneAt", "hasConfirmedBasicInformations", "condoManagerNomCompte", "currentProjectId", note, "sendOnPressEnter", "cantDeliverEmailFullReason", "chatNotificationTypes", "expoNotificationTokens", "managerNotificationTypes", "orderNotificationTypes") VALUES ('cked9i0d10002mjheojfhs57r', 'MANAGER', NULL, NULL, '2020-08-27 20:30:51.733', '2020-08-27 20:30:51.734', 'test@golo.io', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '$2a$10$XTCiOwkCL6qYiOXIEeLxe.JMYmP.1rIjXIfdPNfk7EU1eZvnSkD4a', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);`) await client.end() }