4.3.18 • Published 2 months ago

tm-apps-db v4.3.18

Weekly downloads
73
License
ISC
Repository
-
Last release
2 months ago

tm-apps-db

Apps backend database using the Sequalize library. See /models and /migrations.

  • index.js: provides access to the DB using Sequalize APIs
  • sync.js: command-line tool to init/synchronize the DB models.

Docs

In order to view or generate an entity diagram of the database, go to ./db/docs/ and follow the instructions.

Database workings

Migrations

Read in MIGRATIONS.md

Credentials

The following database credentials are created according to their use:

  • master used for deployment pipeline
  • consumer used by lambdas with read/write permissions
  • api used by APIs with read permissions

The above credentials are injected in a migration file (sql\V001__add_roles.sql) through prebuild.sh script and is part of a migration step in the deployment pipeline using the Flyway tool.

This migration file is granting the right permissions on the database to the consumers and apis.

The credentials are stored in AWS Parameter Store in the format: /tm-apps-db/<DB_ROLE>/<username|password>. Example: /tm-apps-db/api/username.

Seeding

There is a Chronos Seeding Job on Jenkins that is triggering the consumers based on DynamoDB data, but is not indicated to run on a large number of entries (more than 60k).

Go to Jenkins Chronos Reseeder job, fill in the the EscenicStateStore-<component>-<env>, fill in the lambda name and run the job. This will query the specified table and run the consumer for each entry which in the end will write down to the PosgresDB.

Links:

Deploy

NPM package

Apps APIs are using database models for queries and hence this repo is delivered as an NPM package.

A new version can be deployed by updating the version number from package.json file by following the semantic versioning syntax and tagging the commit with that version. When pushing the newly tag to git, a new package version will be published.

To bump up the package version, use npm version with --git-tag-version option - this will bump up the package version and it will automatically commit and tag the version change.

Eg:

npm version patch --git-tag-version

The above will increment the third digit of the version and it will tag the commit with that version.

Short description for semantic versioning syntax:

Stage  | Rule                                                            | Example
----------------------------------------------------------------------------------
major  | Increment the first digit and reset 2nd and 3rd digits to zero  | 4.0.0
minor  | Increment the middle digit and reset the 3rd digit to zero      | 3.4.0
patch  | Increment the 3rd digit                                         | 3.4.2

To trigger the drone pipeline, run:

git push origin <tag>

Note that git push origin --tags does not trigger the drone pipeline. It is however better to trigger it with only 1 tag referenced using git push origin <tag>.

Drone pipelines

The database itself is deployed as a Cloudformation stack through Drone pipelines by promote events to a specific environment:

drone build promote trinitymirror-ondemand/tm-apps-db 268 bertha

More information on promote on the drone documentation.

