Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Db snake case #89

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db/migrations/20230526063604_collections-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports.up = function (knex) {
useNative: true,
enumName: FLAVORS_ENUM_NAME,
});
table.timestamps(false, true, true);
table.timestamps(false, true, false);
});
};

Expand Down
2 changes: 1 addition & 1 deletion db/migrations/20230526065020_user-roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.up = function (knex) {
table.string('slug', NAME_LENGTH).unique();
table.string('label', NAME_LENGTH);
table.jsonb('permissions').nullable();
table.timestamps(false, true, true);
table.timestamps(false, true, false);
});
};

Expand Down
10 changes: 5 additions & 5 deletions db/migrations/20230526070609_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ exports.up = function (knex) {
return knex.schema.createTable(USERS_TABLE, (table) => {
table.increments('id');
table.string('password', PASSWORD_LENGTH).nullable();
table.string('firstName', NAME_LENGTH);
table.string('lastName', NAME_LENGTH);
table.integer('roleId', FOREIGN_KEY_LENGTH).unsigned().index();
table.timestamps(false, true, true);
table.string('first_name', NAME_LENGTH);
table.string('last_name', NAME_LENGTH);
table.integer('role_id', FOREIGN_KEY_LENGTH).unsigned().index();
table.timestamps(false, true, false);

table
.foreign('roleId', 'fk_roleId')
.foreign('role_id', 'fk_role_id')
.references('id')
.inTable(USER_ROLES_TABLE)
.onDelete('CASCADE');
Expand Down
6 changes: 3 additions & 3 deletions db/migrations/20230526071428_user-api-tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const { USERS_TABLE, USER_API_TOKENS_TABLE, FOREIGN_KEY_LENGTH } = require('../.
exports.up = function (knex) {
return knex.schema.createTable(USER_API_TOKENS_TABLE, (table) => {
table.increments('id');
table.integer('userId', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('user_id', FOREIGN_KEY_LENGTH).unsigned().index();
table.string('token').unique();
table.timestamps(false, true, true);
table.timestamps(false, true, false);

table.primary('id', { constraintName: 'pk_user_api_token' });
table
.foreign('userId', 'fk_userId')
.foreign('user_id', 'fk_user_id')
.references('id')
.inTable(USERS_TABLE)
.onDelete('CASCADE');
Expand Down
8 changes: 4 additions & 4 deletions db/migrations/20230526071911_users-emails.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const {
*/
exports.up = function (knex) {
return knex.schema.createTable(USERS_EMAILS_TABLE, (table) => {
table.integer('userId', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('user_id', FOREIGN_KEY_LENGTH).unsigned().index();
table.string('email', EMAIL_LENGTH).unique().index();
table.string('code', PASSWORD_LENGTH).unique().index();
table.boolean('verified').defaultTo(false);
table.timestamps(false, true, true);
table.timestamps(false, true, false);

table.primary(['userId', 'email'], { constraintName: 'pk_user_email' });
table.primary(['user_id', 'email'], { constraintName: 'pk_user_email' });
table
.foreign('userId', 'fk_userId')
.foreign('user_id', 'fk_user_id')
.references('id')
.inTable(USERS_TABLE)
.onDelete('CASCADE');
Expand Down
10 changes: 5 additions & 5 deletions db/migrations/20230526072327_users-oauths.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ const OAUTH_PROVIDERS_ENUM_NAME = 'oauth_providers';
*/
exports.up = function (knex) {
return knex.schema.createTable(USERS_OAUTHS_TABLE, (table) => {
table.integer('userId', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('user_id', FOREIGN_KEY_LENGTH).unsigned().index();
table.enu('provider', OAUTH_PROVIDERS_ENUM, {
useNative: true,
enumName: OAUTH_PROVIDERS_ENUM_NAME,
});
table.string('providerId').unique();
table.string('provider_id').unique();
table.jsonb('profile').nullable();
table.timestamps(false, true, true);
table.timestamps(false, true, false);

table.primary(['userId', 'provider'], {
table.primary(['user_id', 'provider'], {
constraintName: 'pk_user_provider',
});
table
.foreign('userId', 'fk_userId')
.foreign('user_id', 'fk_user_id')
.references('id')
.inTable(USERS_TABLE)
.onDelete('CASCADE');
Expand Down
6 changes: 3 additions & 3 deletions db/migrations/20230526072716_user-calculations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const { USERS_TABLE, USER_CALCULATIONS_TABLE, FOREIGN_KEY_LENGTH } = require('..
exports.up = function (knex) {
return knex.schema.createTable(USER_CALCULATIONS_TABLE, (table) => {
table.increments('id');
table.integer('userId', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('user_id', FOREIGN_KEY_LENGTH).unsigned().index();
table.uuid('uuid').unique();
table.timestamps(false, true, true);
table.timestamps(false, true, false);

table
.foreign('userId', 'fk_userId')
.foreign('user_id', 'fk_user_id')
.references('id')
.inTable(USERS_TABLE)
.onDelete('CASCADE');
Expand Down
6 changes: 3 additions & 3 deletions db/migrations/20230526073013_user-datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const { USERS_TABLE, USER_DATASOURCES_TABLE, FOREIGN_KEY_LENGTH } = require('../
exports.up = function (knex) {
return knex.schema.createTable(USER_DATASOURCES_TABLE, (table) => {
table.increments('id');
table.integer('userId', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('user_id', FOREIGN_KEY_LENGTH).unsigned().index();
table.uuid('uuid').unique();
table.timestamps(false, true, true);
table.timestamps(false, true, false);

table
.foreign('userId', 'fk_userId')
.foreign('user_id', 'fk_user_id')
.references('id')
.inTable(USERS_TABLE)
.onDelete('CASCADE');
Expand Down
10 changes: 5 additions & 5 deletions db/migrations/20230526073335_user-collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const VISIBILITY_ENUM_NAME = 'collection_visibility';
exports.up = function (knex) {
return knex.schema.createTable(USER_COLLECTIONS_TABLE, (table) => {
table.increments('id');
table.integer('userId', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('typeId', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('user_id', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('type_id', FOREIGN_KEY_LENGTH).unsigned().index();
table.string('title', 32);
table.string('description', 64);
table
Expand All @@ -25,15 +25,15 @@ exports.up = function (knex) {
enumName: VISIBILITY_ENUM_NAME,
})
.defaultTo(VISIBILITY_ENUM[0]);
table.timestamps(false, true, true);
table.timestamps(false, true, false);

table
.foreign('userId', 'fk_userId')
.foreign('user_id', 'fk_user_id')
.references('id')
.inTable(USERS_TABLE)
.onDelete('CASCADE');
table
.foreign('typeId', 'fk_typeId')
.foreign('type_id', 'fk_type_id')
.references('id')
.inTable(COLLECTIONS_TYPES_TABLE)
.onDelete('CASCADE');
Expand Down
12 changes: 6 additions & 6 deletions db/migrations/20230526073811_user-shared-collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ const {
*/
exports.up = function (knex) {
return knex.schema.createTable(USER_SHARED_COLLECTIONS_TABLE, (table) => {
table.integer('collectionId', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('userId', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('collection_id', FOREIGN_KEY_LENGTH).unsigned().index();
table.integer('user_id', FOREIGN_KEY_LENGTH).unsigned().index();
table.jsonb('permissions').nullable();
table.timestamps(false, true, true);
table.timestamps(false, true, false);

table.primary(['userId', 'collectionId'], {
table.primary(['user_id', 'collection_id'], {
constraintName: 'pk_user_collection',
});
table
.foreign('userId', 'fk_userId')
.foreign('user_id', 'fk_user_id')
.references('id')
.inTable(USERS_TABLE)
.onDelete('CASCADE');
table
.foreign('collectionId', 'fk_collectionId')
.foreign('collection_id', 'fk_collection_id')
.references('id')
.inTable(USER_COLLECTIONS_TABLE)
.onDelete('CASCADE');
Expand Down
14 changes: 7 additions & 7 deletions db/migrations/20230526074155_user-collections-datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ const {
*/
exports.up = function (knex) {
return knex.schema.createTable(USER_COLLECTIONS_DATASOURCES_TABLE, (table) => {
table.integer('collectionId').unsigned().index();
table.integer('dataSourceId').unsigned().index();
table.timestamps(false, true, true);
table.integer('collection_id').unsigned().index();
table.integer('data_source_id').unsigned().index();
table.timestamps(false, true, false);

table.primary(['collectionId', 'dataSourceId'], {
constraintName: 'pk_collection_dataSource',
table.primary(['collection_id', 'data_source_id'], {
constraintName: 'pk_collection_data_source',
});
table
.foreign('collectionId', 'fk_collectionId')
.foreign('collection_id', 'fk_collection_id')
.references('id')
.inTable(USER_COLLECTIONS_TABLE)
.onDelete('CASCADE');
table
.foreign('dataSourceId', 'fk_dataSourceId')
.foreign('data_source_id', 'fk_data_source_id')
.references('id')
.inTable(USER_DATASOURCES_TABLE)
.onDelete('CASCADE');
Expand Down
12 changes: 6 additions & 6 deletions db/migrations/20230526074529_logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ exports.up = function (knex) {
return knex.schema
.createTable(LOGS_TABLE, (table) => {
table.increments('id');
table.integer('userId').unsigned().index();
table.integer('user_id').unsigned().index();
table.string('type');
table.jsonb('value');
table.timestamp('createdAt').defaultTo(knex.fn.now()).index();
table.timestamp('created_at').defaultTo(knex.fn.now()).index();

table.primary('id', { constraintName: 'pk_logs' });
})
Expand All @@ -36,15 +36,15 @@ exports.up = function (knex) {
const query = `
create or replace function ${LOG_FUNCTION} () returns trigger as $$
declare
userId integer;
user_id integer;
type varchar := lower(tg_table_name);
begin
if type = lower('${USERS_TABLE}') then
userId := new.id;
user_id := new.id;
else
userId := new."userId";
user_id := new."user_id";
end if;
insert into "${LOGS_TABLE}" ("userId", "type", "value") values (userId, type, to_jsonb(new));
insert into "${LOGS_TABLE}" ("user_id", "type", "value") values (user_id, type, to_jsonb(new));
return new;
end;
$$ language plpgsql;
Expand Down
24 changes: 12 additions & 12 deletions db/seeds/030-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ exports.seed = async function (knex) {
const [member, admin, test] = await knex(USERS_TABLE).insert(
[
{
firstName: 'Test',
lastName: 'Member',
roleId: memberRole.id,
first_name: 'Test',
last_name: 'Member',
role_id: memberRole.id,
password,
},
{
firstName: 'Test',
lastName: 'Admin',
roleId: adminRole.id,
first_name: 'Test',
last_name: 'Admin',
role_id: adminRole.id,
password,
},
{
firstName: 'Test',
lastName: 'Test',
roleId: memberRole.id,
first_name: 'Test',
last_name: 'Test',
role_id: memberRole.id,
password,
},
],
Expand All @@ -57,17 +57,17 @@ exports.seed = async function (knex) {

await knex(USERS_EMAILS_TABLE).insert([
{
userId: member.id,
user_id: member.id,
email: memberEmail,
code: code1,
},
{
userId: admin.id,
user_id: admin.id,
email: adminEmail,
code: code2,
},
{
userId: test.id,
user_id: test.id,
email: testEmail,
code: code3,
},
Expand Down
4 changes: 2 additions & 2 deletions db/seeds/040-user-api-tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ exports.seed = async function (knex) {
// Deletes ALL existing entries
await knex(USER_API_TOKENS_TABLE).del();
await knex(USER_API_TOKENS_TABLE).insert(
emails.map(({ userId, email }) => ({
userId,
emails.map(({ user_id, email }) => ({
user_id,
token: email, // inserting email as token just for test
}))
);
Expand Down