Adapt migrations for knex

This commit is contained in:
2020-05-02 21:06:34 +02:00
parent 546e150ea5
commit 150f4a924c
15 changed files with 756 additions and 143 deletions

8
data/db.js Normal file
View File

@@ -0,0 +1,8 @@
const knex = require('knex');
const knexfile = require('../knexfile');
const env = process.env.NODE_ENV || 'development';
const configOptions = knexfile[env];
module.exports = knex(configOptions);

View File

@@ -1,5 +0,0 @@
const pgp = require('pg-promise')();
const db = pgp(process.env.DATABASE_URL);
module.exports = db;

View File

@@ -2,4 +2,4 @@
- Create `.env` to the root folder
- Add the database url to the `.env` file:
`DATABASE_URL=postgres://username:password@host:port/database`
- Run `npm run migrate up`
- Run `npx knex migrate:latest`

View File

@@ -1,97 +0,0 @@
const { PgLiteral } = require('node-pg-migrate');
exports.shorthands = {
id: { type: 'serial', primaryKey: true },
varchar: { type: 'varchar(128)' },
varchar_req: { type: 'varchar(128)', notNull: true },
created_at: {
type: 'timestamp',
notNull: true,
default: PgLiteral.create('CURRENT_TIMESTAMP'),
},
};
exports.up = (pgm) => {
pgm.createTable('account', {
id: 'id',
username: 'varchar_req',
email: 'varchar_req',
password: 'varchar_req',
timezone: 'varchar',
earliest_time: 'time',
latest_time: 'time',
createdAt: 'created_at',
});
pgm.createTable('meeting', {
id: { type: 'uuid', primaryKey: true },
title: 'varchar_req',
description: 'varchar',
start_time: 'time',
timezone: 'varchar',
duration: 'int',
status: 'boolean',
password: 'varchar',
createdAt: 'created_at',
});
pgm.createTable('possible_date', {
id: 'id',
meeting_id: {
type: 'uuid',
references: 'meeting(id)',
notNull: true,
onDelete: 'cascade',
},
possible_date: { type: 'date', notNull: true },
});
pgm.createTable('participant', {
id: 'id',
account_id: {
type: 'int',
references: 'account(id)',
notNull: true,
onDelete: 'cascade',
},
meeting_id: {
type: 'uuid',
references: 'meeting(id)',
notNull: true,
onDelete: 'cascade',
},
earliest_time: 'time',
latest_time: 'time',
quorum: 'boolean',
mandatory: 'boolean',
host: 'boolean',
answered: 'boolean',
timezone: 'varchar',
createdAt: 'created_at',
});
pgm.createTable('availibility', {
id: 'id',
participant_id: {
type: 'id',
references: 'participant(id)',
notNull: true,
onDelete: 'cascade',
},
possible_date_id: {
type: 'id',
references: 'possible_date(id)',
notNull: true,
onDelete: 'cascade',
},
preference: { type: 'boolean', notNull: true },
start_time: { type: 'timestamp', notNull: true },
end_time: { type: 'timestamp', notNull: true },
timezone: 'varchar_req',
createdAt: 'created_at',
});
};
// exports.down = (pgm) => {
// pgm.dropTable('accounts', { ifExists: true });
// };

View File

@@ -0,0 +1,16 @@
exports.up = (knex) => {
return knex.schema.createTable('account', (table) => {
table.increments('id').primary();
table.string('username').notNullable();
table.string('email').notNullable().unique();
table.string('password').notNullable();
table.string('timezone');
table.time('earliest_time');
table.time('latest_time');
table.timestamps(true, true);
});
};
exports.down = (knex) => {
return knex.schema.dropTable('account');
};

View File

@@ -0,0 +1,17 @@
exports.up = (knex) => {
return knex.schema.createTable('meeting', (table) => {
table.uuid('id').primary();
table.string('title').notNullable();
table.string('description');
table.time('start_time');
table.string('timezone').notNullable();
table.integer('duration').notNullable();
table.boolean('status').notNullable();
table.string('password');
table.timestamps(true, true);
});
};
exports.down = (knex) => {
return knex.schema.dropTable('meeting');
};

View File

@@ -0,0 +1,29 @@
exports.up = (knex) => {
return knex.schema.createTable('participant', (table) => {
table.increments('id').primary();
table
.integer('account_id')
.unsigned()
.notNullable()
.references('account.id')
.onDelete('cascade');
table
.uuid('meeting_id')
.unsigned()
.notNullable()
.references('meeting.id')
.onDelete('cascade');
table.time('earliest_time');
table.time('latest_time');
table.boolean('quorum');
table.boolean('mandatory');
table.boolean('host');
table.boolean('answered');
table.string('timezone').notNullable();
table.timestamps(true, true);
});
};
exports.down = (knex) => {
return knex.schema.dropTable('participant');
};

View File

@@ -0,0 +1,17 @@
exports.up = (knex) => {
return knex.schema.createTable('possible_date', (table) => {
table.increments('id').primary();
table
.uuid('meeting_id')
.unsigned()
.notNullable()
.references('meeting.id')
.onDelete('cascade');
table.date('possible_date').notNullable();
table.timestamps(true, true);
});
};
exports.down = (knex) => {
return knex.schema.dropTable('possible_date');
};

View File

@@ -0,0 +1,26 @@
exports.up = (knex) => {
return knex.schema.createTable('availibility', (table) => {
table.increments('id').primary();
table
.integer('participant_id')
.unsigned()
.notNullable()
.references('participant.id')
.onDelete('cascade');
table
.integer('possible_date_id')
.unsigned()
.notNullable()
.references('possible_date.id')
.onDelete('cascade');
table.boolean('preference').notNullable();
table.time('start_time').notNullable();
table.time('end_time').notNullable();
table.string('timezone').notNullable();
table.timestamps(true, true);
});
};
exports.down = (knex) => {
return knex.schema.dropTable('availibility');
};