Remove SQLite from twenty-website (#5142)

Removed SQLite from twenty-website

---------

Co-authored-by: Ady Beraud <a.beraud96@gmail.com>
Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
This commit is contained in:
Ady Beraud
2024-04-24 18:02:02 +03:00
committed by GitHub
parent 0f47426d19
commit 5d2d6bae08
18 changed files with 57 additions and 848 deletions

View File

@ -1,4 +1,3 @@
GITHUB_TOKEN=your_github_token
DATABASE_DRIVER=sqlite # or pg
DATABASE_PG_URL=postgres://website:website@localhost:5432/website # only if using postgres

View File

@ -10,8 +10,7 @@
"lint": "npx next lint",
"github:sync": "npx tsx src/github-sync/github-sync.ts",
"database:migrate": "npx tsx src/database/migrate-database.ts",
"database:generate:pg": "npx drizzle-kit generate:pg --config=src/database/postgres/drizzle-posgres.config.ts",
"database:generate:sqlite": "npx drizzle-kit generate:sqlite --config=src/database/sqlite/drizzle-sqlite.config.ts"
"database:generate:pg": "npx drizzle-kit generate:pg --config=src/database/drizzle-posgres.config.ts"
},
"dependencies": {
"postgres": "^3.4.3"

View File

@ -1,135 +1,56 @@
import { global } from '@apollo/client/utilities/globals';
import { createClient } from '@libsql/client';
import { drizzle as sqliteDrizzle, LibSQLDatabase } from 'drizzle-orm/libsql';
import { migrate as sqliteMigrate } from 'drizzle-orm/libsql/migrator';
import {
drizzle as pgDrizzle,
PostgresJsDatabase,
} from 'drizzle-orm/postgres-js';
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate as postgresMigrate } from 'drizzle-orm/postgres-js/migrator';
import { SQLiteTableWithColumns } from 'drizzle-orm/sqlite-core';
import postgres from 'postgres';
import 'dotenv/config';
// Todo: Deprecate SQLite once prototyping is complete, this is making things impossible to type properly
const databaseDriver = global.process.env.DATABASE_DRIVER || 'sqlite';
const isSqliteDriver = databaseDriver === 'sqlite';
const isPgDriver = databaseDriver === 'pg';
let sqliteDb: LibSQLDatabase;
let pgDb: PostgresJsDatabase;
if (isSqliteDriver) {
const sqliteClient = createClient({
url: 'file:twenty-website.sqlite',
});
sqliteDb = sqliteDrizzle(sqliteClient, { logger: false });
}
if (isPgDriver) {
const pgClient = postgres(`${global.process.env.DATABASE_PG_URL}`);
pgDb = pgDrizzle(pgClient, { logger: false });
}
const pgClient = postgres(`${global.process.env.DATABASE_PG_URL}`);
const pgDb = drizzle(pgClient, { logger: false });
const migrate = async () => {
if (isSqliteDriver) {
await sqliteMigrate(sqliteDb, {
migrationsFolder: './src/database/sqlite/migrations',
});
return;
}
if (isPgDriver) {
await postgresMigrate(pgDb, {
migrationsFolder: './src/database/postgres/migrations',
});
return;
}
throw new Error('Unsupported database driver');
await postgresMigrate(pgDb, {
migrationsFolder: './src/database/migrations',
});
};
const findOne = (model: SQLiteTableWithColumns<any>, orderBy: any) => {
if (isSqliteDriver) {
return sqliteDb.select().from(model).orderBy(orderBy).limit(1).execute();
}
if (isPgDriver) {
return pgDb.select().from(model).orderBy(orderBy).limit(1).execute();
}
throw new Error('Unsupported database driver');
const findOne = (model: any, orderBy: any) => {
return pgDb.select().from(model).orderBy(orderBy).limit(1).execute();
};
const findAll = (model: SQLiteTableWithColumns<any>) => {
if (isSqliteDriver) {
return sqliteDb.select().from(model).all();
}
if (isPgDriver) {
return pgDb.select().from(model).execute();
}
throw new Error('Unsupported database driver');
const findAll = (model: any) => {
return pgDb.select().from(model).execute();
};
const insertMany = async (
model: SQLiteTableWithColumns<any>,
model: any,
data: any,
options?: {
onConflictKey?: string;
onConflictUpdateObject?: any;
},
) => {
if (isSqliteDriver) {
const query = sqliteDb.insert(model).values(data);
if (options?.onConflictUpdateObject) {
if (options?.onConflictKey) {
return query
.onConflictDoUpdate({
target: [model[options.onConflictKey]],
set: options.onConflictUpdateObject,
})
.execute();
}
}
const query = pgDb.insert(model).values(data);
if (options?.onConflictUpdateObject) {
if (options?.onConflictKey) {
return query
.onConflictDoNothing({
.onConflictDoUpdate({
target: [model[options.onConflictKey]],
set: options.onConflictUpdateObject,
})
.execute();
}
return query.execute();
}
if (isPgDriver) {
const query = pgDb.insert(model).values(data);
if (options?.onConflictUpdateObject) {
if (options?.onConflictKey) {
return query
.onConflictDoUpdate({
target: [model[options.onConflictKey]],
set: options.onConflictUpdateObject,
})
.execute();
}
}
if (options?.onConflictKey) {
return query
.onConflictDoNothing({
target: [model[options.onConflictKey]],
})
.execute();
}
return query.execute();
}
throw new Error('Unsupported database driver');
if (options?.onConflictKey) {
return query
.onConflictDoNothing({
target: [model[options.onConflictKey]],
})
.execute();
}
return query.execute();
};
export { findAll, findOne, insertMany, migrate };

View File

@ -3,8 +3,8 @@ import { Config } from 'drizzle-kit';
import 'dotenv/config';
const pgConfig = {
schema: './src/database/postgres/schema-postgres.ts',
out: './src/database/postgres/migrations',
schema: './src/database/schema-postgres.ts',
out: './src/database/migrations',
driver: 'pg',
dbCredentials: {
connectionString: process.env.DATABASE_PG_URL ?? '',

View File

@ -6,31 +6,14 @@ import {
pgPullRequestLabels,
pgPullRequests,
pgUsers,
} from '@/database/postgres/schema-postgres';
import {
sqlLiteIssueLabels,
sqlLiteIssues,
sqlLiteLabels,
sqlLitePullRequestLabels,
sqlLitePullRequests,
sqlLiteUsers,
} from '@/database/sqlite/schema-sqlite';
} from '@/database/schema-postgres';
const databaseDriver = global.process.env.DATABASE_DRIVER;
const isSqliteDriver = databaseDriver === 'sqlite';
export const userModel = isSqliteDriver ? sqlLiteUsers : pgUsers;
export const pullRequestModel = isSqliteDriver
? sqlLitePullRequests
: pgPullRequests;
export const issueModel = isSqliteDriver ? sqlLiteIssues : pgIssues;
export const labelModel = isSqliteDriver ? sqlLiteLabels : pgLabels;
export const pullRequestLabelModel = isSqliteDriver
? sqlLitePullRequestLabels
: pgPullRequestLabels;
export const issueLabelModel = isSqliteDriver
? sqlLiteIssueLabels
: pgIssueLabels;
export const userModel = pgUsers;
export const pullRequestModel = pgPullRequests;
export const issueModel = pgIssues;
export const labelModel = pgLabels;
export const pullRequestLabelModel = pgPullRequestLabels;
export const issueLabelModel = pgIssueLabels;
export const githubStarsModel = pgGithubStars;

View File

@ -1,14 +0,0 @@
import { Config } from 'drizzle-kit';
import 'dotenv/config';
const sqliteConfig = {
schema: './src/database/sqlite/schema-sqlite.ts',
out: './src/database/sqlite/migrations',
driver: 'libsql',
dbCredentials: {
url: 'twenty-website.sqlite',
},
} satisfies Config;
export default sqliteConfig;

View File

@ -1,54 +0,0 @@
CREATE TABLE `issueLabels` (
`issueId` text,
`labelId` text,
FOREIGN KEY (`issueId`) REFERENCES `issues`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`labelId`) REFERENCES `labels`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `issues` (
`id` text PRIMARY KEY NOT NULL,
`externalId` text,
`title` text,
`body` text,
`url` text,
`createdAt` text,
`updatedAt` text,
`closedAt` text,
`authorId` text,
FOREIGN KEY (`authorId`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `labels` (
`id` text PRIMARY KEY NOT NULL,
`externalId` text,
`name` text,
`color` text,
`description` text
);
--> statement-breakpoint
CREATE TABLE `pullRequestLabels` (
`pullRequestExternalId` text,
`labelId` text,
FOREIGN KEY (`pullRequestExternalId`) REFERENCES `pullRequests`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`labelId`) REFERENCES `labels`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `pullRequests` (
`id` text PRIMARY KEY NOT NULL,
`title` text,
`body` text,
`url` text,
`createdAt` text,
`updatedAt` text,
`closedAt` text,
`mergedAt` text,
`authorId` text,
FOREIGN KEY (`authorId`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `users` (
`id` text PRIMARY KEY NOT NULL,
`avatarUrl` text,
`url` text,
`isEmployee` text
);

View File

@ -1,367 +0,0 @@
{
"version": "5",
"dialect": "sqlite",
"id": "033cd768-53b9-4c60-99ee-be070ea7abd6",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"issueLabels": {
"name": "issueLabels",
"columns": {
"issueId": {
"name": "issueId",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"labelId": {
"name": "labelId",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"issueLabels_issueId_issues_id_fk": {
"name": "issueLabels_issueId_issues_id_fk",
"tableFrom": "issueLabels",
"tableTo": "issues",
"columnsFrom": [
"issueId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"issueLabels_labelId_labels_id_fk": {
"name": "issueLabels_labelId_labels_id_fk",
"tableFrom": "issueLabels",
"tableTo": "labels",
"columnsFrom": [
"labelId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"issues": {
"name": "issues",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"externalId": {
"name": "externalId",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"title": {
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"body": {
"name": "body",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"url": {
"name": "url",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"updatedAt": {
"name": "updatedAt",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"closedAt": {
"name": "closedAt",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"authorId": {
"name": "authorId",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"issues_authorId_users_id_fk": {
"name": "issues_authorId_users_id_fk",
"tableFrom": "issues",
"tableTo": "users",
"columnsFrom": [
"authorId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"labels": {
"name": "labels",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"externalId": {
"name": "externalId",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"color": {
"name": "color",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"pullRequestLabels": {
"name": "pullRequestLabels",
"columns": {
"pullRequestExternalId": {
"name": "pullRequestExternalId",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"labelId": {
"name": "labelId",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"pullRequestLabels_pullRequestExternalId_pullRequests_id_fk": {
"name": "pullRequestLabels_pullRequestExternalId_pullRequests_id_fk",
"tableFrom": "pullRequestLabels",
"tableTo": "pullRequests",
"columnsFrom": [
"pullRequestExternalId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"pullRequestLabels_labelId_labels_id_fk": {
"name": "pullRequestLabels_labelId_labels_id_fk",
"tableFrom": "pullRequestLabels",
"tableTo": "labels",
"columnsFrom": [
"labelId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"pullRequests": {
"name": "pullRequests",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"title": {
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"body": {
"name": "body",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"url": {
"name": "url",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"updatedAt": {
"name": "updatedAt",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"closedAt": {
"name": "closedAt",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"mergedAt": {
"name": "mergedAt",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"authorId": {
"name": "authorId",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"pullRequests_authorId_users_id_fk": {
"name": "pullRequests_authorId_users_id_fk",
"tableFrom": "pullRequests",
"tableTo": "users",
"columnsFrom": [
"authorId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"users": {
"name": "users",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"avatarUrl": {
"name": "avatarUrl",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"url": {
"name": "url",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"isEmployee": {
"name": "isEmployee",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}

View File

@ -1,13 +0,0 @@
{
"version": "5",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1707920913359,
"tag": "0000_small_karma",
"breakpoints": true
}
]
}

View File

@ -1,52 +0,0 @@
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
export const sqlLiteUsers = sqliteTable('users', {
id: text('id').primaryKey(),
avatarUrl: text('avatarUrl'),
url: text('url'),
isEmployee: text('isEmployee'),
});
export const sqlLitePullRequests = sqliteTable('pullRequests', {
id: text('id').primaryKey(),
title: text('title'),
body: text('body'),
url: text('url'),
createdAt: text('createdAt'),
updatedAt: text('updatedAt'),
closedAt: text('closedAt'),
mergedAt: text('mergedAt'),
authorId: text('authorId').references(() => sqlLiteUsers.id),
});
export const sqlLiteIssues = sqliteTable('issues', {
id: text('id').primaryKey(),
externalId: text('externalId'),
title: text('title'),
body: text('body'),
url: text('url'),
createdAt: text('createdAt'),
updatedAt: text('updatedAt'),
closedAt: text('closedAt'),
authorId: text('authorId').references(() => sqlLiteUsers.id),
});
export const sqlLiteLabels = sqliteTable('labels', {
id: text('id').primaryKey(),
externalId: text('externalId'),
name: text('name'),
color: text('color'),
description: text('description'),
});
export const sqlLitePullRequestLabels = sqliteTable('pullRequestLabels', {
pullRequestId: text('pullRequestExternalId').references(
() => sqlLitePullRequests.id,
),
labelId: text('labelId').references(() => sqlLiteLabels.id),
});
export const sqlLiteIssueLabels = sqliteTable('issueLabels', {
issueId: text('issueId').references(() => sqlLiteIssues.id),
labelId: text('labelId').references(() => sqlLiteLabels.id),
});