Class: MigrationBundler::Targets::SqliteTarget
- Inherits:
-
Base
- Object
- Thor
- Base
- MigrationBundler::Targets::SqliteTarget
show all
- Defined in:
- lib/migration_bundler/targets/sqlite/sqlite_target.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#generate, name, #push, source_root, #validate
Methods included from Actions
#bundle, #git, #git_add, #truncate_database, #unique_tag_for_version
Class Method Details
.register_with_cli(cli) ⇒ Object
TODO: Need a way to do this for self elegantly…
7
8
9
10
|
# File 'lib/migration_bundler/targets/sqlite/sqlite_target.rb', line 7
def self.register_with_cli(cli)
cli.method_option :database, type: :string, aliases: '-d', desc: "Set target DATABASE", for: :dump
cli.method_option :database, type: :string, aliases: '-d', desc: "Set target DATABASE", for: :load
end
|
Instance Method Details
#drop ⇒ Object
86
87
88
89
|
# File 'lib/migration_bundler/targets/sqlite/sqlite_target.rb', line 86
def drop
say_status :truncate, database.path, :yellow
database.drop
end
|
#dump ⇒ Object
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
|
# File 'lib/migration_bundler/targets/sqlite/sqlite_target.rb', line 34
def dump
database_url = (options[:database] && URI(options[:database])) || project.database_url
database_path = database_url.path || database_url.opaque
fail Error, "Cannot dump database: no file at path '#{database_path}'." unless File.exists?(database_path)
@database = MigrationBundler::Databases::SqliteDatabase.new(database_url)
fail Error, "Cannot dump database: the database at path '#{database_path}' does not have a `schema_migrations` table." unless database.migrations_table?
say "Dumping schema from database '#{database_path}'"
File.truncate(project.schema_path, 0)
types = { table: 'tables', index: 'indexes', trigger: 'triggers', view: 'views'}
types.each do |type, name|
say "Dumping #{name}..."
with_padding do
database.dump_to_schema(type, project.schema_path) do |name|
say "wrote #{type}: #{name}", :green
end
end
say
end
File.open(project.schema_path, 'a') do |f|
project.config['db.dump_tables'].each do |table_name|
say "Dumping rows from '#{table_name}'..."
with_padding do
row_statements = database.dump_rows(table_name)
f.puts row_statements.join("\n")
say "wrote #{row_statements.size} rows.", :green
end
end
say
end
say "Dump complete. Schema written to #{project.schema_path}."
end
|
#init ⇒ Object
12
13
14
15
16
17
18
|
# File 'lib/migration_bundler/targets/sqlite/sqlite_target.rb', line 12
def init
migration_name = MigrationBundler::Util.migration_named('create_' + options['name'])
template('create_migration_bundler_tables.sql.erb', "migrations/#{migration_name}.sql")
git_add "migrations/#{migration_name}.sql"
create_file(database_path)
append_to_file '.gitignore', database_path
end
|
#load ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/migration_bundler/targets/sqlite/sqlite_target.rb', line 71
def load
project = MigrationBundler::Project.load
unless File.size?(project.schema_path)
raise Error, "Cannot load database: empty schema found at #{project.schema_path}. Maybe you need to `mb migrate`?"
end
drop
command = "sqlite3 #{database.path} < #{project.schema_path}"
say_status :executing, command
stdout_str, stderr_str, status = Open3.capture3(command)
fail Error, "Failed loading schema: #{stderr_str}" unless stderr_str.empty?
say "Loaded schema at version #{database.current_version}"
end
|
#new(name) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/migration_bundler/targets/sqlite/sqlite_target.rb', line 27
def new(name)
migration_ext = project.database_class.migration_ext
migration_name = MigrationBundler::Util.migration_named(name) + migration_ext
template('migration.sql.erb', "migrations/#{migration_name}")
git_add "migrations/#{migration_name}"
end
|