File: Migration.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (146 lines) | stat: -rw-r--r-- 3,548 bytes parent folder | download
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
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once
#include "Core/Array.h"
#include "Schema.h"
#include "QueryStr.h"

namespace sql {

	/**
	 * A type that describes migrations to a database in a high-level way. This lets us describe
	 * what needs to be changed in a database-independent way (the syntax and capabilities for alter
	 * table differs quite a bit).
	 */
	class Migration : public Object {
		STORM_CLASS;
	public:
		// Create an empty migration.
		STORM_CTOR Migration();

		/**
		 * Modifications to column attributes.
		 *
		 * Note that modifications to primary keys are handled separately, since it is necessary to
		 * know *all* columns that are a part of the primary key, not just the ones that were
		 * changed.
		 */
		class ColAttrs : public Object {
			STORM_CLASS;
		public:
			// Create.
			STORM_CTOR ColAttrs(Str *name, QueryType type);

			// Name of the column.
			Str *name;

			// Type of the column (for queries).
			QueryType type;

			// Columns in the current version of the column.
			Schema::Attributes currentAttributes;

			// Columns in the desired version of the column.
			Schema::Attributes desiredAttributes;

			// Any current default value.
			MAYBE(Str *) currentDefault;

			// Desired default value.
			MAYBE(Str *) desiredDefault;

			// Any changes?
			Bool STORM_FN any() const;
			Bool STORM_FN empty() const;

			// To string.
			void STORM_FN toS(StrBuf *to) const;

			// Get a column that represents the current version of the column.
			Schema::Column *STORM_FN toSchema() const;
		};

		/**
		 * Modifications to a table.
		 */
		class Table : public Object {
			STORM_CLASS;
		public:
			// Create.
			STORM_CTOR Table(Str *table);

			// Name of the table.
			Str *table;

			// Columns to remove.
			Array<Str *> *colRemove;

			// Modifications to columns.
			Array<ColAttrs *> *colMigrate;

			// Columns to add.
			Array<Schema::Column *> *colAdd;

			// Update the set of primary keys? If true, the array `primaryKeys` contains the new
			// (possibly empty) set of primary keys to apply. Otherwise, the migration should not
			// touch the set of primary keys.
			Bool updatePrimaryKeys;

			// Remove the current primary key? Only valid if `updatePrimaryKeys` is true.
			Bool dropPrimaryKeys;

			// New set of primary keys (or empty, if they should be removed).
			Array<Str *> *primaryKeys;

			// Anything to do?
			Bool STORM_FN any() const;
			Bool STORM_FN empty() const;

			// To string.
			virtual void STORM_FN toS(StrBuf *to) const;
		};

		// Tables to remove.
		Array<Str *> *tableRemove;

		// Tables to modify.
		Array<Table *> *tableMigrate;

		// Tables to add.
		Array<Schema *> *tableAdd;

		/**
		 * Modifications to an index.
		 */
		class Index : public Schema::Index {
			STORM_CLASS;
		public:
			// Create.
			STORM_CTOR Index(Str *table, Schema::Index *index);
			STORM_CTOR Index(Str *table, Str *name, Array<Str *> *columns);

			// The table the index is intended for.
			Str *table;

			// To string.
			virtual void STORM_FN toS(StrBuf *to) const;

			// To SQL.
			virtual void STORM_FN toSQL(QueryStrBuilder *to) const;
			using Schema::Index::toSQL;
		};

		// Indices to remove. Note that the columns may be empty for removed indices, the table name
		// is the important part since e.g. MySQL and MariaDB requires the table name for the index.
		Array<Index *> *indexRemove;

		// Indices to add.
		Array<Index *> *indexAdd;

		// Anything to do?
		Bool STORM_FN any() const;
		Bool STORM_FN empty() const;

		// To string.
		virtual void STORM_FN toS(StrBuf *to) const;
	};

}