-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
normalize hostname and pathname columns
to save on disk storage
- Loading branch information
1 parent
69986d9
commit dbcadcd
Showing
39 changed files
with
415 additions
and
43 deletions.
There are no files selected for viewing
Binary file not shown.
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,30 @@ | ||
package sqlstore | ||
|
||
import ( | ||
"database/sql" | ||
) | ||
|
||
func (db *sqlstore) HostnameID(name string) (int64, error) { | ||
var id int64 | ||
query := db.Rebind("SELECT id FROM hostnames WHERE name = ? LIMIT 1") | ||
err := db.Get(&id, query, name) | ||
|
||
if err == sql.ErrNoRows { | ||
// Postgres does not support LastInsertID, so use a "... RETURNING" select query | ||
query := db.Rebind(`INSERT INTO hostnames(name) VALUES(?)`) | ||
if db.Driver == POSTGRES { | ||
err := db.Get(&id, query+" RETURNING id", name) | ||
return id, err | ||
} | ||
|
||
// MySQL and SQLite do support LastInsertID, so use that | ||
r, err := db.Exec(query, name) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return r.LastInsertId() | ||
} | ||
|
||
return id, err | ||
} |
8 changes: 8 additions & 0 deletions
8
pkg/datastore/sqlstore/migrations/mysql/12_create_hostnames_table.sql
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,8 @@ | ||
-- +migrate Up | ||
CREATE TABLE hostnames( | ||
id INTEGER AUTO_INCREMENT PRIMARY KEY NOT NULL, | ||
name VARCHAR(255) NOT NULL | ||
) CHARACTER SET=utf8; | ||
|
||
-- +migrate Down | ||
DROP TABLE IF EXISTS hostnames; |
5 changes: 5 additions & 0 deletions
5
pkg/datastore/sqlstore/migrations/mysql/13_create_unique_hostname_index.sql
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,5 @@ | ||
-- +migrate Up | ||
CREATE UNIQUE INDEX unique_hostnames_name ON hostnames(name); | ||
|
||
-- +migrate Down | ||
DROP INDEX IF EXISTS unique_hostnames_name; |
8 changes: 8 additions & 0 deletions
8
pkg/datastore/sqlstore/migrations/mysql/14_create_pathnames_table.sql
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,8 @@ | ||
-- +migrate Up | ||
CREATE TABLE pathnames( | ||
id INTEGER AUTO_INCREMENT PRIMARY KEY NOT NULL, | ||
name VARCHAR(255) NOT NULL | ||
) CHARACTER SET=utf8; | ||
|
||
-- +migrate Down | ||
DROP TABLE IF EXISTS pathnames; |
5 changes: 5 additions & 0 deletions
5
pkg/datastore/sqlstore/migrations/mysql/15_create_unique_pathname_index.sql
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,5 @@ | ||
-- +migrate Up | ||
CREATE UNIQUE INDEX unique_pathnames_name ON pathnames(name); | ||
|
||
-- +migrate Down | ||
DROP INDEX IF EXISTS unique_pathnames_name; |
6 changes: 6 additions & 0 deletions
6
pkg/datastore/sqlstore/migrations/mysql/16_fill_hostnames_table.sql
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,6 @@ | ||
-- +migrate Up | ||
INSERT IGNORE INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_page_stats; | ||
INSERT IGNORE INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_referrer_stats; | ||
|
||
-- +migrate Down | ||
|
6 changes: 6 additions & 0 deletions
6
pkg/datastore/sqlstore/migrations/mysql/17_fill_pathnames_table.sql
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,6 @@ | ||
-- +migrate Up | ||
INSERT IGNORE INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_page_stats; | ||
INSERT IGNORE INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_referrer_stats; | ||
|
||
-- +migrate Down | ||
|
24 changes: 24 additions & 0 deletions
24
pkg/datastore/sqlstore/migrations/mysql/18_alter_page_stats_table.sql
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,24 @@ | ||
-- +migrate Up | ||
DROP TABLE IF EXISTS daily_page_stats_old; | ||
RENAME TABLE daily_page_stats TO daily_page_stats_old; | ||
CREATE TABLE daily_page_stats( | ||
site_id INTEGER NOT NULL DEFAULT 1, | ||
hostname_id INTEGER NOT NULL, | ||
pathname_id INTEGER NOT NULL, | ||
pageviews INTEGER NOT NULL, | ||
visitors INTEGER NOT NULL, | ||
entries INTEGER NOT NULL, | ||
bounce_rate FLOAT NOT NULL, | ||
known_durations INTEGER NOT NULL DEFAULT 0, | ||
avg_duration FLOAT NOT NULL, | ||
date DATE NOT NULL | ||
) CHARACTER SET=utf8; | ||
INSERT INTO daily_page_stats | ||
SELECT site_id, h.id, p.id, pageviews, visitors, entries, bounce_rate, known_durations, avg_duration, date | ||
FROM daily_page_stats_old s | ||
LEFT JOIN hostnames h ON h.name = s.hostname | ||
LEFT JOIN pathnames p ON p.name = s.pathname; | ||
DROP TABLE daily_page_stats_old; | ||
|
||
-- +migrate Down | ||
|
24 changes: 24 additions & 0 deletions
24
pkg/datastore/sqlstore/migrations/mysql/19_alter_referrer_stats_table.sql
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,24 @@ | ||
-- +migrate Up | ||
DROP TABLE IF EXISTS daily_referrer_stats_old; | ||
RENAME TABLE daily_referrer_stats TO daily_referrer_stats_old; | ||
CREATE TABLE daily_referrer_stats( | ||
site_id INTEGER NOT NULL DEFAULT 1, | ||
hostname_id INTEGER NOT NULL, | ||
pathname_id INTEGER NOT NULL, | ||
groupname VARCHAR(255) NULL, | ||
pageviews INTEGER NOT NULL, | ||
visitors INTEGER NOT NULL, | ||
bounce_rate FLOAT NOT NULL, | ||
known_durations INTEGER NOT NULL DEFAULT 0, | ||
avg_duration FLOAT NOT NULL, | ||
date DATE NOT NULL | ||
) CHARACTER SET=utf8; | ||
INSERT INTO daily_referrer_stats | ||
SELECT site_id, h.id, p.id, groupname, pageviews, visitors, bounce_rate, known_durations, avg_duration, date | ||
FROM daily_referrer_stats_old s | ||
LEFT JOIN hostnames h ON h.name = s.hostname | ||
LEFT JOIN pathnames p ON p.name = s.pathname; | ||
DROP TABLE daily_referrer_stats_old; | ||
|
||
-- +migrate Down | ||
|
7 changes: 7 additions & 0 deletions
7
pkg/datastore/sqlstore/migrations/mysql/20_recreate_stats_indices.sql
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,7 @@ | ||
-- +migrate Up | ||
CREATE UNIQUE INDEX unique_daily_page_stats ON daily_page_stats(site_id, hostname_id, pathname_id, date); | ||
CREATE UNIQUE INDEX unique_daily_referrer_stats ON daily_referrer_stats(site_id, hostname_id, pathname_id, date); | ||
|
||
-- +migrate Down | ||
DROP INDEX unique_daily_page_stats ON daily_page_stats; | ||
DROP INDEX unique_daily_referrer_stats ON daily_referrer_stats; |
8 changes: 8 additions & 0 deletions
8
pkg/datastore/sqlstore/migrations/postgres/13_create_hostnames_table.sql
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,8 @@ | ||
-- +migrate Up | ||
CREATE TABLE hostnames( | ||
id SERIAL PRIMARY KEY NOT NULL, | ||
name VARCHAR(255) NOT NULL | ||
); | ||
|
||
-- +migrate Down | ||
DROP TABLE IF EXISTS hostnames; |
5 changes: 5 additions & 0 deletions
5
pkg/datastore/sqlstore/migrations/postgres/14_create_unique_hostname_index.sql
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,5 @@ | ||
-- +migrate Up | ||
CREATE UNIQUE INDEX unique_hostnames_name ON hostnames(name); | ||
|
||
-- +migrate Down | ||
DROP INDEX IF EXISTS unique_hostnames_name; |
8 changes: 8 additions & 0 deletions
8
pkg/datastore/sqlstore/migrations/postgres/15_create_pathnames_table.sql
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,8 @@ | ||
-- +migrate Up | ||
CREATE TABLE pathnames( | ||
id SERIAL PRIMARY KEY NOT NULL, | ||
name VARCHAR(255) NOT NULL | ||
); | ||
|
||
-- +migrate Down | ||
DROP TABLE IF EXISTS pathnames; |
5 changes: 5 additions & 0 deletions
5
pkg/datastore/sqlstore/migrations/postgres/16_create_unique_pathname_index.sql
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,5 @@ | ||
-- +migrate Up | ||
CREATE UNIQUE INDEX unique_pathnames_name ON pathnames(name); | ||
|
||
-- +migrate Down | ||
DROP INDEX IF EXISTS unique_pathnames_name; |
6 changes: 6 additions & 0 deletions
6
pkg/datastore/sqlstore/migrations/postgres/17_fill_hostnames_table.sql
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,6 @@ | ||
-- +migrate Up | ||
INSERT INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_page_stats; | ||
INSERT INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_referrer_stats ON CONFLICT(name) DO NOTHING; | ||
|
||
-- +migrate Down | ||
|
6 changes: 6 additions & 0 deletions
6
pkg/datastore/sqlstore/migrations/postgres/18_fill_pathnames_table.sql
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,6 @@ | ||
-- +migrate Up | ||
INSERT INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_page_stats ON CONFLICT(name) DO NOTHING; ; | ||
INSERT INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_referrer_stats ON CONFLICT(name) DO NOTHING; ; | ||
|
||
-- +migrate Down | ||
|
24 changes: 24 additions & 0 deletions
24
pkg/datastore/sqlstore/migrations/postgres/19_alter_page_stats_table.sql
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,24 @@ | ||
-- +migrate Up | ||
DROP TABLE IF EXISTS daily_page_stats_old; | ||
ALTER TABLE daily_page_stats RENAME TO daily_page_stats_old; | ||
CREATE TABLE daily_page_stats( | ||
site_id INTEGER NOT NULL DEFAULT 1, | ||
hostname_id INTEGER NOT NULL, | ||
pathname_id INTEGER NOT NULL, | ||
pageviews INTEGER NOT NULL, | ||
visitors INTEGER NOT NULL, | ||
entries INTEGER NOT NULL, | ||
bounce_rate FLOAT NOT NULL, | ||
known_durations INTEGER NOT NULL DEFAULT 0, | ||
avg_duration FLOAT NOT NULL, | ||
date DATE NOT NULL | ||
); | ||
INSERT INTO daily_page_stats | ||
SELECT site_id, h.id, p.id, pageviews, visitors, entries, bounce_rate, known_durations, avg_duration, date | ||
FROM daily_page_stats_old s | ||
LEFT JOIN hostnames h ON h.name = s.hostname | ||
LEFT JOIN pathnames p ON p.name = s.pathname; | ||
DROP TABLE daily_page_stats_old; | ||
|
||
-- +migrate Down | ||
|
24 changes: 24 additions & 0 deletions
24
pkg/datastore/sqlstore/migrations/postgres/20_alter_referrer_stats_table.sql
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,24 @@ | ||
-- +migrate Up | ||
DROP TABLE IF EXISTS daily_referrer_stats_old; | ||
ALTER TABLE daily_referrer_stats RENAME TO daily_referrer_stats_old; | ||
CREATE TABLE daily_referrer_stats( | ||
site_id INTEGER NOT NULL DEFAULT 1, | ||
hostname_id INTEGER NOT NULL, | ||
pathname_id INTEGER NOT NULL, | ||
groupname VARCHAR(255) NULL, | ||
pageviews INTEGER NOT NULL, | ||
visitors INTEGER NOT NULL, | ||
bounce_rate FLOAT NOT NULL, | ||
known_durations INTEGER NOT NULL DEFAULT 0, | ||
avg_duration FLOAT NOT NULL, | ||
date DATE NOT NULL | ||
); | ||
INSERT INTO daily_referrer_stats | ||
SELECT site_id, h.id, p.id, groupname, pageviews, visitors, bounce_rate, known_durations, avg_duration, date | ||
FROM daily_referrer_stats_old s | ||
LEFT JOIN hostnames h ON h.name = s.hostname | ||
LEFT JOIN pathnames p ON p.name = s.pathname; | ||
DROP TABLE daily_referrer_stats_old; | ||
|
||
-- +migrate Down | ||
|
7 changes: 7 additions & 0 deletions
7
pkg/datastore/sqlstore/migrations/postgres/21_recreate_stats_indices.sql
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,7 @@ | ||
-- +migrate Up | ||
CREATE UNIQUE INDEX unique_daily_page_stats ON daily_page_stats(site_id, hostname_id, pathname_id, date); | ||
CREATE UNIQUE INDEX unique_daily_referrer_stats ON daily_referrer_stats(site_id, hostname_id, pathname_id, date); | ||
|
||
-- +migrate Down | ||
DROP INDEX unique_daily_page_stats; | ||
DROP INDEX unique_daily_referrer_stats; |
8 changes: 8 additions & 0 deletions
8
pkg/datastore/sqlstore/migrations/sqlite3/12_create_hostnames_table.sql
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,8 @@ | ||
-- +migrate Up | ||
CREATE TABLE hostnames( | ||
id INTEGER PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL | ||
); | ||
|
||
-- +migrate Down | ||
DROP TABLE IF EXISTS hostnames; |
5 changes: 5 additions & 0 deletions
5
pkg/datastore/sqlstore/migrations/sqlite3/13_create_unique_hostname_index.sql
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,5 @@ | ||
-- +migrate Up | ||
CREATE UNIQUE INDEX unique_hostnames_name ON hostnames(name); | ||
|
||
-- +migrate Down | ||
DROP INDEX IF EXISTS unique_hostnames_name; |
8 changes: 8 additions & 0 deletions
8
pkg/datastore/sqlstore/migrations/sqlite3/14_create_pathnames_table.sql
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,8 @@ | ||
-- +migrate Up | ||
CREATE TABLE pathnames( | ||
id INTEGER PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL | ||
); | ||
|
||
-- +migrate Down | ||
DROP TABLE IF EXISTS pathnames; |
5 changes: 5 additions & 0 deletions
5
pkg/datastore/sqlstore/migrations/sqlite3/15_create_unique_pathname_index.sql
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,5 @@ | ||
-- +migrate Up | ||
CREATE UNIQUE INDEX unique_pathnames_name ON pathnames(name); | ||
|
||
-- +migrate Down | ||
DROP INDEX IF EXISTS unique_pathnames_name; |
6 changes: 6 additions & 0 deletions
6
pkg/datastore/sqlstore/migrations/sqlite3/16_fill_hostnames_table.sql
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,6 @@ | ||
-- +migrate Up | ||
INSERT OR IGNORE INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_page_stats; | ||
INSERT OR IGNORE INTO hostnames(name) SELECT DISTINCT(hostname) FROM daily_referrer_stats; | ||
|
||
-- +migrate Down | ||
|
6 changes: 6 additions & 0 deletions
6
pkg/datastore/sqlstore/migrations/sqlite3/17_fill_pathnames_table.sql
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,6 @@ | ||
-- +migrate Up | ||
INSERT OR IGNORE INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_page_stats; | ||
INSERT OR IGNORE INTO pathnames(name) SELECT DISTINCT(pathname) FROM daily_referrer_stats; | ||
|
||
-- +migrate Down | ||
|
Oops, something went wrong.