Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add back legacy accounts table for now #10450

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { config } from '../src/config'
async function start() {
console.info('Running migrations')
console.warn('It is no longer necessary to run db migrations seperately prior to startup')
await initDatabase(config, undefined)
await initDatabase(config, undefined, false)
}

start()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { rootLogger } from '@celo/phone-number-privacy-common'
import Logger from 'bunyan'
import { Knex, knex } from 'knex'
import { DEV_MODE, SignerConfig, SupportedDatabase, VERBOSE_DB_LOGGING } from '../../config'
import { ACCOUNTS_COLUMNS, ACCOUNTS_TABLE } from './models/account'

export async function initDatabase(config: SignerConfig, migrationsPath?: string): Promise<Knex> {
export async function initDatabase(
config: SignerConfig,
migrationsPath?: string,
doTestQuery = true
): Promise<Knex> {
const logger = rootLogger(config.serviceName)
logger.info({ config: config.db }, 'Initializing database connection')
const { type, host, port, user, password, database, ssl, poolMaxSize } = config.db
Expand Down Expand Up @@ -66,6 +72,26 @@ export async function initDatabase(config: SignerConfig, migrationsPath?: string
loadExtensions: ['.js'],
})

if (doTestQuery) {
await executeTestQuery(db, logger)
}

logger.info('Database initialized successfully')
return db
}

async function executeTestQuery(db: Knex, logger: Logger) {
logger.info('Counting accounts')
const result = await db(ACCOUNTS_TABLE.LEGACY).count(ACCOUNTS_COLUMNS.address).first()

if (!result) {
throw new Error('No result from count, have migrations been run?')
}

const count = Object.values(result)[0]
if (count === undefined || count === null || count === '') {
throw new Error('No result from count, have migrations been run?')
}

logger.info(`Found ${count} accounts`)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Knex } from 'knex'
import { ACCOUNTS_COLUMNS, ACCOUNTS_TABLE } from '../models/account'

export async function up(knex: Knex): Promise<any> {
// This check was necessary to switch from using .ts migrations to .js migrations.
if (!(await knex.schema.hasTable(ACCOUNTS_TABLE.LEGACY))) {
return knex.schema.createTable(ACCOUNTS_TABLE.LEGACY, (t) => {
t.string(ACCOUNTS_COLUMNS.address).notNullable().primary()
t.dateTime(ACCOUNTS_COLUMNS.createdAt).notNullable()
t.integer(ACCOUNTS_COLUMNS.numLookups).unsigned()
})
}
return null
}

export async function down(knex: Knex): Promise<any> {
return knex.schema.dropTable(ACCOUNTS_TABLE.LEGACY)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Knex } from 'knex'
import { REQUESTS_COLUMNS, REQUESTS_TABLE } from '../models/request'

export async function up(knex: Knex): Promise<any> {
// This check was necessary to switch from using .ts migrations to .js migrations.
if (!(await knex.schema.hasTable(REQUESTS_TABLE.LEGACY))) {
return knex.schema.createTable(REQUESTS_TABLE.LEGACY, (t) => {
t.string(REQUESTS_COLUMNS.address).notNullable()
t.dateTime(REQUESTS_COLUMNS.timestamp).notNullable()
t.string(REQUESTS_COLUMNS.blindedQuery).notNullable()
t.primary([
REQUESTS_COLUMNS.address,
REQUESTS_COLUMNS.timestamp,
REQUESTS_COLUMNS.blindedQuery,
])
})
}
return null
}

export async function down(knex: Knex): Promise<any> {
return knex.schema.dropTable(REQUESTS_TABLE.LEGACY)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Knex } from 'knex'
import { ACCOUNTS_COLUMNS, ACCOUNTS_TABLE } from '../models/account'

export async function up(knex: Knex): Promise<any> {
if (!(await knex.schema.hasTable(ACCOUNTS_TABLE.LEGACY))) {
throw new Error('Unexpected error: Could not find ACCOUNTS_TABLE.LEGACY')
}
return knex.schema.alterTable(ACCOUNTS_TABLE.LEGACY, (t) => {
t.index(ACCOUNTS_COLUMNS.address)
})
}

export async function down(knex: Knex): Promise<any> {
return knex.schema.alterTable(ACCOUNTS_TABLE.LEGACY, (t) => {
t.dropIndex(ACCOUNTS_COLUMNS.address)
})
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum ACCOUNTS_TABLE {
ONCHAIN = 'accountsOnChain',
LEGACY = 'accounts', // TODO figure out right way to drop this table now that it's no longer in use
}

export enum ACCOUNTS_COLUMNS {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum REQUESTS_TABLE {
LEGACY = 'requests',
ONCHAIN = 'requestsOnChain',
}

Expand Down
Loading