on main help sign in
Check for new schema rather than old in helper
8888557fabianlindfors
src/helpers.rs+7−16
@@ -2,37 +2,28 @@ use anyhow::Context;
2
2
3use crate::db::Conn;
3use crate::db::Conn;
4
4
5−pub fn set_up_helpers(db: &mut dyn Conn, current_migration: &Option<String>) -> anyhow::Result<()> {
5+pub fn set_up_helpers(db: &mut dyn Conn, target_migration: &str) -> anyhow::Result<()> {
6− let predicate = if let Some(current_migration) = current_migration {
7− format!(
8− "current_setting('search_path') = 'migration_{}' OR setting_bool",
9− current_migration
10− )
11− } else {
12− "setting_bool".to_string()
13− };
14−
15 let query = format!(
6 let query = format!(
16 "
7 "
17− CREATE OR REPLACE FUNCTION reshape.is_old_schema()
8+ CREATE OR REPLACE FUNCTION reshape.is_new_schema()
18 RETURNS BOOLEAN AS $$
9 RETURNS BOOLEAN AS $$
19 DECLARE
10 DECLARE
20− setting TEXT := current_setting('reshape.is_old_schema', TRUE);
11+ setting TEXT := current_setting('reshape.is_new_schema', TRUE);
21 setting_bool BOOLEAN := setting IS NOT NULL AND setting = 'YES';
12 setting_bool BOOLEAN := setting IS NOT NULL AND setting = 'YES';
22 BEGIN
13 BEGIN
23− RETURN {};
14+ RETURN current_setting('search_path') = 'migration_{}' OR setting_bool;
24 END
15 END
25 $$ language 'plpgsql';
16 $$ language 'plpgsql';
26 ",
17 ",
27− predicate
18+ target_migration,
28 );
19 );
29 db.query(&query)
20 db.query(&query)
30− .context("failed creating helper function reshape.is_old_schema()")?;
21+ .context("failed creating helper function reshape.is_new_schema()")?;
31
22
32 Ok(())
23 Ok(())
33}
24}
34
25
35pub fn tear_down_helpers(db: &mut dyn Conn) -> anyhow::Result<()> {
26pub fn tear_down_helpers(db: &mut dyn Conn) -> anyhow::Result<()> {
36− db.query("DROP FUNCTION IF EXISTS reshape.is_old_schema;")?;
27+ db.query("DROP FUNCTION IF EXISTS reshape.is_new_schema;")?;
37 Ok(())
28 Ok(())
38}
29}
src/lib.rs+2−2
@@ -189,7 +189,8 @@ fn migrate(
189
189
190 println!("Applying {} migrations\n", remaining_migrations.len());
190 println!("Applying {} migrations\n", remaining_migrations.len());
191
191
192+ let target_migration = remaining_migrations.last().unwrap().name.to_string();
192− helpers::set_up_helpers(db, &current_migration).context("failed to set up helpers")?;
193+ helpers::set_up_helpers(db, &target_migration).context("failed to set up helpers")?;
193
194
194 let mut new_schema = Schema::new();
195 let mut new_schema = Schema::new();
195 let mut last_migration_index = usize::MAX;
196 let mut last_migration_index = usize::MAX;
@@ -244,7 +245,6 @@ fn migrate(
244 }
245 }
245
246
246 // Create schema and views for migration
247 // Create schema and views for migration
247− let target_migration = remaining_migrations.last().unwrap().name.to_string();
248 create_schema_for_migration(db, &target_migration, &new_schema)
248 create_schema_for_migration(db, &target_migration, &new_schema)
249 .with_context(|| format!("failed to create schema for migration {}", target_migration))?;
249 .with_context(|| format!("failed to create schema for migration {}", target_migration))?;
250
250
src/migrations/add_column.rs+1−1
@@ -106,7 +106,7 @@ impl Action for AddColumn {
106 CREATE OR REPLACE FUNCTION {trigger_name}()
106 CREATE OR REPLACE FUNCTION {trigger_name}()
107 RETURNS TRIGGER AS $$
107 RETURNS TRIGGER AS $$
108 BEGIN
108 BEGIN
109− IF reshape.is_old_schema() THEN
109+ IF NOT reshape.is_new_schema() THEN
110 DECLARE
110 DECLARE
111 {declarations}
111 {declarations}
112 BEGIN
112 BEGIN
src/migrations/alter_column.rs+2−2
@@ -103,7 +103,7 @@ impl Action for AlterColumn {
103 CREATE OR REPLACE FUNCTION {up_trigger}()
103 CREATE OR REPLACE FUNCTION {up_trigger}()
104 RETURNS TRIGGER AS $$
104 RETURNS TRIGGER AS $$
105 BEGIN
105 BEGIN
106− IF reshape.is_old_schema() THEN
106+ IF NOT reshape.is_new_schema() THEN
107 DECLARE
107 DECLARE
108 {declarations}
108 {declarations}
109 {existing_column} public.{table}.{existing_column_real}%TYPE := NEW.{existing_column_real};
109 {existing_column} public.{table}.{existing_column_real}%TYPE := NEW.{existing_column_real};
@@ -121,7 +121,7 @@ impl Action for AlterColumn {
121 CREATE OR REPLACE FUNCTION {down_trigger}()
121 CREATE OR REPLACE FUNCTION {down_trigger}()
122 RETURNS TRIGGER AS $$
122 RETURNS TRIGGER AS $$
123 BEGIN
123 BEGIN
124− IF NOT reshape.is_old_schema() THEN
124+ IF reshape.is_new_schema() THEN
125 DECLARE
125 DECLARE
126 {declarations}
126 {declarations}
127 {existing_column} public.{table}.{temp_column}%TYPE := NEW.{temp_column};
127 {existing_column} public.{table}.{temp_column}%TYPE := NEW.{temp_column};
src/migrations/common.rs−4
@@ -72,8 +72,6 @@ impl ToSql for PostgresRawValue {
72pub fn batch_touch_rows(db: &mut dyn Conn, table: &str, column: &str) -> anyhow::Result<()> {
72pub fn batch_touch_rows(db: &mut dyn Conn, table: &str, column: &str) -> anyhow::Result<()> {
73 const BATCH_SIZE: u16 = 1000;
73 const BATCH_SIZE: u16 = 1000;
74
74
75− db.query("SET reshape.is_old_schema = 'YES'")?;
76−
77 let mut cursor: Option<PostgresRawValue> = None;
75 let mut cursor: Option<PostgresRawValue> = None;
78
76
79 loop {
77 loop {
@@ -152,8 +150,6 @@ pub fn batch_touch_rows(db: &mut dyn Conn, table: &str, column: &str) -> anyhow:
152 cursor = last_value
150 cursor = last_value
153 }
151 }
154
152
155− db.query("SET reshape.is_old_schema = ''")?;
156−
157 Ok(())
153 Ok(())
158}
154}
159
155
src/migrations/remove_column.rs+1−1
@@ -61,7 +61,7 @@ impl Action for RemoveColumn {
61 CREATE OR REPLACE FUNCTION {trigger_name}()
61 CREATE OR REPLACE FUNCTION {trigger_name}()
62 RETURNS TRIGGER AS $$
62 RETURNS TRIGGER AS $$
63 BEGIN
63 BEGIN
64− IF NOT reshape.is_old_schema() IS NULL THEN
64+ IF reshape.is_new_schema() THEN
65 DECLARE
65 DECLARE
66 {declarations}
66 {declarations}
67 BEGIN
67 BEGIN