Skip to content

Commit

Permalink
chore: use sql file
Browse files Browse the repository at this point in the history
  • Loading branch information
yannamsellem committed Jan 6, 2025
1 parent f790925 commit c0aed3e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
43 changes: 33 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>

<body>
<div id="app"></div>
<script type="module">
import { MigrationManager } from './src/migrate.ts';

const migrationManager = new MigrationManager({
exec: async (query, params) => {
console.log(query, params);
return [{ hash: 'a9b51dc76b96ac5b6b027074ca183f85f8b3252af4593d9d605f7b9d4e60ceff' }]
}
});

migrationManager.migrate([{
name: 'create_users_table',
script: `CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);`,
}]).catch(console.error);
</script>
</body>

</html>
12 changes: 3 additions & 9 deletions src/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import MIGRATIONS_TABLE from './migrations_table.sql?raw';

interface IDatabase {
exec: (query: string, params?: any[]) => Promise<any[]>;
}
Expand All @@ -8,7 +10,6 @@ export interface Migration {
}

export class MigrationManager {
private readonly table_name = 'migrations';
constructor(private database: IDatabase) { }

async migrate(migrations: Migration[]) {
Expand All @@ -27,14 +28,7 @@ export class MigrationManager {
}

private async init() {
await this.database.exec(`CREATE TABLE IF NOT EXISTS ${this.table_name} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
hash TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_migrations_name ON ${this.table_name} (name);`);
await this.database.exec(MIGRATIONS_TABLE);
}

private async migrateOne(migration: Migration) {
Expand Down
8 changes: 8 additions & 0 deletions src/migrations_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS migrations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
hash TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_migrations_name ON migrations (name);
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
"noUncheckedSideEffectImports": true,

"types": ["vite/client"]
},
"include": ["src/migrate.ts"]
}

0 comments on commit c0aed3e

Please sign in to comment.