Skip to content

Commit c007992

Browse files
committed
Merge branch 'master' of github.com:FAC9/READMES into restructure-folders
2 parents 3b5ec6d + a67edd0 commit c007992

8 files changed

+101
-33
lines changed

databases/Testing.md

+28-15
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
1+
# Testing
2+
13
## If not mocking the database, how do you ensure you don't fill your database with test insertions?
24

3-
To test a real database, you need a way to create a test table at the start of each test, then test your database calls, and finally drop your table after each test.
5+
To test a real database, you either need a way to create a test table at the start of each test, then test your database calls, and finally drop your table after each test... or have an existing test table in place, and simply ensure that after adding test insertions you remove them after testing.
46

5-
If you want to work with similar table data in your tests, you can use SQLs VIEW method:
7+
If you want to work with a similar table data in your tests, you can set up a View with SQLs VIEW method:
68
```sql
79
CREATE VIEW my_view AS
810
SELECT column_name(s)
911
FROM original_table
1012
WHERE condition
1113
```
12-
Views act like temporary tables in memory. You can perform the same actions as you would a real table. After working with your view table, you can drop it...
14+
Views act like temporary tables in memory. You can perform the same actions as you would a real table. You can even combine multiple tables into one view. After working with your view table, you can drop it...
1315
```sql
1416
DROP VIEW my_view
1517
```
1618

17-
Otherwise, you can simply create a table in the same way, and drop it after testing.
19+
Otherwise, you can simply create a new table as you are testing, then drop it afterwards.
1820

19-
To integrate this approach in a real testing framework, you would perform the following tasks-
21+
To integrate the create/drop method in a real testing framework, you would need to perform the following tasks-
2022
1. (Before Test) Create test database from desired testing table.
2123
2. Perform actions (e.g add/delete/modify data).
2224
3. Use SELECT queries on this test table, and asserts to test that data is manipulated.
2325
4. (After Test) Drop test table.
2426

2527
Many testing frameworks allow beforeEach and AfterEach as a testing method, so steps **1** and **4** would be performed before and after each test.
2628

