-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #456 from mikefranze/DB-Updates
Election creation dates, IP hashes, migration scripts
- Loading branch information
Showing
23 changed files
with
170 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Kysely } from 'kysely' | ||
|
||
export async function up(db: Kysely<any>): Promise<void> { | ||
await db.schema.alterTable('electionDB') | ||
.addColumn('claim_key_hash', 'varchar') | ||
.addColumn('is_public', 'boolean') | ||
.addColumn('create_date', 'varchar') | ||
.execute() | ||
|
||
await db.updateTable('electionDB') | ||
.set({ create_date: new Date().toISOString() }) | ||
.execute() | ||
|
||
await db.schema.alterTable('electionDB') | ||
.alterColumn('create_date', (col) => col.setNotNull()) | ||
.execute() | ||
|
||
await db.schema.alterTable('electionRollDB') | ||
.dropColumn('ip_address') | ||
.addColumn('ip_hash', 'varchar') | ||
.execute() | ||
|
||
|
||
await db.schema.alterTable('ballotDB') | ||
.dropColumn('ip_address') | ||
.addColumn('ip_hash', 'varchar') | ||
.execute() | ||
} | ||
|
||
export async function down(db: Kysely<any>): Promise<void> { | ||
await db.schema.alterTable('electionDB') | ||
.dropColumn('claim_key_hash') | ||
.dropColumn('is_public') | ||
.dropColumn('create_date') | ||
.execute() | ||
|
||
await db.schema.alterTable('electionRollDB') | ||
.dropColumn('ip_hash') | ||
.addColumn('ip_address', 'varchar') | ||
.execute() | ||
|
||
|
||
await db.schema.alterTable('ballotDB') | ||
.dropColumn('ip_hash') | ||
.addColumn('ip_address', 'varchar') | ||
.execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require('dotenv').config() | ||
import { createMigrator, handleMigration } from "./migration-utils" | ||
|
||
async function migrateDown() { | ||
const { migrator, db } = createMigrator() | ||
handleMigration(await migrator.migrateDown()) | ||
|
||
await db.destroy() | ||
} | ||
|
||
migrateDown() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require('dotenv').config() | ||
import { createMigrator, handleMigration } from "./migration-utils" | ||
|
||
async function migrateToLatest() { | ||
const { migrator, db } = createMigrator() | ||
handleMigration(await migrator.migrateToLatest()) | ||
|
||
await db.destroy() | ||
} | ||
|
||
migrateToLatest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require('dotenv').config() | ||
import { createMigrator, handleMigration } from "./migration-utils" | ||
|
||
async function migrateUp() { | ||
const { migrator, db } = createMigrator() | ||
handleMigration(await migrator.migrateUp()) | ||
|
||
await db.destroy() | ||
} | ||
|
||
migrateUp() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as path from 'path' | ||
import { promises as fs } from 'fs' | ||
import { | ||
Migrator, | ||
FileMigrationProvider, | ||
MigrationResultSet, | ||
} from 'kysely' | ||
|
||
import servicelocator from '../ServiceLocator' | ||
|
||
export function createMigrator() { | ||
const db = servicelocator.database() | ||
const migrator = new Migrator({ | ||
db, | ||
provider: new FileMigrationProvider({ | ||
fs, | ||
path, | ||
// This needs to be an absolute path. | ||
migrationFolder: path.join(__dirname, '../Migrations'), | ||
}), | ||
}) | ||
return { | ||
migrator, | ||
db | ||
} | ||
} | ||
|
||
export async function handleMigration(migrationResult: MigrationResultSet) { | ||
migrationResult.results?.forEach((it) => { | ||
if (it.status === 'Success') { | ||
console.log(`migration "${it.migrationName}" was executed successfully`) | ||
} else if (it.status === 'Error') { | ||
console.error(`failed to execute migration "${it.migrationName}"`) | ||
} | ||
}) | ||
|
||
if (migrationResult.error) { | ||
console.error('failed to migrate') | ||
console.error(migrationResult.error) | ||
process.exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.