Skip to content

Commit

Permalink
Merge pull request #42 from jordan-dalby/add-share-table-in-migration
Browse files Browse the repository at this point in the history
Create share table if it doesn't exist during the fragment migration
  • Loading branch information
jordan-dalby authored Nov 11, 2024
2 parents 73e11ae + 9a4245d commit 78e00f2
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 78e00f2

Please sign in to comment.