27-
Check out [redtape](https://github.com/eugeneware/redtape) for examples on how to use a tape like testing environment with before each and after each methods.
29+
Check out [redtape](https://github.com/eugeneware/redtape) for examples on how to use a tape like testing environment with before each and after each methods. When testing, an example test could appear like so;
30+
```javascript
31+
test('checks results', function() {
32+
var client = new Client();
33+
client.connect();
34+
client.query('SELECT * FROM person WHERE name = $1', ['Brian'], assert.calls(function(err, result) {
35+
t.notOk(err);
36+
t.equal(result.rows[0].name, 'Brian');
37+
client.end();
38+
t.end();
39+
}))
40+
}));
41+
})
42+
```
2843

29-
## Research and figure out how to use a mocking library.
3044
## What are some advantages and disadvantages of mocking?
3145
### Advantages of mocking
3246
- using mock objects and data can be a good way to unit-test, but integration testing is needed to ensure that the system runs as a whole
@@ -39,11 +53,10 @@ Check out [redtape](https://github.com/eugeneware/redtape) for examples on how t
3953
- there are security and privacy implications to mock databases
4054
- once the db gets big, taking a copy of it and putting it into your dev environment can be a big deal
4155

42-
## What exactly needs testing about the database?
43-
- Test if any errors are shown while executing queries
44-
- Data Integrity is maintained while creating, updating or deleting data in database
45-
- Check response time of queries and fine tune them if necessary
46-
- Test data retrieved from your database is shown accurately in your web application
47-
48-
Find out more: http://www.guru99.com/web-application-testing.html
49-
56+
## Resources
57+
- [An overview of database testing](http://www.softwaretestinghelp.com/database-testing-process/)
58+
- [Strategies for unit-testing database-driven applications](http://stackoverflow.com/questions/145131/whats-the-best-strategy-for-unit-testing-database-driven-applications)
59+
- [Testing: when to connect the database](http://softwareengineering.stackexchange.com/questions/206539/unit-tests-and-databases-at-which-point-do-i-actually-connect-to-the-database)
60+
- [Wikipedia entry on mock objects](https://en.wikipedia.org/wiki/Mock_object)
61+
- [Martin Fowler on classical testing vs mocking](http://martinfowler.com/articles/mocksArentStubs.html)
62+
- [Node Postgres Test](https://www.npmjs.com/package/pgtest)

databases/importing-exporting-migrating.md

+66-15
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,72 @@
22

33
## How to import databases from an existing project
44

5-
## How to export databases
5+
The two methods to do this are:
6+
- GUI database client tool
7+
- The command-Line
8+
9+
##### GUI tools:
10+
- SQL Server Management Studio
11+
- Toad for SQL Server
12+
- Database fishing tool
13+
- Apex SQL Studio
14+
- SQuirreL SQL
15+
16+
#### Walk through to import a sql file into your database.
17+
18+
1. Create a database / decide which database you want to import into:<br>
19+
<br> ![](https://github.com/FAC9/READMES/blob/master/week-7/images/1.%20Create%20database.png)
20+
21+
2. As you can see the database is empty:<br>
22+
<br> ![](https://github.com/FAC9/READMES/blob/master/week-7/images/2.%20The%20database%20is%20empty.png)
23+
24+
3. Get the sql file that you want to import:<br>
25+
<br> ![](https://github.com/FAC9/READMES/blob/master/week-7/images/3.%20beg:borrow:steal%20sql%20file.png)
26+
27+
4. Type the command into Bash, NOT in postgresql command line:<br>
28+
<br> ![](https://github.com/FAC9/READMES/blob/master/week-7/images/4%20Command%20to%20import%20database.png)
29+
30+
5. The database has been imported (here is the proof):<br>
31+
<br> ![](https://github.com/FAC9/READMES/blob/master/week-7/images/5.%20Full%20database!.png)
632

733
## Migrations in Postgres
834

9-
### Things to consider
35+
### What is a database schema?
36+
37+
A database schema defines how a database is structured. It defines the tables that exist and their names, how those tables are configured, the fields that those tables contain, and the data format for those fields. A schema describes how real-world (or conceptual) entities are modeled by the database.
38+
39+
The schema doesn’t contain any data in itself. It is simply a description of how data added to a database must be structured.
40+
41+
If you start working on a project with an existing database, the first thing you should do is ask for a schema diagram. The diagram will show all of the tables in the database and the foreign keys they use to relate. It should look like this:
42+
43+
![schema-image](http://www.codeproject.com/KB/database/UnitTestDbAppsWithNDbUnit/NDbUnitXPath.jpg)
44+
45+
### What is migration?
1046

11-
- You have a live site with live users, and ideally you want to avoid downtime
12-
- User data is very sensitive and not to be messed with
47+
Migration is the processing of transferring data from one schema to another - often within the same database. (It may also refer to the process of moving data between two different databases with the same schema, although this tends to be a simpler task - just export from one then import into the other.)
1348

49+
### When might you need to migrate a database?
1450

15-
### Example: cat lovers database 🐱
51+
As a company expands or develops, the real-world or conceptual entities it deals with may change or become more complicated. When this happens, you might need to update your database schema to reflect the changes in the real-world entities that you're dealing with – but all of the data created under the old schema will need to be ‘migrated’ into the new format in order to preserve your previous records. See _Example: cat lovers database_ below.
52+
53+
### The steps of migrating a database
54+
55+
There are many approaches to database migration. One of the most commonly used patterns is __ETL (export, transform, load)__, although others exist.
56+
57+
To migrate data using the ETL pattern, you would first __identify which tables are affected by the migration__. Databases can be huge, so you only want to be working with the data that is specifically affected by the change of schema.
58+
59+
Making changes directly into a live database is a _very bad idea_ – a single typo or poorly written query can destroy millions of records. So when you’ve selected the affected data, you should __export__ it to a safe area, separate from your live data.
60+
61+
When you’ve exported your data, you then need to __transform__ the data from the old schema into the new schema. This should be done using scripts – firstly because this reduces the possibility of human error, and secondly because the transformation can then be applied to your databases in other environments (for example, your production, test and development environments). See _Example: cat lovers database_ for an example script.
62+
63+
When the data has been transformed and validated, you can then __load__ the transformed data back into the database.
64+
65+
### Things to consider
66+
67+
- If you have a live site with live users, ideally you want to avoid downtime.
68+
- User data is very sensitive and not to be messed with.
69+
70+
### Example: cat lovers database 🐱
1671

1772
In our example, we have a really basic database with just one table. It stores a list of cat lovers and the names of their cats. This works OK for the organisation updating the cat lovers database for a few years, until some of the members start adopting new cats. The single-table database structure just isn't sufficient any more.
1873

@@ -36,29 +91,25 @@ We'll write some SQL for that:
3691

3792
```
3893
INSERT INTO cats (name, color, catLoverId) VALUES
39-
SELECT cat_name, cat_color, id FROM catLovers
94+
SELECT cat_name, cat_color, id FROM catLovers;
4095
```
41-
- Now all our data is in one place but we've got some extra columns left in our cat_lovers table which we don't really need any more. In practice, these columns aren't causing us any problems, so we'll leave them in place for an interim period so that we can be 500% sure that none of our data has been lost in migration.
42-
- Later on, we might want to delete the old data for good. After all, some of those cats might have changed colour by now. We can do this with SQL:
96+
- Now all our data is in one place but we've got some extra columns left in our cat_lovers table which we don't really need any more. In practice, these columns aren't causing us any problems, so we'll leave them in place for an interim period so that we can be 500% sure that none of our data has got lost in migration.
97+
- Later on, we might want to delete the old data for good. After all, some of those cats might have changed colour by now and we wouldn't want to be storing inaccurate data. We can do this with SQL:
4398

4499
```
45100
ALTER TABLE cat_lovers
46-
DROP COLUMN (cat_name, cat_color)
101+
DROP COLUMN (cat_name, cat_color);
47102
```
48103

49104
- Our lovely two-columned database now looks more like this:
50105

51106
![image_2](./images/Database_schema2.png)
52-
53107
😻
54108

55109
## Resources
56110

111+
The [Wikipedia entry on ETL](https://en.wikipedia.org/wiki/Extract,_transform,_load) explains a little more about the pattern, its history and its uses.
112+
57113
This [post on StackOverflow](http://dba.stackexchange.com/questions/10913/best-practices-for-schema-changes-and-data-migrations-to-a-live-database-without) informed the cats example and talks about how to minimize impact of database migrations
58114

59115
Here's an [article](https://kostasbariotis.com/data-migration-with-nodejs/) that talks about migration in a node.js context
60-
61-
Steve links
62-
- https://www.postgresql.org/docs/9.1/static/backup-dump.html
63-
- https://www.postgresql.org/docs/8.1/static/app-pgdump.html
64-
- https://www.postgresql.org/docs/8.1/static/backup.html

databases/tables-structure-joining.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,12 @@ This diagram may be initally confusing as rows in the diagram correspond to colu
235235

236236
## Resources
237237

238-
- [A Visual Explanation of SQL Joins](https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/)
239-
- [PostgreSQL Tutorial](http://www.postgresqltutorial.com/), section 4
240-
Normalisation resources:
238+
Normalisation resources:
241239
- http://www.studytonight.com/dbms/database-normalization.php,
242240
- http://agiledata.org/essays/dataNormalization.html
241+
242+
Table joining resources:
243+
- [A Visual Explanation of SQL Joins](https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/)
244+
- [PostgreSQL Tutorial](http://www.postgresqltutorial.com/), section 4
245+
246+

week-7/images/1. Create database.png

44.1 KB
Loading
11.7 KB
Loading
218 KB
Loading
11.1 KB
Loading

week-7/images/5. Full database!.png

53.9 KB
Loading

0 commit comments

Comments
 (0)