20200510140704_domains.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. const models = require("../models");
  2. async function up(knex) {
  3. await models.createUserTable(knex);
  4. await models.createIPTable(knex);
  5. await models.createDomainTable(knex);
  6. await models.createHostTable(knex);
  7. await models.createLinkTable(knex);
  8. await models.createVisitTable(knex);
  9. // drop unique user id constraint only if database is postgres
  10. // because other databases use the new version of the app and they start fresh with the correct model
  11. // if i use table.dropUnique() method it would throw error on fresh install because the constraint does not exist
  12. // and if it throws error, the rest of the transactions fail as well
  13. if (knex.client.driverName === "pg") {
  14. knex.raw(`
  15. ALTER TABLE domains
  16. DROP CONSTRAINT IF EXISTS domains_user_id_unique
  17. `)
  18. }
  19. const hasUUID = await knex.schema.hasColumn("domains", "uuid");
  20. if (!hasUUID) {
  21. await knex.schema.alterTable("domains", (table) => {
  22. table.uuid("uuid").notNullable().defaultTo(knex.fn.uuid());
  23. });
  24. }
  25. }
  26. async function down() {
  27. // do nothing
  28. }
  29. module.exports = {
  30. up,
  31. down
  32. }