Skip to content
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
6 changes: 6 additions & 0 deletions packages/core-database-postgres/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

## 0.2.11 - 2018-12-05

### Added

- Store executed migrations in the database

## 0.2.1 - 2018-12-05

### Added
Expand Down
17 changes: 16 additions & 1 deletion packages/core-database-postgres/lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const crypto = require('crypto')
const chunk = require('lodash/chunk')
const pluralize = require('pluralize')
const fs = require('fs')
const path = require('path')

const { ConnectionInterface } = require('@arkecosystem/core-database')

Expand Down Expand Up @@ -658,7 +659,21 @@ module.exports = class PostgresConnection extends ConnectionInterface {
*/
async __runMigrations() {
for (const migration of migrations) {
await this.query.none(migration)
const { name } = path.parse(migration.file)

if (name === '20180304100000-create-migrations-table') {
await this.query.none(migration)
} else {
const row = await this.db.migrations.findByName(name)

if (row === null) {
logger.debug(`Migrating ${name}`)

await this.query.none(migration)

await this.db.migrations.create({ name })
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Table Definition
CREATE TABLE IF NOT EXISTS ${schema~}.migrations (
"id" SERIAL PRIMARY KEY,
"name" VARCHAR(255) UNIQUE NOT NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,3 @@ CREATE TABLE IF NOT EXISTS ${schema~}.blocks (
"generator_public_key" VARCHAR(66) NOT NULL,
"block_signature" VARCHAR(256) NOT NULL
);

-- Constraints
CREATE UNIQUE INDEX IF NOT EXISTS "blocks_unique" ON blocks ("height", "generator_public_key");
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
-- Constraints
CREATE INDEX IF NOT EXISTS "transactions_block_id" ON transactions ("block_id");
CREATE INDEX IF NOT EXISTS "transactions_block_id" ON ${schema~}.transactions ("block_id");
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DROP INDEX "blocks_unique";
DROP INDEX IF EXISTS "blocks_unique";
CREATE INDEX IF NOT EXISTS "blocks_generator_public_key" ON ${schema~}.blocks ("generator_public_key");
21 changes: 17 additions & 4 deletions packages/core-database-postgres/lib/migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
const { loadQueryFile } = require('../utils')

module.exports = [
loadQueryFile(__dirname, './20180304100000-create-migrations-table.sql'),
loadQueryFile(__dirname, './20180305100000-create-wallets-table.sql'),
loadQueryFile(__dirname, './20180305200000-create-rounds-table.sql'),
loadQueryFile(__dirname, './20180305300000-create-blocks-table.sql'),
loadQueryFile(__dirname, './20180305400000-create-transactions-table.sql'),
loadQueryFile(__dirname, './20181129400000-add-block_id-index-to-transactions-table.sql'),
loadQueryFile(__dirname, './20181204100000-blocks-generator_public_key-index.sql'),
loadQueryFile(__dirname, './20181204200000-transactions-timestamp-index.sql'),
loadQueryFile(__dirname, './20181204300000-transactions-sender-and-receiver-indexes.sql'),
loadQueryFile(
__dirname,
'./20181129400000-add-block_id-index-to-transactions-table.sql',
),
loadQueryFile(
__dirname,
'./20181204100000-add-generator_public_key-index-to-blocks-table.sql',
),
loadQueryFile(
__dirname,
'./20181204200000-add-transactions-timestamp-index-to-blocks-table.sql',
),
loadQueryFile(
__dirname,
'./20181204300000-add-transactions-sender-and-receiver-indices-to-transactions-table.sql',
),
]
1 change: 1 addition & 0 deletions packages/core-database-postgres/lib/models/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
Block: require('./block'),
Migration: require('./migration'),
Round: require('./round'),
Transaction: require('./transaction'),
Wallet: require('./wallet'),
Expand Down
23 changes: 23 additions & 0 deletions packages/core-database-postgres/lib/models/migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Model = require('./model')

module.exports = class Round extends Model {
/**
* The table associated with the model.
* @return {String}
*/
getTable() {
return 'migrations'
}

/**
* The read-only structure with query-formatting columns.
* @return {Object}
*/
getColumnSet() {
return this.createColumnSet([
{
name: 'name',
},
])
}
}
4 changes: 4 additions & 0 deletions packages/core-database-postgres/lib/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module.exports = {
statistics: loadQueryFile(__dirname, './blocks/statistics.sql'),
top: loadQueryFile(__dirname, './blocks/top.sql'),
},
migrations: {
create: loadQueryFile(__dirname, './migrations/create.sql'),
find: loadQueryFile(__dirname, './migrations/find.sql'),
},
rounds: {
delete: loadQueryFile(__dirname, './rounds/delete.sql'),
find: loadQueryFile(__dirname, './rounds/find.sql'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO migrations (name) VALUES (${name})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT *
FROM migrations
WHERE name = ${name}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
blocks: require('./blocks'),
migrations: require('./migrations'),
rounds: require('./rounds'),
transactions: require('./transactions'),
wallets: require('./wallets'),
Expand Down
22 changes: 22 additions & 0 deletions packages/core-database-postgres/lib/repositories/migrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const Repository = require('./repository')
const { Migration } = require('../models')
const { migrations: sql } = require('../queries')

module.exports = class MigrationsRepository extends Repository {
/**
* Find a migration by its name.
* @param {String} name
* @return {Promise}
*/
async findByName(name) {
return this.db.oneOrNone(sql.find, { name })
}

/**
* Get the model related to this repository.
* @return {Object}
*/
getModel() {
return new Migration(this.pgp)
}
}