Skip to content

Commit 2d412a6

Browse files
pietroalbiniJoshua Nelson
authored and
Joshua Nelson
committed
db: automatically replace "CREATE TABLE" in migrations
1 parent 9556b7b commit 2d412a6

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/db/migrate.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use error::Result as CratesfyiResult;
44
use postgres::{Connection, transaction::Transaction, Error as PostgresError};
55
use schemamama::{Migration, Migrator, Version};
66
use schemamama_postgres::{PostgresAdapter, PostgresMigration};
7+
use std::borrow::Cow;
78

89

910
#[derive(Copy, Clone)]
@@ -18,11 +19,13 @@ struct MigrationContext {
1819
}
1920

2021
impl MigrationContext {
21-
fn format_query(&self, query: &str) -> String {
22-
query.replace("{create_table}", match self.apply_mode {
23-
ApplyMode::Permanent => "CREATE TABLE",
24-
ApplyMode::Temporary => "CREATE TEMPORARY TABLE",
25-
})
22+
fn format_query<'a>(&self, query: &'a str) -> Cow<'a, str> {
23+
match self.apply_mode {
24+
ApplyMode::Permanent => Cow::Borrowed(query),
25+
ApplyMode::Temporary => {
26+
Cow::Owned(query.replace("CREATE TABLE", "CREATE TEMPORARY TABLE"))
27+
}
28+
}
2629
}
2730
}
2831

@@ -35,7 +38,7 @@ impl MigrationContext {
3538
/// ```
3639
/// let my_migration = migration!(100,
3740
/// "Create test table",
38-
/// "{create_table} test ( id SERIAL);",
41+
/// "CREATE TABLE test ( id SERIAL);",
3942
/// "DROP TABLE test;");
4043
/// ```
4144
macro_rules! migration {
@@ -79,7 +82,7 @@ fn migrate_inner(version: Option<Version>, conn: &Connection, apply_mode: ApplyM
7982

8083
conn.execute(
8184
&context.format_query(
82-
"{create_table} IF NOT EXISTS database_versions (version BIGINT PRIMARY KEY);"
85+
"CREATE TABLE IF NOT EXISTS database_versions (version BIGINT PRIMARY KEY);"
8386
),
8487
&[],
8588
)?;
@@ -95,7 +98,7 @@ fn migrate_inner(version: Option<Version>, conn: &Connection, apply_mode: ApplyM
9598
// description
9699
"Initial database schema",
97100
// upgrade query
98-
"{create_table} crates (
101+
"CREATE TABLE crates (
99102
id SERIAL PRIMARY KEY,
100103
name VARCHAR(255) UNIQUE NOT NULL,
101104
latest_version_id INT DEFAULT 0,
@@ -109,7 +112,7 @@ fn migrate_inner(version: Option<Version>, conn: &Connection, apply_mode: ApplyM
109112
github_last_update TIMESTAMP,
110113
content tsvector
111114
);
112-
{create_table} releases (
115+
CREATE TABLE releases (
113116
id SERIAL PRIMARY KEY,
114117
crate_id INT NOT NULL REFERENCES crates(id),
115118
version VARCHAR(100),
@@ -138,40 +141,40 @@ fn migrate_inner(version: Option<Version>, conn: &Connection, apply_mode: ApplyM
138141
default_target VARCHAR(100),
139142
UNIQUE (crate_id, version)
140143
);
141-
{create_table} authors (
144+
CREATE TABLE authors (
142145
id SERIAL PRIMARY KEY,
143146
name VARCHAR(255),
144147
email VARCHAR(255),
145148
slug VARCHAR(255) UNIQUE NOT NULL
146149
);
147-
{create_table} author_rels (
150+
CREATE TABLE author_rels (
148151
rid INT REFERENCES releases(id),
149152
aid INT REFERENCES authors(id),
150153
UNIQUE(rid, aid)
151154
);
152-
{create_table} keywords (
155+
CREATE TABLE keywords (
153156
id SERIAL PRIMARY KEY,
154157
name VARCHAR(255),
155158
slug VARCHAR(255) NOT NULL UNIQUE
156159
);
157-
{create_table} keyword_rels (
160+
CREATE TABLE keyword_rels (
158161
rid INT REFERENCES releases(id),
159162
kid INT REFERENCES keywords(id),
160163
UNIQUE(rid, kid)
161164
);
162-
{create_table} owners (
165+
CREATE TABLE owners (
163166
id SERIAL PRIMARY KEY,
164167
login VARCHAR(255) NOT NULL UNIQUE,
165168
avatar VARCHAR(255),
166169
name VARCHAR(255),
167170
email VARCHAR(255)
168171
);
169-
{create_table} owner_rels (
172+
CREATE TABLE owner_rels (
170173
cid INT REFERENCES releases(id),
171174
oid INT REFERENCES owners(id),
172175
UNIQUE(cid, oid)
173176
);
174-
{create_table} builds (
177+
CREATE TABLE builds (
175178
id SERIAL,
176179
rid INT NOT NULL REFERENCES releases(id),
177180
rustc_version VARCHAR(100) NOT NULL,
@@ -180,22 +183,22 @@ fn migrate_inner(version: Option<Version>, conn: &Connection, apply_mode: ApplyM
180183
build_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
181184
output TEXT
182185
);
183-
{create_table} queue (
186+
CREATE TABLE queue (
184187
id SERIAL,
185188
name VARCHAR(255),
186189
version VARCHAR(100),
187190
attempt INT DEFAULT 0,
188191
date_added TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
189192
UNIQUE(name, version)
190193
);
191-
{create_table} files (
194+
CREATE TABLE files (
192195
path VARCHAR(4096) NOT NULL PRIMARY KEY,
193196
mime VARCHAR(100) NOT NULL,
194197
date_added TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
195198
date_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
196199
content BYTEA
197200
);
198-
{create_table} config (
201+
CREATE TABLE config (
199202
name VARCHAR(100) NOT NULL PRIMARY KEY,
200203
value JSON NOT NULL
201204
);
@@ -223,7 +226,7 @@ fn migrate_inner(version: Option<Version>, conn: &Connection, apply_mode: ApplyM
223226
// description
224227
"Added sandbox_overrides table",
225228
// upgrade query
226-
"{create_table} sandbox_overrides (
229+
"CREATE TABLE sandbox_overrides (
227230
crate_name VARCHAR NOT NULL PRIMARY KEY,
228231
max_memory_bytes INTEGER,
229232
timeout_seconds INTEGER

0 commit comments

Comments
 (0)