Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 7ad820d0

Přidáno uživatelem David Friesecký před téměř 4 roky(ů)

Re #8927 - Improved loading scripts into DB

Zobrazit rozdíly:

SQLite_database.sql
1
/* ---------------------------------------------------- */
2
/*  Generated by Enterprise Architect Version 13.5 		*/
3
/*  Created On : 29-dub-2021 11:55:46 				*/
4
/*  DBMS       : SQLite 								*/
5
/* ---------------------------------------------------- */
6

  
7 1
/* Drop Tables */
8

  
9
DROP TABLE IF EXISTS 'Certificates'
10
;
11

  
12 2
DROP TABLE IF EXISTS 'CertificateTypes'
13 3
;
14 4

  
15 5
DROP TABLE IF EXISTS 'CertificateUsages'
16 6
;
17 7

  
18
DROP TABLE IF EXISTS 'PrivateKeys'
19
;
20

  
21
DROP TABLE IF EXISTS 'UsageTypes'
22
;
23

  
24 8
/* Create Tables with Primary and Foreign Keys, Check and Unique Constraints */
25 9

  
26
CREATE TABLE 'Certificates'
10
CREATE TABLE IF NOT EXISTS 'Certificates'
27 11
(
28 12
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
29 13
	'valid_from' TEXT NOT NULL,
......
48 32
)
49 33
;
50 34

  
51
CREATE TABLE 'CertificateTypes'
35
CREATE TABLE IF NOT EXISTS 'CertificateTypes'
52 36
(
53 37
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
54 38
	'certificate_type' TEXT NOT NULL
55 39
)
56 40
;
57 41

  
58
CREATE TABLE 'CertificateUsages'
42
CREATE TABLE IF NOT EXISTS 'CertificateUsages'
59 43
(
60 44
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
61 45
	'certificate_id' INTEGER NOT NULL,
......
65 49
)
66 50
;
67 51

  
68
CREATE TABLE 'PrivateKeys'
52
CREATE TABLE IF NOT EXISTS 'PrivateKeys'
69 53
(
70 54
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
71 55
	'private_key' TEXT NOT NULL,
......
73 57
)
74 58
;
75 59

  
76
CREATE TABLE 'UsageTypes'
60
CREATE TABLE IF NOT EXISTS 'UsageTypes'
77 61
(
78 62
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
79 63
	'usage_type' TEXT NOT NULL
SQLite_database_check.sql
1
/* Drop Tables */
2
DROP TABLE IF EXISTS 'CertificateTypes'
3
;
4

  
5
DROP TABLE IF EXISTS 'CertificateUsages'
6
;
7

  
8
/* Create Tables with Primary and Foreign Keys, Check and Unique Constraints */
9

  
10
CREATE TABLE IF NOT EXISTS 'Certificates'
11
(
12
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
13
	'valid_from' TEXT NOT NULL,
14
	'valid_to' TEXT NOT NULL,
15
	'pem_data' TEXT NOT NULL,
16
	'common_name' TEXT NOT NULL,
17
	'country_code' TEXT NULL,
18
	'locality' TEXT NULL,
19
	'province' TEXT NULL,
20
	'organization' TEXT NULL,
21
	'organizational_unit' TEXT NULL,
22
	'email_address' TEXT NULL,
23
	'revocation_date' TEXT NULL,
24
	'revocation_reason' TEXT NULL,
25
	'deletion_date' TEXT NULL,
26
	'certificate_type_id' INTEGER NOT NULL,
27
	'parent_certificate_id' INTEGER NOT NULL,
28
	'private_key_id' INTEGER NOT NULL,
29
	CONSTRAINT 'FK_Certificates' FOREIGN KEY ('parent_certificate_id') REFERENCES 'Certificates' ('id') ON DELETE No Action ON UPDATE No Action,
30
	CONSTRAINT 'FK_CertificateTypes' FOREIGN KEY ('certificate_type_id') REFERENCES 'CertificateTypes' ('id') ON DELETE No Action ON UPDATE No Action,
31
	CONSTRAINT 'FK_PrivateKeys' FOREIGN KEY ('private_key_id') REFERENCES 'PrivateKeys' ('id') ON DELETE No Action ON UPDATE No Action
32
)
33
;
34

  
35
CREATE TABLE IF NOT EXISTS 'CertificateTypes'
36
(
37
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
38
	'certificate_type' TEXT NOT NULL
39
)
40
;
41

  
42
CREATE TABLE IF NOT EXISTS 'CertificateUsages'
43
(
44
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
45
	'certificate_id' INTEGER NOT NULL,
46
	'usage_type_id' INTEGER NOT NULL,
47
	CONSTRAINT 'FK_Certificates' FOREIGN KEY ('certificate_id') REFERENCES 'Certificates' ('id') ON DELETE Cascade ON UPDATE No Action,
48
	CONSTRAINT 'FK_UsageTypes' FOREIGN KEY ('usage_type_id') REFERENCES 'UsageTypes' ('id') ON DELETE No Action ON UPDATE No Action
49
)
50
;
51

  
52
CREATE TABLE IF NOT EXISTS 'PrivateKeys'
53
(
54
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
55
	'private_key' TEXT NOT NULL,
56
	'password' TEXT NULL
57
)
58
;
59

  
60
CREATE TABLE IF NOT EXISTS 'UsageTypes'
61
(
62
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
63
	'usage_type' TEXT NOT NULL
64
)
65
;
src/config/connection_provider.py
5 5
from injector import Module, provider, singleton
6 6

  
7 7
from src.config.configuration import Configuration
8
from src.constants import DB_DIR, TEST_DATABASE_FILE
9
from src.db.init_queries import DEFAULT_VALUES_SQL, CHECK_SCHEMA_SQL
8
from src.constants import DB_DIR, TEST_DATABASE_FILE, SCHEMA_SQL_FILE, VALUES_SQL_FILE
10 9
from src.utils.file_anchor import FileAnchor
11 10
from src.utils.logger import Logger
12 11

  
......
34 33

  
35 34
            co = sqlite3.connect(database=configuration.connection_string, check_same_thread=False)
36 35
            cu = co.cursor()
37
            cu.executescript(CHECK_SCHEMA_SQL)
38
            cu.executescript(DEFAULT_VALUES_SQL)
36

  
37
            schema_sql_file = open(FileAnchor(configuration.root_dir, SCHEMA_SQL_FILE).shortest_relative_path())
38
            schema_sql = schema_sql_file.read()
39

  
40
            values_sql_file = open(FileAnchor(configuration.root_dir, VALUES_SQL_FILE).shortest_relative_path())
41
            values_sql = values_sql_file.read()
42

  
43
            cu.executescript(schema_sql)
44
            cu.executescript(values_sql)
39 45
        except sqlite3.Error as e:
40 46
            Logger.error(f"Unknown error during database setting.")
41 47
            raise e
src/constants.py
3 3
TEST_DATABASE_FILE = INMEMORY_DATABASE_FILE
4 4
DB_DIR = "db"
5 5
DATETIME_FORMAT = "%d.%m.%Y %H:%M:%S"
6
SCHEMA_SQL_FILE = "SQLite_database.sql"
7
VALUES_SQL_FILE = "SQLite_default_values.sql"
6 8

  
7 9
# Logging
8 10
LOG_DIR = "logs"
src/db/init_queries.py
1
SCHEMA_SQL = """
2
/* ---------------------------------------------------- */
3
/*  Generated by Enterprise Architect Version 13.5 		*/
4
/*  Created On : 29-dub-2021 11:55:46 				    */
5
/*  DBMS       : SQLite 								*/
6
/* ---------------------------------------------------- */
7

  
8
/* Drop Tables */
9

  
10
DROP TABLE IF EXISTS 'Certificates'
11
;
12

  
13
DROP TABLE IF EXISTS 'CertificateTypes'
14
;
15

  
16
DROP TABLE IF EXISTS 'CertificateUsages'
17
;
18

  
19
DROP TABLE IF EXISTS 'PrivateKeys'
20
;
21

  
22
DROP TABLE IF EXISTS 'UsageTypes'
23
;
24

  
25
/* Create Tables with Primary and Foreign Keys, Check and Unique Constraints */
26

  
27
CREATE TABLE 'Certificates'
28
(
29
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
30
	'valid_from' TEXT NOT NULL,
31
	'valid_to' TEXT NOT NULL,
32
	'pem_data' TEXT NOT NULL,
33
	'common_name' TEXT NOT NULL,
34
	'country_code' TEXT NULL,
35
	'locality' TEXT NULL,
36
	'province' TEXT NULL,
37
	'organization' TEXT NULL,
38
	'organizational_unit' TEXT NULL,
39
	'email_address' TEXT NULL,
40
	'revocation_date' TEXT NULL,
41
	'revocation_reason' TEXT NULL,
42
	'deletion_date' TEXT NULL,
43
	'certificate_type_id' INTEGER NOT NULL,
44
	'parent_certificate_id' INTEGER NOT NULL,
45
	'private_key_id' INTEGER NOT NULL,
46
	CONSTRAINT 'FK_Certificates' FOREIGN KEY ('parent_certificate_id') REFERENCES 'Certificates' ('id') ON DELETE No Action ON UPDATE No Action,
47
	CONSTRAINT 'FK_CertificateTypes' FOREIGN KEY ('certificate_type_id') REFERENCES 'CertificateTypes' ('id') ON DELETE No Action ON UPDATE No Action,
48
	CONSTRAINT 'FK_PrivateKeys' FOREIGN KEY ('private_key_id') REFERENCES 'PrivateKeys' ('id') ON DELETE No Action ON UPDATE No Action
49
)
50
;
51

  
52
CREATE TABLE 'CertificateTypes'
53
(
54
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
55
	'certificate_type' TEXT NOT NULL
56
)
57
;
58

  
59
CREATE TABLE 'CertificateUsages'
60
(
61
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
62
	'certificate_id' INTEGER NOT NULL,
63
	'usage_type_id' INTEGER NOT NULL,
64
	CONSTRAINT 'FK_Certificates' FOREIGN KEY ('certificate_id') REFERENCES 'Certificates' ('id') ON DELETE Cascade ON UPDATE No Action,
65
	CONSTRAINT 'FK_UsageTypes' FOREIGN KEY ('usage_type_id') REFERENCES 'UsageTypes' ('id') ON DELETE No Action ON UPDATE No Action
66
)
67
;
68

  
69
CREATE TABLE 'PrivateKeys'
70
(
71
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
72
	'private_key' TEXT NOT NULL,
73
	'password' TEXT NULL
74
)
75
;
76

  
77
CREATE TABLE 'UsageTypes'
78
(
79
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
80
	'usage_type' TEXT NOT NULL
81
)
82
;
83

  
84
"""
85

  
86
# TODO static tables are dropped
87
CHECK_SCHEMA_SQL = """
88
/* Drop Tables */
89
DROP TABLE IF EXISTS 'CertificateTypes'
90
;
91

  
92
DROP TABLE IF EXISTS 'CertificateUsages'
93
;
94

  
95
/* Create Tables with Primary and Foreign Keys, Check and Unique Constraints */
96

  
97
CREATE TABLE IF NOT EXISTS 'Certificates'
98
(
99
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
100
	'valid_from' TEXT NOT NULL,
101
	'valid_to' TEXT NOT NULL,
102
	'pem_data' TEXT NOT NULL,
103
	'common_name' TEXT NOT NULL,
104
	'country_code' TEXT NULL,
105
	'locality' TEXT NULL,
106
	'province' TEXT NULL,
107
	'organization' TEXT NULL,
108
	'organizational_unit' TEXT NULL,
109
	'email_address' TEXT NULL,
110
	'revocation_date' TEXT NULL,
111
	'revocation_reason' TEXT NULL,
112
	'deletion_date' TEXT NULL,
113
	'certificate_type_id' INTEGER NOT NULL,
114
	'parent_certificate_id' INTEGER NOT NULL,
115
	'private_key_id' INTEGER NOT NULL,
116
	CONSTRAINT 'FK_Certificates' FOREIGN KEY ('parent_certificate_id') REFERENCES 'Certificates' ('id') ON DELETE No Action ON UPDATE No Action,
117
	CONSTRAINT 'FK_CertificateTypes' FOREIGN KEY ('certificate_type_id') REFERENCES 'CertificateTypes' ('id') ON DELETE No Action ON UPDATE No Action,
118
	CONSTRAINT 'FK_PrivateKeys' FOREIGN KEY ('private_key_id') REFERENCES 'PrivateKeys' ('id') ON DELETE No Action ON UPDATE No Action
119
)
120
;
121

  
122
CREATE TABLE IF NOT EXISTS 'CertificateTypes'
123
(
124
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
125
	'certificate_type' TEXT NOT NULL
126
)
127
;
128

  
129
CREATE TABLE IF NOT EXISTS 'CertificateUsages'
130
(
131
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
132
	'certificate_id' INTEGER NOT NULL,
133
	'usage_type_id' INTEGER NOT NULL,
134
	CONSTRAINT 'FK_Certificates' FOREIGN KEY ('certificate_id') REFERENCES 'Certificates' ('id') ON DELETE Cascade ON UPDATE No Action,
135
	CONSTRAINT 'FK_UsageTypes' FOREIGN KEY ('usage_type_id') REFERENCES 'UsageTypes' ('id') ON DELETE No Action ON UPDATE No Action
136
)
137
;
138

  
139
CREATE TABLE IF NOT EXISTS 'PrivateKeys'
140
(
141
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
142
	'private_key' TEXT NOT NULL,
143
	'password' TEXT NULL
144
)
145
;
146

  
147
CREATE TABLE IF NOT EXISTS 'UsageTypes'
148
(
149
	'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
150
	'usage_type' TEXT NOT NULL
151
)
152
;
153

  
154
"""
155

  
156
DEFAULT_VALUES_SQL = """
157
/* Insert default values */
158

  
159
INSERT INTO CertificateTypes(certificate_type) VALUES('ROOT_CA');
160
INSERT INTO CertificateTypes(certificate_type) VALUES('INTERMEDIATE_CA');
161
INSERT INTO CertificateTypes(certificate_type) VALUES('CERTIFICATE');
162

  
163
INSERT INTO UsageTypes(usage_type) VALUES('CA');
164
INSERT INTO UsageTypes(usage_type) VALUES('SSL');
165
INSERT INTO UsageTypes(usage_type) VALUES('SIGNATURE');
166
INSERT INTO UsageTypes(usage_type) VALUES('AUTHENTICATION');
167

  
168
"""
tests/integration_tests/dao/conftest.py
6 6
from src.config.connection_provider import ConnectionProvider
7 7
from src.dao.certificate_repository import CertificateRepository
8 8
from src.dao.private_key_repository import PrivateKeyRepository
9
from src.db.init_queries import SCHEMA_SQL, DEFAULT_VALUES_SQL
10 9

  
11 10

  
12 11
# scope="module" means that this fixture is run once per module
tests/integration_tests/services/conftest.py
8 8
from src.config.connection_provider import ConnectionProvider
9 9
from src.dao.certificate_repository import CertificateRepository
10 10
from src.dao.private_key_repository import PrivateKeyRepository
11
from src.db.init_queries import SCHEMA_SQL, DEFAULT_VALUES_SQL
12 11
from src.model.subject import Subject
13 12
from src.services.certificate_service import CertificateService
14 13
from src.services.crl_ocsp.crl_ocsp_service import CrlOcspService

Také k dispozici: Unified diff