Skip to content

Commit

Permalink
Create share table if it doesn't exist during the fragment migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-dalby committed Nov 11, 2024
1 parent 73e11ae commit 9a4245d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions server/src/config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function needsMigration(db) {
return hasCodeColumn;
}

async function migrateToFragments(db) {
async function migrateToV1_4_0(db) {
console.log('Starting migration to fragments...');

if (!needsMigration(db)) {
Expand All @@ -73,6 +73,17 @@ async function migrateToFragments(db) {
);
CREATE INDEX IF NOT EXISTS idx_fragments_snippet_id ON fragments(snippet_id);
CREATE TABLE IF NOT EXISTS shared_snippets (
id TEXT PRIMARY KEY,
snippet_id INTEGER NOT NULL,
requires_auth BOOLEAN NOT NULL DEFAULT false,
expires_at DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (snippet_id) REFERENCES snippets(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_shared_snippets_snippet_id ON shared_snippets(snippet_id);
`);

const snippets = db.prepare('SELECT id, code, language FROM snippets').all();
Expand Down Expand Up @@ -176,7 +187,7 @@ function initializeDatabase() {
console.log('Database needs migration, creating backup...');
if (backupDatabase(dbPath)) {
console.log('Starting migration process...');
migrateToFragments(db);
migrateToV1_4_0(db);
}
} else {
console.log('Database schema is up to date');
Expand All @@ -200,6 +211,6 @@ function getDb() {

module.exports = {
initializeDatabase,
migrateToFragments,
migrateToV1_4_0,
getDb
};

0 comments on commit 9a4245d

Please sign in to comment.