Note: You may need to configure drone locally before being able to run this command (to get the DRONE_TOKEN token, go to your drone's account):

export DRONE_SERVER=https://droneb.tm-dev-awx.com
export DRONE_TOKEN=<your_token_here>
drone info

CloudFormation Stacks

There are 2 Cloudformation stacks that gets deployed:

  • tm-apps-db-env containing a RDS instance (postgres), a security and a subnet group, with alarms set up for CPU and Storage space
  • tm-apps-db-dns-env containing a Record Set for the RDS Instance address

CF Stack Policy

The drone step add-stack-policy is adding a stack policy to the stack tm-apps-db-<env> that block any deletion or replacement on RDS instances.

More information on AWS documentation.

CF termination protection

The drone step add-stack-termination-protection is adding a termination protection to the stack tm-apps-db-<env>.

More information on AWS documentation.

DB Snapshot Identifier

If the database is created (or replaced during update), it can (or should) be based on a hardcoded DBSnapshotIdentifier specified in the Drone file to properly support restore situations. A snapshot can be created via the AWS console (either from tm-apps-db-stable for the dev pipeline or tm-apps-db-prod for the prod pipeline). Then, replace the respective DBSnapshotIdentifier field inside of the drone file. If the DB experienced some downtime during update, run the seeding job described further down in this file.

Note that once a DB instance is restored with a DBSnapshotIdentifier property, the same DBSnapshotIdentifier property must be specified for any future updates to the DB instance. This means that once the DB is created base on a snapshot, any further updates with the same snapshot identifier will not require a replacement of that DB.

Read more on AWS Guide about restoring a database from a snapshot using DBSnapshotIdentifier.

Notes

  • When a new migration for adding a new table is performed against the database, ensure that you update sql\V001__add_roles.sql or create a new migration file for granting the right permissions to consumers and apis which will run in the deployment pipeline with flyway. If a new migration file is added, then consider to update the prebuild.sh script accordingly.
  • an ignore field was added to the lint-cf steps in drone.yml related to rule W3011 that require the UpdateReplacePolicy field for an RDS instance. Adding this field would require a replacement of the DB which is not wanted at that time (as it is a risky operation); this field could be added when a time arrives when we need to delete or replace the RDS instance.

How to recover in case of a Database Outage

  1. Create or use an existing snapshot identifier
  2. Update DBSnapshotIdentifier value in .drone.yml file
  3. Deploy the stack using the specified snapshot
  4. Recovering missing data:
    • all consumers that are populating the database have TRIM_HORIZON as their starting position, meaning that we can rely on the state store database streams to repopulate the missing data from the last 24h
    • if the missing data is older than 24h, use the Chronos Reseeder (Jenkins job)
  5. There is no need to update the consumers and the apis to point on the new database since they are pointing to the database DNS which gets updated with the new database address (default TTL is set to 60 seconds)

Recent failures

  • CRITICAL : Copying a table won’t copy indexes, so when a query with JOIN operations is executed is not performing as expected: make sure to recreate all the indexes on a table (June 2017, observed high levels of CPU Usage, Follow Author Epic).

TODO

  • update .npmignore with the right files / folders
  • move CI files into a specific CI folder to clean up root
  • find a way to integrate the creation of stacks to the drone step add-stack-policy, potentially by including stack policies into the trinitymirror/drone-cloudformation image.

Change Logs

version 4.2.0

  • added is_live field to article table + created a migration file

version 4.1.1

  • removed virtual setter on tag_id column from tag model. This was probably an error in the first place but was not damaging with previous versions of Sequelize. However, with v5, it nows breaks the code so it was needed to be taken out

version 4.1.0 - NOT WORKING

  • correct model definition adding the right foreignKey to each relevant association

version 4.0.0 to 4.0.5 - NOT WORKING

  • upgrade sequelize from v3.24.3 to v5.21.5

    • model.find alias was deprecated and replaced by model.findOne
    • removed classMethods and instanceMethods options from sequelize.define and replaced by

      // Class Method
      Model.associate = function (models) {
          ...associate the models
      };

      and

      // Instance Method
      Model.prototype.someMethod = function () {..}
  • upgrade commander from v2.20.0 to v5.0.0

  • upgrade pg from v6.1.0 to v7.18.2
  • upgrade sequelize-cli from v5.5.0 to v5.5.1
  • upgrade rambda from v to v0.27.0
  • add foreignKey to relevant model associations
  • add define property to configuration given new underscored rule of sequelize v5, as:

    config.define = {
        createdAt: 'created_at',
        updatedAt: 'updated_at',
        deletedAt: 'deleted_at'
    }

Note: version 4.0.0 to 4.0.5 are not working, first working version of v4 is v4.1.0

version 3.3.1

Last version (working) not tracked in this changelog.

4.3.17

2 months ago

4.3.18

2 months ago

4.3.16

8 months ago

4.3.15

8 months ago

4.3.14

1 year ago

4.3.13

1 year ago

4.3.12

1 year ago

4.3.11

1 year ago

4.3.6

2 years ago

4.3.4

3 years ago

4.3.5

3 years ago

4.3.3

3 years ago

4.3.2

3 years ago

4.3.1

3 years ago

4.3.0

4 years ago

4.2.1

4 years ago

4.2.0

4 years ago

4.2.0-0

4 years ago

4.1.1

4 years ago

4.0.5

4 years ago

4.1.0

4 years ago

4.0.4

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

3.3.1-spike

4 years ago

3.3.1

5 years ago

3.3.0

5 years ago

3.2.20-factbox4

5 years ago

3.2.20-factbox3

5 years ago

3.2.20-factbox2

5 years ago

3.2.20-factbox

5 years ago

3.2.20

7 years ago

3.2.19-pubs4

7 years ago

3.2.19-pubs3

7 years ago

3.2.19-pubs2

7 years ago

3.2.19-pubs

7 years ago

3.2.19

7 years ago

3.2.18

7 years ago

3.2.17

7 years ago

3.2.16

7 years ago

3.2.15

7 years ago

3.2.14

7 years ago

3.2.13

7 years ago

3.2.12

7 years ago

3.2.11

7 years ago

3.2.10

7 years ago

3.2.9

7 years ago

3.2.8

7 years ago

3.2.7

7 years ago

3.2.6

7 years ago

3.2.5

7 years ago

3.2.4

7 years ago

3.2.3

7 years ago

3.2.2

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.2

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.10

7 years ago

3.0.9

7 years ago

3.0.8

7 years ago

3.0.7

7 years ago

3.0.6

7 years ago

3.0.5

8 years ago

3.0.4

8 years ago

3.0.3

8 years ago

3.0.2

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.0.17

8 years ago

2.0.16

8 years ago

2.0.15

8 years ago

2.0.14

8 years ago

2.0.13

8 years ago

2.0.12

8 years ago

2.0.11

8 years ago

2.0.10

8 years ago

2.0.9

8 years ago

2.0.8

8 years ago

2.0.7

8 years ago

2.0.6

8 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.6.4

8 years ago

1.6.3

8 years ago

1.6.2

8 years ago

1.6.1

8 years ago

1.6.0

8 years ago

1.5.3

8 years ago

1.5.2

8 years ago

1.5.1

8 years ago

1.4.18

8 years ago

1.4.17

8 years ago

1.4.16

8 years ago

1.4.15

8 years ago

1.4.14

8 years ago

1.4.13

8 years ago

1.4.12

8 years ago

1.4.11

8 years ago

1.4.10

8 years ago

1.4.9

8 years ago

1.4.8

8 years ago

1.4.7

8 years ago

1.4.6

8 years ago

1.3.6

8 years ago

1.3.5

8 years ago

1.4.5

8 years ago

1.4.4

8 years ago

1.4.3

8 years ago

1.3.4

8 years ago

1.3.3

8 years ago

1.4.2

8 years ago

1.3.2

8 years ago

1.4.1

8 years ago

1.3.1

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago