summaryrefslogtreecommitdiff
path: root/tools/generate-schemas
blob: 9d9821c56597504fc4bd3dfa73f882c5f910086f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash
#
# Script to generate schemas for the various versions.
#
# Some setup is required, similar to the opportunistic tests.
#
# MySQL ->
#
# $ mysql -uroot
# MariaDB [(none)]> CREATE DATABASE keystone
# MariaDB [(none)]> GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' IDENTIFIED BY 'password';
# MariaDB [(none)]> quit;
#
# Postgres ->
#
# $ sudo -u postgres psql
# postgres=# create user keystone with createdb login password 'password';
# postgres=# create database keystone with owner keystone;
# postgres=# quit;
#
# Note that you may also have to configure 'pg_hba.conf' to use password-based
# auth instead of "ident", if you haven't done so already. You can locate this
# with 'locate pg_hba.conf'. More details at
# https://ubuntu.com/server/docs/databases-postgresql

set -o xtrace
set -e

source .tox/py38/bin/activate

INIT_VERSION=$(ls -1 keystone/common/sql/expand_repo/versions/ | head -1 | awk -F_ '{print $1}' | sed 's/^0*//')
INIT_VERSION=$(($INIT_VERSION-1))

echo "Detected init version of $INIT_VERSION"

mkdir -p /tmp/keystone-schemas
rm -f "/tmp/keystone-schemas/$INIT_VERSION-*.sql"

#
# functions
#

function sync () {
    DB_URL=$1

    python keystone/common/sql/expand_repo/manage.py version_control \
      --database "$DB_URL" \
      --version "$INIT_VERSION" \
      --repository keystone/common/sql/expand_repo/
    python keystone/common/sql/data_migration_repo/manage.py version_control \
      --database "$DB_URL" \
      --version "$INIT_VERSION" \
      --repository keystone/common/sql/data_migration_repo/
    python keystone/common/sql/contract_repo/manage.py version_control \
      --database "$DB_URL" \
      --version "$INIT_VERSION" \
      --repository keystone/common/sql/contract_repo/

    python keystone/common/sql/expand_repo/manage.py upgrade \
      --database "$DB_URL" \
      --repository keystone/common/sql/expand_repo/
    python keystone/common/sql/data_migration_repo/manage.py upgrade \
      --database "$DB_URL" \
      --repository keystone/common/sql/data_migration_repo/
    python keystone/common/sql/contract_repo/manage.py upgrade \
      --database "$DB_URL" \
      --repository keystone/common/sql/contract_repo/
}

#
# sqlite
#

# cleanup from previous runs

rm -f /tmp/keystone.db

# sync schema

sync 'sqlite:////tmp/keystone.db'

# dump the schema

sqlite3 /tmp/keystone.db << EOF
.output "/tmp/keystone-schemas/${INIT_VERSION}-sqlite.sql"
.schema
.quit
EOF

rm -f /tmp/keystone.db

#
# mysql
#

# cleanup from previous runs

mysql -u keystone -ppassword << EOF
DROP DATABASE IF EXISTS keystone;
CREATE DATABASE keystone;
EOF

# sync schema

sync 'mysql+pymysql://keystone:password@localhost/keystone'

# dump the schema

mysqldump --no-data --skip-comments -u keystone -ppassword \
    keystone > "/tmp/keystone-schemas/${INIT_VERSION}-mysql.sql"

mysql -u keystone -ppassword << EOF
DROP DATABASE IF EXISTS keystone;
EOF

#
# postgres
#

# cleanup from previous runs

sudo -u postgres dropdb --if-exists keystone
sudo -u postgres createdb --owner=keystone keystone

# sync to initial version

sync 'postgresql://keystone:password@localhost/keystone'

# dump the schema

pg_dump postgresql://keystone:password@localhost/keystone \
    --schema-only > "/tmp/keystone-schemas/${INIT_VERSION}-postgres.sql"

sudo -u postgres dropdb --if-exists keystone